CGObjC.cpp revision 0c70181854a95fca0e0d56dfa1203eb2216052ea
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(); 8989d232c884ea9872d6555df0fd7359699819bc1f1John McCall switch (getEvaluationKind(ivarType)) { 8999d232c884ea9872d6555df0fd7359699819bc1f1John McCall case TEK_Complex: { 9009d232c884ea9872d6555df0fd7359699819bc1f1John McCall ComplexPairTy pair = EmitLoadOfComplex(LV); 9019d232c884ea9872d6555df0fd7359699819bc1f1John McCall EmitStoreOfComplex(pair, 9029d232c884ea9872d6555df0fd7359699819bc1f1John McCall MakeNaturalAlignAddrLValue(ReturnValue, ivarType), 9039d232c884ea9872d6555df0fd7359699819bc1f1John McCall /*init*/ true); 9049d232c884ea9872d6555df0fd7359699819bc1f1John McCall return; 9059d232c884ea9872d6555df0fd7359699819bc1f1John McCall } 9069d232c884ea9872d6555df0fd7359699819bc1f1John McCall case TEK_Aggregate: 9071e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // The return value slot is guaranteed to not be aliased, but 9081e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // that's not necessarily the same as "on the stack", so 9091e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // we still potentially need objc_memmove_collectable. 910649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier EmitAggregateCopy(ReturnValue, LV.getAddress(), ivarType); 9119d232c884ea9872d6555df0fd7359699819bc1f1John McCall return; 9129d232c884ea9872d6555df0fd7359699819bc1f1John McCall case TEK_Scalar: { 913ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall llvm::Value *value; 914ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall if (propType->isReferenceType()) { 915ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall value = LV.getAddress(); 916ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall } else { 917ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall // We want to load and autoreleaseReturnValue ARC __weak ivars. 918ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak) { 9191e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall value = emitARCRetainLoadOfScalar(*this, LV, ivarType); 920ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall 921ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall // Otherwise we want to do a simple load, suppressing the 922ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall // final autorelease. 923f85e193739c953358c865005855253af4f68a497John McCall } else { 924ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall value = EmitLoadOfLValue(LV).getScalarVal(); 925ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall AutoreleaseResult = false; 92614086764e340267e17803d0f8243070ffae2c76eFariborz Jahanian } 927f85e193739c953358c865005855253af4f68a497John McCall 928ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall value = Builder.CreateBitCast(value, ConvertType(propType)); 929490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian value = Builder.CreateBitCast(value, 930490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian ConvertType(GetterMethodDecl->getResultType())); 931ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall } 932ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall 933ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall EmitReturnOfRValue(RValue::get(value), propType); 9349d232c884ea9872d6555df0fd7359699819bc1f1John McCall return; 935ed1d29d62595a83ccf6ef23eb2759d355206df2eFariborz Jahanian } 9369d232c884ea9872d6555df0fd7359699819bc1f1John McCall } 9379d232c884ea9872d6555df0fd7359699819bc1f1John McCall llvm_unreachable("bad evaluation kind"); 938c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar } 939af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 9401e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 9411e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm_unreachable("bad @property implementation strategy!"); 942af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar} 943af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 94441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall/// emitStructSetterCall - Call the runtime function to store the value 94541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall/// from the first formal parameter into the given ivar. 94641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCallstatic void emitStructSetterCall(CodeGenFunction &CGF, ObjCMethodDecl *OMD, 94741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall ObjCIvarDecl *ivar) { 9482846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian // objc_copyStruct (&structIvar, &Arg, 9492846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian // sizeof (struct something), true, false); 950bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall CallArgList args; 951bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall 952bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall // The first argument is the address of the ivar. 95341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall llvm::Value *ivarAddr = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), 95441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall CGF.LoadObjCSelf(), ivar, 0) 95541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall .getAddress(); 95641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy); 95741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy); 958bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall 959bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall // The second argument is the address of the parameter variable. 96041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall ParmVarDecl *argVar = *OMD->param_begin(); 961f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr argRef(argVar, false, argVar->getType().getNonReferenceType(), 962c3953aab1b6167b7f394ca87096e91ab840026adFariborz Jahanian VK_LValue, SourceLocation()); 96341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall llvm::Value *argAddr = CGF.EmitLValue(&argRef).getAddress(); 96441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy); 96541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy); 966bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall 967bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall // The third argument is the sizeof the type. 968bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall llvm::Value *size = 96941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall CGF.CGM.getSize(CGF.getContext().getTypeSizeInChars(ivar->getType())); 97041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(size), CGF.getContext().getSizeType()); 97141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 97241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall // The fourth argument is the 'isAtomic' flag. 97341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(CGF.Builder.getTrue()), CGF.getContext().BoolTy); 974bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall 97541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall // The fifth argument is the 'hasStrong' flag. 97641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall // FIXME: should this really always be false? 97741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(CGF.Builder.getFalse()), CGF.getContext().BoolTy); 978bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall 97941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall llvm::Value *copyStructFn = CGF.CGM.getObjCRuntime().GetSetStructFunction(); 9800f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(CGF.getContext().VoidTy, 9810f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall args, 9820f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 9830f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 98441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall copyStructFn, ReturnValueSlot(), args); 9852846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian} 9862846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian 987cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian/// emitCPPObjectAtomicSetterCall - Call the runtime function to store 988cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian/// the value from the first formal parameter into the given ivar, using 989cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian/// the Cpp API for atomic Cpp objects with non-trivial copy assignment. 990cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanianstatic void emitCPPObjectAtomicSetterCall(CodeGenFunction &CGF, 991cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ObjCMethodDecl *OMD, 992cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ObjCIvarDecl *ivar, 993cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian llvm::Constant *AtomicHelperFn) { 994cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // objc_copyCppObjectAtomic (&CppObjectIvar, &Arg, 995cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // AtomicHelperFn); 996cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian CallArgList args; 997cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 998cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // The first argument is the address of the ivar. 999cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian llvm::Value *ivarAddr = 1000cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), 1001cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian CGF.LoadObjCSelf(), ivar, 0).getAddress(); 1002cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy); 1003cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy); 1004cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 1005cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // The second argument is the address of the parameter variable. 1006cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ParmVarDecl *argVar = *OMD->param_begin(); 1007f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr argRef(argVar, false, argVar->getType().getNonReferenceType(), 1008cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian VK_LValue, SourceLocation()); 1009cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian llvm::Value *argAddr = CGF.EmitLValue(&argRef).getAddress(); 1010cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy); 1011cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy); 1012cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 1013cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // Third argument is the helper function. 1014cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy); 1015cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 1016cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian llvm::Value *copyCppAtomicObjectFn = 1017d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall CGF.CGM.getObjCRuntime().GetCppAtomicObjectSetFunction(); 10180f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(CGF.getContext().VoidTy, 10190f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall args, 10200f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 10210f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 1022cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian copyCppAtomicObjectFn, ReturnValueSlot(), args); 1023cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 1024cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 1025cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian} 1026cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 102720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 10281e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallstatic bool hasTrivialSetExpr(const ObjCPropertyImplDecl *PID) { 10291e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Expr *setter = PID->getSetterCXXAssignment(); 10301e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (!setter) return true; 10311e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 10321e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Sema only makes only of these when the ivar has a C++ class type, 10331e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // so the form is pretty constrained. 103471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 103571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // An operator call is trivial if the function it calls is trivial. 10361e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // This also implies that there's nothing non-trivial going on with 10371e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // the arguments, because operator= can only be trivial if it's a 10381e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // synthesized assignment operator and therefore both parameters are 10391e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // references. 10401e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (CallExpr *call = dyn_cast<CallExpr>(setter)) { 104171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (const FunctionDecl *callee 104271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall = dyn_cast_or_null<FunctionDecl>(call->getCalleeDecl())) 104371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (callee->isTrivial()) 104471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return true; 104571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return false; 104601cb307f0b84a368cdbc0738d6680aab8ed7423fFariborz Jahanian } 104771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 10481e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall assert(isa<ExprWithCleanups>(setter)); 104971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return false; 105071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall} 105186957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar 10524e494cf56325f26a6c3eb5a065c35e54835f14e6Benjamin Kramerstatic bool UseOptimizedSetter(CodeGenModule &CGM) { 10534e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().getGC() != LangOptions::NonGC) 1054ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return false; 10557a0c064770355a4918df69f95de8dea6338a59a2Daniel Dunbar return CGM.getLangOpts().ObjCRuntime.hasOptimizedSetter(); 1056ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek} 1057ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 105871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCallvoid 105971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCallCodeGenFunction::generateObjCSetterBody(const ObjCImplementationDecl *classImpl, 1060cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian const ObjCPropertyImplDecl *propImpl, 1061cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian llvm::Constant *AtomicHelperFn) { 1062cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian const ObjCPropertyDecl *prop = propImpl->getPropertyDecl(); 1063cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); 1064cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ObjCMethodDecl *setterMethod = prop->getSetterMethodDecl(); 1065cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 106671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // Just use the setter expression if Sema gave us one and it's 1067cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // non-trivial. 10681e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (!hasTrivialSetExpr(propImpl)) { 1069cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian if (!AtomicHelperFn) 1070cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // If non-atomic, assignment is called directly. 1071cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian EmitStmt(propImpl->getSetterCXXAssignment()); 1072cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian else 1073cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // If atomic, assignment is called via a locking api. 1074cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian emitCPPObjectAtomicSetterCall(*this, setterMethod, ivar, 1075cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian AtomicHelperFn); 107671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return; 107771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall } 107871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 10791e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall PropertyImplStrategy strategy(CGM, propImpl); 10801e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall switch (strategy.getKind()) { 10811e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::Native: { 1082aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman // We don't need to do anything for a zero-size struct. 1083aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman if (strategy.getIvarSize().isZero()) 1084aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman return; 1085aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman 10861e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *argAddr = LocalDeclMap[*setterMethod->param_begin()]; 108771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 10881e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall LValue ivarLValue = 10891e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, /*quals*/ 0); 10901e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *ivarAddr = ivarLValue.getAddress(); 10911e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 10921e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Currently, all atomic accesses have to be through integer 10931e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // types, so there's no point in trying to pick a prettier type. 10941e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Type *bitcastType = 10951e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Type::getIntNTy(getLLVMContext(), 10961e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall getContext().toBits(strategy.getIvarSize())); 10971e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall bitcastType = bitcastType->getPointerTo(); // addrspace 0 okay 10981e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 10991e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Cast both arguments to the chosen operation type. 11001e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall argAddr = Builder.CreateBitCast(argAddr, bitcastType); 11011e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall ivarAddr = Builder.CreateBitCast(ivarAddr, bitcastType); 110271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 11031e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // This bitcast load is likely to cause some nasty IR. 11041e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *load = Builder.CreateLoad(argAddr); 11051e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 11061e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Perform an atomic store. There are no memory ordering requirements. 11071e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::StoreInst *store = Builder.CreateStore(load, ivarAddr); 11081e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall store->setAlignment(strategy.getIvarAlignment().getQuantity()); 11091e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall store->setAtomic(llvm::Unordered); 11101e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 11111e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 11121e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 11131e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::GetSetProperty: 11141e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::SetPropertyAndExpressionGet: { 1115ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 1116ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek llvm::Value *setOptimizedPropertyFn = 0; 1117ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek llvm::Value *setPropertyFn = 0; 1118ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (UseOptimizedSetter(CGM)) { 11197a0c064770355a4918df69f95de8dea6338a59a2Daniel Dunbar // 10.8 and iOS 6.0 code and GC is off 1120ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek setOptimizedPropertyFn = 112116098f366097305c348b356a44638d6c959c6e60Eric Christopher CGM.getObjCRuntime() 112216098f366097305c348b356a44638d6c959c6e60Eric Christopher .GetOptimizedPropertySetFunction(strategy.isAtomic(), 112316098f366097305c348b356a44638d6c959c6e60Eric Christopher strategy.isCopy()); 1124ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (!setOptimizedPropertyFn) { 1125ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek CGM.ErrorUnsupported(propImpl, "Obj-C optimized setter - NYI"); 1126ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return; 1127ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 112886957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar } 1129ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek else { 1130ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek setPropertyFn = CGM.getObjCRuntime().GetPropertySetFunction(); 1131ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (!setPropertyFn) { 1132ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek CGM.ErrorUnsupported(propImpl, "Obj-C setter requiring atomic copy"); 1133ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return; 1134ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 1135ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 1136ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 11371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Emit objc_setProperty((id) self, _cmd, offset, arg, 113886957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar // <is-atomic>, <is-copy>). 113971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall llvm::Value *cmd = 114071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall Builder.CreateLoad(LocalDeclMap[setterMethod->getCmdDecl()]); 114171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall llvm::Value *self = 114271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy); 114371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall llvm::Value *ivarOffset = 114471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall EmitIvarOffset(classImpl->getClassInterface(), ivar); 114571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall llvm::Value *arg = LocalDeclMap[*setterMethod->param_begin()]; 114671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall arg = Builder.CreateBitCast(Builder.CreateLoad(arg, "arg"), VoidPtrTy); 114771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 114871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall CallArgList args; 114971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall args.add(RValue::get(self), getContext().getObjCIdType()); 115071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall args.add(RValue::get(cmd), getContext().getObjCSelType()); 1151ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (setOptimizedPropertyFn) { 1152ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(arg), getContext().getObjCIdType()); 1153ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(ivarOffset), getContext().getPointerDiffType()); 11540f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall EmitCall(getTypes().arrangeFreeFunctionCall(getContext().VoidTy, args, 11550f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 11560f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 1157ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek setOptimizedPropertyFn, ReturnValueSlot(), args); 1158ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } else { 1159ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(ivarOffset), getContext().getPointerDiffType()); 1160ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(arg), getContext().getObjCIdType()); 1161ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(Builder.getInt1(strategy.isAtomic())), 1162ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek getContext().BoolTy); 1163ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(Builder.getInt1(strategy.isCopy())), 1164ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek getContext().BoolTy); 1165ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // FIXME: We shouldn't need to get the function info here, the runtime 1166ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // already should have computed it to build the function. 11670f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall EmitCall(getTypes().arrangeFreeFunctionCall(getContext().VoidTy, args, 11680f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 11690f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 1170ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek setPropertyFn, ReturnValueSlot(), args); 1171ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 1172ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 117371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return; 117471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall } 117571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 11761e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::CopyStruct: 117741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall emitStructSetterCall(*this, setterMethod, ivar); 117871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return; 11791e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 11801e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::Expression: 11811e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall break; 118271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall } 118371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 118471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // Otherwise, fake up some ASTs and emit a normal assignment. 118571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ValueDecl *selfDecl = setterMethod->getSelfDecl(); 1186f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr self(selfDecl, false, selfDecl->getType(), 1187f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_LValue, SourceLocation()); 118871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ImplicitCastExpr selfLoad(ImplicitCastExpr::OnStack, 118971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall selfDecl->getType(), CK_LValueToRValue, &self, 119071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall VK_RValue); 119171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ObjCIvarRefExpr ivarRef(ivar, ivar->getType().getNonReferenceType(), 11920c70181854a95fca0e0d56dfa1203eb2216052eaFariborz Jahanian SourceLocation(), SourceLocation(), 11930c70181854a95fca0e0d56dfa1203eb2216052eaFariborz Jahanian &selfLoad, true, true); 119471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 119571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ParmVarDecl *argDecl = *setterMethod->param_begin(); 119671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall QualType argType = argDecl->getType().getNonReferenceType(); 1197f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr arg(argDecl, false, argType, VK_LValue, SourceLocation()); 119871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ImplicitCastExpr argLoad(ImplicitCastExpr::OnStack, 119971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argType.getUnqualifiedType(), CK_LValueToRValue, 120071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall &arg, VK_RValue); 120145e8423d7dcea657c14c55347e8a30ac904d7501Daniel Dunbar 120271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // The property type can differ from the ivar type in some situations with 120371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // Objective-C pointer types, we can always bit cast the RHS in these cases. 120471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // The following absurdity is just to ensure well-formed IR. 120571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall CastKind argCK = CK_NoOp; 120671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (ivarRef.getType()->isObjCObjectPointerType()) { 120771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (argLoad.getType()->isObjCObjectPointerType()) 120871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_BitCast; 120971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall else if (argLoad.getType()->isBlockPointerType()) 121071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_BlockPointerToObjCPointerCast; 121171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall else 121271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_CPointerToObjCPointerCast; 121371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall } else if (ivarRef.getType()->isBlockPointerType()) { 121471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (argLoad.getType()->isBlockPointerType()) 121571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_BitCast; 121671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall else 121771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_AnyPointerToBlockPointerCast; 121871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall } else if (ivarRef.getType()->isPointerType()) { 121971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_BitCast; 122086957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar } 122171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ImplicitCastExpr argCast(ImplicitCastExpr::OnStack, 122271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ivarRef.getType(), argCK, &argLoad, 122371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall VK_RValue); 122471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall Expr *finalArg = &argLoad; 122571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (!getContext().hasSameUnqualifiedType(ivarRef.getType(), 122671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argLoad.getType())) 122771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall finalArg = &argCast; 122871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 122971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 123071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall BinaryOperator assign(&ivarRef, finalArg, BO_Assign, 123171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ivarRef.getType(), VK_RValue, OK_Ordinary, 1232be9af1288881110e406b87914162eaa59f1e5918Lang Hames SourceLocation(), false); 123371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall EmitStmt(&assign); 123471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall} 123571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 12362ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// \brief Generate an Objective-C property setter function. 12372ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// 12382ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// The given Decl must be an ObjCImplementationDecl. \@synthesize 123971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall/// is illegal within a category. 124071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCallvoid CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP, 124171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall const ObjCPropertyImplDecl *PID) { 1242b6e5fe3cae9bf456f084c555c3ecc256f4181cc5Fariborz Jahanian llvm::Constant *AtomicHelperFn = 124320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian GenerateObjCAtomicSetterCopyHelperFunction(PID); 124471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 124571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ObjCMethodDecl *OMD = PD->getSetterMethodDecl(); 124671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall assert(OMD && "Invalid call to generate setter (empty method)"); 1247ea32047660159811a4c14d008a4b7e3807a705d6Eric Christopher StartObjCMethod(OMD, IMP->getClassInterface(), OMD->getLocStart()); 124871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 1249cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian generateObjCSetterBody(IMP, PID, AtomicHelperFn); 1250af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 1251af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar FinishFunction(); 12524111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner} 12534111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner 1254e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCallnamespace { 12559928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall struct DestroyIvar : EHScopeStack::Cleanup { 12569928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall private: 12579928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall llvm::Value *addr; 1258e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall const ObjCIvarDecl *ivar; 1259516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne CodeGenFunction::Destroyer *destroyer; 12609928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall bool useEHCleanupForArray; 12619928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall public: 12629928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall DestroyIvar(llvm::Value *addr, const ObjCIvarDecl *ivar, 12639928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CodeGenFunction::Destroyer *destroyer, 12649928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall bool useEHCleanupForArray) 1265516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne : addr(addr), ivar(ivar), destroyer(destroyer), 12669928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall useEHCleanupForArray(useEHCleanupForArray) {} 1267e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1268ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall void Emit(CodeGenFunction &CGF, Flags flags) { 12699928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall LValue lvalue 12709928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), addr, ivar, /*CVR*/ 0); 12719928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CGF.emitDestroy(lvalue.getAddress(), ivar->getType(), destroyer, 1272ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall flags.isForNormalCleanup() && useEHCleanupForArray); 1273e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall } 1274e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall }; 1275e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall} 1276e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 12779928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall/// Like CodeGenFunction::destroyARCStrong, but do it with a call. 12789928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCallstatic void destroyARCStrongWithStore(CodeGenFunction &CGF, 12799928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall llvm::Value *addr, 12809928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall QualType type) { 12819928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall llvm::Value *null = getNullForVariable(addr); 12829928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CGF.EmitARCStoreStrongCall(addr, null, /*ignored*/ true); 12839928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall} 1284f85e193739c953358c865005855253af4f68a497John McCall 1285e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCallstatic void emitCXXDestructMethod(CodeGenFunction &CGF, 1286e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall ObjCImplementationDecl *impl) { 1287e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall CodeGenFunction::RunCleanupsScope scope(CGF); 1288e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1289e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall llvm::Value *self = CGF.LoadObjCSelf(); 1290e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1291db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose const ObjCInterfaceDecl *iface = impl->getClassInterface(); 1292db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin(); 1293e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall ivar; ivar = ivar->getNextIvar()) { 1294e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall QualType type = ivar->getType(); 1295e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1296e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall // Check whether the ivar is a destructible type. 12979928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall QualType::DestructionKind dtorKind = type.isDestructedType(); 12989928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall if (!dtorKind) continue; 12999928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall 13009928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CodeGenFunction::Destroyer *destroyer = 0; 13019928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall 13029928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall // Use a call to objc_storeStrong to destroy strong ivars, for the 13039928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall // general benefit of the tools. 13049928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall if (dtorKind == QualType::DK_objc_strong_lifetime) { 1305516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne destroyer = destroyARCStrongWithStore; 13069928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall 13079928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall // Otherwise use the default for the destruction kind. 13089928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall } else { 1309516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne destroyer = CGF.getDestroyer(dtorKind); 1310e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall } 13119928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall 13129928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CleanupKind cleanupKind = CGF.getCleanupKind(dtorKind); 13139928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall 13149928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CGF.EHStack.pushCleanup<DestroyIvar>(cleanupKind, self, ivar, destroyer, 13159928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall cleanupKind & EHCleanup); 1316e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall } 1317e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1318e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall assert(scope.requiresCleanups() && "nothing to do in .cxx_destruct?"); 1319e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall} 1320e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1321109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanianvoid CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP, 1322109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian ObjCMethodDecl *MD, 1323109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian bool ctor) { 1324109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface()); 13258d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel StartObjCMethod(MD, IMP->getClassInterface(), MD->getLocStart()); 1326e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1327e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall // Emit .cxx_construct. 1328109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian if (ctor) { 1329f85e193739c953358c865005855253af4f68a497John McCall // Suppress the final autorelease in ARC. 1330f85e193739c953358c865005855253af4f68a497John McCall AutoreleaseResult = false; 1331f85e193739c953358c865005855253af4f68a497John McCall 13325f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<CXXCtorInitializer *, 8> IvarInitializers; 1333e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall for (ObjCImplementationDecl::init_const_iterator B = IMP->init_begin(), 1334e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall E = IMP->init_end(); B != E; ++B) { 1335e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall CXXCtorInitializer *IvarInit = (*B); 133600eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet FieldDecl *Field = IvarInit->getAnyMember(); 1337109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field); 13389b4d4fc49f30f1caa35d680702f1921afad81971Fariborz Jahanian LValue LV = EmitLValueForIvar(TypeOfSelfObject(), 13399b4d4fc49f30f1caa35d680702f1921afad81971Fariborz Jahanian LoadObjCSelf(), Ivar, 0); 13407c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall EmitAggExpr(IvarInit->getInit(), 13417c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall AggValueSlot::forLValue(LV, AggValueSlot::IsDestructed, 1342410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall AggValueSlot::DoesNotNeedGCBarriers, 1343649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier AggValueSlot::IsNotAliased)); 1344109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian } 1345109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian // constructor returns 'self'. 1346109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian CodeGenTypes &Types = CGM.getTypes(); 1347109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian QualType IdTy(CGM.getContext().getObjCIdType()); 1348109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian llvm::Value *SelfAsId = 1349109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy)); 1350109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian EmitReturnOfRValue(RValue::get(SelfAsId), IdTy); 1351e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1352e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall // Emit .cxx_destruct. 1353bc397cf90355f17c974b0bdf3960e8fb38caf5d6Chandler Carruth } else { 1354e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall emitCXXDestructMethod(*this, IMP); 1355109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian } 1356109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian FinishFunction(); 1357109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian} 1358109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian 13590b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanianbool CodeGenFunction::IndirectObjCSetterArg(const CGFunctionInfo &FI) { 13600b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian CGFunctionInfo::const_arg_iterator it = FI.arg_begin(); 13610b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian it++; it++; 13620b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian const ABIArgInfo &AI = it->info; 13630b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian // FIXME. Is this sufficient check? 13640b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian return (AI.getKind() == ABIArgInfo::Indirect); 13650b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian} 13660b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian 136715bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanianbool CodeGenFunction::IvarTypeWithAggrGCObjects(QualType Ty) { 13684e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().getGC() == LangOptions::NonGC) 136915bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian return false; 137015bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian if (const RecordType *FDTTy = Ty.getTypePtr()->getAs<RecordType>()) 137115bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian return FDTTy->getDecl()->hasObjectMember(); 137215bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian return false; 137315bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian} 137415bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian 1375c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbarllvm::Value *CodeGenFunction::LoadObjCSelf() { 1376b7ec246872b412f0e7bb9e93eacfd78cfa6adfb3Daniel Dunbar const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); 1377b7ec246872b412f0e7bb9e93eacfd78cfa6adfb3Daniel Dunbar return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self"); 13784111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner} 13794111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner 138045012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz JahanianQualType CodeGenFunction::TypeOfSelfObject() { 138145012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); 138245012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian ImplicitParamDecl *selfDecl = OMD->getSelfDecl(); 138314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>( 138414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff getContext().getCanonicalType(selfDecl->getType())); 138545012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian return PTy->getPointeeType(); 138645012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian} 138745012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian 138874391b48b4791cded373683a3baf67314f358d50Chris Lattnervoid CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){ 13891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Constant *EnumerationMutationFn = 1390c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar CGM.getObjCRuntime().EnumerationMutationFunction(); 13911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1392c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar if (!EnumerationMutationFn) { 1393c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime"); 1394c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar return; 1395c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar } 1396c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar 1397bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel CGDebugInfo *DI = getDebugInfo(); 139873fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher if (DI) 139973fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin()); 1400bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel 14019d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel // The local variable comes into scope immediately. 14029d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel AutoVarEmission variable = AutoVarEmission::invalid(); 14039d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) 14049d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel variable = EmitAutoVarAlloca(*cast<VarDecl>(SD->getSingleDecl())); 14059d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel 1406d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end"); 14071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1408f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson // Fast enumeration state. 14090815b579b31cb3129f732bb7ea36fd6ba6949e98Douglas Gregor QualType StateTy = CGM.getObjCFastEnumerationStateType(); 1410195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr"); 14111884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson EmitNullInitialization(StatePtr, StateTy); 14121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1413f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson // Number of elements in the items array. 14142abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson static const unsigned NumItems = 16; 14151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1416d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Fetch the countByEnumeratingWithState:objects:count: selector. 1417ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer IdentifierInfo *II[] = { 1418ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer &CGM.getContext().Idents.get("countByEnumeratingWithState"), 1419ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer &CGM.getContext().Idents.get("objects"), 1420ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer &CGM.getContext().Idents.get("count") 1421ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer }; 1422ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer Selector FastEnumSel = 1423ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]); 1424f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1425f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson QualType ItemsTy = 1426f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson getContext().getConstantArrayType(getContext().getObjCIdType(), 14271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::APInt(32, NumItems), 1428f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson ArrayType::Normal, 0); 1429195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr"); 14301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1431990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Emit the collection pointer. In ARC, we do a retain. 1432990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall llvm::Value *Collection; 14334e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().ObjCAutoRefCount) { 1434990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall Collection = EmitARCRetainScalarExpr(S.getCollection()); 1435990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1436990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Enter a cleanup to do the release. 1437990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall EmitObjCConsumeObject(S.getCollection()->getType(), Collection); 1438990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall } else { 1439990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall Collection = EmitScalarExpr(S.getCollection()); 1440990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall } 14411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14424b302d32378b364703b212834f908762e570c29cJohn McCall // The 'continue' label needs to appear within the cleanup for the 14434b302d32378b364703b212834f908762e570c29cJohn McCall // collection object. 14444b302d32378b364703b212834f908762e570c29cJohn McCall JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next"); 14454b302d32378b364703b212834f908762e570c29cJohn McCall 1446d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Send it our message: 1447f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson CallArgList Args; 1448d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1449d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The first argument is a temporary of the enumeration-state type. 145004c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman Args.add(RValue::get(StatePtr), getContext().getPointerType(StateTy)); 14511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1452d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The second argument is a temporary array with space for NumItems 1453d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // pointers. We'll actually be loading elements from the array 1454d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // pointer written into the control state; this buffer is so that 1455d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // collections that *aren't* backed by arrays can still queue up 1456d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // batches of elements. 145704c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman Args.add(RValue::get(ItemsPtr), getContext().getPointerType(ItemsTy)); 14581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1459d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The third argument is the capacity of that temporary array. 14602acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy); 14614a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems); 146204c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman Args.add(RValue::get(Count), getContext().UnsignedLongTy); 14631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1464d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Start the enumeration. 14651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump RValue CountRV = 1466ef072fd2f3347cfd857d6eb787b245b950771430John McCall CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), 1467f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson getContext().UnsignedLongTy, 1468f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson FastEnumSel, 1469c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall Collection, Args); 1470f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1471d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The initial number of objects that were returned in the buffer. 1472d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *initialBufferLimit = CountRV.getScalarVal(); 14731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1474d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty"); 1475d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit"); 1476f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1477d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *zero = llvm::Constant::getNullValue(UnsignedLongLTy); 1478f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1479d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // If the limit pointer was zero to begin with, the collection is 1480d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // empty; skip all this. 1481d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateCondBr(Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"), 1482d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmptyBB, LoopInitBB); 14831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1484d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Otherwise, initialize the loop. 1485d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(LoopInitBB); 14861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1487d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Save the initial mutations value. This is the value at an 1488d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // address that was written into the state object by 1489d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // countByEnumeratingWithState:objects:count:. 14901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Value *StateMutationsPtrPtr = 14912abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr"); 14921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, 14932abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson "mutationsptr"); 14941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1495d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *initialMutations = 1496d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateLoad(StateMutationsPtr, "forcoll.initial-mutations"); 14971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1498d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Start looping. This is the point we return to whenever we have a 1499d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // fresh, non-empty batch of objects. 1500d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody"); 1501d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(LoopBodyBB); 15021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1503d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The current index into the buffer. 1504bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad llvm::PHINode *index = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.index"); 1505d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall index->addIncoming(zero, LoopInitBB); 1506f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1507d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The current buffer size. 1508bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad llvm::PHINode *count = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.count"); 1509d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall count->addIncoming(initialBufferLimit, LoopInitBB); 1510f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1511d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Check whether the mutations value has changed from where it was 1512d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // at start. StateMutationsPtr should actually be invariant between 1513d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // refreshes. 15142abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr"); 1515d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *currentMutations 1516d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall = Builder.CreateLoad(StateMutationsPtr, "statemutations"); 15171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1518d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated"); 1519361cf980a7d976ef11a37b49567412b6b63a89d7Dan Gohman llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcoll.notmutated"); 15201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1521d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations), 1522d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall WasNotMutatedBB, WasMutatedBB); 15231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1524d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // If so, call the enumeration-mutation function. 1525d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(WasMutatedBB); 15262abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson llvm::Value *V = 15271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Builder.CreateBitCast(Collection, 1528578faa837b552403e2002b97fdfbfde14f2448e5Benjamin Kramer ConvertType(getContext().getObjCIdType())); 15292b2105e92fc77016992dae3f117f526e73af5ea9Daniel Dunbar CallArgList Args2; 153004c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman Args2.add(RValue::get(V), getContext().getObjCIdType()); 1531f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // FIXME: We shouldn't need to get the function info here, the runtime already 1532f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // should have computed it to build the function. 15330f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall EmitCall(CGM.getTypes().arrangeFreeFunctionCall(getContext().VoidTy, Args2, 15340f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 15350f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 1536f3c47c9525153aea2de0ec4bd615b9cf2d81c103Anders Carlsson EnumerationMutationFn, ReturnValueSlot(), Args2); 15371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1538d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Otherwise, or if the mutation function returns, just continue. 1539d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(WasNotMutatedBB); 15401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1541d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Initialize the element variable. 1542d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall RunCleanupsScope elementVariableScope(*this); 154357b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall bool elementIsVariable; 1544d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall LValue elementLValue; 1545d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall QualType elementType; 1546d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) { 154757b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall // Initialize the variable, in case it's a __block variable or something. 154857b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall EmitAutoVarInit(variable); 1549f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 155057b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall const VarDecl* D = cast<VarDecl>(SD->getSingleDecl()); 1551f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr tempDRE(const_cast<VarDecl*>(D), false, D->getType(), 1552d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall VK_LValue, SourceLocation()); 1553d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementLValue = EmitLValue(&tempDRE); 1554d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementType = D->getType(); 155557b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall elementIsVariable = true; 15567acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall 15577acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall if (D->isARCPseudoStrong()) 15587acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall elementLValue.getQuals().setObjCLifetime(Qualifiers::OCL_ExplicitNone); 1559d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall } else { 1560d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementLValue = LValue(); // suppress warning 1561d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementType = cast<Expr>(S.getElement())->getType(); 156257b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall elementIsVariable = false; 1563d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall } 15642acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *convertedElementType = ConvertType(elementType); 1565f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1566d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Fetch the buffer out of the enumeration state. 1567d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // TODO: this pointer should actually be invariant between 1568d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // refreshes, which would help us do certain loop optimizations. 1569d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *StateItemsPtr = 1570d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr"); 1571d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *EnumStateItems = 1572d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateLoad(StateItemsPtr, "stateitems"); 1573f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1574d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Fetch the value at the current index from the buffer. 15751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Value *CurrentItemPtr = 1576d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateGEP(EnumStateItems, index, "currentitem.ptr"); 1577d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr); 15781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1579d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Cast that value to the right type. 1580d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType, 1581d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall "currentitem"); 15821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1583d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Make sure we have an l-value. Yes, this gets evaluated every 1584d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // time through the loop. 15857acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall if (!elementIsVariable) { 1586d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementLValue = EmitLValue(cast<Expr>(S.getElement())); 1587545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue); 15887acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall } else { 15897acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall EmitScalarInit(CurrentItem, elementLValue); 15907acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall } 15911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 159257b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall // If we do have an element variable, this assignment is the end of 159357b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall // its initialization. 159457b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall if (elementIsVariable) 159557b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall EmitAutoVarCleanups(variable); 159657b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall 1597d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Perform the loop body, setting up break and continue labels. 1598e4b6d342c29d5cb9d311756100df1603810fa892Anders Carlsson BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody)); 1599d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall { 1600d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall RunCleanupsScope Scope(*this); 1601d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitStmt(S.getBody()); 1602d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall } 1603f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson BreakContinueStack.pop_back(); 16041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1605d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Destroy the element variable now. 1606d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementVariableScope.ForceCleanup(); 1607d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1608d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Check whether there are more elements. 1609ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall EmitBlock(AfterBody.getBlock()); 16101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1611d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch"); 1612d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1613d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // First we check in the local buffer. 1614d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *indexPlusOne 1615d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall = Builder.CreateAdd(index, llvm::ConstantInt::get(UnsignedLongLTy, 1)); 1616d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1617d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // If we haven't overrun the buffer yet, we can continue. 1618d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateCondBr(Builder.CreateICmpULT(indexPlusOne, count), 1619d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall LoopBodyBB, FetchMoreBB); 1620f0906c4edb37b20141428ca77fa7dfd00b976eafFariborz Jahanian 1621d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall index->addIncoming(indexPlusOne, AfterBody.getBlock()); 1622d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall count->addIncoming(count, AfterBody.getBlock()); 1623f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1624d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Otherwise, we have to fetch more elements. 1625d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(FetchMoreBB); 16261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump CountRV = 1628ef072fd2f3347cfd857d6eb787b245b950771430John McCall CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), 1629f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson getContext().UnsignedLongTy, 16301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump FastEnumSel, 1631c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall Collection, Args); 16321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1633d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // If we got a zero count, we're done. 1634d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *refetchCount = CountRV.getScalarVal(); 1635d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1636d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // (note that the message send might split FetchMoreBB) 1637d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall index->addIncoming(zero, Builder.GetInsertBlock()); 1638d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall count->addIncoming(refetchCount, Builder.GetInsertBlock()); 1639d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1640d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero), 1641d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmptyBB, LoopBodyBB); 16421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1643f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson // No more elements. 1644d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(EmptyBB); 1645f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 164657b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall if (!elementIsVariable) { 1647f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson // If the element was not a declaration, set it to be null. 1648f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1649d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *null = llvm::Constant::getNullValue(convertedElementType); 1650d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementLValue = EmitLValue(cast<Expr>(S.getElement())); 1651545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall EmitStoreThroughLValue(RValue::get(null), elementLValue); 1652f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson } 1653f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 165473fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher if (DI) 165573fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd()); 1656bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel 1657990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Leave the cleanup we entered in ARC. 16584e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().ObjCAutoRefCount) 1659990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall PopCleanupBlock(); 1660990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1661ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall EmitBlock(LoopEnd.getBlock()); 16623d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson} 16633d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson 16641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) { 1665f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall CGM.getObjCRuntime().EmitTryStmt(*this, S); 166664d5d6c5903157c521af496479d06dc26032d718Anders Carlsson} 166764d5d6c5903157c521af496479d06dc26032d718Anders Carlsson 16681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) { 166964d5d6c5903157c521af496479d06dc26032d718Anders Carlsson CGM.getObjCRuntime().EmitThrowStmt(*this, S); 167064d5d6c5903157c521af496479d06dc26032d718Anders Carlsson} 167164d5d6c5903157c521af496479d06dc26032d718Anders Carlsson 167210cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattnervoid CodeGenFunction::EmitObjCAtSynchronizedStmt( 16731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const ObjCAtSynchronizedStmt &S) { 1674f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S); 167510cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattner} 167610cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattner 167733e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall/// Produce the code for a CK_ARCProduceObject. Just does a 1678f85e193739c953358c865005855253af4f68a497John McCall/// primitive retain. 1679f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCProduceObject(QualType type, 1680f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value) { 1681f85e193739c953358c865005855253af4f68a497John McCall return EmitARCRetain(type, value); 1682f85e193739c953358c865005855253af4f68a497John McCall} 1683f85e193739c953358c865005855253af4f68a497John McCall 1684f85e193739c953358c865005855253af4f68a497John McCallnamespace { 1685f85e193739c953358c865005855253af4f68a497John McCall struct CallObjCRelease : EHScopeStack::Cleanup { 1686bddfd87863bac7aa17d226cdfb228f49b30dd5f2John McCall CallObjCRelease(llvm::Value *object) : object(object) {} 1687bddfd87863bac7aa17d226cdfb228f49b30dd5f2John McCall llvm::Value *object; 1688f85e193739c953358c865005855253af4f68a497John McCall 1689ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall void Emit(CodeGenFunction &CGF, Flags flags) { 16905b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall // Releases at the end of the full-expression are imprecise. 16915b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall CGF.EmitARCRelease(object, ARCImpreciseLifetime); 1692f85e193739c953358c865005855253af4f68a497John McCall } 1693f85e193739c953358c865005855253af4f68a497John McCall }; 1694f85e193739c953358c865005855253af4f68a497John McCall} 1695f85e193739c953358c865005855253af4f68a497John McCall 169633e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall/// Produce the code for a CK_ARCConsumeObject. Does a primitive 1697f85e193739c953358c865005855253af4f68a497John McCall/// release at the end of the full-expression. 1698f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCConsumeObject(QualType type, 1699f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *object) { 1700f85e193739c953358c865005855253af4f68a497John McCall // If we're in a conditional branch, we need to make the cleanup 1701bddfd87863bac7aa17d226cdfb228f49b30dd5f2John McCall // conditional. 1702bddfd87863bac7aa17d226cdfb228f49b30dd5f2John McCall pushFullExprCleanup<CallObjCRelease>(getARCCleanupKind(), object); 1703f85e193739c953358c865005855253af4f68a497John McCall return object; 1704f85e193739c953358c865005855253af4f68a497John McCall} 1705f85e193739c953358c865005855253af4f68a497John McCall 1706f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCExtendObjectLifetime(QualType type, 1707f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value) { 1708f85e193739c953358c865005855253af4f68a497John McCall return EmitARCRetainAutorelease(type, value); 1709f85e193739c953358c865005855253af4f68a497John McCall} 1710f85e193739c953358c865005855253af4f68a497John McCall 1711b6a6079449a5275c283982e19b0c38e165833bb2John McCall/// Given a number of pointers, inform the optimizer that they're 1712b6a6079449a5275c283982e19b0c38e165833bb2John McCall/// being intrinsically used up until this point in the program. 1713b6a6079449a5275c283982e19b0c38e165833bb2John McCallvoid CodeGenFunction::EmitARCIntrinsicUse(ArrayRef<llvm::Value*> values) { 1714b6a6079449a5275c283982e19b0c38e165833bb2John McCall llvm::Constant *&fn = CGM.getARCEntrypoints().clang_arc_use; 1715b6a6079449a5275c283982e19b0c38e165833bb2John McCall if (!fn) { 1716b6a6079449a5275c283982e19b0c38e165833bb2John McCall llvm::FunctionType *fnType = 1717b6a6079449a5275c283982e19b0c38e165833bb2John McCall llvm::FunctionType::get(CGM.VoidTy, ArrayRef<llvm::Type*>(), true); 1718b6a6079449a5275c283982e19b0c38e165833bb2John McCall fn = CGM.CreateRuntimeFunction(fnType, "clang.arc.use"); 1719b6a6079449a5275c283982e19b0c38e165833bb2John McCall } 1720b6a6079449a5275c283982e19b0c38e165833bb2John McCall 1721b6a6079449a5275c283982e19b0c38e165833bb2John McCall // This isn't really a "runtime" function, but as an intrinsic it 1722b6a6079449a5275c283982e19b0c38e165833bb2John McCall // doesn't really matter as long as we align things up. 1723b6a6079449a5275c283982e19b0c38e165833bb2John McCall EmitNounwindRuntimeCall(fn, values); 1724b6a6079449a5275c283982e19b0c38e165833bb2John McCall} 1725b6a6079449a5275c283982e19b0c38e165833bb2John McCall 1726f85e193739c953358c865005855253af4f68a497John McCall 1727f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Constant *createARCRuntimeFunction(CodeGenModule &CGM, 17282acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *type, 17295f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef fnName) { 1730f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *fn = CGM.CreateRuntimeFunction(type, fnName); 1731f85e193739c953358c865005855253af4f68a497John McCall 1732554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman if (llvm::Function *f = dyn_cast<llvm::Function>(fn)) { 1733cfe18a195128002c5861e362db1f11ee1bebbea6Michael Gottesman // If the target runtime doesn't naturally support ARC, emit weak 1734cfe18a195128002c5861e362db1f11ee1bebbea6Michael Gottesman // references to the runtime support library. We don't really 1735cfe18a195128002c5861e362db1f11ee1bebbea6Michael Gottesman // permit this to fail, but we need a particular relocation style. 1736554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman if (!CGM.getLangOpts().ObjCRuntime.hasNativeARC()) { 1737f85e193739c953358c865005855253af4f68a497John McCall f->setLinkage(llvm::Function::ExternalWeakLinkage); 1738554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman } else if (fnName == "objc_retain" || fnName == "objc_release") { 1739554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman // If we have Native ARC, set nonlazybind attribute for these APIs for 1740554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman // performance. 174172390b39c545426023ec104afe8706395d732badBill Wendling f->addFnAttr(llvm::Attribute::NonLazyBind); 1742db99e8b72ae2b66d3c2f8d896f49513b8d8e2633Michael Gottesman } 1743554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman } 1744f85e193739c953358c865005855253af4f68a497John McCall 1745f85e193739c953358c865005855253af4f68a497John McCall return fn; 1746f85e193739c953358c865005855253af4f68a497John McCall} 1747f85e193739c953358c865005855253af4f68a497John McCall 1748f85e193739c953358c865005855253af4f68a497John McCall/// Perform an operation having the signature 1749f85e193739c953358c865005855253af4f68a497John McCall/// i8* (i8*) 1750f85e193739c953358c865005855253af4f68a497John McCall/// where a null input causes a no-op and returns null. 1751f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCValueOperation(CodeGenFunction &CGF, 1752f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value, 1753f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn, 1754df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier StringRef fnName, 1755df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier bool isTailCall = false) { 1756f85e193739c953358c865005855253af4f68a497John McCall if (isa<llvm::ConstantPointerNull>(value)) return value; 1757f85e193739c953358c865005855253af4f68a497John McCall 1758f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 17592acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 176076ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrTy, false); 1761f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); 1762f85e193739c953358c865005855253af4f68a497John McCall } 1763f85e193739c953358c865005855253af4f68a497John McCall 1764f85e193739c953358c865005855253af4f68a497John McCall // Cast the argument to 'id'. 17652acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *origType = value->getType(); 1766f85e193739c953358c865005855253af4f68a497John McCall value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy); 1767f85e193739c953358c865005855253af4f68a497John McCall 1768f85e193739c953358c865005855253af4f68a497John McCall // Call the function. 1769bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::CallInst *call = CGF.EmitNounwindRuntimeCall(fn, value); 1770df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier if (isTailCall) 1771df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier call->setTailCall(); 1772f85e193739c953358c865005855253af4f68a497John McCall 1773f85e193739c953358c865005855253af4f68a497John McCall // Cast the result back to the original type. 1774f85e193739c953358c865005855253af4f68a497John McCall return CGF.Builder.CreateBitCast(call, origType); 1775f85e193739c953358c865005855253af4f68a497John McCall} 1776f85e193739c953358c865005855253af4f68a497John McCall 1777f85e193739c953358c865005855253af4f68a497John McCall/// Perform an operation having the following signature: 1778f85e193739c953358c865005855253af4f68a497John McCall/// i8* (i8**) 1779f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCLoadOperation(CodeGenFunction &CGF, 1780f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *addr, 1781f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn, 17825f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef fnName) { 1783f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 17842acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 178576ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrPtrTy, false); 1786f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); 1787f85e193739c953358c865005855253af4f68a497John McCall } 1788f85e193739c953358c865005855253af4f68a497John McCall 1789f85e193739c953358c865005855253af4f68a497John McCall // Cast the argument to 'id*'. 17902acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *origType = addr->getType(); 1791f85e193739c953358c865005855253af4f68a497John McCall addr = CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy); 1792f85e193739c953358c865005855253af4f68a497John McCall 1793f85e193739c953358c865005855253af4f68a497John McCall // Call the function. 1794bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *result = CGF.EmitNounwindRuntimeCall(fn, addr); 1795f85e193739c953358c865005855253af4f68a497John McCall 1796f85e193739c953358c865005855253af4f68a497John McCall // Cast the result back to a dereference of the original type. 1797f85e193739c953358c865005855253af4f68a497John McCall if (origType != CGF.Int8PtrPtrTy) 1798f85e193739c953358c865005855253af4f68a497John McCall result = CGF.Builder.CreateBitCast(result, 1799f85e193739c953358c865005855253af4f68a497John McCall cast<llvm::PointerType>(origType)->getElementType()); 1800f85e193739c953358c865005855253af4f68a497John McCall 1801f85e193739c953358c865005855253af4f68a497John McCall return result; 1802f85e193739c953358c865005855253af4f68a497John McCall} 1803f85e193739c953358c865005855253af4f68a497John McCall 1804f85e193739c953358c865005855253af4f68a497John McCall/// Perform an operation having the following signature: 1805f85e193739c953358c865005855253af4f68a497John McCall/// i8* (i8**, i8*) 1806f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCStoreOperation(CodeGenFunction &CGF, 1807f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *addr, 1808f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value, 1809f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn, 18105f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef fnName, 1811f85e193739c953358c865005855253af4f68a497John McCall bool ignored) { 1812f85e193739c953358c865005855253af4f68a497John McCall assert(cast<llvm::PointerType>(addr->getType())->getElementType() 1813f85e193739c953358c865005855253af4f68a497John McCall == value->getType()); 1814f85e193739c953358c865005855253af4f68a497John McCall 1815f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 18161d236ab930816f5da27bade92904914c44b73b4cBenjamin Kramer llvm::Type *argTypes[] = { CGF.Int8PtrPtrTy, CGF.Int8PtrTy }; 1817f85e193739c953358c865005855253af4f68a497John McCall 18182acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType 1819f85e193739c953358c865005855253af4f68a497John McCall = llvm::FunctionType::get(CGF.Int8PtrTy, argTypes, false); 1820f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); 1821f85e193739c953358c865005855253af4f68a497John McCall } 1822f85e193739c953358c865005855253af4f68a497John McCall 18232acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *origType = value->getType(); 1824f85e193739c953358c865005855253af4f68a497John McCall 1825bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *args[] = { 1826bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy), 1827bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy) 1828bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall }; 1829bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::CallInst *result = CGF.EmitNounwindRuntimeCall(fn, args); 1830f85e193739c953358c865005855253af4f68a497John McCall 1831f85e193739c953358c865005855253af4f68a497John McCall if (ignored) return 0; 1832f85e193739c953358c865005855253af4f68a497John McCall 1833f85e193739c953358c865005855253af4f68a497John McCall return CGF.Builder.CreateBitCast(result, origType); 1834f85e193739c953358c865005855253af4f68a497John McCall} 1835f85e193739c953358c865005855253af4f68a497John McCall 1836f85e193739c953358c865005855253af4f68a497John McCall/// Perform an operation having the following signature: 1837f85e193739c953358c865005855253af4f68a497John McCall/// void (i8**, i8**) 1838f85e193739c953358c865005855253af4f68a497John McCallstatic void emitARCCopyOperation(CodeGenFunction &CGF, 1839f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *dst, 1840f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *src, 1841f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn, 18425f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef fnName) { 1843f85e193739c953358c865005855253af4f68a497John McCall assert(dst->getType() == src->getType()); 1844f85e193739c953358c865005855253af4f68a497John McCall 1845f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 184676ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::Type *argTypes[] = { CGF.Int8PtrPtrTy, CGF.Int8PtrPtrTy }; 184776ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer 18482acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType 1849f85e193739c953358c865005855253af4f68a497John McCall = llvm::FunctionType::get(CGF.Builder.getVoidTy(), argTypes, false); 1850f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); 1851f85e193739c953358c865005855253af4f68a497John McCall } 1852f85e193739c953358c865005855253af4f68a497John McCall 1853bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *args[] = { 1854bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall CGF.Builder.CreateBitCast(dst, CGF.Int8PtrPtrTy), 1855bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall CGF.Builder.CreateBitCast(src, CGF.Int8PtrPtrTy) 1856bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall }; 1857bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall CGF.EmitNounwindRuntimeCall(fn, args); 1858f85e193739c953358c865005855253af4f68a497John McCall} 1859f85e193739c953358c865005855253af4f68a497John McCall 1860f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do a retain. Based on the type, calls one of: 18619d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retain(i8* %value) 18629d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainBlock(i8* %value) 1863f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCRetain(QualType type, llvm::Value *value) { 1864f85e193739c953358c865005855253af4f68a497John McCall if (type->isBlockPointerType()) 1865348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall return EmitARCRetainBlock(value, /*mandatory*/ false); 1866f85e193739c953358c865005855253af4f68a497John McCall else 1867f85e193739c953358c865005855253af4f68a497John McCall return EmitARCRetainNonBlock(value); 1868f85e193739c953358c865005855253af4f68a497John McCall} 1869f85e193739c953358c865005855253af4f68a497John McCall 1870f85e193739c953358c865005855253af4f68a497John McCall/// Retain the given object, with normal retain semantics. 18719d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retain(i8* %value) 1872f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCRetainNonBlock(llvm::Value *value) { 1873f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 1874f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_retain, 1875f85e193739c953358c865005855253af4f68a497John McCall "objc_retain"); 1876f85e193739c953358c865005855253af4f68a497John McCall} 1877f85e193739c953358c865005855253af4f68a497John McCall 1878f85e193739c953358c865005855253af4f68a497John McCall/// Retain the given block, with _Block_copy semantics. 18799d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainBlock(i8* %value) 1880348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall/// 1881348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall/// \param mandatory - If false, emit the call with metadata 1882348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall/// indicating that it's okay for the optimizer to eliminate this call 1883348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall/// if it can prove that the block never escapes except down the stack. 1884348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCallllvm::Value *CodeGenFunction::EmitARCRetainBlock(llvm::Value *value, 1885348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall bool mandatory) { 1886348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall llvm::Value *result 1887348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall = emitARCValueOperation(*this, value, 1888348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall CGM.getARCEntrypoints().objc_retainBlock, 1889348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall "objc_retainBlock"); 1890348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 1891348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall // If the copy isn't mandatory, add !clang.arc.copy_on_escape to 1892348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall // tell the optimizer that it doesn't need to do this copy if the 1893348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall // block doesn't escape, where being passed as an argument doesn't 1894348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall // count as escaping. 1895348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall if (!mandatory && isa<llvm::Instruction>(result)) { 1896348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall llvm::CallInst *call 1897348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall = cast<llvm::CallInst>(result->stripPointerCasts()); 1898348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall assert(call->getCalledValue() == CGM.getARCEntrypoints().objc_retainBlock); 1899348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 1900348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall SmallVector<llvm::Value*,1> args; 1901348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall call->setMetadata("clang.arc.copy_on_escape", 1902348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall llvm::MDNode::get(Builder.getContext(), args)); 1903348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall } 1904348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 1905348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall return result; 1906f85e193739c953358c865005855253af4f68a497John McCall} 1907f85e193739c953358c865005855253af4f68a497John McCall 1908f85e193739c953358c865005855253af4f68a497John McCall/// Retain the given object which is the result of a function call. 19099d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainAutoreleasedReturnValue(i8* %value) 1910f85e193739c953358c865005855253af4f68a497John McCall/// 1911f85e193739c953358c865005855253af4f68a497John McCall/// Yes, this function name is one character away from a different 1912f85e193739c953358c865005855253af4f68a497John McCall/// call with completely different semantics. 1913f85e193739c953358c865005855253af4f68a497John McCallllvm::Value * 1914f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCRetainAutoreleasedReturnValue(llvm::Value *value) { 1915f85e193739c953358c865005855253af4f68a497John McCall // Fetch the void(void) inline asm which marks that we're going to 1916f85e193739c953358c865005855253af4f68a497John McCall // retain the autoreleased return value. 1917f85e193739c953358c865005855253af4f68a497John McCall llvm::InlineAsm *&marker 1918f85e193739c953358c865005855253af4f68a497John McCall = CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker; 1919f85e193739c953358c865005855253af4f68a497John McCall if (!marker) { 19205f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef assembly 1921f85e193739c953358c865005855253af4f68a497John McCall = CGM.getTargetCodeGenInfo() 1922f85e193739c953358c865005855253af4f68a497John McCall .getARCRetainAutoreleasedReturnValueMarker(); 1923f85e193739c953358c865005855253af4f68a497John McCall 1924f85e193739c953358c865005855253af4f68a497John McCall // If we have an empty assembly string, there's nothing to do. 1925f85e193739c953358c865005855253af4f68a497John McCall if (assembly.empty()) { 1926f85e193739c953358c865005855253af4f68a497John McCall 1927f85e193739c953358c865005855253af4f68a497John McCall // Otherwise, at -O0, build an inline asm that we're going to call 1928f85e193739c953358c865005855253af4f68a497John McCall // in a moment. 1929f85e193739c953358c865005855253af4f68a497John McCall } else if (CGM.getCodeGenOpts().OptimizationLevel == 0) { 1930f85e193739c953358c865005855253af4f68a497John McCall llvm::FunctionType *type = 19318b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner llvm::FunctionType::get(VoidTy, /*variadic*/false); 1932f85e193739c953358c865005855253af4f68a497John McCall 1933f85e193739c953358c865005855253af4f68a497John McCall marker = llvm::InlineAsm::get(type, assembly, "", /*sideeffects*/ true); 1934f85e193739c953358c865005855253af4f68a497John McCall 1935f85e193739c953358c865005855253af4f68a497John McCall // If we're at -O1 and above, we don't want to litter the code 1936f85e193739c953358c865005855253af4f68a497John McCall // with this marker yet, so leave a breadcrumb for the ARC 1937f85e193739c953358c865005855253af4f68a497John McCall // optimizer to pick up. 1938f85e193739c953358c865005855253af4f68a497John McCall } else { 1939f85e193739c953358c865005855253af4f68a497John McCall llvm::NamedMDNode *metadata = 1940f85e193739c953358c865005855253af4f68a497John McCall CGM.getModule().getOrInsertNamedMetadata( 1941f85e193739c953358c865005855253af4f68a497John McCall "clang.arc.retainAutoreleasedReturnValueMarker"); 1942f85e193739c953358c865005855253af4f68a497John McCall assert(metadata->getNumOperands() <= 1); 1943f85e193739c953358c865005855253af4f68a497John McCall if (metadata->getNumOperands() == 0) { 1944f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *string = llvm::MDString::get(getLLVMContext(), assembly); 1945da549e8995c447542d5631b8b67fcc3a9582797aJay Foad metadata->addOperand(llvm::MDNode::get(getLLVMContext(), string)); 1946f85e193739c953358c865005855253af4f68a497John McCall } 1947f85e193739c953358c865005855253af4f68a497John McCall } 1948f85e193739c953358c865005855253af4f68a497John McCall } 1949f85e193739c953358c865005855253af4f68a497John McCall 1950f85e193739c953358c865005855253af4f68a497John McCall // Call the marker asm if we made one, which we do only at -O0. 1951f85e193739c953358c865005855253af4f68a497John McCall if (marker) Builder.CreateCall(marker); 1952f85e193739c953358c865005855253af4f68a497John McCall 1953f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 1954f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_retainAutoreleasedReturnValue, 1955f85e193739c953358c865005855253af4f68a497John McCall "objc_retainAutoreleasedReturnValue"); 1956f85e193739c953358c865005855253af4f68a497John McCall} 1957f85e193739c953358c865005855253af4f68a497John McCall 1958f85e193739c953358c865005855253af4f68a497John McCall/// Release the given object. 19599d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call void \@objc_release(i8* %value) 19605b07e8077a20b80fee90bd76c43c6150c676e4a8John McCallvoid CodeGenFunction::EmitARCRelease(llvm::Value *value, 19615b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall ARCPreciseLifetime_t precise) { 1962f85e193739c953358c865005855253af4f68a497John McCall if (isa<llvm::ConstantPointerNull>(value)) return; 1963f85e193739c953358c865005855253af4f68a497John McCall 1964f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn = CGM.getARCEntrypoints().objc_release; 1965f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 19662acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 196776ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false); 1968f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGM, fnType, "objc_release"); 1969f85e193739c953358c865005855253af4f68a497John McCall } 1970f85e193739c953358c865005855253af4f68a497John McCall 1971f85e193739c953358c865005855253af4f68a497John McCall // Cast the argument to 'id'. 1972f85e193739c953358c865005855253af4f68a497John McCall value = Builder.CreateBitCast(value, Int8PtrTy); 1973f85e193739c953358c865005855253af4f68a497John McCall 1974f85e193739c953358c865005855253af4f68a497John McCall // Call objc_release. 1975bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::CallInst *call = EmitNounwindRuntimeCall(fn, value); 1976f85e193739c953358c865005855253af4f68a497John McCall 19775b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall if (precise == ARCImpreciseLifetime) { 19785f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<llvm::Value*,1> args; 1979f85e193739c953358c865005855253af4f68a497John McCall call->setMetadata("clang.imprecise_release", 1980f85e193739c953358c865005855253af4f68a497John McCall llvm::MDNode::get(Builder.getContext(), args)); 1981f85e193739c953358c865005855253af4f68a497John McCall } 1982f85e193739c953358c865005855253af4f68a497John McCall} 1983f85e193739c953358c865005855253af4f68a497John McCall 1984015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// Destroy a __strong variable. 1985015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// 1986015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// At -O0, emit a call to store 'null' into the address; 1987015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// instrumenting tools prefer this because the address is exposed, 1988015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// but it's relatively cumbersome to optimize. 1989015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// 1990015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// At -O1 and above, just load and call objc_release. 1991015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// 1992015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// call void \@objc_storeStrong(i8** %addr, i8* null) 19935b07e8077a20b80fee90bd76c43c6150c676e4a8John McCallvoid CodeGenFunction::EmitARCDestroyStrong(llvm::Value *addr, 19945b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall ARCPreciseLifetime_t precise) { 1995015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall if (CGM.getCodeGenOpts().OptimizationLevel == 0) { 1996015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall llvm::PointerType *addrTy = cast<llvm::PointerType>(addr->getType()); 1997015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall llvm::Value *null = llvm::ConstantPointerNull::get( 1998015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall cast<llvm::PointerType>(addrTy->getElementType())); 1999015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall EmitARCStoreStrongCall(addr, null, /*ignored*/ true); 2000015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall return; 2001015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall } 2002015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall 2003015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall llvm::Value *value = Builder.CreateLoad(addr); 2004015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall EmitARCRelease(value, precise); 2005015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall} 2006015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall 2007f85e193739c953358c865005855253af4f68a497John McCall/// Store into a strong object. Always calls this: 20089d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call void \@objc_storeStrong(i8** %addr, i8* %value) 2009f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCStoreStrongCall(llvm::Value *addr, 2010f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value, 2011f85e193739c953358c865005855253af4f68a497John McCall bool ignored) { 2012f85e193739c953358c865005855253af4f68a497John McCall assert(cast<llvm::PointerType>(addr->getType())->getElementType() 2013f85e193739c953358c865005855253af4f68a497John McCall == value->getType()); 2014f85e193739c953358c865005855253af4f68a497John McCall 2015f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn = CGM.getARCEntrypoints().objc_storeStrong; 2016f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 20179cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::Type *argTypes[] = { Int8PtrPtrTy, Int8PtrTy }; 20182acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType 2019f85e193739c953358c865005855253af4f68a497John McCall = llvm::FunctionType::get(Builder.getVoidTy(), argTypes, false); 2020f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGM, fnType, "objc_storeStrong"); 2021f85e193739c953358c865005855253af4f68a497John McCall } 2022f85e193739c953358c865005855253af4f68a497John McCall 2023bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *args[] = { 2024bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall Builder.CreateBitCast(addr, Int8PtrPtrTy), 2025bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall Builder.CreateBitCast(value, Int8PtrTy) 2026bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall }; 2027bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall EmitNounwindRuntimeCall(fn, args); 2028f85e193739c953358c865005855253af4f68a497John McCall 2029f85e193739c953358c865005855253af4f68a497John McCall if (ignored) return 0; 2030f85e193739c953358c865005855253af4f68a497John McCall return value; 2031f85e193739c953358c865005855253af4f68a497John McCall} 2032f85e193739c953358c865005855253af4f68a497John McCall 2033f85e193739c953358c865005855253af4f68a497John McCall/// Store into a strong object. Sometimes calls this: 20349d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call void \@objc_storeStrong(i8** %addr, i8* %value) 2035f85e193739c953358c865005855253af4f68a497John McCall/// Other times, breaks it down into components. 2036545d996ec5a3113f046944f11b27cc2d6cb055b4John McCallllvm::Value *CodeGenFunction::EmitARCStoreStrong(LValue dst, 2037f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *newValue, 2038f85e193739c953358c865005855253af4f68a497John McCall bool ignored) { 2039545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall QualType type = dst.getType(); 2040f85e193739c953358c865005855253af4f68a497John McCall bool isBlock = type->isBlockPointerType(); 2041f85e193739c953358c865005855253af4f68a497John McCall 2042f85e193739c953358c865005855253af4f68a497John McCall // Use a store barrier at -O0 unless this is a block type or the 2043f85e193739c953358c865005855253af4f68a497John McCall // lvalue is inadequately aligned. 2044f85e193739c953358c865005855253af4f68a497John McCall if (shouldUseFusedARCCalls() && 2045f85e193739c953358c865005855253af4f68a497John McCall !isBlock && 20466da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman (dst.getAlignment().isZero() || 20476da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman dst.getAlignment() >= CharUnits::fromQuantity(PointerAlignInBytes))) { 2048f85e193739c953358c865005855253af4f68a497John McCall return EmitARCStoreStrongCall(dst.getAddress(), newValue, ignored); 2049f85e193739c953358c865005855253af4f68a497John McCall } 2050f85e193739c953358c865005855253af4f68a497John McCall 2051f85e193739c953358c865005855253af4f68a497John McCall // Otherwise, split it out. 2052f85e193739c953358c865005855253af4f68a497John McCall 2053f85e193739c953358c865005855253af4f68a497John McCall // Retain the new value. 2054f85e193739c953358c865005855253af4f68a497John McCall newValue = EmitARCRetain(type, newValue); 2055f85e193739c953358c865005855253af4f68a497John McCall 2056f85e193739c953358c865005855253af4f68a497John McCall // Read the old value. 2057545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall llvm::Value *oldValue = EmitLoadOfScalar(dst); 2058f85e193739c953358c865005855253af4f68a497John McCall 2059f85e193739c953358c865005855253af4f68a497John McCall // Store. We do this before the release so that any deallocs won't 2060f85e193739c953358c865005855253af4f68a497John McCall // see the old value. 2061545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall EmitStoreOfScalar(newValue, dst); 2062f85e193739c953358c865005855253af4f68a497John McCall 2063f85e193739c953358c865005855253af4f68a497John McCall // Finally, release the old value. 20645b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall EmitARCRelease(oldValue, dst.isARCPreciseLifetime()); 2065f85e193739c953358c865005855253af4f68a497John McCall 2066f85e193739c953358c865005855253af4f68a497John McCall return newValue; 2067f85e193739c953358c865005855253af4f68a497John McCall} 2068f85e193739c953358c865005855253af4f68a497John McCall 2069f85e193739c953358c865005855253af4f68a497John McCall/// Autorelease the given object. 20709d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_autorelease(i8* %value) 2071f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCAutorelease(llvm::Value *value) { 2072f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 2073f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_autorelease, 2074f85e193739c953358c865005855253af4f68a497John McCall "objc_autorelease"); 2075f85e193739c953358c865005855253af4f68a497John McCall} 2076f85e193739c953358c865005855253af4f68a497John McCall 2077f85e193739c953358c865005855253af4f68a497John McCall/// Autorelease the given object. 20789d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_autoreleaseReturnValue(i8* %value) 2079f85e193739c953358c865005855253af4f68a497John McCallllvm::Value * 2080f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCAutoreleaseReturnValue(llvm::Value *value) { 2081f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 2082f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_autoreleaseReturnValue, 2083df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier "objc_autoreleaseReturnValue", 2084df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier /*isTailCall*/ true); 2085f85e193739c953358c865005855253af4f68a497John McCall} 2086f85e193739c953358c865005855253af4f68a497John McCall 2087f85e193739c953358c865005855253af4f68a497John McCall/// Do a fused retain/autorelease of the given object. 20889d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainAutoreleaseReturnValue(i8* %value) 2089f85e193739c953358c865005855253af4f68a497John McCallllvm::Value * 2090f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCRetainAutoreleaseReturnValue(llvm::Value *value) { 2091f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 2092f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_retainAutoreleaseReturnValue, 2093df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier "objc_retainAutoreleaseReturnValue", 2094df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier /*isTailCall*/ true); 2095f85e193739c953358c865005855253af4f68a497John McCall} 2096f85e193739c953358c865005855253af4f68a497John McCall 2097f85e193739c953358c865005855253af4f68a497John McCall/// Do a fused retain/autorelease of the given object. 20989d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainAutorelease(i8* %value) 2099f85e193739c953358c865005855253af4f68a497John McCall/// or 21009d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// %retain = call i8* \@objc_retainBlock(i8* %value) 21019d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_autorelease(i8* %retain) 2102f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCRetainAutorelease(QualType type, 2103f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value) { 2104f85e193739c953358c865005855253af4f68a497John McCall if (!type->isBlockPointerType()) 2105f85e193739c953358c865005855253af4f68a497John McCall return EmitARCRetainAutoreleaseNonBlock(value); 2106f85e193739c953358c865005855253af4f68a497John McCall 2107f85e193739c953358c865005855253af4f68a497John McCall if (isa<llvm::ConstantPointerNull>(value)) return value; 2108f85e193739c953358c865005855253af4f68a497John McCall 21092acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *origType = value->getType(); 2110f85e193739c953358c865005855253af4f68a497John McCall value = Builder.CreateBitCast(value, Int8PtrTy); 2111348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall value = EmitARCRetainBlock(value, /*mandatory*/ true); 2112f85e193739c953358c865005855253af4f68a497John McCall value = EmitARCAutorelease(value); 2113f85e193739c953358c865005855253af4f68a497John McCall return Builder.CreateBitCast(value, origType); 2114f85e193739c953358c865005855253af4f68a497John McCall} 2115f85e193739c953358c865005855253af4f68a497John McCall 2116f85e193739c953358c865005855253af4f68a497John McCall/// Do a fused retain/autorelease of the given object. 21179d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainAutorelease(i8* %value) 2118f85e193739c953358c865005855253af4f68a497John McCallllvm::Value * 2119f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCRetainAutoreleaseNonBlock(llvm::Value *value) { 2120f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 2121f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_retainAutorelease, 2122f85e193739c953358c865005855253af4f68a497John McCall "objc_retainAutorelease"); 2123f85e193739c953358c865005855253af4f68a497John McCall} 2124f85e193739c953358c865005855253af4f68a497John McCall 21259d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// i8* \@objc_loadWeak(i8** %addr) 2126f85e193739c953358c865005855253af4f68a497John McCall/// Essentially objc_autorelease(objc_loadWeakRetained(addr)). 2127f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCLoadWeak(llvm::Value *addr) { 2128f85e193739c953358c865005855253af4f68a497John McCall return emitARCLoadOperation(*this, addr, 2129f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_loadWeak, 2130f85e193739c953358c865005855253af4f68a497John McCall "objc_loadWeak"); 2131f85e193739c953358c865005855253af4f68a497John McCall} 2132f85e193739c953358c865005855253af4f68a497John McCall 21339d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// i8* \@objc_loadWeakRetained(i8** %addr) 2134f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCLoadWeakRetained(llvm::Value *addr) { 2135f85e193739c953358c865005855253af4f68a497John McCall return emitARCLoadOperation(*this, addr, 2136f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_loadWeakRetained, 2137f85e193739c953358c865005855253af4f68a497John McCall "objc_loadWeakRetained"); 2138f85e193739c953358c865005855253af4f68a497John McCall} 2139f85e193739c953358c865005855253af4f68a497John McCall 21409d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// i8* \@objc_storeWeak(i8** %addr, i8* %value) 2141f85e193739c953358c865005855253af4f68a497John McCall/// Returns %value. 2142f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCStoreWeak(llvm::Value *addr, 2143f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value, 2144f85e193739c953358c865005855253af4f68a497John McCall bool ignored) { 2145f85e193739c953358c865005855253af4f68a497John McCall return emitARCStoreOperation(*this, addr, value, 2146f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_storeWeak, 2147f85e193739c953358c865005855253af4f68a497John McCall "objc_storeWeak", ignored); 2148f85e193739c953358c865005855253af4f68a497John McCall} 2149f85e193739c953358c865005855253af4f68a497John McCall 21509d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// i8* \@objc_initWeak(i8** %addr, i8* %value) 2151f85e193739c953358c865005855253af4f68a497John McCall/// Returns %value. %addr is known to not have a current weak entry. 2152f85e193739c953358c865005855253af4f68a497John McCall/// Essentially equivalent to: 2153f85e193739c953358c865005855253af4f68a497John McCall/// *addr = nil; objc_storeWeak(addr, value); 2154f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitARCInitWeak(llvm::Value *addr, llvm::Value *value) { 2155f85e193739c953358c865005855253af4f68a497John McCall // If we're initializing to null, just write null to memory; no need 2156f85e193739c953358c865005855253af4f68a497John McCall // to get the runtime involved. But don't do this if optimization 2157f85e193739c953358c865005855253af4f68a497John McCall // is enabled, because accounting for this would make the optimizer 2158f85e193739c953358c865005855253af4f68a497John McCall // much more complicated. 2159f85e193739c953358c865005855253af4f68a497John McCall if (isa<llvm::ConstantPointerNull>(value) && 2160f85e193739c953358c865005855253af4f68a497John McCall CGM.getCodeGenOpts().OptimizationLevel == 0) { 2161f85e193739c953358c865005855253af4f68a497John McCall Builder.CreateStore(value, addr); 2162f85e193739c953358c865005855253af4f68a497John McCall return; 2163f85e193739c953358c865005855253af4f68a497John McCall } 2164f85e193739c953358c865005855253af4f68a497John McCall 2165f85e193739c953358c865005855253af4f68a497John McCall emitARCStoreOperation(*this, addr, value, 2166f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_initWeak, 2167f85e193739c953358c865005855253af4f68a497John McCall "objc_initWeak", /*ignored*/ true); 2168f85e193739c953358c865005855253af4f68a497John McCall} 2169f85e193739c953358c865005855253af4f68a497John McCall 21709d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// void \@objc_destroyWeak(i8** %addr) 2171f85e193739c953358c865005855253af4f68a497John McCall/// Essentially objc_storeWeak(addr, nil). 2172f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitARCDestroyWeak(llvm::Value *addr) { 2173f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn = CGM.getARCEntrypoints().objc_destroyWeak; 2174f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 21752acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 217676ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrPtrTy, false); 2177f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGM, fnType, "objc_destroyWeak"); 2178f85e193739c953358c865005855253af4f68a497John McCall } 2179f85e193739c953358c865005855253af4f68a497John McCall 2180f85e193739c953358c865005855253af4f68a497John McCall // Cast the argument to 'id*'. 2181f85e193739c953358c865005855253af4f68a497John McCall addr = Builder.CreateBitCast(addr, Int8PtrPtrTy); 2182f85e193739c953358c865005855253af4f68a497John McCall 2183bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall EmitNounwindRuntimeCall(fn, addr); 2184f85e193739c953358c865005855253af4f68a497John McCall} 2185f85e193739c953358c865005855253af4f68a497John McCall 21869d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// void \@objc_moveWeak(i8** %dest, i8** %src) 2187f85e193739c953358c865005855253af4f68a497John McCall/// Disregards the current value in %dest. Leaves %src pointing to nothing. 2188f85e193739c953358c865005855253af4f68a497John McCall/// Essentially (objc_copyWeak(dest, src), objc_destroyWeak(src)). 2189f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitARCMoveWeak(llvm::Value *dst, llvm::Value *src) { 2190f85e193739c953358c865005855253af4f68a497John McCall emitARCCopyOperation(*this, dst, src, 2191f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_moveWeak, 2192f85e193739c953358c865005855253af4f68a497John McCall "objc_moveWeak"); 2193f85e193739c953358c865005855253af4f68a497John McCall} 2194f85e193739c953358c865005855253af4f68a497John McCall 21959d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// void \@objc_copyWeak(i8** %dest, i8** %src) 2196f85e193739c953358c865005855253af4f68a497John McCall/// Disregards the current value in %dest. Essentially 2197f85e193739c953358c865005855253af4f68a497John McCall/// objc_release(objc_initWeak(dest, objc_readWeakRetained(src))) 2198f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitARCCopyWeak(llvm::Value *dst, llvm::Value *src) { 2199f85e193739c953358c865005855253af4f68a497John McCall emitARCCopyOperation(*this, dst, src, 2200f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_copyWeak, 2201f85e193739c953358c865005855253af4f68a497John McCall "objc_copyWeak"); 2202f85e193739c953358c865005855253af4f68a497John McCall} 2203f85e193739c953358c865005855253af4f68a497John McCall 2204f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do a objc_autoreleasepool_push. 22059d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_autoreleasePoolPush(void) 2206f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCAutoreleasePoolPush() { 2207f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn = CGM.getRREntrypoints().objc_autoreleasePoolPush; 2208f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 22092acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 2210f85e193739c953358c865005855253af4f68a497John McCall llvm::FunctionType::get(Int8PtrTy, false); 2211f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPush"); 2212f85e193739c953358c865005855253af4f68a497John McCall } 2213f85e193739c953358c865005855253af4f68a497John McCall 2214bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall return EmitNounwindRuntimeCall(fn); 2215f85e193739c953358c865005855253af4f68a497John McCall} 2216f85e193739c953358c865005855253af4f68a497John McCall 2217f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do a primitive release. 22189d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call void \@objc_autoreleasePoolPop(i8* %ptr) 2219f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitObjCAutoreleasePoolPop(llvm::Value *value) { 2220f85e193739c953358c865005855253af4f68a497John McCall assert(value->getType() == Int8PtrTy); 2221f85e193739c953358c865005855253af4f68a497John McCall 2222f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn = CGM.getRREntrypoints().objc_autoreleasePoolPop; 2223f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 22242acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 222576ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false); 2226f85e193739c953358c865005855253af4f68a497John McCall 2227f85e193739c953358c865005855253af4f68a497John McCall // We don't want to use a weak import here; instead we should not 2228f85e193739c953358c865005855253af4f68a497John McCall // fall into this path. 2229f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPop"); 2230f85e193739c953358c865005855253af4f68a497John McCall } 2231f85e193739c953358c865005855253af4f68a497John McCall 2232bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall EmitNounwindRuntimeCall(fn, value); 2233f85e193739c953358c865005855253af4f68a497John McCall} 2234f85e193739c953358c865005855253af4f68a497John McCall 2235f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do an MRR version objc_autoreleasepool_push. 2236f85e193739c953358c865005855253af4f68a497John McCall/// Which is: [[NSAutoreleasePool alloc] init]; 2237f85e193739c953358c865005855253af4f68a497John McCall/// Where alloc is declared as: + (id) alloc; in NSAutoreleasePool class. 2238f85e193739c953358c865005855253af4f68a497John McCall/// init is declared as: - (id) init; in its NSObject super class. 2239f85e193739c953358c865005855253af4f68a497John McCall/// 2240f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCMRRAutoreleasePoolPush() { 2241f85e193739c953358c865005855253af4f68a497John McCall CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 2242bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *Receiver = Runtime.EmitNSAutoreleasePoolClassRef(*this); 2243f85e193739c953358c865005855253af4f68a497John McCall // [NSAutoreleasePool alloc] 2244f85e193739c953358c865005855253af4f68a497John McCall IdentifierInfo *II = &CGM.getContext().Idents.get("alloc"); 2245f85e193739c953358c865005855253af4f68a497John McCall Selector AllocSel = getContext().Selectors.getSelector(0, &II); 2246f85e193739c953358c865005855253af4f68a497John McCall CallArgList Args; 2247f85e193739c953358c865005855253af4f68a497John McCall RValue AllocRV = 2248f85e193739c953358c865005855253af4f68a497John McCall Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 2249f85e193739c953358c865005855253af4f68a497John McCall getContext().getObjCIdType(), 2250f85e193739c953358c865005855253af4f68a497John McCall AllocSel, Receiver, Args); 2251f85e193739c953358c865005855253af4f68a497John McCall 2252f85e193739c953358c865005855253af4f68a497John McCall // [Receiver init] 2253f85e193739c953358c865005855253af4f68a497John McCall Receiver = AllocRV.getScalarVal(); 2254f85e193739c953358c865005855253af4f68a497John McCall II = &CGM.getContext().Idents.get("init"); 2255f85e193739c953358c865005855253af4f68a497John McCall Selector InitSel = getContext().Selectors.getSelector(0, &II); 2256f85e193739c953358c865005855253af4f68a497John McCall RValue InitRV = 2257f85e193739c953358c865005855253af4f68a497John McCall Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 2258f85e193739c953358c865005855253af4f68a497John McCall getContext().getObjCIdType(), 2259f85e193739c953358c865005855253af4f68a497John McCall InitSel, Receiver, Args); 2260f85e193739c953358c865005855253af4f68a497John McCall return InitRV.getScalarVal(); 2261f85e193739c953358c865005855253af4f68a497John McCall} 2262f85e193739c953358c865005855253af4f68a497John McCall 2263f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do a primitive release. 2264f85e193739c953358c865005855253af4f68a497John McCall/// [tmp drain]; 2265f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitObjCMRRAutoreleasePoolPop(llvm::Value *Arg) { 2266f85e193739c953358c865005855253af4f68a497John McCall IdentifierInfo *II = &CGM.getContext().Idents.get("drain"); 2267f85e193739c953358c865005855253af4f68a497John McCall Selector DrainSel = getContext().Selectors.getSelector(0, &II); 2268f85e193739c953358c865005855253af4f68a497John McCall CallArgList Args; 2269f85e193739c953358c865005855253af4f68a497John McCall CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), 2270f85e193739c953358c865005855253af4f68a497John McCall getContext().VoidTy, DrainSel, Arg, Args); 2271f85e193739c953358c865005855253af4f68a497John McCall} 2272f85e193739c953358c865005855253af4f68a497John McCall 2273bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCallvoid CodeGenFunction::destroyARCStrongPrecise(CodeGenFunction &CGF, 2274bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall llvm::Value *addr, 2275bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall QualType type) { 22765b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall CGF.EmitARCDestroyStrong(addr, ARCPreciseLifetime); 2277bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall} 2278bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 2279bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCallvoid CodeGenFunction::destroyARCStrongImprecise(CodeGenFunction &CGF, 2280bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall llvm::Value *addr, 2281bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall QualType type) { 22825b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall CGF.EmitARCDestroyStrong(addr, ARCImpreciseLifetime); 2283bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall} 2284bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 2285bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCallvoid CodeGenFunction::destroyARCWeak(CodeGenFunction &CGF, 2286bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall llvm::Value *addr, 2287bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall QualType type) { 2288bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall CGF.EmitARCDestroyWeak(addr); 2289bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall} 2290bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 2291f85e193739c953358c865005855253af4f68a497John McCallnamespace { 2292f85e193739c953358c865005855253af4f68a497John McCall struct CallObjCAutoreleasePoolObject : EHScopeStack::Cleanup { 2293f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *Token; 2294f85e193739c953358c865005855253af4f68a497John McCall 2295f85e193739c953358c865005855253af4f68a497John McCall CallObjCAutoreleasePoolObject(llvm::Value *token) : Token(token) {} 2296f85e193739c953358c865005855253af4f68a497John McCall 2297ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall void Emit(CodeGenFunction &CGF, Flags flags) { 2298f85e193739c953358c865005855253af4f68a497John McCall CGF.EmitObjCAutoreleasePoolPop(Token); 2299f85e193739c953358c865005855253af4f68a497John McCall } 2300f85e193739c953358c865005855253af4f68a497John McCall }; 2301f85e193739c953358c865005855253af4f68a497John McCall struct CallObjCMRRAutoreleasePoolObject : EHScopeStack::Cleanup { 2302f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *Token; 2303f85e193739c953358c865005855253af4f68a497John McCall 2304f85e193739c953358c865005855253af4f68a497John McCall CallObjCMRRAutoreleasePoolObject(llvm::Value *token) : Token(token) {} 2305f85e193739c953358c865005855253af4f68a497John McCall 2306ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall void Emit(CodeGenFunction &CGF, Flags flags) { 2307f85e193739c953358c865005855253af4f68a497John McCall CGF.EmitObjCMRRAutoreleasePoolPop(Token); 2308f85e193739c953358c865005855253af4f68a497John McCall } 2309f85e193739c953358c865005855253af4f68a497John McCall }; 2310f85e193739c953358c865005855253af4f68a497John McCall} 2311f85e193739c953358c865005855253af4f68a497John McCall 2312f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr) { 23134e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().ObjCAutoRefCount) 2314f85e193739c953358c865005855253af4f68a497John McCall EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, Ptr); 2315f85e193739c953358c865005855253af4f68a497John McCall else 2316f85e193739c953358c865005855253af4f68a497John McCall EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, Ptr); 2317f85e193739c953358c865005855253af4f68a497John McCall} 2318f85e193739c953358c865005855253af4f68a497John McCall 2319f85e193739c953358c865005855253af4f68a497John McCallstatic TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF, 2320f85e193739c953358c865005855253af4f68a497John McCall LValue lvalue, 2321f85e193739c953358c865005855253af4f68a497John McCall QualType type) { 2322f85e193739c953358c865005855253af4f68a497John McCall switch (type.getObjCLifetime()) { 2323f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_None: 2324f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_ExplicitNone: 2325f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_Strong: 2326f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_Autoreleasing: 2327545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall return TryEmitResult(CGF.EmitLoadOfLValue(lvalue).getScalarVal(), 2328f85e193739c953358c865005855253af4f68a497John McCall false); 2329f85e193739c953358c865005855253af4f68a497John McCall 2330f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_Weak: 2331f85e193739c953358c865005855253af4f68a497John McCall return TryEmitResult(CGF.EmitARCLoadWeakRetained(lvalue.getAddress()), 2332f85e193739c953358c865005855253af4f68a497John McCall true); 2333f85e193739c953358c865005855253af4f68a497John McCall } 2334f85e193739c953358c865005855253af4f68a497John McCall 2335f85e193739c953358c865005855253af4f68a497John McCall llvm_unreachable("impossible lifetime!"); 2336f85e193739c953358c865005855253af4f68a497John McCall} 2337f85e193739c953358c865005855253af4f68a497John McCall 2338f85e193739c953358c865005855253af4f68a497John McCallstatic TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF, 2339f85e193739c953358c865005855253af4f68a497John McCall const Expr *e) { 2340f85e193739c953358c865005855253af4f68a497John McCall e = e->IgnoreParens(); 2341f85e193739c953358c865005855253af4f68a497John McCall QualType type = e->getType(); 2342f85e193739c953358c865005855253af4f68a497John McCall 23432148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // If we're loading retained from a __strong xvalue, we can avoid 23442148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // an extra retain/release pair by zeroing out the source of this 23452148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // "move" operation. 23462148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall if (e->isXValue() && 23472148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall !type.isConstQualified() && 23482148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall type.getObjCLifetime() == Qualifiers::OCL_Strong) { 23492148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // Emit the lvalue. 23502148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall LValue lv = CGF.EmitLValue(e); 23512148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall 23522148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // Load the object pointer. 23532148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall llvm::Value *result = CGF.EmitLoadOfLValue(lv).getScalarVal(); 23542148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall 23552148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // Set the source pointer to NULL. 23562148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall CGF.EmitStoreOfScalar(getNullForVariable(lv.getAddress()), lv); 23572148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall 23582148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall return TryEmitResult(result, true); 23592148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall } 23602148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall 2361f85e193739c953358c865005855253af4f68a497John McCall // As a very special optimization, in ARC++, if the l-value is the 2362f85e193739c953358c865005855253af4f68a497John McCall // result of a non-volatile assignment, do a simple retain of the 2363f85e193739c953358c865005855253af4f68a497John McCall // result of the call to objc_storeWeak instead of reloading. 23644e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGF.getLangOpts().CPlusPlus && 2365f85e193739c953358c865005855253af4f68a497John McCall !type.isVolatileQualified() && 2366f85e193739c953358c865005855253af4f68a497John McCall type.getObjCLifetime() == Qualifiers::OCL_Weak && 2367f85e193739c953358c865005855253af4f68a497John McCall isa<BinaryOperator>(e) && 2368f85e193739c953358c865005855253af4f68a497John McCall cast<BinaryOperator>(e)->getOpcode() == BO_Assign) 2369f85e193739c953358c865005855253af4f68a497John McCall return TryEmitResult(CGF.EmitScalarExpr(e), false); 2370f85e193739c953358c865005855253af4f68a497John McCall 2371f85e193739c953358c865005855253af4f68a497John McCall return tryEmitARCRetainLoadOfScalar(CGF, CGF.EmitLValue(e), type); 2372f85e193739c953358c865005855253af4f68a497John McCall} 2373f85e193739c953358c865005855253af4f68a497John McCall 2374f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainAfterCall(CodeGenFunction &CGF, 2375f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value); 2376f85e193739c953358c865005855253af4f68a497John McCall 2377f85e193739c953358c865005855253af4f68a497John McCall/// Given that the given expression is some sort of call (which does 2378f85e193739c953358c865005855253af4f68a497John McCall/// not return retained), emit a retain following it. 2379f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainCall(CodeGenFunction &CGF, const Expr *e) { 2380f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = CGF.EmitScalarExpr(e); 2381f85e193739c953358c865005855253af4f68a497John McCall return emitARCRetainAfterCall(CGF, value); 2382f85e193739c953358c865005855253af4f68a497John McCall} 2383f85e193739c953358c865005855253af4f68a497John McCall 2384f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainAfterCall(CodeGenFunction &CGF, 2385f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value) { 2386f85e193739c953358c865005855253af4f68a497John McCall if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(value)) { 2387f85e193739c953358c865005855253af4f68a497John McCall CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP(); 2388f85e193739c953358c865005855253af4f68a497John McCall 2389f85e193739c953358c865005855253af4f68a497John McCall // Place the retain immediately following the call. 2390f85e193739c953358c865005855253af4f68a497John McCall CGF.Builder.SetInsertPoint(call->getParent(), 2391f85e193739c953358c865005855253af4f68a497John McCall ++llvm::BasicBlock::iterator(call)); 2392f85e193739c953358c865005855253af4f68a497John McCall value = CGF.EmitARCRetainAutoreleasedReturnValue(value); 2393f85e193739c953358c865005855253af4f68a497John McCall 2394f85e193739c953358c865005855253af4f68a497John McCall CGF.Builder.restoreIP(ip); 2395f85e193739c953358c865005855253af4f68a497John McCall return value; 2396f85e193739c953358c865005855253af4f68a497John McCall } else if (llvm::InvokeInst *invoke = dyn_cast<llvm::InvokeInst>(value)) { 2397f85e193739c953358c865005855253af4f68a497John McCall CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP(); 2398f85e193739c953358c865005855253af4f68a497John McCall 2399f85e193739c953358c865005855253af4f68a497John McCall // Place the retain at the beginning of the normal destination block. 2400f85e193739c953358c865005855253af4f68a497John McCall llvm::BasicBlock *BB = invoke->getNormalDest(); 2401f85e193739c953358c865005855253af4f68a497John McCall CGF.Builder.SetInsertPoint(BB, BB->begin()); 2402f85e193739c953358c865005855253af4f68a497John McCall value = CGF.EmitARCRetainAutoreleasedReturnValue(value); 2403f85e193739c953358c865005855253af4f68a497John McCall 2404f85e193739c953358c865005855253af4f68a497John McCall CGF.Builder.restoreIP(ip); 2405f85e193739c953358c865005855253af4f68a497John McCall return value; 2406f85e193739c953358c865005855253af4f68a497John McCall 2407f85e193739c953358c865005855253af4f68a497John McCall // Bitcasts can arise because of related-result returns. Rewrite 2408f85e193739c953358c865005855253af4f68a497John McCall // the operand. 2409f85e193739c953358c865005855253af4f68a497John McCall } else if (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(value)) { 2410f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *operand = bitcast->getOperand(0); 2411f85e193739c953358c865005855253af4f68a497John McCall operand = emitARCRetainAfterCall(CGF, operand); 2412f85e193739c953358c865005855253af4f68a497John McCall bitcast->setOperand(0, operand); 2413f85e193739c953358c865005855253af4f68a497John McCall return bitcast; 2414f85e193739c953358c865005855253af4f68a497John McCall 2415f85e193739c953358c865005855253af4f68a497John McCall // Generic fall-back case. 2416f85e193739c953358c865005855253af4f68a497John McCall } else { 2417f85e193739c953358c865005855253af4f68a497John McCall // Retain using the non-block variant: we never need to do a copy 2418f85e193739c953358c865005855253af4f68a497John McCall // of a block that's been returned to us. 2419f85e193739c953358c865005855253af4f68a497John McCall return CGF.EmitARCRetainNonBlock(value); 2420f85e193739c953358c865005855253af4f68a497John McCall } 2421f85e193739c953358c865005855253af4f68a497John McCall} 2422f85e193739c953358c865005855253af4f68a497John McCall 2423dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall/// Determine whether it might be important to emit a separate 2424dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall/// objc_retain_block on the result of the given expression, or 2425dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall/// whether it's okay to just emit it in a +1 context. 2426dc05b11c67331016473fbc7909827b1b89c9616bJohn McCallstatic bool shouldEmitSeparateBlockRetain(const Expr *e) { 2427dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall assert(e->getType()->isBlockPointerType()); 2428dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall e = e->IgnoreParens(); 2429dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2430dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // For future goodness, emit block expressions directly in +1 2431dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // contexts if we can. 2432dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall if (isa<BlockExpr>(e)) 2433dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return false; 2434dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2435dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall if (const CastExpr *cast = dyn_cast<CastExpr>(e)) { 2436dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall switch (cast->getCastKind()) { 2437dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // Emitting these operations in +1 contexts is goodness. 2438dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall case CK_LValueToRValue: 243933e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCReclaimReturnedObject: 244033e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCConsumeObject: 244133e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCProduceObject: 2442dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return false; 2443dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2444dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // These operations preserve a block type. 2445dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall case CK_NoOp: 2446dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall case CK_BitCast: 2447dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return shouldEmitSeparateBlockRetain(cast->getSubExpr()); 2448dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2449dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // These operations are known to be bad (or haven't been considered). 2450dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall case CK_AnyPointerToBlockPointerCast: 2451dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall default: 2452dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return true; 2453dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall } 2454dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall } 2455dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2456dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return true; 2457dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall} 2458dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 24594b9c2d235fb9449e249d74f48ecfec601650de93John McCall/// Try to emit a PseudoObjectExpr at +1. 24604b9c2d235fb9449e249d74f48ecfec601650de93John McCall/// 24614b9c2d235fb9449e249d74f48ecfec601650de93John McCall/// This massively duplicates emitPseudoObjectRValue. 24624b9c2d235fb9449e249d74f48ecfec601650de93John McCallstatic TryEmitResult tryEmitARCRetainPseudoObject(CodeGenFunction &CGF, 24634b9c2d235fb9449e249d74f48ecfec601650de93John McCall const PseudoObjectExpr *E) { 2464cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques; 24654b9c2d235fb9449e249d74f48ecfec601650de93John McCall 24664b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Find the result expression. 24674b9c2d235fb9449e249d74f48ecfec601650de93John McCall const Expr *resultExpr = E->getResultExpr(); 24684b9c2d235fb9449e249d74f48ecfec601650de93John McCall assert(resultExpr); 24694b9c2d235fb9449e249d74f48ecfec601650de93John McCall TryEmitResult result; 24704b9c2d235fb9449e249d74f48ecfec601650de93John McCall 24714b9c2d235fb9449e249d74f48ecfec601650de93John McCall for (PseudoObjectExpr::const_semantics_iterator 24724b9c2d235fb9449e249d74f48ecfec601650de93John McCall i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) { 24734b9c2d235fb9449e249d74f48ecfec601650de93John McCall const Expr *semantic = *i; 24744b9c2d235fb9449e249d74f48ecfec601650de93John McCall 24754b9c2d235fb9449e249d74f48ecfec601650de93John McCall // If this semantic expression is an opaque value, bind it 24764b9c2d235fb9449e249d74f48ecfec601650de93John McCall // to the result of its source expression. 24774b9c2d235fb9449e249d74f48ecfec601650de93John McCall if (const OpaqueValueExpr *ov = dyn_cast<OpaqueValueExpr>(semantic)) { 24784b9c2d235fb9449e249d74f48ecfec601650de93John McCall typedef CodeGenFunction::OpaqueValueMappingData OVMA; 24794b9c2d235fb9449e249d74f48ecfec601650de93John McCall OVMA opaqueData; 24804b9c2d235fb9449e249d74f48ecfec601650de93John McCall 24814b9c2d235fb9449e249d74f48ecfec601650de93John McCall // If this semantic is the result of the pseudo-object 24824b9c2d235fb9449e249d74f48ecfec601650de93John McCall // expression, try to evaluate the source as +1. 24834b9c2d235fb9449e249d74f48ecfec601650de93John McCall if (ov == resultExpr) { 24844b9c2d235fb9449e249d74f48ecfec601650de93John McCall assert(!OVMA::shouldBindAsLValue(ov)); 24854b9c2d235fb9449e249d74f48ecfec601650de93John McCall result = tryEmitARCRetainScalarExpr(CGF, ov->getSourceExpr()); 24864b9c2d235fb9449e249d74f48ecfec601650de93John McCall opaqueData = OVMA::bind(CGF, ov, RValue::get(result.getPointer())); 24874b9c2d235fb9449e249d74f48ecfec601650de93John McCall 24884b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Otherwise, just bind it. 24894b9c2d235fb9449e249d74f48ecfec601650de93John McCall } else { 24904b9c2d235fb9449e249d74f48ecfec601650de93John McCall opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr()); 24914b9c2d235fb9449e249d74f48ecfec601650de93John McCall } 24924b9c2d235fb9449e249d74f48ecfec601650de93John McCall opaques.push_back(opaqueData); 24934b9c2d235fb9449e249d74f48ecfec601650de93John McCall 24944b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Otherwise, if the expression is the result, evaluate it 24954b9c2d235fb9449e249d74f48ecfec601650de93John McCall // and remember the result. 24964b9c2d235fb9449e249d74f48ecfec601650de93John McCall } else if (semantic == resultExpr) { 24974b9c2d235fb9449e249d74f48ecfec601650de93John McCall result = tryEmitARCRetainScalarExpr(CGF, semantic); 24984b9c2d235fb9449e249d74f48ecfec601650de93John McCall 24994b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Otherwise, evaluate the expression in an ignored context. 25004b9c2d235fb9449e249d74f48ecfec601650de93John McCall } else { 25014b9c2d235fb9449e249d74f48ecfec601650de93John McCall CGF.EmitIgnoredExpr(semantic); 25024b9c2d235fb9449e249d74f48ecfec601650de93John McCall } 25034b9c2d235fb9449e249d74f48ecfec601650de93John McCall } 25044b9c2d235fb9449e249d74f48ecfec601650de93John McCall 25054b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Unbind all the opaques now. 25064b9c2d235fb9449e249d74f48ecfec601650de93John McCall for (unsigned i = 0, e = opaques.size(); i != e; ++i) 25074b9c2d235fb9449e249d74f48ecfec601650de93John McCall opaques[i].unbind(CGF); 25084b9c2d235fb9449e249d74f48ecfec601650de93John McCall 25094b9c2d235fb9449e249d74f48ecfec601650de93John McCall return result; 25104b9c2d235fb9449e249d74f48ecfec601650de93John McCall} 25114b9c2d235fb9449e249d74f48ecfec601650de93John McCall 2512f85e193739c953358c865005855253af4f68a497John McCallstatic TryEmitResult 2513f85e193739c953358c865005855253af4f68a497John McCalltryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e) { 251472dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // We should *never* see a nested full-expression here, because if 251572dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // we fail to emit at +1, our caller must not retain after we close 251672dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // out the full-expression. 251772dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall assert(!isa<ExprWithCleanups>(e)); 2518990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 2519f85e193739c953358c865005855253af4f68a497John McCall // The desired result type, if it differs from the type of the 2520f85e193739c953358c865005855253af4f68a497John McCall // ultimate opaque expression. 25212acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *resultType = 0; 2522f85e193739c953358c865005855253af4f68a497John McCall 2523f85e193739c953358c865005855253af4f68a497John McCall while (true) { 2524f85e193739c953358c865005855253af4f68a497John McCall e = e->IgnoreParens(); 2525f85e193739c953358c865005855253af4f68a497John McCall 2526f85e193739c953358c865005855253af4f68a497John McCall // There's a break at the end of this if-chain; anything 2527f85e193739c953358c865005855253af4f68a497John McCall // that wants to keep looping has to explicitly continue. 2528f85e193739c953358c865005855253af4f68a497John McCall if (const CastExpr *ce = dyn_cast<CastExpr>(e)) { 2529f85e193739c953358c865005855253af4f68a497John McCall switch (ce->getCastKind()) { 2530f85e193739c953358c865005855253af4f68a497John McCall // No-op casts don't change the type, so we just ignore them. 2531f85e193739c953358c865005855253af4f68a497John McCall case CK_NoOp: 2532f85e193739c953358c865005855253af4f68a497John McCall e = ce->getSubExpr(); 2533f85e193739c953358c865005855253af4f68a497John McCall continue; 2534f85e193739c953358c865005855253af4f68a497John McCall 2535f85e193739c953358c865005855253af4f68a497John McCall case CK_LValueToRValue: { 2536f85e193739c953358c865005855253af4f68a497John McCall TryEmitResult loadResult 2537f85e193739c953358c865005855253af4f68a497John McCall = tryEmitARCRetainLoadOfScalar(CGF, ce->getSubExpr()); 2538f85e193739c953358c865005855253af4f68a497John McCall if (resultType) { 2539f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = loadResult.getPointer(); 2540f85e193739c953358c865005855253af4f68a497John McCall value = CGF.Builder.CreateBitCast(value, resultType); 2541f85e193739c953358c865005855253af4f68a497John McCall loadResult.setPointer(value); 2542f85e193739c953358c865005855253af4f68a497John McCall } 2543f85e193739c953358c865005855253af4f68a497John McCall return loadResult; 2544f85e193739c953358c865005855253af4f68a497John McCall } 2545f85e193739c953358c865005855253af4f68a497John McCall 2546f85e193739c953358c865005855253af4f68a497John McCall // These casts can change the type, so remember that and 2547f85e193739c953358c865005855253af4f68a497John McCall // soldier on. We only need to remember the outermost such 2548f85e193739c953358c865005855253af4f68a497John McCall // cast, though. 25491d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall case CK_CPointerToObjCPointerCast: 25501d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall case CK_BlockPointerToObjCPointerCast: 2551f85e193739c953358c865005855253af4f68a497John McCall case CK_AnyPointerToBlockPointerCast: 2552f85e193739c953358c865005855253af4f68a497John McCall case CK_BitCast: 2553f85e193739c953358c865005855253af4f68a497John McCall if (!resultType) 2554f85e193739c953358c865005855253af4f68a497John McCall resultType = CGF.ConvertType(ce->getType()); 2555f85e193739c953358c865005855253af4f68a497John McCall e = ce->getSubExpr(); 2556f85e193739c953358c865005855253af4f68a497John McCall assert(e->getType()->hasPointerRepresentation()); 2557f85e193739c953358c865005855253af4f68a497John McCall continue; 2558f85e193739c953358c865005855253af4f68a497John McCall 2559f85e193739c953358c865005855253af4f68a497John McCall // For consumptions, just emit the subexpression and thus elide 2560f85e193739c953358c865005855253af4f68a497John McCall // the retain/release pair. 256133e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCConsumeObject: { 2562f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *result = CGF.EmitScalarExpr(ce->getSubExpr()); 2563f85e193739c953358c865005855253af4f68a497John McCall if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); 2564f85e193739c953358c865005855253af4f68a497John McCall return TryEmitResult(result, true); 2565f85e193739c953358c865005855253af4f68a497John McCall } 2566f85e193739c953358c865005855253af4f68a497John McCall 2567dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // Block extends are net +0. Naively, we could just recurse on 2568dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // the subexpression, but actually we need to ensure that the 2569dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // value is copied as a block, so there's a little filter here. 257033e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCExtendBlockObject: { 2571dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall llvm::Value *result; // will be a +0 value 2572dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2573dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // If we can't safely assume the sub-expression will produce a 2574dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // block-copied value, emit the sub-expression at +0. 2575dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall if (shouldEmitSeparateBlockRetain(ce->getSubExpr())) { 2576dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall result = CGF.EmitScalarExpr(ce->getSubExpr()); 2577dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2578dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // Otherwise, try to emit the sub-expression at +1 recursively. 2579dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall } else { 2580dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall TryEmitResult subresult 2581dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall = tryEmitARCRetainScalarExpr(CGF, ce->getSubExpr()); 2582dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall result = subresult.getPointer(); 2583dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2584dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // If that produced a retained value, just use that, 2585dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // possibly casting down. 2586dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall if (subresult.getInt()) { 2587dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall if (resultType) 2588dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall result = CGF.Builder.CreateBitCast(result, resultType); 2589dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return TryEmitResult(result, true); 2590dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall } 2591dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2592dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // Otherwise it's +0. 2593dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall } 2594dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2595dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // Retain the object as a block, then cast down. 2596348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall result = CGF.EmitARCRetainBlock(result, /*mandatory*/ true); 2597dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); 2598dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return TryEmitResult(result, true); 2599dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall } 2600dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 26017e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall // For reclaims, emit the subexpression as a retained call and 26027e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall // skip the consumption. 260333e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCReclaimReturnedObject: { 26047e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall llvm::Value *result = emitARCRetainCall(CGF, ce->getSubExpr()); 26057e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); 26067e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall return TryEmitResult(result, true); 26077e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall } 26087e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall 2609f85e193739c953358c865005855253af4f68a497John McCall default: 2610f85e193739c953358c865005855253af4f68a497John McCall break; 2611f85e193739c953358c865005855253af4f68a497John McCall } 2612f85e193739c953358c865005855253af4f68a497John McCall 2613f85e193739c953358c865005855253af4f68a497John McCall // Skip __extension__. 2614f85e193739c953358c865005855253af4f68a497John McCall } else if (const UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { 2615f85e193739c953358c865005855253af4f68a497John McCall if (op->getOpcode() == UO_Extension) { 2616f85e193739c953358c865005855253af4f68a497John McCall e = op->getSubExpr(); 2617f85e193739c953358c865005855253af4f68a497John McCall continue; 2618f85e193739c953358c865005855253af4f68a497John McCall } 2619f85e193739c953358c865005855253af4f68a497John McCall 2620f85e193739c953358c865005855253af4f68a497John McCall // For calls and message sends, use the retained-call logic. 2621f85e193739c953358c865005855253af4f68a497John McCall // Delegate inits are a special case in that they're the only 2622f85e193739c953358c865005855253af4f68a497John McCall // returns-retained expression that *isn't* surrounded by 2623f85e193739c953358c865005855253af4f68a497John McCall // a consume. 2624f85e193739c953358c865005855253af4f68a497John McCall } else if (isa<CallExpr>(e) || 2625f85e193739c953358c865005855253af4f68a497John McCall (isa<ObjCMessageExpr>(e) && 2626f85e193739c953358c865005855253af4f68a497John McCall !cast<ObjCMessageExpr>(e)->isDelegateInitCall())) { 2627f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *result = emitARCRetainCall(CGF, e); 2628f85e193739c953358c865005855253af4f68a497John McCall if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); 2629f85e193739c953358c865005855253af4f68a497John McCall return TryEmitResult(result, true); 26304b9c2d235fb9449e249d74f48ecfec601650de93John McCall 26314b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Look through pseudo-object expressions. 26324b9c2d235fb9449e249d74f48ecfec601650de93John McCall } else if (const PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) { 26334b9c2d235fb9449e249d74f48ecfec601650de93John McCall TryEmitResult result 26344b9c2d235fb9449e249d74f48ecfec601650de93John McCall = tryEmitARCRetainPseudoObject(CGF, pseudo); 26354b9c2d235fb9449e249d74f48ecfec601650de93John McCall if (resultType) { 26364b9c2d235fb9449e249d74f48ecfec601650de93John McCall llvm::Value *value = result.getPointer(); 26374b9c2d235fb9449e249d74f48ecfec601650de93John McCall value = CGF.Builder.CreateBitCast(value, resultType); 26384b9c2d235fb9449e249d74f48ecfec601650de93John McCall result.setPointer(value); 26394b9c2d235fb9449e249d74f48ecfec601650de93John McCall } 26404b9c2d235fb9449e249d74f48ecfec601650de93John McCall return result; 2641f85e193739c953358c865005855253af4f68a497John McCall } 2642f85e193739c953358c865005855253af4f68a497John McCall 2643f85e193739c953358c865005855253af4f68a497John McCall // Conservatively halt the search at any other expression kind. 2644f85e193739c953358c865005855253af4f68a497John McCall break; 2645f85e193739c953358c865005855253af4f68a497John McCall } 2646f85e193739c953358c865005855253af4f68a497John McCall 2647f85e193739c953358c865005855253af4f68a497John McCall // We didn't find an obvious production, so emit what we've got and 2648f85e193739c953358c865005855253af4f68a497John McCall // tell the caller that we didn't manage to retain. 2649f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *result = CGF.EmitScalarExpr(e); 2650f85e193739c953358c865005855253af4f68a497John McCall if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); 2651f85e193739c953358c865005855253af4f68a497John McCall return TryEmitResult(result, false); 2652f85e193739c953358c865005855253af4f68a497John McCall} 2653f85e193739c953358c865005855253af4f68a497John McCall 2654f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF, 2655f85e193739c953358c865005855253af4f68a497John McCall LValue lvalue, 2656f85e193739c953358c865005855253af4f68a497John McCall QualType type) { 2657f85e193739c953358c865005855253af4f68a497John McCall TryEmitResult result = tryEmitARCRetainLoadOfScalar(CGF, lvalue, type); 2658f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = result.getPointer(); 2659f85e193739c953358c865005855253af4f68a497John McCall if (!result.getInt()) 2660f85e193739c953358c865005855253af4f68a497John McCall value = CGF.EmitARCRetain(type, value); 2661f85e193739c953358c865005855253af4f68a497John McCall return value; 2662f85e193739c953358c865005855253af4f68a497John McCall} 2663f85e193739c953358c865005855253af4f68a497John McCall 2664f85e193739c953358c865005855253af4f68a497John McCall/// EmitARCRetainScalarExpr - Semantically equivalent to 2665f85e193739c953358c865005855253af4f68a497John McCall/// EmitARCRetainObject(e->getType(), EmitScalarExpr(e)), but making a 2666f85e193739c953358c865005855253af4f68a497John McCall/// best-effort attempt to peephole expressions that naturally produce 2667f85e193739c953358c865005855253af4f68a497John McCall/// retained objects. 2668f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCRetainScalarExpr(const Expr *e) { 266972dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // The retain needs to happen within the full-expression. 267072dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) { 267172dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall enterFullExpression(cleanups); 267272dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall RunCleanupsScope scope(*this); 267372dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall return EmitARCRetainScalarExpr(cleanups->getSubExpr()); 267472dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall } 267572dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall 2676f85e193739c953358c865005855253af4f68a497John McCall TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e); 2677f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = result.getPointer(); 2678f85e193739c953358c865005855253af4f68a497John McCall if (!result.getInt()) 2679f85e193739c953358c865005855253af4f68a497John McCall value = EmitARCRetain(e->getType(), value); 2680f85e193739c953358c865005855253af4f68a497John McCall return value; 2681f85e193739c953358c865005855253af4f68a497John McCall} 2682f85e193739c953358c865005855253af4f68a497John McCall 2683f85e193739c953358c865005855253af4f68a497John McCallllvm::Value * 2684f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCRetainAutoreleaseScalarExpr(const Expr *e) { 268572dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // The retain needs to happen within the full-expression. 268672dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) { 268772dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall enterFullExpression(cleanups); 268872dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall RunCleanupsScope scope(*this); 268972dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall return EmitARCRetainAutoreleaseScalarExpr(cleanups->getSubExpr()); 269072dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall } 269172dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall 2692f85e193739c953358c865005855253af4f68a497John McCall TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e); 2693f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = result.getPointer(); 2694f85e193739c953358c865005855253af4f68a497John McCall if (result.getInt()) 2695f85e193739c953358c865005855253af4f68a497John McCall value = EmitARCAutorelease(value); 2696f85e193739c953358c865005855253af4f68a497John McCall else 2697f85e193739c953358c865005855253af4f68a497John McCall value = EmitARCRetainAutorelease(e->getType(), value); 2698f85e193739c953358c865005855253af4f68a497John McCall return value; 2699f85e193739c953358c865005855253af4f68a497John McCall} 2700f85e193739c953358c865005855253af4f68a497John McCall 2701348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCallllvm::Value *CodeGenFunction::EmitARCExtendBlockObject(const Expr *e) { 2702348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall llvm::Value *result; 2703348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall bool doRetain; 2704348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 2705348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall if (shouldEmitSeparateBlockRetain(e)) { 2706348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall result = EmitScalarExpr(e); 2707348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall doRetain = true; 2708348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall } else { 2709348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall TryEmitResult subresult = tryEmitARCRetainScalarExpr(*this, e); 2710348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall result = subresult.getPointer(); 2711348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall doRetain = !subresult.getInt(); 2712348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall } 2713348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 2714348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall if (doRetain) 2715348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall result = EmitARCRetainBlock(result, /*mandatory*/ true); 2716348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall return EmitObjCConsumeObject(e->getType(), result); 2717348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall} 2718348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 27192b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCallllvm::Value *CodeGenFunction::EmitObjCThrowOperand(const Expr *expr) { 27202b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // In ARC, retain and autorelease the expression. 27214e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().ObjCAutoRefCount) { 27222b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // Do so before running any cleanups for the full-expression. 272372dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // EmitARCRetainAutoreleaseScalarExpr does this for us. 27242b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall return EmitARCRetainAutoreleaseScalarExpr(expr); 27252b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall } 27262b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall 27272b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // Otherwise, use the normal scalar-expression emission. The 27282b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // exception machinery doesn't do anything special with the 27292b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // exception like retaining it, so there's no safety associated with 27302b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // only running cleanups after the throw has started, and when it 27312b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // matters it tends to be substantially inferior code. 27322b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall return EmitScalarExpr(expr); 27332b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall} 27342b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall 2735f85e193739c953358c865005855253af4f68a497John McCallstd::pair<LValue,llvm::Value*> 2736f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCStoreStrong(const BinaryOperator *e, 2737f85e193739c953358c865005855253af4f68a497John McCall bool ignored) { 2738f85e193739c953358c865005855253af4f68a497John McCall // Evaluate the RHS first. 2739f85e193739c953358c865005855253af4f68a497John McCall TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e->getRHS()); 2740f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = result.getPointer(); 2741f85e193739c953358c865005855253af4f68a497John McCall 2742fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall bool hasImmediateRetain = result.getInt(); 2743fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall 2744fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall // If we didn't emit a retained object, and the l-value is of block 2745fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall // type, then we need to emit the block-retain immediately in case 2746fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall // it invalidates the l-value. 2747fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall if (!hasImmediateRetain && e->getType()->isBlockPointerType()) { 2748348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall value = EmitARCRetainBlock(value, /*mandatory*/ false); 2749fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall hasImmediateRetain = true; 2750fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall } 2751fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall 2752f85e193739c953358c865005855253af4f68a497John McCall LValue lvalue = EmitLValue(e->getLHS()); 2753f85e193739c953358c865005855253af4f68a497John McCall 2754f85e193739c953358c865005855253af4f68a497John McCall // If the RHS was emitted retained, expand this. 2755fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall if (hasImmediateRetain) { 2756f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *oldValue = 27576da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman EmitLoadOfScalar(lvalue); 27586da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman EmitStoreOfScalar(value, lvalue); 27595b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall EmitARCRelease(oldValue, lvalue.isARCPreciseLifetime()); 2760f85e193739c953358c865005855253af4f68a497John McCall } else { 2761545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall value = EmitARCStoreStrong(lvalue, value, ignored); 2762f85e193739c953358c865005855253af4f68a497John McCall } 2763f85e193739c953358c865005855253af4f68a497John McCall 2764f85e193739c953358c865005855253af4f68a497John McCall return std::pair<LValue,llvm::Value*>(lvalue, value); 2765f85e193739c953358c865005855253af4f68a497John McCall} 2766f85e193739c953358c865005855253af4f68a497John McCall 2767f85e193739c953358c865005855253af4f68a497John McCallstd::pair<LValue,llvm::Value*> 2768f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCStoreAutoreleasing(const BinaryOperator *e) { 2769f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = EmitARCRetainAutoreleaseScalarExpr(e->getRHS()); 2770f85e193739c953358c865005855253af4f68a497John McCall LValue lvalue = EmitLValue(e->getLHS()); 2771f85e193739c953358c865005855253af4f68a497John McCall 27726da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman EmitStoreOfScalar(value, lvalue); 2773f85e193739c953358c865005855253af4f68a497John McCall 2774f85e193739c953358c865005855253af4f68a497John McCall return std::pair<LValue,llvm::Value*>(lvalue, value); 2775f85e193739c953358c865005855253af4f68a497John McCall} 2776f85e193739c953358c865005855253af4f68a497John McCall 2777f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitObjCAutoreleasePoolStmt( 277816098f366097305c348b356a44638d6c959c6e60Eric Christopher const ObjCAutoreleasePoolStmt &ARPS) { 2779f85e193739c953358c865005855253af4f68a497John McCall const Stmt *subStmt = ARPS.getSubStmt(); 2780f85e193739c953358c865005855253af4f68a497John McCall const CompoundStmt &S = cast<CompoundStmt>(*subStmt); 2781f85e193739c953358c865005855253af4f68a497John McCall 2782f85e193739c953358c865005855253af4f68a497John McCall CGDebugInfo *DI = getDebugInfo(); 278373fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher if (DI) 278473fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher DI->EmitLexicalBlockStart(Builder, S.getLBracLoc()); 2785f85e193739c953358c865005855253af4f68a497John McCall 2786f85e193739c953358c865005855253af4f68a497John McCall // Keep track of the current cleanup stack depth. 2787f85e193739c953358c865005855253af4f68a497John McCall RunCleanupsScope Scope(*this); 27880a7dd788dbef975f35f273c7ab913f480f7edd60John McCall if (CGM.getLangOpts().ObjCRuntime.hasNativeARC()) { 2789f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *token = EmitObjCAutoreleasePoolPush(); 2790f85e193739c953358c865005855253af4f68a497John McCall EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, token); 2791f85e193739c953358c865005855253af4f68a497John McCall } else { 2792f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *token = EmitObjCMRRAutoreleasePoolPush(); 2793f85e193739c953358c865005855253af4f68a497John McCall EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, token); 2794f85e193739c953358c865005855253af4f68a497John McCall } 2795f85e193739c953358c865005855253af4f68a497John McCall 2796f85e193739c953358c865005855253af4f68a497John McCall for (CompoundStmt::const_body_iterator I = S.body_begin(), 2797f85e193739c953358c865005855253af4f68a497John McCall E = S.body_end(); I != E; ++I) 2798f85e193739c953358c865005855253af4f68a497John McCall EmitStmt(*I); 2799f85e193739c953358c865005855253af4f68a497John McCall 280073fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher if (DI) 280173fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher DI->EmitLexicalBlockEnd(Builder, S.getRBracLoc()); 2802f85e193739c953358c865005855253af4f68a497John McCall} 28030c24c808e4dce43e88abf2d5f98546b901bd4315John McCall 28040c24c808e4dce43e88abf2d5f98546b901bd4315John McCall/// EmitExtendGCLifetime - Given a pointer to an Objective-C object, 28050c24c808e4dce43e88abf2d5f98546b901bd4315John McCall/// make sure it survives garbage collection until this point. 28060c24c808e4dce43e88abf2d5f98546b901bd4315John McCallvoid CodeGenFunction::EmitExtendGCLifetime(llvm::Value *object) { 28070c24c808e4dce43e88abf2d5f98546b901bd4315John McCall // We just use an inline assembly. 28080c24c808e4dce43e88abf2d5f98546b901bd4315John McCall llvm::FunctionType *extenderType 2809de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall = llvm::FunctionType::get(VoidTy, VoidPtrTy, RequiredArgs::All); 28100c24c808e4dce43e88abf2d5f98546b901bd4315John McCall llvm::Value *extender 28110c24c808e4dce43e88abf2d5f98546b901bd4315John McCall = llvm::InlineAsm::get(extenderType, 28120c24c808e4dce43e88abf2d5f98546b901bd4315John McCall /* assembly */ "", 28130c24c808e4dce43e88abf2d5f98546b901bd4315John McCall /* constraints */ "r", 28140c24c808e4dce43e88abf2d5f98546b901bd4315John McCall /* side effects */ true); 28150c24c808e4dce43e88abf2d5f98546b901bd4315John McCall 28160c24c808e4dce43e88abf2d5f98546b901bd4315John McCall object = Builder.CreateBitCast(object, VoidPtrTy); 2817bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall EmitNounwindRuntimeCall(extender, object); 28180c24c808e4dce43e88abf2d5f98546b901bd4315John McCall} 28190c24c808e4dce43e88abf2d5f98546b901bd4315John McCall 282020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian/// GenerateObjCAtomicSetterCopyHelperFunction - Given a c++ object type with 282184e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian/// non-trivial copy assignment function, produce following helper function. 282284e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian/// static void copyHelper(Ty *dest, const Ty *source) { *dest = *source; } 282384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian/// 282484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanianllvm::Constant * 282520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz JahanianCodeGenFunction::GenerateObjCAtomicSetterCopyHelperFunction( 282620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian const ObjCPropertyImplDecl *PID) { 2827260611a32535c851237926bfcf78869b13c07d5bJohn McCall if (!getLangOpts().CPlusPlus || 282890f692611bd486198ffa67a0e097013a2e6e3d2eRafael Espindola !getLangOpts().ObjCRuntime.hasAtomicCopyHelper()) 282984e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian return 0; 283084e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian QualType Ty = PID->getPropertyIvarDecl()->getType(); 283184e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian if (!Ty->isRecordType()) 283284e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian return 0; 283384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 283420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if ((!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_atomic))) 283584e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian return 0; 2836b08cfb3c5909752c0e501a4e5c31a507a4cc1f72Fariborz Jahanian llvm::Constant * HelperFn = 0; 283720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if (hasTrivialSetExpr(PID)) 283820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return 0; 283920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian assert(PID->getSetterCXXAssignment() && "SetterCXXAssignment - null"); 284020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if ((HelperFn = CGM.getAtomicSetterHelperFnMap(Ty))) 284120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return HelperFn; 284284e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 284384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian ASTContext &C = getContext(); 284484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian IdentifierInfo *II 284520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian = &CGM.getContext().Idents.get("__assign_helper_atomic_property_"); 284684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian FunctionDecl *FD = FunctionDecl::Create(C, 284784e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian C.getTranslationUnitDecl(), 284884e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian SourceLocation(), 284984e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian SourceLocation(), II, C.VoidTy, 0, 285084e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian SC_Static, 285184e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian SC_None, 285284e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian false, 2853b92bd4b3271b7892abe9fd8c74fb54a27ad702abEric Christopher false); 285484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 285584e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian QualType DestTy = C.getPointerType(Ty); 285684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian QualType SrcTy = Ty; 285784e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian SrcTy.addConst(); 285884e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian SrcTy = C.getPointerType(SrcTy); 285984e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 286084e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian FunctionArgList args; 286184e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian ImplicitParamDecl dstDecl(FD, SourceLocation(), 0, DestTy); 286284e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian args.push_back(&dstDecl); 286384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian ImplicitParamDecl srcDecl(FD, SourceLocation(), 0, SrcTy); 286484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian args.push_back(&srcDecl); 286584e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 286684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian const CGFunctionInfo &FI = 2867de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall CGM.getTypes().arrangeFunctionDeclaration(C.VoidTy, args, 2868de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall FunctionType::ExtInfo(), 2869de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall RequiredArgs::All); 287084e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 2871de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI); 287284e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 287384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian llvm::Function *Fn = 287484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 287516098f366097305c348b356a44638d6c959c6e60Eric Christopher "__assign_helper_atomic_property_", 287616098f366097305c348b356a44638d6c959c6e60Eric Christopher &CGM.getModule()); 287784e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 2878a240df2ec1b374b3e9e7f760875ffb17cd64506fAlexey Samsonov // Initialize debug info if needed. 2879a240df2ec1b374b3e9e7f760875ffb17cd64506fAlexey Samsonov maybeInitializeDebugInfo(); 288084e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 288184e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation()); 288284e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 2883f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr DstExpr(&dstDecl, false, DestTy, 2884f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_RValue, SourceLocation()); 2885f4b88a45902af1802a1cb42ba48b1c474474f228John McCall UnaryOperator DST(&DstExpr, UO_Deref, DestTy->getPointeeType(), 2886f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_LValue, OK_Ordinary, SourceLocation()); 288784e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 2888f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr SrcExpr(&srcDecl, false, SrcTy, 2889f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_RValue, SourceLocation()); 2890f4b88a45902af1802a1cb42ba48b1c474474f228John McCall UnaryOperator SRC(&SrcExpr, UO_Deref, SrcTy->getPointeeType(), 2891f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_LValue, OK_Ordinary, SourceLocation()); 289284e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 2893f4b88a45902af1802a1cb42ba48b1c474474f228John McCall Expr *Args[2] = { &DST, &SRC }; 289420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CallExpr *CalleeExp = cast<CallExpr>(PID->getSetterCXXAssignment()); 2895f4b88a45902af1802a1cb42ba48b1c474474f228John McCall CXXOperatorCallExpr TheCall(C, OO_Equal, CalleeExp->getCallee(), 28963b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer Args, DestTy->getPointeeType(), 2897be9af1288881110e406b87914162eaa59f1e5918Lang Hames VK_LValue, SourceLocation(), false); 2898f4b88a45902af1802a1cb42ba48b1c474474f228John McCall 2899f4b88a45902af1802a1cb42ba48b1c474474f228John McCall EmitStmt(&TheCall); 290084e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 290184e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian FinishFunction(); 2902cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); 290320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CGM.setAtomicSetterHelperFnMap(Ty, HelperFn); 2904cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian return HelperFn; 290520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian} 290620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 290720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanianllvm::Constant * 290820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz JahanianCodeGenFunction::GenerateObjCAtomicGetterCopyHelperFunction( 290920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian const ObjCPropertyImplDecl *PID) { 2910260611a32535c851237926bfcf78869b13c07d5bJohn McCall if (!getLangOpts().CPlusPlus || 291190f692611bd486198ffa67a0e097013a2e6e3d2eRafael Espindola !getLangOpts().ObjCRuntime.hasAtomicCopyHelper()) 291220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return 0; 291320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 291420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian QualType Ty = PD->getType(); 291520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if (!Ty->isRecordType()) 291620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return 0; 291720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if ((!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_atomic))) 291820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return 0; 291920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Constant * HelperFn = 0; 292020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 292120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if (hasTrivialGetExpr(PID)) 292220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return 0; 292320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian assert(PID->getGetterCXXConstructor() && "getGetterCXXConstructor - null"); 292420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if ((HelperFn = CGM.getAtomicGetterHelperFnMap(Ty))) 292520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return HelperFn; 292620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 292720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 292820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ASTContext &C = getContext(); 292920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian IdentifierInfo *II 293020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian = &CGM.getContext().Idents.get("__copy_helper_atomic_property_"); 293120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian FunctionDecl *FD = FunctionDecl::Create(C, 293220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian C.getTranslationUnitDecl(), 293320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SourceLocation(), 293420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SourceLocation(), II, C.VoidTy, 0, 293520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SC_Static, 293620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SC_None, 293720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian false, 2938b92bd4b3271b7892abe9fd8c74fb54a27ad702abEric Christopher false); 293920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 294020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian QualType DestTy = C.getPointerType(Ty); 294120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian QualType SrcTy = Ty; 294220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SrcTy.addConst(); 294320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SrcTy = C.getPointerType(SrcTy); 294420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 294520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian FunctionArgList args; 294620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ImplicitParamDecl dstDecl(FD, SourceLocation(), 0, DestTy); 294720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian args.push_back(&dstDecl); 294820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ImplicitParamDecl srcDecl(FD, SourceLocation(), 0, SrcTy); 294920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian args.push_back(&srcDecl); 295020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 295120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian const CGFunctionInfo &FI = 2952de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall CGM.getTypes().arrangeFunctionDeclaration(C.VoidTy, args, 2953de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall FunctionType::ExtInfo(), 2954de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall RequiredArgs::All); 295520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 2956de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI); 295720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 295820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Function *Fn = 295920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 296020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian "__copy_helper_atomic_property_", &CGM.getModule()); 296184e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 2962a240df2ec1b374b3e9e7f760875ffb17cd64506fAlexey Samsonov // Initialize debug info if needed. 2963a240df2ec1b374b3e9e7f760875ffb17cd64506fAlexey Samsonov maybeInitializeDebugInfo(); 296420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 296520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation()); 296620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 2967f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr SrcExpr(&srcDecl, false, SrcTy, 296820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian VK_RValue, SourceLocation()); 296920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 2970f4b88a45902af1802a1cb42ba48b1c474474f228John McCall UnaryOperator SRC(&SrcExpr, UO_Deref, SrcTy->getPointeeType(), 2971f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_LValue, OK_Ordinary, SourceLocation()); 297220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 297320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstructExpr *CXXConstExpr = 297420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian cast<CXXConstructExpr>(PID->getGetterCXXConstructor()); 297520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 297620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SmallVector<Expr*, 4> ConstructorArgs; 2977f4b88a45902af1802a1cb42ba48b1c474474f228John McCall ConstructorArgs.push_back(&SRC); 297820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstructExpr::arg_iterator A = CXXConstExpr->arg_begin(); 297920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ++A; 298020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 298120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian for (CXXConstructExpr::arg_iterator AEnd = CXXConstExpr->arg_end(); 298220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian A != AEnd; ++A) 298320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ConstructorArgs.push_back(*A); 298420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 298520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstructExpr *TheCXXConstructExpr = 298620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstructExpr::Create(C, Ty, SourceLocation(), 298720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstExpr->getConstructor(), 298820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstExpr->isElidable(), 29893b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer ConstructorArgs, 29905b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl CXXConstExpr->hadMultipleCandidates(), 29915b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl CXXConstExpr->isListInitialization(), 299220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstExpr->requiresZeroInitialization(), 299316098f366097305c348b356a44638d6c959c6e60Eric Christopher CXXConstExpr->getConstructionKind(), 299416098f366097305c348b356a44638d6c959c6e60Eric Christopher SourceRange()); 299520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 2996f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr DstExpr(&dstDecl, false, DestTy, 2997f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_RValue, SourceLocation()); 299820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 2999f4b88a45902af1802a1cb42ba48b1c474474f228John McCall RValue DV = EmitAnyExpr(&DstExpr); 300016098f366097305c348b356a44638d6c959c6e60Eric Christopher CharUnits Alignment 300116098f366097305c348b356a44638d6c959c6e60Eric Christopher = getContext().getTypeAlignInChars(TheCXXConstructExpr->getType()); 300220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian EmitAggExpr(TheCXXConstructExpr, 300320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian AggValueSlot::forAddr(DV.getScalarVal(), Alignment, Qualifiers(), 300420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian AggValueSlot::IsDestructed, 300520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian AggValueSlot::DoesNotNeedGCBarriers, 3006649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier AggValueSlot::IsNotAliased)); 300720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 300820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian FinishFunction(); 300920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); 301020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CGM.setAtomicGetterHelperFnMap(Ty, HelperFn); 301120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return HelperFn; 301284e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian} 301384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 3014cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedmanllvm::Value * 3015cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli FriedmanCodeGenFunction::EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty) { 3016cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman // Get selectors for retain/autorelease. 30178c72a7db34a63c38c6065b73fda4aeb0fe19eb65Eli Friedman IdentifierInfo *CopyID = &getContext().Idents.get("copy"); 30188c72a7db34a63c38c6065b73fda4aeb0fe19eb65Eli Friedman Selector CopySelector = 30198c72a7db34a63c38c6065b73fda4aeb0fe19eb65Eli Friedman getContext().Selectors.getNullarySelector(CopyID); 3020cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman IdentifierInfo *AutoreleaseID = &getContext().Idents.get("autorelease"); 3021cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Selector AutoreleaseSelector = 3022cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman getContext().Selectors.getNullarySelector(AutoreleaseID); 3023cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman 3024cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman // Emit calls to retain/autorelease. 3025cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 3026cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman llvm::Value *Val = Block; 3027cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman RValue Result; 3028cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 30298c72a7db34a63c38c6065b73fda4aeb0fe19eb65Eli Friedman Ty, CopySelector, 3030cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Val, CallArgList(), 0, 0); 3031cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Val = Result.getScalarVal(); 3032cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 3033cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Ty, AutoreleaseSelector, 3034cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Val, CallArgList(), 0, 0); 3035cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Val = Result.getScalarVal(); 3036cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman return Val; 3037cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman} 3038cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman 303984e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 30402979ec73b4f974d85f2ce84167712177a44c6f09Ted KremenekCGObjCRuntime::~CGObjCRuntime() {} 3041