CGObjC.cpp revision 651f13cea278ec967336033dd032faef0e9fc2ec
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" 238b54999a831bb195c08541ca995ef0505c96193fMark Lacey#include "clang/CodeGen/CGFunctionInfo.h" 243d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson#include "llvm/ADT/STLExtras.h" 25651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "llvm/IR/CallSite.h" 263b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/DataLayout.h" 273b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/InlineAsm.h" 285508518a2702b00be3b15a26d772bde968972f54Anders Carlssonusing namespace clang; 295508518a2702b00be3b15a26d772bde968972f54Anders Carlssonusing namespace CodeGen; 305508518a2702b00be3b15a26d772bde968972f54Anders Carlsson 31f85e193739c953358c865005855253af4f68a497John McCalltypedef llvm::PointerIntPair<llvm::Value*,1,bool> TryEmitResult; 32f85e193739c953358c865005855253af4f68a497John McCallstatic TryEmitResult 33f85e193739c953358c865005855253af4f68a497John McCalltryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e); 34ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenekstatic RValue AdjustRelatedResultType(CodeGenFunction &CGF, 35490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian QualType ET, 36ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const ObjCMethodDecl *Method, 37ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek RValue Result); 38f85e193739c953358c865005855253af4f68a497John McCall 39f85e193739c953358c865005855253af4f68a497John McCall/// Given the address of a variable of pointer type, find the correct 40f85e193739c953358c865005855253af4f68a497John McCall/// null to store into it. 41f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Constant *getNullForVariable(llvm::Value *addr) { 422acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *type = 43f85e193739c953358c865005855253af4f68a497John McCall cast<llvm::PointerType>(addr->getType())->getElementType(); 44f85e193739c953358c865005855253af4f68a497John McCall return llvm::ConstantPointerNull::get(cast<llvm::PointerType>(type)); 45f85e193739c953358c865005855253af4f68a497John McCall} 46f85e193739c953358c865005855253af4f68a497John McCall 478fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner/// Emits an instance of NSConstantString representing the object. 481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpllvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E) 4971fcec9abf2ce66d5e17a24bd021680e94e42f0dDaniel Dunbar{ 500d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall llvm::Constant *C = 510d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall CGM.getObjCRuntime().GenerateConstantString(E->getString()); 52ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbar // FIXME: This bitcast should just be made an invariant on the Runtime. 533c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType())); 548fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner} 558fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner 56eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard/// EmitObjCBoxedExpr - This routine generates code to call 57eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard/// the appropriate expression boxing method. This will either be 58eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard/// one of +[NSNumber numberWith<Type>:], or +[NSString stringWithUTF8String:]. 59ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek/// 6016098f366097305c348b356a44638d6c959c6e60Eric Christopherllvm::Value * 61eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick BeardCodeGenFunction::EmitObjCBoxedExpr(const ObjCBoxedExpr *E) { 62ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Generate the correct selector for this literal's concrete type. 63eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard const Expr *SubExpr = E->getSubExpr(); 64ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Get the method. 65eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard const ObjCMethodDecl *BoxingMethod = E->getBoxingMethod(); 66eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard assert(BoxingMethod && "BoxingMethod is null"); 67eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard assert(BoxingMethod->isClassMethod() && "BoxingMethod must be a class method"); 68eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard Selector Sel = BoxingMethod->getSelector(); 69ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 70ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Generate a reference to the class pointer, which will be the receiver. 71eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard // Assumes that the method was introduced in the class that should be 72eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard // messaged (avoids pulling it out of the result type). 73ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 74eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard const ObjCInterfaceDecl *ClassDecl = BoxingMethod->getClassInterface(); 75bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *Receiver = Runtime.GetClass(*this, ClassDecl); 76eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard 77eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard const ParmVarDecl *argDecl = *BoxingMethod->param_begin(); 78ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek QualType ArgQT = argDecl->getType().getUnqualifiedType(); 79eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard RValue RV = EmitAnyExpr(SubExpr); 80ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek CallArgList Args; 81ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Args.add(RV, ArgQT); 82651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 83651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines RValue result = Runtime.GenerateMessageSend( 84651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines *this, ReturnValueSlot(), BoxingMethod->getReturnType(), Sel, Receiver, 85651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines Args, ClassDecl, BoxingMethod); 86ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return Builder.CreateBitCast(result.getScalarVal(), 87ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ConvertType(E->getType())); 88ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek} 89ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 90ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenekllvm::Value *CodeGenFunction::EmitObjCCollectionLiteral(const Expr *E, 91ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const ObjCMethodDecl *MethodWithObjects) { 92ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ASTContext &Context = CGM.getContext(); 93ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const ObjCDictionaryLiteral *DLE = 0; 94ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const ObjCArrayLiteral *ALE = dyn_cast<ObjCArrayLiteral>(E); 95ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (!ALE) 96ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek DLE = cast<ObjCDictionaryLiteral>(E); 97ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 98ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Compute the type of the array we're initializing. 99ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek uint64_t NumElements = 100ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ALE ? ALE->getNumElements() : DLE->getNumElements(); 101ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek llvm::APInt APNumElements(Context.getTypeSize(Context.getSizeType()), 102ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek NumElements); 103ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek QualType ElementType = Context.getObjCIdType().withConst(); 104ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek QualType ElementArrayType 105ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek = Context.getConstantArrayType(ElementType, APNumElements, 106ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ArrayType::Normal, /*IndexTypeQuals=*/0); 107ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 108ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Allocate the temporary array(s). 109ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek llvm::Value *Objects = CreateMemTemp(ElementArrayType, "objects"); 110ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek llvm::Value *Keys = 0; 111ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (DLE) 112ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Keys = CreateMemTemp(ElementArrayType, "keys"); 113ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 114527842f97b684831de2b1a455649131ed381ece4John McCall // In ARC, we may need to do extra work to keep all the keys and 115527842f97b684831de2b1a455649131ed381ece4John McCall // values alive until after the call. 116527842f97b684831de2b1a455649131ed381ece4John McCall SmallVector<llvm::Value *, 16> NeededObjects; 117527842f97b684831de2b1a455649131ed381ece4John McCall bool TrackNeededObjects = 118527842f97b684831de2b1a455649131ed381ece4John McCall (getLangOpts().ObjCAutoRefCount && 119527842f97b684831de2b1a455649131ed381ece4John McCall CGM.getCodeGenOpts().OptimizationLevel != 0); 120527842f97b684831de2b1a455649131ed381ece4John McCall 121ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Perform the actual initialialization of the array(s). 122ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek for (uint64_t i = 0; i < NumElements; i++) { 123ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (ALE) { 124527842f97b684831de2b1a455649131ed381ece4John McCall // Emit the element and store it to the appropriate array slot. 125ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const Expr *Rhs = ALE->getElement(i); 126ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek LValue LV = LValue::MakeAddr(Builder.CreateStructGEP(Objects, i), 127ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ElementType, 128ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Context.getTypeAlignInChars(Rhs->getType()), 129ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Context); 130527842f97b684831de2b1a455649131ed381ece4John McCall 131527842f97b684831de2b1a455649131ed381ece4John McCall llvm::Value *value = EmitScalarExpr(Rhs); 132527842f97b684831de2b1a455649131ed381ece4John McCall EmitStoreThroughLValue(RValue::get(value), LV, true); 133527842f97b684831de2b1a455649131ed381ece4John McCall if (TrackNeededObjects) { 134527842f97b684831de2b1a455649131ed381ece4John McCall NeededObjects.push_back(value); 135527842f97b684831de2b1a455649131ed381ece4John McCall } 136ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } else { 137527842f97b684831de2b1a455649131ed381ece4John McCall // Emit the key and store it to the appropriate array slot. 138ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const Expr *Key = DLE->getKeyValueElement(i).Key; 139ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek LValue KeyLV = LValue::MakeAddr(Builder.CreateStructGEP(Keys, i), 140ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ElementType, 141ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Context.getTypeAlignInChars(Key->getType()), 142ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Context); 143527842f97b684831de2b1a455649131ed381ece4John McCall llvm::Value *keyValue = EmitScalarExpr(Key); 144527842f97b684831de2b1a455649131ed381ece4John McCall EmitStoreThroughLValue(RValue::get(keyValue), KeyLV, /*isInit=*/true); 145ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 146527842f97b684831de2b1a455649131ed381ece4John McCall // Emit the value and store it to the appropriate array slot. 147ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const Expr *Value = DLE->getKeyValueElement(i).Value; 148ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek LValue ValueLV = LValue::MakeAddr(Builder.CreateStructGEP(Objects, i), 149ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ElementType, 150ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Context.getTypeAlignInChars(Value->getType()), 151ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Context); 152527842f97b684831de2b1a455649131ed381ece4John McCall llvm::Value *valueValue = EmitScalarExpr(Value); 153527842f97b684831de2b1a455649131ed381ece4John McCall EmitStoreThroughLValue(RValue::get(valueValue), ValueLV, /*isInit=*/true); 154527842f97b684831de2b1a455649131ed381ece4John McCall if (TrackNeededObjects) { 155527842f97b684831de2b1a455649131ed381ece4John McCall NeededObjects.push_back(keyValue); 156527842f97b684831de2b1a455649131ed381ece4John McCall NeededObjects.push_back(valueValue); 157527842f97b684831de2b1a455649131ed381ece4John McCall } 158ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 159ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 160ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 161ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Generate the argument list. 162ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek CallArgList Args; 163ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ObjCMethodDecl::param_const_iterator PI = MethodWithObjects->param_begin(); 164ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const ParmVarDecl *argDecl = *PI++; 165ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek QualType ArgQT = argDecl->getType().getUnqualifiedType(); 166ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Args.add(RValue::get(Objects), ArgQT); 167ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (DLE) { 168ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek argDecl = *PI++; 169ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ArgQT = argDecl->getType().getUnqualifiedType(); 170ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Args.add(RValue::get(Keys), ArgQT); 171ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 172ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek argDecl = *PI; 173ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ArgQT = argDecl->getType().getUnqualifiedType(); 174ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek llvm::Value *Count = 175ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek llvm::ConstantInt::get(CGM.getTypes().ConvertType(ArgQT), NumElements); 176ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Args.add(RValue::get(Count), ArgQT); 177ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 178ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Generate a reference to the class pointer, which will be the receiver. 179ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Selector Sel = MethodWithObjects->getSelector(); 180ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek QualType ResultType = E->getType(); 181ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const ObjCObjectPointerType *InterfacePointerType 182ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek = ResultType->getAsObjCInterfacePointerType(); 183ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ObjCInterfaceDecl *Class 184ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek = InterfacePointerType->getObjectType()->getInterface(); 185ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 186bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *Receiver = Runtime.GetClass(*this, Class); 187ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 188ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Generate the message send. 189651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines RValue result = Runtime.GenerateMessageSend( 190651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines *this, ReturnValueSlot(), MethodWithObjects->getReturnType(), Sel, 191651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines Receiver, Args, Class, MethodWithObjects); 192527842f97b684831de2b1a455649131ed381ece4John McCall 193527842f97b684831de2b1a455649131ed381ece4John McCall // The above message send needs these objects, but in ARC they are 194527842f97b684831de2b1a455649131ed381ece4John McCall // passed in a buffer that is essentially __unsafe_unretained. 195527842f97b684831de2b1a455649131ed381ece4John McCall // Therefore we must prevent the optimizer from releasing them until 196527842f97b684831de2b1a455649131ed381ece4John McCall // after the call. 197527842f97b684831de2b1a455649131ed381ece4John McCall if (TrackNeededObjects) { 198527842f97b684831de2b1a455649131ed381ece4John McCall EmitARCIntrinsicUse(NeededObjects); 199527842f97b684831de2b1a455649131ed381ece4John McCall } 200527842f97b684831de2b1a455649131ed381ece4John McCall 201ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return Builder.CreateBitCast(result.getScalarVal(), 202ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ConvertType(E->getType())); 203ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek} 204ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 205ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenekllvm::Value *CodeGenFunction::EmitObjCArrayLiteral(const ObjCArrayLiteral *E) { 206ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return EmitObjCCollectionLiteral(E, E->getArrayWithObjectsMethod()); 207ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek} 208ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 209ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenekllvm::Value *CodeGenFunction::EmitObjCDictionaryLiteral( 210ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const ObjCDictionaryLiteral *E) { 211ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return EmitObjCCollectionLiteral(E, E->getDictWithObjectsMethod()); 212ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek} 213ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 2148fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner/// Emit a selector. 2158fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattnerllvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) { 2168fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // Untyped selector. 2178fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // Note that this implementation allows for non-constant strings to be passed 2188fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // as arguments to @selector(). Currently, the only thing preventing this 2198fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // behaviour is the type checking in the front end. 220bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall return CGM.getObjCRuntime().GetSelector(*this, E->getSelector()); 2218fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner} 2228fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner 223ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbarllvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) { 224ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbar // FIXME: This should pass the Decl not the name. 225bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall return CGM.getObjCRuntime().GenerateProtocolRef(*this, E->getProtocol()); 226ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbar} 2278fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner 228926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor/// \brief Adjust the type of the result of an Objective-C message send 229926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor/// expression when the method has a related result type. 230926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregorstatic RValue AdjustRelatedResultType(CodeGenFunction &CGF, 231490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian QualType ExpT, 232926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor const ObjCMethodDecl *Method, 233926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor RValue Result) { 234926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor if (!Method) 235926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor return Result; 236f85e193739c953358c865005855253af4f68a497John McCall 237926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor if (!Method->hasRelatedResultType() || 238651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines CGF.getContext().hasSameType(ExpT, Method->getReturnType()) || 239926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor !Result.isScalar()) 240926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor return Result; 241926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor 242926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor // We have applied a related result type. Cast the rvalue appropriately. 243926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor return RValue::get(CGF.Builder.CreateBitCast(Result.getScalarVal(), 244490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian CGF.ConvertType(ExpT))); 245926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor} 2468fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner 247dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall/// Decide whether to extend the lifetime of the receiver of a 248dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall/// returns-inner-pointer message. 249dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCallstatic bool 250dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCallshouldExtendReceiverForInnerPointerMessage(const ObjCMessageExpr *message) { 251dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall switch (message->getReceiverKind()) { 252dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 253dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // For a normal instance message, we should extend unless the 254dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // receiver is loaded from a variable with precise lifetime. 255dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall case ObjCMessageExpr::Instance: { 256dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall const Expr *receiver = message->getInstanceReceiver(); 257dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(receiver); 258dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall if (!ice || ice->getCastKind() != CK_LValueToRValue) return true; 259dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall receiver = ice->getSubExpr()->IgnoreParens(); 260dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 261dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // Only __strong variables. 262dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall if (receiver->getType().getObjCLifetime() != Qualifiers::OCL_Strong) 263dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall return true; 264dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 265dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // All ivars and fields have precise lifetime. 266dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall if (isa<MemberExpr>(receiver) || isa<ObjCIvarRefExpr>(receiver)) 267dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall return false; 268dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 269dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // Otherwise, check for variables. 270dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall const DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(ice->getSubExpr()); 271dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall if (!declRef) return true; 272dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall const VarDecl *var = dyn_cast<VarDecl>(declRef->getDecl()); 273dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall if (!var) return true; 274dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 275dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // All variables have precise lifetime except local variables with 276dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // automatic storage duration that aren't specially marked. 277dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall return (var->hasLocalStorage() && 278dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall !var->hasAttr<ObjCPreciseLifetimeAttr>()); 279dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall } 280dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 281dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall case ObjCMessageExpr::Class: 282dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall case ObjCMessageExpr::SuperClass: 283dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // It's never necessary for class objects. 284dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall return false; 285dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 286dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall case ObjCMessageExpr::SuperInstance: 287dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // We generally assume that 'self' lives throughout a method call. 288dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall return false; 289dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall } 290dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 291dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall llvm_unreachable("invalid receiver kind"); 292dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall} 293dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 294ef072fd2f3347cfd857d6eb787b245b950771430John McCallRValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E, 295ef072fd2f3347cfd857d6eb787b245b950771430John McCall ReturnValueSlot Return) { 2968fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // Only the lookup mechanism and first two arguments of the method 2978fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // implementation vary between runtimes. We can get the receiver and 2988fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // arguments in generic code. 2991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 300f85e193739c953358c865005855253af4f68a497John McCall bool isDelegateInit = E->isDelegateInitCall(); 301f85e193739c953358c865005855253af4f68a497John McCall 302dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall const ObjCMethodDecl *method = E->getMethodDecl(); 3034e1524babb095e70de1da882573eb6fbee98a857Fariborz Jahanian 304f85e193739c953358c865005855253af4f68a497John McCall // We don't retain the receiver in delegate init calls, and this is 305f85e193739c953358c865005855253af4f68a497John McCall // safe because the receiver value is always loaded from 'self', 306f85e193739c953358c865005855253af4f68a497John McCall // which we zero out. We don't want to Block_copy block receivers, 307f85e193739c953358c865005855253af4f68a497John McCall // though. 308f85e193739c953358c865005855253af4f68a497John McCall bool retainSelf = 309f85e193739c953358c865005855253af4f68a497John McCall (!isDelegateInit && 3104e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie CGM.getLangOpts().ObjCAutoRefCount && 311dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall method && 312dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall method->hasAttr<NSConsumesSelfAttr>()); 313f85e193739c953358c865005855253af4f68a497John McCall 314208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 3158fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner bool isSuperMessage = false; 316f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar bool isClassMessage = false; 317c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall ObjCInterfaceDecl *OID = 0; 3188fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // Find the receiver 319926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor QualType ReceiverType; 3200b647a6ea18151149d624ab373e6fe0e819e4a9aDaniel Dunbar llvm::Value *Receiver = 0; 32104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor switch (E->getReceiverKind()) { 32204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor case ObjCMessageExpr::Instance: 323926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor ReceiverType = E->getInstanceReceiver()->getType(); 324f85e193739c953358c865005855253af4f68a497John McCall if (retainSelf) { 325f85e193739c953358c865005855253af4f68a497John McCall TryEmitResult ter = tryEmitARCRetainScalarExpr(*this, 326f85e193739c953358c865005855253af4f68a497John McCall E->getInstanceReceiver()); 327f85e193739c953358c865005855253af4f68a497John McCall Receiver = ter.getPointer(); 328dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall if (ter.getInt()) retainSelf = false; 329f85e193739c953358c865005855253af4f68a497John McCall } else 330f85e193739c953358c865005855253af4f68a497John McCall Receiver = EmitScalarExpr(E->getInstanceReceiver()); 33104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor break; 3321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 33304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor case ObjCMessageExpr::Class: { 334926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor ReceiverType = E->getClassReceiver(); 335926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor const ObjCObjectType *ObjTy = ReceiverType->getAs<ObjCObjectType>(); 3363031c63f7b5b09d5f64609fa7a1922a05b520fa7John McCall assert(ObjTy && "Invalid Objective-C class message send"); 3373031c63f7b5b09d5f64609fa7a1922a05b520fa7John McCall OID = ObjTy->getInterface(); 3383031c63f7b5b09d5f64609fa7a1922a05b520fa7John McCall assert(OID && "Invalid Objective-C class message send"); 339bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall Receiver = Runtime.GetClass(*this, OID); 340f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar isClassMessage = true; 34104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor break; 34204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor } 34304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor 34404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor case ObjCMessageExpr::SuperInstance: 345926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor ReceiverType = E->getSuperType(); 34604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor Receiver = LoadObjCSelf(); 3478fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner isSuperMessage = true; 34804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor break; 34904badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor 35004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor case ObjCMessageExpr::SuperClass: 351926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor ReceiverType = E->getSuperType(); 3528fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner Receiver = LoadObjCSelf(); 35304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor isSuperMessage = true; 35404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor isClassMessage = true; 35504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor break; 3568fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner } 3578fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner 358dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall if (retainSelf) 359dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall Receiver = EmitARCRetainNonBlock(Receiver); 360dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 361dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // In ARC, we sometimes want to "extend the lifetime" 362dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // (i.e. retain+autorelease) of receivers of returns-inner-pointer 363dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // messages. 3644e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().ObjCAutoRefCount && method && 365dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall method->hasAttr<ObjCReturnsInnerPointerAttr>() && 366dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall shouldExtendReceiverForInnerPointerMessage(E)) 367dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall Receiver = EmitARCRetainAutorelease(ReceiverType, Receiver); 368dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 369651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines QualType ResultType = method ? method->getReturnType() : E->getType(); 370f85e193739c953358c865005855253af4f68a497John McCall 37119cd87eb5fb3c197e631ce08fd52c446c4d4e8f1Daniel Dunbar CallArgList Args; 372dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall EmitCallArgs(Args, method, E->arg_begin(), E->arg_end()); 3731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 374f85e193739c953358c865005855253af4f68a497John McCall // For delegate init calls in ARC, do an unsafe store of null into 375f85e193739c953358c865005855253af4f68a497John McCall // self. This represents the call taking direct ownership of that 376f85e193739c953358c865005855253af4f68a497John McCall // value. We have to do this after emitting the other call 377f85e193739c953358c865005855253af4f68a497John McCall // arguments because they might also reference self, but we don't 378f85e193739c953358c865005855253af4f68a497John McCall // have to worry about any of them modifying self because that would 379f85e193739c953358c865005855253af4f68a497John McCall // be an undefined read and write of an object in unordered 380f85e193739c953358c865005855253af4f68a497John McCall // expressions. 381f85e193739c953358c865005855253af4f68a497John McCall if (isDelegateInit) { 3824e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie assert(getLangOpts().ObjCAutoRefCount && 383f85e193739c953358c865005855253af4f68a497John McCall "delegate init calls should only be marked in ARC"); 384f85e193739c953358c865005855253af4f68a497John McCall 385f85e193739c953358c865005855253af4f68a497John McCall // Do an unsafe store of null into self. 386f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *selfAddr = 387f85e193739c953358c865005855253af4f68a497John McCall LocalDeclMap[cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()]; 388f85e193739c953358c865005855253af4f68a497John McCall assert(selfAddr && "no self entry for a delegate init call?"); 389f85e193739c953358c865005855253af4f68a497John McCall 390f85e193739c953358c865005855253af4f68a497John McCall Builder.CreateStore(getNullForVariable(selfAddr), selfAddr); 391f85e193739c953358c865005855253af4f68a497John McCall } 3927e70fb217dcdf96faf34df3e197c3831c86f8089Anders Carlsson 393926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor RValue result; 3948fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner if (isSuperMessage) { 3959384c768e93f270118a30ce96546083a666da284Chris Lattner // super is only valid in an Objective-C method 3969384c768e93f270118a30ce96546083a666da284Chris Lattner const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); 3977ce77920a35060f1c8dd72e541e42ce296ccd168Fariborz Jahanian bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext()); 398926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor result = Runtime.GenerateMessageSendSuper(*this, Return, ResultType, 399926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor E->getSelector(), 400926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor OMD->getClassInterface(), 401926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor isCategoryImpl, 402926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor Receiver, 403926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor isClassMessage, 404926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor Args, 405dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall method); 406926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor } else { 407926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor result = Runtime.GenerateMessageSend(*this, Return, ResultType, 408926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor E->getSelector(), 409926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor Receiver, Args, OID, 410dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall method); 4118fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner } 412f85e193739c953358c865005855253af4f68a497John McCall 413f85e193739c953358c865005855253af4f68a497John McCall // For delegate init calls in ARC, implicitly store the result of 414f85e193739c953358c865005855253af4f68a497John McCall // the call back into self. This takes ownership of the value. 415f85e193739c953358c865005855253af4f68a497John McCall if (isDelegateInit) { 416f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *selfAddr = 417f85e193739c953358c865005855253af4f68a497John McCall LocalDeclMap[cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()]; 418f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *newSelf = result.getScalarVal(); 419f85e193739c953358c865005855253af4f68a497John McCall 420f85e193739c953358c865005855253af4f68a497John McCall // The delegate return type isn't necessarily a matching type; in 421f85e193739c953358c865005855253af4f68a497John McCall // fact, it's quite likely to be 'id'. 4222acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *selfTy = 423f85e193739c953358c865005855253af4f68a497John McCall cast<llvm::PointerType>(selfAddr->getType())->getElementType(); 424f85e193739c953358c865005855253af4f68a497John McCall newSelf = Builder.CreateBitCast(newSelf, selfTy); 425f85e193739c953358c865005855253af4f68a497John McCall 426f85e193739c953358c865005855253af4f68a497John McCall Builder.CreateStore(newSelf, selfAddr); 427f85e193739c953358c865005855253af4f68a497John McCall } 4284e1524babb095e70de1da882573eb6fbee98a857Fariborz Jahanian 429490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian return AdjustRelatedResultType(*this, E->getType(), method, result); 4305508518a2702b00be3b15a26d772bde968972f54Anders Carlsson} 4315508518a2702b00be3b15a26d772bde968972f54Anders Carlsson 432f85e193739c953358c865005855253af4f68a497John McCallnamespace { 433f85e193739c953358c865005855253af4f68a497John McCallstruct FinishARCDealloc : EHScopeStack::Cleanup { 434651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines void Emit(CodeGenFunction &CGF, Flags flags) override { 435f85e193739c953358c865005855253af4f68a497John McCall const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CGF.CurCodeDecl); 436799d34e9505a833549c71f2ac5f842da157ea031John McCall 437799d34e9505a833549c71f2ac5f842da157ea031John McCall const ObjCImplDecl *impl = cast<ObjCImplDecl>(method->getDeclContext()); 438f85e193739c953358c865005855253af4f68a497John McCall const ObjCInterfaceDecl *iface = impl->getClassInterface(); 439f85e193739c953358c865005855253af4f68a497John McCall if (!iface->getSuperClass()) return; 440f85e193739c953358c865005855253af4f68a497John McCall 441799d34e9505a833549c71f2ac5f842da157ea031John McCall bool isCategory = isa<ObjCCategoryImplDecl>(impl); 442799d34e9505a833549c71f2ac5f842da157ea031John McCall 443f85e193739c953358c865005855253af4f68a497John McCall // Call [super dealloc] if we have a superclass. 444f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *self = CGF.LoadObjCSelf(); 445f85e193739c953358c865005855253af4f68a497John McCall 446f85e193739c953358c865005855253af4f68a497John McCall CallArgList args; 447f85e193739c953358c865005855253af4f68a497John McCall CGF.CGM.getObjCRuntime().GenerateMessageSendSuper(CGF, ReturnValueSlot(), 448f85e193739c953358c865005855253af4f68a497John McCall CGF.getContext().VoidTy, 449f85e193739c953358c865005855253af4f68a497John McCall method->getSelector(), 450f85e193739c953358c865005855253af4f68a497John McCall iface, 451799d34e9505a833549c71f2ac5f842da157ea031John McCall isCategory, 452f85e193739c953358c865005855253af4f68a497John McCall self, 453f85e193739c953358c865005855253af4f68a497John McCall /*is class msg*/ false, 454f85e193739c953358c865005855253af4f68a497John McCall args, 455f85e193739c953358c865005855253af4f68a497John McCall method); 456f85e193739c953358c865005855253af4f68a497John McCall } 457f85e193739c953358c865005855253af4f68a497John McCall}; 458f85e193739c953358c865005855253af4f68a497John McCall} 459f85e193739c953358c865005855253af4f68a497John McCall 460af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// StartObjCMethod - Begin emission of an ObjCMethod. This generates 461af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// the LLVM function and sets the other context used by 462af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// CodeGenFunction. 463679a502d462ef819e6175b58e255ca3f3391e7cfFariborz Jahanianvoid CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD, 4648d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel const ObjCContainerDecl *CD, 4658d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel SourceLocation StartLoc) { 466d26bc76c98006609002d9930f8840490e88ac5b5John McCall FunctionArgList args; 4674800ea6ff8017cf803c32a5fd63b94c0614014e3Devang Patel // Check if we should generate debug info for this method. 468c3030bc285d90a139fb74629daadef5617283203David Blaikie if (OMD->hasAttr<NoDebugAttr>()) 469c3030bc285d90a139fb74629daadef5617283203David Blaikie DebugInfo = NULL; // disable debug info indefinitely for this function 4704800ea6ff8017cf803c32a5fd63b94c0614014e3Devang Patel 471679a502d462ef819e6175b58e255ca3f3391e7cfFariborz Jahanian llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD); 472f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 473de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall const CGFunctionInfo &FI = CGM.getTypes().arrangeObjCMethodDeclaration(OMD); 4740e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar CGM.SetInternalFunctionAttributes(OMD, Fn, FI); 4754111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner 476d26bc76c98006609002d9930f8840490e88ac5b5John McCall args.push_back(OMD->getSelfDecl()); 477d26bc76c98006609002d9930f8840490e88ac5b5John McCall args.push_back(OMD->getCmdDecl()); 4784111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner 479651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (const auto *PI : OMD->params()) 480651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines args.push_back(PI); 481b7ec246872b412f0e7bb9e93eacfd78cfa6adfb3Daniel Dunbar 48214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne CurGD = OMD; 48314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne 484651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines StartFunction(OMD, OMD->getReturnType(), Fn, FI, args, StartLoc); 485f85e193739c953358c865005855253af4f68a497John McCall 486f85e193739c953358c865005855253af4f68a497John McCall // In ARC, certain methods get an extra cleanup. 4874e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().ObjCAutoRefCount && 488f85e193739c953358c865005855253af4f68a497John McCall OMD->isInstanceMethod() && 489f85e193739c953358c865005855253af4f68a497John McCall OMD->getSelector().isUnarySelector()) { 490f85e193739c953358c865005855253af4f68a497John McCall const IdentifierInfo *ident = 491f85e193739c953358c865005855253af4f68a497John McCall OMD->getSelector().getIdentifierInfoForSlot(0); 492f85e193739c953358c865005855253af4f68a497John McCall if (ident->isStr("dealloc")) 493f85e193739c953358c865005855253af4f68a497John McCall EHStack.pushCleanup<FinishARCDealloc>(getARCCleanupKind()); 494f85e193739c953358c865005855253af4f68a497John McCall } 495af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar} 496af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 497f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF, 498f85e193739c953358c865005855253af4f68a497John McCall LValue lvalue, QualType type); 499f85e193739c953358c865005855253af4f68a497John McCall 500af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// Generate an Objective-C method. An Objective-C method is a C function with 5011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// its pointer, name, and types registered in the class struture. 502af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) { 5038d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel StartObjCMethod(OMD, OMD->getClassInterface(), OMD->getLocStart()); 504651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines PGO.assignRegionCounters(OMD, CurFn); 505651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines assert(isa<CompoundStmt>(OMD->getBody())); 506651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines RegionCounter Cnt = getPGORegionCounter(OMD->getBody()); 507651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines Cnt.beginRegion(Builder); 508651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines EmitCompoundStmtWithoutScope(*cast<CompoundStmt>(OMD->getBody())); 5096fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis FinishFunction(OMD->getBodyRBrace()); 510651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines PGO.emitInstrumentationData(); 511651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines PGO.destroyRegionCounters(); 512af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar} 513af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 51441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall/// emitStructGetterCall - Call the runtime function to load a property 51541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall/// into the return value slot. 51641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCallstatic void emitStructGetterCall(CodeGenFunction &CGF, ObjCIvarDecl *ivar, 51741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall bool isAtomic, bool hasStrong) { 51841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall ASTContext &Context = CGF.getContext(); 51941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 52041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall llvm::Value *src = 52141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(), 52241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall ivar, 0).getAddress(); 52341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 52441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall // objc_copyStruct (ReturnValue, &structIvar, 52541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall // sizeof (Type of Ivar), isAtomic, false); 52641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall CallArgList args; 52741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 52841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall llvm::Value *dest = CGF.Builder.CreateBitCast(CGF.ReturnValue, CGF.VoidPtrTy); 52941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(dest), Context.VoidPtrTy); 53041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 53141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall src = CGF.Builder.CreateBitCast(src, CGF.VoidPtrTy); 53241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(src), Context.VoidPtrTy); 53341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 53441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall CharUnits size = CGF.getContext().getTypeSizeInChars(ivar->getType()); 53541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(CGF.CGM.getSize(size)), Context.getSizeType()); 53641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(CGF.Builder.getInt1(isAtomic)), Context.BoolTy); 53741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(CGF.Builder.getInt1(hasStrong)), Context.BoolTy); 53841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 53941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall llvm::Value *fn = CGF.CGM.getObjCRuntime().GetGetStructFunction(); 5400f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(Context.VoidTy, args, 5410f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 5420f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 54341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall fn, ReturnValueSlot(), args); 54441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall} 54541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 5461e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// Determine whether the given architecture supports unaligned atomic 5471e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// accesses. They don't have to be fast, just faster than a function 5481e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// call and a mutex. 5491e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallstatic bool hasUnalignedAtomics(llvm::Triple::ArchType arch) { 550de24d44665486e98df2aeb2ef5bbc163abfe7981Eli Friedman // FIXME: Allow unaligned atomic load/store on x86. (It is not 551de24d44665486e98df2aeb2ef5bbc163abfe7981Eli Friedman // currently supported by the backend.) 552de24d44665486e98df2aeb2ef5bbc163abfe7981Eli Friedman return 0; 5531e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall} 5541e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5551e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// Return the maximum size that permits atomic accesses for the given 5561e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// architecture. 5571e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallstatic CharUnits getMaxAtomicAccessSize(CodeGenModule &CGM, 5581e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Triple::ArchType arch) { 5591e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // ARM has 8-byte atomic accesses, but it's not clear whether we 5601e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // want to rely on them here. 5611e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5621e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // In the default case, just assume that any size up to a pointer is 5631e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // fine given adequate alignment. 5641e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return CharUnits::fromQuantity(CGM.PointerSizeInBytes); 5651e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall} 5661e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5671e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallnamespace { 5681e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall class PropertyImplStrategy { 5691e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall public: 5701e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall enum StrategyKind { 5711e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// The 'native' strategy is to use the architecture's provided 5721e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// reads and writes. 5731e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Native, 5741e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5751e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// Use objc_setProperty and objc_getProperty. 5761e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall GetSetProperty, 5771e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5781e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// Use objc_setProperty for the setter, but use expression 5791e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// evaluation for the getter. 5801e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall SetPropertyAndExpressionGet, 5811e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5821e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// Use objc_copyStruct. 5831e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CopyStruct, 5841e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5851e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// The 'expression' strategy is to emit normal assignment or 5861e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// lvalue-to-rvalue expressions. 5871e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Expression 5881e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall }; 5891e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5901e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall StrategyKind getKind() const { return StrategyKind(Kind); } 5911e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5921e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall bool hasStrongMember() const { return HasStrong; } 5931e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall bool isAtomic() const { return IsAtomic; } 5941e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall bool isCopy() const { return IsCopy; } 5951e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5961e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CharUnits getIvarSize() const { return IvarSize; } 5971e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CharUnits getIvarAlignment() const { return IvarAlignment; } 5981e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5991e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall PropertyImplStrategy(CodeGenModule &CGM, 6001e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall const ObjCPropertyImplDecl *propImpl); 6011e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6021e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall private: 6031e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall unsigned Kind : 8; 6041e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall unsigned IsAtomic : 1; 6051e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall unsigned IsCopy : 1; 6061e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall unsigned HasStrong : 1; 6071e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6081e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CharUnits IvarSize; 6091e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CharUnits IvarAlignment; 6101e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall }; 6111e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall} 6121e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 613bed28ac1d1463adca3ecf24fca5c30646fa9dbb2Sylvestre Ledru/// Pick an implementation strategy for the given property synthesis. 6141e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallPropertyImplStrategy::PropertyImplStrategy(CodeGenModule &CGM, 6151e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall const ObjCPropertyImplDecl *propImpl) { 6161e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall const ObjCPropertyDecl *prop = propImpl->getPropertyDecl(); 617265941bc308d65cc270d5c4de5806f37ce405606John McCall ObjCPropertyDecl::SetterKind setterKind = prop->getSetterKind(); 6181e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 619265941bc308d65cc270d5c4de5806f37ce405606John McCall IsCopy = (setterKind == ObjCPropertyDecl::Copy); 620265941bc308d65cc270d5c4de5806f37ce405606John McCall IsAtomic = prop->isAtomic(); 6211e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall HasStrong = false; // doesn't matter here. 6221e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6231e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Evaluate the ivar's size and alignment. 6241e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); 6251e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall QualType ivarType = ivar->getType(); 626651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines std::tie(IvarSize, IvarAlignment) = 627651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines CGM.getContext().getTypeInfoInChars(ivarType); 6281e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6291e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // If we have a copy property, we always have to use getProperty/setProperty. 630265941bc308d65cc270d5c4de5806f37ce405606John McCall // TODO: we could actually use setProperty and an expression for non-atomics. 6311e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (IsCopy) { 6321e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = GetSetProperty; 6331e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 6341e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 6351e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 636265941bc308d65cc270d5c4de5806f37ce405606John McCall // Handle retain. 637265941bc308d65cc270d5c4de5806f37ce405606John McCall if (setterKind == ObjCPropertyDecl::Retain) { 6381e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // In GC-only, there's nothing special that needs to be done. 6394e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) { 6401e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // fallthrough 6411e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6421e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // In ARC, if the property is non-atomic, use expression emission, 6431e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // which translates to objc_storeStrong. This isn't required, but 6441e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // it's slightly nicer. 6454e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (CGM.getLangOpts().ObjCAutoRefCount && !IsAtomic) { 646d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall // Using standard expression emission for the setter is only 647d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall // acceptable if the ivar is __strong, which won't be true if 648d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall // the property is annotated with __attribute__((NSObject)). 649d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall // TODO: falling all the way back to objc_setProperty here is 650d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall // just laziness, though; we could still use objc_storeStrong 651d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall // if we hacked it right. 652d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall if (ivarType.getObjCLifetime() == Qualifiers::OCL_Strong) 653d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall Kind = Expression; 654d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall else 655d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall Kind = SetPropertyAndExpressionGet; 6561e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 6571e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6581e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Otherwise, we need to at least use setProperty. However, if 6591e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // the property isn't atomic, we can use normal expression 6601e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // emission for the getter. 6611e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } else if (!IsAtomic) { 6621e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = SetPropertyAndExpressionGet; 6631e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 6641e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6651e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Otherwise, we have to use both setProperty and getProperty. 6661e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } else { 6671e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = GetSetProperty; 6681e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 6691e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 6701e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 6711e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6721e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // If we're not atomic, just use expression accesses. 6731e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (!IsAtomic) { 6741e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = Expression; 6751e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 6761e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 6771e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6785889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall // Properties on bitfield ivars need to be emitted using expression 6795889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall // accesses even if they're nominally atomic. 6805889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall if (ivar->isBitField()) { 6815889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall Kind = Expression; 6825889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall return; 6835889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall } 6845889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall 6851e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // GC-qualified or ARC-qualified ivars need to be emitted as 6861e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // expressions. This actually works out to being atomic anyway, 6871e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // except for ARC __strong, but that should trigger the above code. 6881e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (ivarType.hasNonTrivialObjCLifetime() || 6894e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie (CGM.getLangOpts().getGC() && 6901e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CGM.getContext().getObjCGCAttrKind(ivarType))) { 6911e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = Expression; 6921e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 6931e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 6941e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6951e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Compute whether the ivar has strong members. 6964e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().getGC()) 6971e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (const RecordType *recordType = ivarType->getAs<RecordType>()) 6981e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall HasStrong = recordType->getDecl()->hasObjectMember(); 6991e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7001e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // We can never access structs with object members with a native 7011e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // access, because we need to use write barriers. This is what 7021e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // objc_copyStruct is for. 7031e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (HasStrong) { 7041e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = CopyStruct; 7051e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 7061e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 7071e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7081e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Otherwise, this is target-dependent and based on the size and 7091e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // alignment of the ivar. 710c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall 711c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall // If the size of the ivar is not a power of two, give up. We don't 712c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall // want to get into the business of doing compare-and-swaps. 713c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall if (!IvarSize.isPowerOfTwo()) { 714c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall Kind = CopyStruct; 715c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall return; 716c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall } 717c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall 7181e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Triple::ArchType arch = 71964aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall CGM.getTarget().getTriple().getArch(); 7201e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7211e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Most architectures require memory to fit within a single cache 7221e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // line, so the alignment has to be at least the size of the access. 7231e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Otherwise we have to grab a lock. 7241e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (IvarAlignment < IvarSize && !hasUnalignedAtomics(arch)) { 7251e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = CopyStruct; 7261e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 7271e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 7281e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7291e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // If the ivar's size exceeds the architecture's maximum atomic 7301e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // access size, we have to use CopyStruct. 7311e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (IvarSize > getMaxAtomicAccessSize(CGM, arch)) { 7321e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = CopyStruct; 7331e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 7341e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 7351e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7361e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Otherwise, we can use native loads and stores. 7371e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = Native; 7381e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall} 739af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 7402ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// \brief Generate an Objective-C property getter function. 7412ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// 7422ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// The given Decl must be an ObjCImplementationDecl. \@synthesize 743489034cf8bde09360e0089f401b2929597b125d8Steve Naroff/// is illegal within a category. 744fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanianvoid CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP, 745fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian const ObjCPropertyImplDecl *PID) { 746b6e5fe3cae9bf456f084c555c3ecc256f4181cc5Fariborz Jahanian llvm::Constant *AtomicHelperFn = 74720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian GenerateObjCAtomicGetterCopyHelperFunction(PID); 748af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 749af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCMethodDecl *OMD = PD->getGetterMethodDecl(); 750af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar assert(OMD && "Invalid call to generate getter (empty method)"); 751ea32047660159811a4c14d008a4b7e3807a705d6Eric Christopher StartObjCMethod(OMD, IMP->getClassInterface(), OMD->getLocStart()); 7521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 753490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian generateObjCGetterBody(IMP, PID, OMD, AtomicHelperFn); 7541e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7551e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall FinishFunction(); 7561e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall} 7571e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7586c11f0b3106263600af2ea0438ebb372821514aaJohn McCallstatic bool hasTrivialGetExpr(const ObjCPropertyImplDecl *propImpl) { 7596c11f0b3106263600af2ea0438ebb372821514aaJohn McCall const Expr *getter = propImpl->getGetterCXXConstructor(); 7601e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (!getter) return true; 7611e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7621e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Sema only makes only of these when the ivar has a C++ class type, 7631e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // so the form is pretty constrained. 7641e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7656c11f0b3106263600af2ea0438ebb372821514aaJohn McCall // If the property has a reference type, we might just be binding a 7666c11f0b3106263600af2ea0438ebb372821514aaJohn McCall // reference, in which case the result will be a gl-value. We should 7676c11f0b3106263600af2ea0438ebb372821514aaJohn McCall // treat this as a non-trivial operation. 7686c11f0b3106263600af2ea0438ebb372821514aaJohn McCall if (getter->isGLValue()) 7696c11f0b3106263600af2ea0438ebb372821514aaJohn McCall return false; 7706c11f0b3106263600af2ea0438ebb372821514aaJohn McCall 7711e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // If we selected a trivial copy-constructor, we're okay. 7721e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (const CXXConstructExpr *construct = dyn_cast<CXXConstructExpr>(getter)) 7731e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return (construct->getConstructor()->isTrivial()); 7741e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7751e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // The constructor might require cleanups (in which case it's never 7761e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // trivial). 7771e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall assert(isa<ExprWithCleanups>(getter)); 7781e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return false; 7791e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall} 7801e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 78120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian/// emitCPPObjectAtomicGetterCall - Call the runtime function to 78220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian/// copy the ivar into the resturn slot. 78320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanianstatic void emitCPPObjectAtomicGetterCall(CodeGenFunction &CGF, 78420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Value *returnAddr, 78520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ObjCIvarDecl *ivar, 78620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Constant *AtomicHelperFn) { 78720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian // objc_copyCppObjectAtomic (&returnSlot, &CppObjectIvar, 78820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian // AtomicHelperFn); 78920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CallArgList args; 79020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 79120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian // The 1st argument is the return Slot. 79220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian args.add(RValue::get(returnAddr), CGF.getContext().VoidPtrTy); 79320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 79420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian // The 2nd argument is the address of the ivar. 79520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Value *ivarAddr = 79620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), 79720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CGF.LoadObjCSelf(), ivar, 0).getAddress(); 79820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy); 79920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy); 80020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 80120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian // Third argument is the helper function. 80220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy); 80320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 80420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Value *copyCppAtomicObjectFn = 805d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall CGF.CGM.getObjCRuntime().GetCppAtomicObjectGetFunction(); 8060f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(CGF.getContext().VoidTy, 8070f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall args, 8080f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 8090f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 81020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian copyCppAtomicObjectFn, ReturnValueSlot(), args); 81120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian} 81220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 8131e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallvoid 8141e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallCodeGenFunction::generateObjCGetterBody(const ObjCImplementationDecl *classImpl, 815b6e5fe3cae9bf456f084c555c3ecc256f4181cc5Fariborz Jahanian const ObjCPropertyImplDecl *propImpl, 816490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian const ObjCMethodDecl *GetterMethodDecl, 817b6e5fe3cae9bf456f084c555c3ecc256f4181cc5Fariborz Jahanian llvm::Constant *AtomicHelperFn) { 8181e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // If there's a non-trivial 'get' expression, we just have to emit that. 8191e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (!hasTrivialGetExpr(propImpl)) { 82020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if (!AtomicHelperFn) { 82120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ReturnStmt ret(SourceLocation(), propImpl->getGetterCXXConstructor(), 82220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian /*nrvo*/ 0); 82320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian EmitReturnStmt(ret); 82420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian } 82520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian else { 82620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); 82720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian emitCPPObjectAtomicGetterCall(*this, ReturnValue, 82820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ivar, AtomicHelperFn); 82920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian } 8301e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 8311e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 8321e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8331e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall const ObjCPropertyDecl *prop = propImpl->getPropertyDecl(); 8341e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall QualType propType = prop->getType(); 8351e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall ObjCMethodDecl *getterMethod = prop->getGetterMethodDecl(); 8361e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8371e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); 8381e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8391e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Pick an implementation strategy. 8401e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall PropertyImplStrategy strategy(CGM, propImpl); 8411e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall switch (strategy.getKind()) { 8421e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::Native: { 843aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman // We don't need to do anything for a zero-size struct. 844aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman if (strategy.getIvarSize().isZero()) 845aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman return; 846aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman 8471e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0); 8481e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8491e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Currently, all atomic accesses have to be through integer 8501e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // types, so there's no point in trying to pick a prettier type. 8511e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Type *bitcastType = 8521e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Type::getIntNTy(getLLVMContext(), 8531e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall getContext().toBits(strategy.getIvarSize())); 8541e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall bitcastType = bitcastType->getPointerTo(); // addrspace 0 okay 8551e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8561e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Perform an atomic load. This does not impose ordering constraints. 8571e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *ivarAddr = LV.getAddress(); 8581e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall ivarAddr = Builder.CreateBitCast(ivarAddr, bitcastType); 8591e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::LoadInst *load = Builder.CreateLoad(ivarAddr, "load"); 8601e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall load->setAlignment(strategy.getIvarAlignment().getQuantity()); 8611e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall load->setAtomic(llvm::Unordered); 8621e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8631e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Store that value into the return address. Doing this with a 8641e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // bitcast is likely to produce some pretty ugly IR, but it's not 8651e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // the *most* terrible thing in the world. 8661e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Builder.CreateStore(load, Builder.CreateBitCast(ReturnValue, bitcastType)); 8671e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8681e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Make sure we don't do an autorelease. 8691e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall AutoreleaseResult = false; 8701e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 8711e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 8721e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8731e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::GetSetProperty: { 8741e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *getPropertyFn = 8751e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CGM.getObjCRuntime().GetPropertyGetFunction(); 8761e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (!getPropertyFn) { 8771e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CGM.ErrorUnsupported(propImpl, "Obj-C getter requiring atomic copy"); 878c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar return; 879c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar } 880c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar 881c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true). 882c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar // FIXME: Can't this be simpler? This might even be worse than the 883c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar // corresponding gcc code. 8841e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *cmd = 8851e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Builder.CreateLoad(LocalDeclMap[getterMethod->getCmdDecl()], "cmd"); 8861e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *self = Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy); 8871e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *ivarOffset = 8881e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall EmitIvarOffset(classImpl->getClassInterface(), ivar); 8891e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8901e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CallArgList args; 8911e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall args.add(RValue::get(self), getContext().getObjCIdType()); 8921e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall args.add(RValue::get(cmd), getContext().getObjCSelType()); 8931e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall args.add(RValue::get(ivarOffset), getContext().getPointerDiffType()); 894265941bc308d65cc270d5c4de5806f37ce405606John McCall args.add(RValue::get(Builder.getInt1(strategy.isAtomic())), 895265941bc308d65cc270d5c4de5806f37ce405606John McCall getContext().BoolTy); 8961e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 897e4be5a66072f7c7618071284c8d2a9c6d8e691cfDaniel Dunbar // FIXME: We shouldn't need to get the function info here, the 898e4be5a66072f7c7618071284c8d2a9c6d8e691cfDaniel Dunbar // runtime already should have computed it to build the function. 899651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines llvm::Instruction *CallInstruction; 9000f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RValue RV = EmitCall(getTypes().arrangeFreeFunctionCall(propType, args, 9010f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 9020f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 903651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines getPropertyFn, ReturnValueSlot(), args, 0, 904651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines &CallInstruction); 905651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(CallInstruction)) 906651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines call->setTailCall(); 9071e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 908c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar // We need to fix the type here. Ivars with copy & retain are 909c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar // always objects so we don't need to worry about complex or 910c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar // aggregates. 911651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines RV = RValue::get(Builder.CreateBitCast( 912651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines RV.getScalarVal(), 913651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines getTypes().ConvertType(getterMethod->getReturnType()))); 9141e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 9151e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall EmitReturnOfRValue(RV, propType); 916f85e193739c953358c865005855253af4f68a497John McCall 917f85e193739c953358c865005855253af4f68a497John McCall // objc_getProperty does an autorelease, so we should suppress ours. 918f85e193739c953358c865005855253af4f68a497John McCall AutoreleaseResult = false; 9191e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 9201e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 9211e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 9221e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 9231e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::CopyStruct: 9241e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall emitStructGetterCall(*this, ivar, strategy.isAtomic(), 9251e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall strategy.hasStrongMember()); 9261e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 9271e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 9281e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::Expression: 9291e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::SetPropertyAndExpressionGet: { 9301e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0); 9311e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 9321e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall QualType ivarType = ivar->getType(); 9339d232c884ea9872d6555df0fd7359699819bc1f1John McCall switch (getEvaluationKind(ivarType)) { 9349d232c884ea9872d6555df0fd7359699819bc1f1John McCall case TEK_Complex: { 9354ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky ComplexPairTy pair = EmitLoadOfComplex(LV, SourceLocation()); 9369d232c884ea9872d6555df0fd7359699819bc1f1John McCall EmitStoreOfComplex(pair, 9379d232c884ea9872d6555df0fd7359699819bc1f1John McCall MakeNaturalAlignAddrLValue(ReturnValue, ivarType), 9389d232c884ea9872d6555df0fd7359699819bc1f1John McCall /*init*/ true); 9399d232c884ea9872d6555df0fd7359699819bc1f1John McCall return; 9409d232c884ea9872d6555df0fd7359699819bc1f1John McCall } 9419d232c884ea9872d6555df0fd7359699819bc1f1John McCall case TEK_Aggregate: 9421e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // The return value slot is guaranteed to not be aliased, but 9431e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // that's not necessarily the same as "on the stack", so 9441e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // we still potentially need objc_memmove_collectable. 945649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier EmitAggregateCopy(ReturnValue, LV.getAddress(), ivarType); 9469d232c884ea9872d6555df0fd7359699819bc1f1John McCall return; 9479d232c884ea9872d6555df0fd7359699819bc1f1John McCall case TEK_Scalar: { 948ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall llvm::Value *value; 949ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall if (propType->isReferenceType()) { 950ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall value = LV.getAddress(); 951ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall } else { 952ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall // We want to load and autoreleaseReturnValue ARC __weak ivars. 953ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak) { 9541e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall value = emitARCRetainLoadOfScalar(*this, LV, ivarType); 955ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall 956ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall // Otherwise we want to do a simple load, suppressing the 957ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall // final autorelease. 958f85e193739c953358c865005855253af4f68a497John McCall } else { 9594ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky value = EmitLoadOfLValue(LV, SourceLocation()).getScalarVal(); 960ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall AutoreleaseResult = false; 96114086764e340267e17803d0f8243070ffae2c76eFariborz Jahanian } 962f85e193739c953358c865005855253af4f68a497John McCall 963ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall value = Builder.CreateBitCast(value, ConvertType(propType)); 964651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines value = Builder.CreateBitCast( 965651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines value, ConvertType(GetterMethodDecl->getReturnType())); 966ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall } 967ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall 968ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall EmitReturnOfRValue(RValue::get(value), propType); 9699d232c884ea9872d6555df0fd7359699819bc1f1John McCall return; 970ed1d29d62595a83ccf6ef23eb2759d355206df2eFariborz Jahanian } 9719d232c884ea9872d6555df0fd7359699819bc1f1John McCall } 9729d232c884ea9872d6555df0fd7359699819bc1f1John McCall llvm_unreachable("bad evaluation kind"); 973c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar } 974af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 9751e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 9761e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm_unreachable("bad @property implementation strategy!"); 977af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar} 978af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 97941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall/// emitStructSetterCall - Call the runtime function to store the value 98041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall/// from the first formal parameter into the given ivar. 98141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCallstatic void emitStructSetterCall(CodeGenFunction &CGF, ObjCMethodDecl *OMD, 98241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall ObjCIvarDecl *ivar) { 9832846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian // objc_copyStruct (&structIvar, &Arg, 9842846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian // sizeof (struct something), true, false); 985bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall CallArgList args; 986bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall 987bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall // The first argument is the address of the ivar. 98841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall llvm::Value *ivarAddr = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), 98941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall CGF.LoadObjCSelf(), ivar, 0) 99041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall .getAddress(); 99141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy); 99241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy); 993bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall 994bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall // The second argument is the address of the parameter variable. 99541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall ParmVarDecl *argVar = *OMD->param_begin(); 996f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr argRef(argVar, false, argVar->getType().getNonReferenceType(), 997c3953aab1b6167b7f394ca87096e91ab840026adFariborz Jahanian VK_LValue, SourceLocation()); 99841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall llvm::Value *argAddr = CGF.EmitLValue(&argRef).getAddress(); 99941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy); 100041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy); 1001bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall 1002bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall // The third argument is the sizeof the type. 1003bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall llvm::Value *size = 100441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall CGF.CGM.getSize(CGF.getContext().getTypeSizeInChars(ivar->getType())); 100541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(size), CGF.getContext().getSizeType()); 100641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 100741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall // The fourth argument is the 'isAtomic' flag. 100841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(CGF.Builder.getTrue()), CGF.getContext().BoolTy); 1009bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall 101041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall // The fifth argument is the 'hasStrong' flag. 101141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall // FIXME: should this really always be false? 101241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(CGF.Builder.getFalse()), CGF.getContext().BoolTy); 1013bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall 101441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall llvm::Value *copyStructFn = CGF.CGM.getObjCRuntime().GetSetStructFunction(); 10150f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(CGF.getContext().VoidTy, 10160f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall args, 10170f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 10180f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 101941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall copyStructFn, ReturnValueSlot(), args); 10202846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian} 10212846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian 1022cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian/// emitCPPObjectAtomicSetterCall - Call the runtime function to store 1023cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian/// the value from the first formal parameter into the given ivar, using 1024cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian/// the Cpp API for atomic Cpp objects with non-trivial copy assignment. 1025cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanianstatic void emitCPPObjectAtomicSetterCall(CodeGenFunction &CGF, 1026cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ObjCMethodDecl *OMD, 1027cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ObjCIvarDecl *ivar, 1028cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian llvm::Constant *AtomicHelperFn) { 1029cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // objc_copyCppObjectAtomic (&CppObjectIvar, &Arg, 1030cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // AtomicHelperFn); 1031cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian CallArgList args; 1032cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 1033cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // The first argument is the address of the ivar. 1034cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian llvm::Value *ivarAddr = 1035cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), 1036cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian CGF.LoadObjCSelf(), ivar, 0).getAddress(); 1037cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy); 1038cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy); 1039cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 1040cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // The second argument is the address of the parameter variable. 1041cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ParmVarDecl *argVar = *OMD->param_begin(); 1042f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr argRef(argVar, false, argVar->getType().getNonReferenceType(), 1043cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian VK_LValue, SourceLocation()); 1044cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian llvm::Value *argAddr = CGF.EmitLValue(&argRef).getAddress(); 1045cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy); 1046cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy); 1047cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 1048cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // Third argument is the helper function. 1049cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy); 1050cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 1051cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian llvm::Value *copyCppAtomicObjectFn = 1052d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall CGF.CGM.getObjCRuntime().GetCppAtomicObjectSetFunction(); 10530f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(CGF.getContext().VoidTy, 10540f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall args, 10550f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 10560f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 1057cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian copyCppAtomicObjectFn, ReturnValueSlot(), args); 1058cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian} 1059cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 106020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 10611e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallstatic bool hasTrivialSetExpr(const ObjCPropertyImplDecl *PID) { 10621e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Expr *setter = PID->getSetterCXXAssignment(); 10631e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (!setter) return true; 10641e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 10651e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Sema only makes only of these when the ivar has a C++ class type, 10661e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // so the form is pretty constrained. 106771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 106871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // An operator call is trivial if the function it calls is trivial. 10691e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // This also implies that there's nothing non-trivial going on with 10701e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // the arguments, because operator= can only be trivial if it's a 10711e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // synthesized assignment operator and therefore both parameters are 10721e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // references. 10731e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (CallExpr *call = dyn_cast<CallExpr>(setter)) { 107471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (const FunctionDecl *callee 107571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall = dyn_cast_or_null<FunctionDecl>(call->getCalleeDecl())) 107671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (callee->isTrivial()) 107771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return true; 107871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return false; 107901cb307f0b84a368cdbc0738d6680aab8ed7423fFariborz Jahanian } 108071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 10811e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall assert(isa<ExprWithCleanups>(setter)); 108271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return false; 108371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall} 108486957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar 10854e494cf56325f26a6c3eb5a065c35e54835f14e6Benjamin Kramerstatic bool UseOptimizedSetter(CodeGenModule &CGM) { 10864e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().getGC() != LangOptions::NonGC) 1087ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return false; 10887a0c064770355a4918df69f95de8dea6338a59a2Daniel Dunbar return CGM.getLangOpts().ObjCRuntime.hasOptimizedSetter(); 1089ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek} 1090ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 109171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCallvoid 109271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCallCodeGenFunction::generateObjCSetterBody(const ObjCImplementationDecl *classImpl, 1093cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian const ObjCPropertyImplDecl *propImpl, 1094cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian llvm::Constant *AtomicHelperFn) { 1095cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian const ObjCPropertyDecl *prop = propImpl->getPropertyDecl(); 1096cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); 1097cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ObjCMethodDecl *setterMethod = prop->getSetterMethodDecl(); 1098cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 109971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // Just use the setter expression if Sema gave us one and it's 1100cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // non-trivial. 11011e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (!hasTrivialSetExpr(propImpl)) { 1102cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian if (!AtomicHelperFn) 1103cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // If non-atomic, assignment is called directly. 1104cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian EmitStmt(propImpl->getSetterCXXAssignment()); 1105cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian else 1106cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // If atomic, assignment is called via a locking api. 1107cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian emitCPPObjectAtomicSetterCall(*this, setterMethod, ivar, 1108cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian AtomicHelperFn); 110971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return; 111071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall } 111171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 11121e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall PropertyImplStrategy strategy(CGM, propImpl); 11131e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall switch (strategy.getKind()) { 11141e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::Native: { 1115aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman // We don't need to do anything for a zero-size struct. 1116aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman if (strategy.getIvarSize().isZero()) 1117aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman return; 1118aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman 11191e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *argAddr = LocalDeclMap[*setterMethod->param_begin()]; 112071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 11211e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall LValue ivarLValue = 11221e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, /*quals*/ 0); 11231e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *ivarAddr = ivarLValue.getAddress(); 11241e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 11251e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Currently, all atomic accesses have to be through integer 11261e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // types, so there's no point in trying to pick a prettier type. 11271e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Type *bitcastType = 11281e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Type::getIntNTy(getLLVMContext(), 11291e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall getContext().toBits(strategy.getIvarSize())); 11301e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall bitcastType = bitcastType->getPointerTo(); // addrspace 0 okay 11311e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 11321e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Cast both arguments to the chosen operation type. 11331e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall argAddr = Builder.CreateBitCast(argAddr, bitcastType); 11341e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall ivarAddr = Builder.CreateBitCast(ivarAddr, bitcastType); 113571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 11361e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // This bitcast load is likely to cause some nasty IR. 11371e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *load = Builder.CreateLoad(argAddr); 11381e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 11391e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Perform an atomic store. There are no memory ordering requirements. 11401e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::StoreInst *store = Builder.CreateStore(load, ivarAddr); 11411e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall store->setAlignment(strategy.getIvarAlignment().getQuantity()); 11421e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall store->setAtomic(llvm::Unordered); 11431e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 11441e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 11451e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 11461e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::GetSetProperty: 11471e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::SetPropertyAndExpressionGet: { 1148ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 1149ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek llvm::Value *setOptimizedPropertyFn = 0; 1150ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek llvm::Value *setPropertyFn = 0; 1151ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (UseOptimizedSetter(CGM)) { 11527a0c064770355a4918df69f95de8dea6338a59a2Daniel Dunbar // 10.8 and iOS 6.0 code and GC is off 1153ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek setOptimizedPropertyFn = 115416098f366097305c348b356a44638d6c959c6e60Eric Christopher CGM.getObjCRuntime() 115516098f366097305c348b356a44638d6c959c6e60Eric Christopher .GetOptimizedPropertySetFunction(strategy.isAtomic(), 115616098f366097305c348b356a44638d6c959c6e60Eric Christopher strategy.isCopy()); 1157ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (!setOptimizedPropertyFn) { 1158ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek CGM.ErrorUnsupported(propImpl, "Obj-C optimized setter - NYI"); 1159ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return; 1160ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 116186957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar } 1162ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek else { 1163ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek setPropertyFn = CGM.getObjCRuntime().GetPropertySetFunction(); 1164ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (!setPropertyFn) { 1165ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek CGM.ErrorUnsupported(propImpl, "Obj-C setter requiring atomic copy"); 1166ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return; 1167ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 1168ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 1169ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 11701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Emit objc_setProperty((id) self, _cmd, offset, arg, 117186957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar // <is-atomic>, <is-copy>). 117271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall llvm::Value *cmd = 117371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall Builder.CreateLoad(LocalDeclMap[setterMethod->getCmdDecl()]); 117471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall llvm::Value *self = 117571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy); 117671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall llvm::Value *ivarOffset = 117771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall EmitIvarOffset(classImpl->getClassInterface(), ivar); 117871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall llvm::Value *arg = LocalDeclMap[*setterMethod->param_begin()]; 117971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall arg = Builder.CreateBitCast(Builder.CreateLoad(arg, "arg"), VoidPtrTy); 118071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 118171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall CallArgList args; 118271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall args.add(RValue::get(self), getContext().getObjCIdType()); 118371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall args.add(RValue::get(cmd), getContext().getObjCSelType()); 1184ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (setOptimizedPropertyFn) { 1185ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(arg), getContext().getObjCIdType()); 1186ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(ivarOffset), getContext().getPointerDiffType()); 11870f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall EmitCall(getTypes().arrangeFreeFunctionCall(getContext().VoidTy, args, 11880f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 11890f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 1190ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek setOptimizedPropertyFn, ReturnValueSlot(), args); 1191ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } else { 1192ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(ivarOffset), getContext().getPointerDiffType()); 1193ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(arg), getContext().getObjCIdType()); 1194ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(Builder.getInt1(strategy.isAtomic())), 1195ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek getContext().BoolTy); 1196ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(Builder.getInt1(strategy.isCopy())), 1197ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek getContext().BoolTy); 1198ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // FIXME: We shouldn't need to get the function info here, the runtime 1199ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // already should have computed it to build the function. 12000f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall EmitCall(getTypes().arrangeFreeFunctionCall(getContext().VoidTy, args, 12010f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 12020f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 1203ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek setPropertyFn, ReturnValueSlot(), args); 1204ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 1205ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 120671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return; 120771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall } 120871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 12091e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::CopyStruct: 121041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall emitStructSetterCall(*this, setterMethod, ivar); 121171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return; 12121e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 12131e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::Expression: 12141e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall break; 121571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall } 121671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 121771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // Otherwise, fake up some ASTs and emit a normal assignment. 121871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ValueDecl *selfDecl = setterMethod->getSelfDecl(); 1219f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr self(selfDecl, false, selfDecl->getType(), 1220f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_LValue, SourceLocation()); 122171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ImplicitCastExpr selfLoad(ImplicitCastExpr::OnStack, 122271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall selfDecl->getType(), CK_LValueToRValue, &self, 122371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall VK_RValue); 122471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ObjCIvarRefExpr ivarRef(ivar, ivar->getType().getNonReferenceType(), 12250c70181854a95fca0e0d56dfa1203eb2216052eaFariborz Jahanian SourceLocation(), SourceLocation(), 12260c70181854a95fca0e0d56dfa1203eb2216052eaFariborz Jahanian &selfLoad, true, true); 122771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 122871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ParmVarDecl *argDecl = *setterMethod->param_begin(); 122971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall QualType argType = argDecl->getType().getNonReferenceType(); 1230f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr arg(argDecl, false, argType, VK_LValue, SourceLocation()); 123171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ImplicitCastExpr argLoad(ImplicitCastExpr::OnStack, 123271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argType.getUnqualifiedType(), CK_LValueToRValue, 123371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall &arg, VK_RValue); 123445e8423d7dcea657c14c55347e8a30ac904d7501Daniel Dunbar 123571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // The property type can differ from the ivar type in some situations with 123671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // Objective-C pointer types, we can always bit cast the RHS in these cases. 123771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // The following absurdity is just to ensure well-formed IR. 123871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall CastKind argCK = CK_NoOp; 123971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (ivarRef.getType()->isObjCObjectPointerType()) { 124071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (argLoad.getType()->isObjCObjectPointerType()) 124171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_BitCast; 124271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall else if (argLoad.getType()->isBlockPointerType()) 124371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_BlockPointerToObjCPointerCast; 124471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall else 124571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_CPointerToObjCPointerCast; 124671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall } else if (ivarRef.getType()->isBlockPointerType()) { 124771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (argLoad.getType()->isBlockPointerType()) 124871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_BitCast; 124971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall else 125071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_AnyPointerToBlockPointerCast; 125171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall } else if (ivarRef.getType()->isPointerType()) { 125271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_BitCast; 125386957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar } 125471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ImplicitCastExpr argCast(ImplicitCastExpr::OnStack, 125571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ivarRef.getType(), argCK, &argLoad, 125671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall VK_RValue); 125771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall Expr *finalArg = &argLoad; 125871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (!getContext().hasSameUnqualifiedType(ivarRef.getType(), 125971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argLoad.getType())) 126071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall finalArg = &argCast; 126171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 126271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 126371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall BinaryOperator assign(&ivarRef, finalArg, BO_Assign, 126471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ivarRef.getType(), VK_RValue, OK_Ordinary, 1265be9af1288881110e406b87914162eaa59f1e5918Lang Hames SourceLocation(), false); 126671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall EmitStmt(&assign); 126771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall} 126871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 12692ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// \brief Generate an Objective-C property setter function. 12702ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// 12712ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// The given Decl must be an ObjCImplementationDecl. \@synthesize 127271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall/// is illegal within a category. 127371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCallvoid CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP, 127471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall const ObjCPropertyImplDecl *PID) { 1275b6e5fe3cae9bf456f084c555c3ecc256f4181cc5Fariborz Jahanian llvm::Constant *AtomicHelperFn = 127620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian GenerateObjCAtomicSetterCopyHelperFunction(PID); 127771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 127871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ObjCMethodDecl *OMD = PD->getSetterMethodDecl(); 127971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall assert(OMD && "Invalid call to generate setter (empty method)"); 1280ea32047660159811a4c14d008a4b7e3807a705d6Eric Christopher StartObjCMethod(OMD, IMP->getClassInterface(), OMD->getLocStart()); 128171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 1282cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian generateObjCSetterBody(IMP, PID, AtomicHelperFn); 1283af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 1284af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar FinishFunction(); 12854111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner} 12864111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner 1287e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCallnamespace { 12889928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall struct DestroyIvar : EHScopeStack::Cleanup { 12899928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall private: 12909928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall llvm::Value *addr; 1291e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall const ObjCIvarDecl *ivar; 1292516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne CodeGenFunction::Destroyer *destroyer; 12939928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall bool useEHCleanupForArray; 12949928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall public: 12959928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall DestroyIvar(llvm::Value *addr, const ObjCIvarDecl *ivar, 12969928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CodeGenFunction::Destroyer *destroyer, 12979928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall bool useEHCleanupForArray) 1298516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne : addr(addr), ivar(ivar), destroyer(destroyer), 12999928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall useEHCleanupForArray(useEHCleanupForArray) {} 1300e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1301651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines void Emit(CodeGenFunction &CGF, Flags flags) override { 13029928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall LValue lvalue 13039928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), addr, ivar, /*CVR*/ 0); 13049928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CGF.emitDestroy(lvalue.getAddress(), ivar->getType(), destroyer, 1305ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall flags.isForNormalCleanup() && useEHCleanupForArray); 1306e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall } 1307e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall }; 1308e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall} 1309e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 13109928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall/// Like CodeGenFunction::destroyARCStrong, but do it with a call. 13119928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCallstatic void destroyARCStrongWithStore(CodeGenFunction &CGF, 13129928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall llvm::Value *addr, 13139928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall QualType type) { 13149928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall llvm::Value *null = getNullForVariable(addr); 13159928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CGF.EmitARCStoreStrongCall(addr, null, /*ignored*/ true); 13169928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall} 1317f85e193739c953358c865005855253af4f68a497John McCall 1318e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCallstatic void emitCXXDestructMethod(CodeGenFunction &CGF, 1319e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall ObjCImplementationDecl *impl) { 1320e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall CodeGenFunction::RunCleanupsScope scope(CGF); 1321e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1322e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall llvm::Value *self = CGF.LoadObjCSelf(); 1323e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1324db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose const ObjCInterfaceDecl *iface = impl->getClassInterface(); 1325db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin(); 1326e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall ivar; ivar = ivar->getNextIvar()) { 1327e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall QualType type = ivar->getType(); 1328e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1329e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall // Check whether the ivar is a destructible type. 13309928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall QualType::DestructionKind dtorKind = type.isDestructedType(); 13319928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall if (!dtorKind) continue; 13329928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall 13339928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CodeGenFunction::Destroyer *destroyer = 0; 13349928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall 13359928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall // Use a call to objc_storeStrong to destroy strong ivars, for the 13369928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall // general benefit of the tools. 13379928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall if (dtorKind == QualType::DK_objc_strong_lifetime) { 1338516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne destroyer = destroyARCStrongWithStore; 13399928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall 13409928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall // Otherwise use the default for the destruction kind. 13419928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall } else { 1342516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne destroyer = CGF.getDestroyer(dtorKind); 1343e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall } 13449928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall 13459928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CleanupKind cleanupKind = CGF.getCleanupKind(dtorKind); 13469928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall 13479928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CGF.EHStack.pushCleanup<DestroyIvar>(cleanupKind, self, ivar, destroyer, 13489928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall cleanupKind & EHCleanup); 1349e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall } 1350e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1351e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall assert(scope.requiresCleanups() && "nothing to do in .cxx_destruct?"); 1352e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall} 1353e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1354109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanianvoid CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP, 1355109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian ObjCMethodDecl *MD, 1356109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian bool ctor) { 1357109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface()); 13588d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel StartObjCMethod(MD, IMP->getClassInterface(), MD->getLocStart()); 1359e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1360e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall // Emit .cxx_construct. 1361109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian if (ctor) { 1362f85e193739c953358c865005855253af4f68a497John McCall // Suppress the final autorelease in ARC. 1363f85e193739c953358c865005855253af4f68a497John McCall AutoreleaseResult = false; 1364f85e193739c953358c865005855253af4f68a497John McCall 1365651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (const auto *IvarInit : IMP->inits()) { 136600eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet FieldDecl *Field = IvarInit->getAnyMember(); 1367651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field); 13689b4d4fc49f30f1caa35d680702f1921afad81971Fariborz Jahanian LValue LV = EmitLValueForIvar(TypeOfSelfObject(), 13699b4d4fc49f30f1caa35d680702f1921afad81971Fariborz Jahanian LoadObjCSelf(), Ivar, 0); 13707c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall EmitAggExpr(IvarInit->getInit(), 13717c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall AggValueSlot::forLValue(LV, AggValueSlot::IsDestructed, 1372410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall AggValueSlot::DoesNotNeedGCBarriers, 1373649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier AggValueSlot::IsNotAliased)); 1374109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian } 1375109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian // constructor returns 'self'. 1376109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian CodeGenTypes &Types = CGM.getTypes(); 1377109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian QualType IdTy(CGM.getContext().getObjCIdType()); 1378109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian llvm::Value *SelfAsId = 1379109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy)); 1380109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian EmitReturnOfRValue(RValue::get(SelfAsId), IdTy); 1381e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1382e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall // Emit .cxx_destruct. 1383bc397cf90355f17c974b0bdf3960e8fb38caf5d6Chandler Carruth } else { 1384e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall emitCXXDestructMethod(*this, IMP); 1385109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian } 1386109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian FinishFunction(); 1387109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian} 1388109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian 13890b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanianbool CodeGenFunction::IndirectObjCSetterArg(const CGFunctionInfo &FI) { 13900b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian CGFunctionInfo::const_arg_iterator it = FI.arg_begin(); 13910b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian it++; it++; 13920b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian const ABIArgInfo &AI = it->info; 13930b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian // FIXME. Is this sufficient check? 13940b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian return (AI.getKind() == ABIArgInfo::Indirect); 13950b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian} 13960b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian 139715bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanianbool CodeGenFunction::IvarTypeWithAggrGCObjects(QualType Ty) { 13984e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().getGC() == LangOptions::NonGC) 139915bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian return false; 140015bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian if (const RecordType *FDTTy = Ty.getTypePtr()->getAs<RecordType>()) 140115bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian return FDTTy->getDecl()->hasObjectMember(); 140215bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian return false; 140315bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian} 140415bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian 1405c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbarllvm::Value *CodeGenFunction::LoadObjCSelf() { 1406f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall VarDecl *Self = cast<ObjCMethodDecl>(CurFuncDecl)->getSelfDecl(); 1407f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall DeclRefExpr DRE(Self, /*is enclosing local*/ (CurFuncDecl != CurCodeDecl), 1408f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall Self->getType(), VK_LValue, SourceLocation()); 14094ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky return EmitLoadOfScalar(EmitDeclRefLValue(&DRE), SourceLocation()); 14104111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner} 14114111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner 141245012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz JahanianQualType CodeGenFunction::TypeOfSelfObject() { 141345012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); 141445012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian ImplicitParamDecl *selfDecl = OMD->getSelfDecl(); 141514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>( 141614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff getContext().getCanonicalType(selfDecl->getType())); 141745012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian return PTy->getPointeeType(); 141845012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian} 141945012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian 142074391b48b4791cded373683a3baf67314f358d50Chris Lattnervoid CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){ 14211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Constant *EnumerationMutationFn = 1422c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar CGM.getObjCRuntime().EnumerationMutationFunction(); 14231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1424c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar if (!EnumerationMutationFn) { 1425c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime"); 1426c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar return; 1427c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar } 1428c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar 1429bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel CGDebugInfo *DI = getDebugInfo(); 143073fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher if (DI) 143173fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin()); 1432bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel 14339d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel // The local variable comes into scope immediately. 14349d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel AutoVarEmission variable = AutoVarEmission::invalid(); 14359d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) 14369d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel variable = EmitAutoVarAlloca(*cast<VarDecl>(SD->getSingleDecl())); 14379d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel 1438d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end"); 14391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1440f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson // Fast enumeration state. 14410815b579b31cb3129f732bb7ea36fd6ba6949e98Douglas Gregor QualType StateTy = CGM.getObjCFastEnumerationStateType(); 1442195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr"); 14431884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson EmitNullInitialization(StatePtr, StateTy); 14441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1445f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson // Number of elements in the items array. 14462abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson static const unsigned NumItems = 16; 14471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1448d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Fetch the countByEnumeratingWithState:objects:count: selector. 1449ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer IdentifierInfo *II[] = { 1450ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer &CGM.getContext().Idents.get("countByEnumeratingWithState"), 1451ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer &CGM.getContext().Idents.get("objects"), 1452ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer &CGM.getContext().Idents.get("count") 1453ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer }; 1454ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer Selector FastEnumSel = 1455ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]); 1456f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1457f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson QualType ItemsTy = 1458f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson getContext().getConstantArrayType(getContext().getObjCIdType(), 14591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::APInt(32, NumItems), 1460f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson ArrayType::Normal, 0); 1461195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr"); 14621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1463990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Emit the collection pointer. In ARC, we do a retain. 1464990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall llvm::Value *Collection; 14654e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().ObjCAutoRefCount) { 1466990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall Collection = EmitARCRetainScalarExpr(S.getCollection()); 1467990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1468990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Enter a cleanup to do the release. 1469990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall EmitObjCConsumeObject(S.getCollection()->getType(), Collection); 1470990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall } else { 1471990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall Collection = EmitScalarExpr(S.getCollection()); 1472990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall } 14731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14744b302d32378b364703b212834f908762e570c29cJohn McCall // The 'continue' label needs to appear within the cleanup for the 14754b302d32378b364703b212834f908762e570c29cJohn McCall // collection object. 14764b302d32378b364703b212834f908762e570c29cJohn McCall JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next"); 14774b302d32378b364703b212834f908762e570c29cJohn McCall 1478d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Send it our message: 1479f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson CallArgList Args; 1480d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1481d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The first argument is a temporary of the enumeration-state type. 148204c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman Args.add(RValue::get(StatePtr), getContext().getPointerType(StateTy)); 14831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1484d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The second argument is a temporary array with space for NumItems 1485d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // pointers. We'll actually be loading elements from the array 1486d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // pointer written into the control state; this buffer is so that 1487d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // collections that *aren't* backed by arrays can still queue up 1488d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // batches of elements. 148904c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman Args.add(RValue::get(ItemsPtr), getContext().getPointerType(ItemsTy)); 14901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1491d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The third argument is the capacity of that temporary array. 14922acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy); 14934a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems); 149404c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman Args.add(RValue::get(Count), getContext().UnsignedLongTy); 14951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1496d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Start the enumeration. 14971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump RValue CountRV = 1498ef072fd2f3347cfd857d6eb787b245b950771430John McCall CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), 1499f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson getContext().UnsignedLongTy, 1500f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson FastEnumSel, 1501c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall Collection, Args); 1502f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1503d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The initial number of objects that were returned in the buffer. 1504d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *initialBufferLimit = CountRV.getScalarVal(); 15051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1506d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty"); 1507d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit"); 1508f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1509d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *zero = llvm::Constant::getNullValue(UnsignedLongLTy); 1510f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1511d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // If the limit pointer was zero to begin with, the collection is 1512651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // empty; skip all this. Set the branch weight assuming this has the same 1513651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // probability of exiting the loop as any other loop exit. 1514651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines uint64_t EntryCount = PGO.getCurrentRegionCount(); 1515651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines RegionCounter Cnt = getPGORegionCounter(&S); 1516d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateCondBr(Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"), 1517651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines EmptyBB, LoopInitBB, 1518651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines PGO.createBranchWeights(EntryCount, Cnt.getCount())); 15191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1520d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Otherwise, initialize the loop. 1521d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(LoopInitBB); 15221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1523d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Save the initial mutations value. This is the value at an 1524d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // address that was written into the state object by 1525d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // countByEnumeratingWithState:objects:count:. 15261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Value *StateMutationsPtrPtr = 15272abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr"); 15281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, 15292abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson "mutationsptr"); 15301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1531d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *initialMutations = 1532d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateLoad(StateMutationsPtr, "forcoll.initial-mutations"); 15331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1534d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Start looping. This is the point we return to whenever we have a 1535d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // fresh, non-empty batch of objects. 1536d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody"); 1537d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(LoopBodyBB); 15381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1539d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The current index into the buffer. 1540bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad llvm::PHINode *index = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.index"); 1541d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall index->addIncoming(zero, LoopInitBB); 1542f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1543d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The current buffer size. 1544bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad llvm::PHINode *count = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.count"); 1545d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall count->addIncoming(initialBufferLimit, LoopInitBB); 1546f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1547651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines Cnt.beginRegion(Builder); 1548651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 1549d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Check whether the mutations value has changed from where it was 1550d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // at start. StateMutationsPtr should actually be invariant between 1551d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // refreshes. 15522abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr"); 1553d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *currentMutations 1554d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall = Builder.CreateLoad(StateMutationsPtr, "statemutations"); 15551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1556d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated"); 1557361cf980a7d976ef11a37b49567412b6b63a89d7Dan Gohman llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcoll.notmutated"); 15581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1559d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations), 1560d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall WasNotMutatedBB, WasMutatedBB); 15611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1562d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // If so, call the enumeration-mutation function. 1563d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(WasMutatedBB); 15642abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson llvm::Value *V = 15651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Builder.CreateBitCast(Collection, 1566578faa837b552403e2002b97fdfbfde14f2448e5Benjamin Kramer ConvertType(getContext().getObjCIdType())); 15672b2105e92fc77016992dae3f117f526e73af5ea9Daniel Dunbar CallArgList Args2; 156804c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman Args2.add(RValue::get(V), getContext().getObjCIdType()); 1569f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // FIXME: We shouldn't need to get the function info here, the runtime already 1570f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // should have computed it to build the function. 15710f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall EmitCall(CGM.getTypes().arrangeFreeFunctionCall(getContext().VoidTy, Args2, 15720f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 15730f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 1574f3c47c9525153aea2de0ec4bd615b9cf2d81c103Anders Carlsson EnumerationMutationFn, ReturnValueSlot(), Args2); 15751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1576d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Otherwise, or if the mutation function returns, just continue. 1577d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(WasNotMutatedBB); 15781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1579d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Initialize the element variable. 1580d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall RunCleanupsScope elementVariableScope(*this); 158157b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall bool elementIsVariable; 1582d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall LValue elementLValue; 1583d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall QualType elementType; 1584d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) { 158557b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall // Initialize the variable, in case it's a __block variable or something. 158657b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall EmitAutoVarInit(variable); 1587f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 158857b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall const VarDecl* D = cast<VarDecl>(SD->getSingleDecl()); 1589f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr tempDRE(const_cast<VarDecl*>(D), false, D->getType(), 1590d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall VK_LValue, SourceLocation()); 1591d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementLValue = EmitLValue(&tempDRE); 1592d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementType = D->getType(); 159357b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall elementIsVariable = true; 15947acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall 15957acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall if (D->isARCPseudoStrong()) 15967acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall elementLValue.getQuals().setObjCLifetime(Qualifiers::OCL_ExplicitNone); 1597d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall } else { 1598d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementLValue = LValue(); // suppress warning 1599d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementType = cast<Expr>(S.getElement())->getType(); 160057b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall elementIsVariable = false; 1601d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall } 16022acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *convertedElementType = ConvertType(elementType); 1603f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1604d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Fetch the buffer out of the enumeration state. 1605d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // TODO: this pointer should actually be invariant between 1606d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // refreshes, which would help us do certain loop optimizations. 1607d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *StateItemsPtr = 1608d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr"); 1609d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *EnumStateItems = 1610d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateLoad(StateItemsPtr, "stateitems"); 1611f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1612d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Fetch the value at the current index from the buffer. 16131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Value *CurrentItemPtr = 1614d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateGEP(EnumStateItems, index, "currentitem.ptr"); 1615d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr); 16161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1617d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Cast that value to the right type. 1618d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType, 1619d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall "currentitem"); 16201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1621d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Make sure we have an l-value. Yes, this gets evaluated every 1622d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // time through the loop. 16237acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall if (!elementIsVariable) { 1624d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementLValue = EmitLValue(cast<Expr>(S.getElement())); 1625545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue); 16267acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall } else { 16277acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall EmitScalarInit(CurrentItem, elementLValue); 16287acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall } 16291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 163057b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall // If we do have an element variable, this assignment is the end of 163157b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall // its initialization. 163257b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall if (elementIsVariable) 163357b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall EmitAutoVarCleanups(variable); 163457b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall 1635d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Perform the loop body, setting up break and continue labels. 1636e4b6d342c29d5cb9d311756100df1603810fa892Anders Carlsson BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody)); 1637d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall { 1638d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall RunCleanupsScope Scope(*this); 1639d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitStmt(S.getBody()); 1640d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall } 1641f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson BreakContinueStack.pop_back(); 16421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1643d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Destroy the element variable now. 1644d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementVariableScope.ForceCleanup(); 1645d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1646d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Check whether there are more elements. 1647ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall EmitBlock(AfterBody.getBlock()); 16481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1649d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch"); 1650d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1651d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // First we check in the local buffer. 1652d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *indexPlusOne 1653d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall = Builder.CreateAdd(index, llvm::ConstantInt::get(UnsignedLongLTy, 1)); 1654d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1655d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // If we haven't overrun the buffer yet, we can continue. 1656651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // Set the branch weights based on the simplifying assumption that this is 1657651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // like a while-loop, i.e., ignoring that the false branch fetches more 1658651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines // elements and then returns to the loop. 1659d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateCondBr(Builder.CreateICmpULT(indexPlusOne, count), 1660651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines LoopBodyBB, FetchMoreBB, 1661651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines PGO.createBranchWeights(Cnt.getCount(), EntryCount)); 1662f0906c4edb37b20141428ca77fa7dfd00b976eafFariborz Jahanian 1663d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall index->addIncoming(indexPlusOne, AfterBody.getBlock()); 1664d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall count->addIncoming(count, AfterBody.getBlock()); 1665f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1666d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Otherwise, we have to fetch more elements. 1667d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(FetchMoreBB); 16681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump CountRV = 1670ef072fd2f3347cfd857d6eb787b245b950771430John McCall CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), 1671f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson getContext().UnsignedLongTy, 16721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump FastEnumSel, 1673c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall Collection, Args); 16741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1675d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // If we got a zero count, we're done. 1676d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *refetchCount = CountRV.getScalarVal(); 1677d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1678d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // (note that the message send might split FetchMoreBB) 1679d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall index->addIncoming(zero, Builder.GetInsertBlock()); 1680d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall count->addIncoming(refetchCount, Builder.GetInsertBlock()); 1681d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1682d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero), 1683d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmptyBB, LoopBodyBB); 16841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1685f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson // No more elements. 1686d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(EmptyBB); 1687f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 168857b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall if (!elementIsVariable) { 1689f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson // If the element was not a declaration, set it to be null. 1690f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1691d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *null = llvm::Constant::getNullValue(convertedElementType); 1692d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementLValue = EmitLValue(cast<Expr>(S.getElement())); 1693545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall EmitStoreThroughLValue(RValue::get(null), elementLValue); 1694f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson } 1695f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 169673fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher if (DI) 169773fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd()); 1698bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel 1699990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Leave the cleanup we entered in ARC. 17004e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().ObjCAutoRefCount) 1701990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall PopCleanupBlock(); 1702990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1703ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall EmitBlock(LoopEnd.getBlock()); 17043d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson} 17053d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson 17061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) { 1707f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall CGM.getObjCRuntime().EmitTryStmt(*this, S); 170864d5d6c5903157c521af496479d06dc26032d718Anders Carlsson} 170964d5d6c5903157c521af496479d06dc26032d718Anders Carlsson 17101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) { 171164d5d6c5903157c521af496479d06dc26032d718Anders Carlsson CGM.getObjCRuntime().EmitThrowStmt(*this, S); 171264d5d6c5903157c521af496479d06dc26032d718Anders Carlsson} 171364d5d6c5903157c521af496479d06dc26032d718Anders Carlsson 171410cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattnervoid CodeGenFunction::EmitObjCAtSynchronizedStmt( 17151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const ObjCAtSynchronizedStmt &S) { 1716f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S); 171710cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattner} 171810cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattner 171933e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall/// Produce the code for a CK_ARCProduceObject. Just does a 1720f85e193739c953358c865005855253af4f68a497John McCall/// primitive retain. 1721f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCProduceObject(QualType type, 1722f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value) { 1723f85e193739c953358c865005855253af4f68a497John McCall return EmitARCRetain(type, value); 1724f85e193739c953358c865005855253af4f68a497John McCall} 1725f85e193739c953358c865005855253af4f68a497John McCall 1726f85e193739c953358c865005855253af4f68a497John McCallnamespace { 1727f85e193739c953358c865005855253af4f68a497John McCall struct CallObjCRelease : EHScopeStack::Cleanup { 1728bddfd87863bac7aa17d226cdfb228f49b30dd5f2John McCall CallObjCRelease(llvm::Value *object) : object(object) {} 1729bddfd87863bac7aa17d226cdfb228f49b30dd5f2John McCall llvm::Value *object; 1730f85e193739c953358c865005855253af4f68a497John McCall 1731651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines void Emit(CodeGenFunction &CGF, Flags flags) override { 17325b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall // Releases at the end of the full-expression are imprecise. 17335b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall CGF.EmitARCRelease(object, ARCImpreciseLifetime); 1734f85e193739c953358c865005855253af4f68a497John McCall } 1735f85e193739c953358c865005855253af4f68a497John McCall }; 1736f85e193739c953358c865005855253af4f68a497John McCall} 1737f85e193739c953358c865005855253af4f68a497John McCall 173833e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall/// Produce the code for a CK_ARCConsumeObject. Does a primitive 1739f85e193739c953358c865005855253af4f68a497John McCall/// release at the end of the full-expression. 1740f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCConsumeObject(QualType type, 1741f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *object) { 1742f85e193739c953358c865005855253af4f68a497John McCall // If we're in a conditional branch, we need to make the cleanup 1743bddfd87863bac7aa17d226cdfb228f49b30dd5f2John McCall // conditional. 1744bddfd87863bac7aa17d226cdfb228f49b30dd5f2John McCall pushFullExprCleanup<CallObjCRelease>(getARCCleanupKind(), object); 1745f85e193739c953358c865005855253af4f68a497John McCall return object; 1746f85e193739c953358c865005855253af4f68a497John McCall} 1747f85e193739c953358c865005855253af4f68a497John McCall 1748f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCExtendObjectLifetime(QualType type, 1749f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value) { 1750f85e193739c953358c865005855253af4f68a497John McCall return EmitARCRetainAutorelease(type, value); 1751f85e193739c953358c865005855253af4f68a497John McCall} 1752f85e193739c953358c865005855253af4f68a497John McCall 1753b6a6079449a5275c283982e19b0c38e165833bb2John McCall/// Given a number of pointers, inform the optimizer that they're 1754b6a6079449a5275c283982e19b0c38e165833bb2John McCall/// being intrinsically used up until this point in the program. 1755b6a6079449a5275c283982e19b0c38e165833bb2John McCallvoid CodeGenFunction::EmitARCIntrinsicUse(ArrayRef<llvm::Value*> values) { 1756b6a6079449a5275c283982e19b0c38e165833bb2John McCall llvm::Constant *&fn = CGM.getARCEntrypoints().clang_arc_use; 1757b6a6079449a5275c283982e19b0c38e165833bb2John McCall if (!fn) { 1758b6a6079449a5275c283982e19b0c38e165833bb2John McCall llvm::FunctionType *fnType = 1759b6a6079449a5275c283982e19b0c38e165833bb2John McCall llvm::FunctionType::get(CGM.VoidTy, ArrayRef<llvm::Type*>(), true); 1760b6a6079449a5275c283982e19b0c38e165833bb2John McCall fn = CGM.CreateRuntimeFunction(fnType, "clang.arc.use"); 1761b6a6079449a5275c283982e19b0c38e165833bb2John McCall } 1762b6a6079449a5275c283982e19b0c38e165833bb2John McCall 1763b6a6079449a5275c283982e19b0c38e165833bb2John McCall // This isn't really a "runtime" function, but as an intrinsic it 1764b6a6079449a5275c283982e19b0c38e165833bb2John McCall // doesn't really matter as long as we align things up. 1765b6a6079449a5275c283982e19b0c38e165833bb2John McCall EmitNounwindRuntimeCall(fn, values); 1766b6a6079449a5275c283982e19b0c38e165833bb2John McCall} 1767b6a6079449a5275c283982e19b0c38e165833bb2John McCall 1768f85e193739c953358c865005855253af4f68a497John McCall 1769f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Constant *createARCRuntimeFunction(CodeGenModule &CGM, 17702acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *type, 17715f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef fnName) { 1772f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *fn = CGM.CreateRuntimeFunction(type, fnName); 1773f85e193739c953358c865005855253af4f68a497John McCall 1774554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman if (llvm::Function *f = dyn_cast<llvm::Function>(fn)) { 1775cfe18a195128002c5861e362db1f11ee1bebbea6Michael Gottesman // If the target runtime doesn't naturally support ARC, emit weak 1776cfe18a195128002c5861e362db1f11ee1bebbea6Michael Gottesman // references to the runtime support library. We don't really 1777cfe18a195128002c5861e362db1f11ee1bebbea6Michael Gottesman // permit this to fail, but we need a particular relocation style. 1778554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman if (!CGM.getLangOpts().ObjCRuntime.hasNativeARC()) { 1779f85e193739c953358c865005855253af4f68a497John McCall f->setLinkage(llvm::Function::ExternalWeakLinkage); 1780554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman } else if (fnName == "objc_retain" || fnName == "objc_release") { 1781554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman // If we have Native ARC, set nonlazybind attribute for these APIs for 1782554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman // performance. 178372390b39c545426023ec104afe8706395d732badBill Wendling f->addFnAttr(llvm::Attribute::NonLazyBind); 1784db99e8b72ae2b66d3c2f8d896f49513b8d8e2633Michael Gottesman } 1785554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman } 1786f85e193739c953358c865005855253af4f68a497John McCall 1787f85e193739c953358c865005855253af4f68a497John McCall return fn; 1788f85e193739c953358c865005855253af4f68a497John McCall} 1789f85e193739c953358c865005855253af4f68a497John McCall 1790f85e193739c953358c865005855253af4f68a497John McCall/// Perform an operation having the signature 1791f85e193739c953358c865005855253af4f68a497John McCall/// i8* (i8*) 1792f85e193739c953358c865005855253af4f68a497John McCall/// where a null input causes a no-op and returns null. 1793f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCValueOperation(CodeGenFunction &CGF, 1794f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value, 1795f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn, 1796df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier StringRef fnName, 1797df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier bool isTailCall = false) { 1798f85e193739c953358c865005855253af4f68a497John McCall if (isa<llvm::ConstantPointerNull>(value)) return value; 1799f85e193739c953358c865005855253af4f68a497John McCall 1800f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 18012acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 180276ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrTy, false); 1803f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); 1804f85e193739c953358c865005855253af4f68a497John McCall } 1805f85e193739c953358c865005855253af4f68a497John McCall 1806f85e193739c953358c865005855253af4f68a497John McCall // Cast the argument to 'id'. 18072acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *origType = value->getType(); 1808f85e193739c953358c865005855253af4f68a497John McCall value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy); 1809f85e193739c953358c865005855253af4f68a497John McCall 1810f85e193739c953358c865005855253af4f68a497John McCall // Call the function. 1811bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::CallInst *call = CGF.EmitNounwindRuntimeCall(fn, value); 1812df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier if (isTailCall) 1813df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier call->setTailCall(); 1814f85e193739c953358c865005855253af4f68a497John McCall 1815f85e193739c953358c865005855253af4f68a497John McCall // Cast the result back to the original type. 1816f85e193739c953358c865005855253af4f68a497John McCall return CGF.Builder.CreateBitCast(call, origType); 1817f85e193739c953358c865005855253af4f68a497John McCall} 1818f85e193739c953358c865005855253af4f68a497John McCall 1819f85e193739c953358c865005855253af4f68a497John McCall/// Perform an operation having the following signature: 1820f85e193739c953358c865005855253af4f68a497John McCall/// i8* (i8**) 1821f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCLoadOperation(CodeGenFunction &CGF, 1822f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *addr, 1823f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn, 18245f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef fnName) { 1825f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 18262acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 182776ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrPtrTy, false); 1828f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); 1829f85e193739c953358c865005855253af4f68a497John McCall } 1830f85e193739c953358c865005855253af4f68a497John McCall 1831f85e193739c953358c865005855253af4f68a497John McCall // Cast the argument to 'id*'. 18322acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *origType = addr->getType(); 1833f85e193739c953358c865005855253af4f68a497John McCall addr = CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy); 1834f85e193739c953358c865005855253af4f68a497John McCall 1835f85e193739c953358c865005855253af4f68a497John McCall // Call the function. 1836bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *result = CGF.EmitNounwindRuntimeCall(fn, addr); 1837f85e193739c953358c865005855253af4f68a497John McCall 1838f85e193739c953358c865005855253af4f68a497John McCall // Cast the result back to a dereference of the original type. 1839f85e193739c953358c865005855253af4f68a497John McCall if (origType != CGF.Int8PtrPtrTy) 1840f85e193739c953358c865005855253af4f68a497John McCall result = CGF.Builder.CreateBitCast(result, 1841f85e193739c953358c865005855253af4f68a497John McCall cast<llvm::PointerType>(origType)->getElementType()); 1842f85e193739c953358c865005855253af4f68a497John McCall 1843f85e193739c953358c865005855253af4f68a497John McCall return result; 1844f85e193739c953358c865005855253af4f68a497John McCall} 1845f85e193739c953358c865005855253af4f68a497John McCall 1846f85e193739c953358c865005855253af4f68a497John McCall/// Perform an operation having the following signature: 1847f85e193739c953358c865005855253af4f68a497John McCall/// i8* (i8**, i8*) 1848f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCStoreOperation(CodeGenFunction &CGF, 1849f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *addr, 1850f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value, 1851f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn, 18525f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef fnName, 1853f85e193739c953358c865005855253af4f68a497John McCall bool ignored) { 1854f85e193739c953358c865005855253af4f68a497John McCall assert(cast<llvm::PointerType>(addr->getType())->getElementType() 1855f85e193739c953358c865005855253af4f68a497John McCall == value->getType()); 1856f85e193739c953358c865005855253af4f68a497John McCall 1857f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 18581d236ab930816f5da27bade92904914c44b73b4cBenjamin Kramer llvm::Type *argTypes[] = { CGF.Int8PtrPtrTy, CGF.Int8PtrTy }; 1859f85e193739c953358c865005855253af4f68a497John McCall 18602acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType 1861f85e193739c953358c865005855253af4f68a497John McCall = llvm::FunctionType::get(CGF.Int8PtrTy, argTypes, false); 1862f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); 1863f85e193739c953358c865005855253af4f68a497John McCall } 1864f85e193739c953358c865005855253af4f68a497John McCall 18652acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *origType = value->getType(); 1866f85e193739c953358c865005855253af4f68a497John McCall 1867bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *args[] = { 1868bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy), 1869bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy) 1870bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall }; 1871bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::CallInst *result = CGF.EmitNounwindRuntimeCall(fn, args); 1872f85e193739c953358c865005855253af4f68a497John McCall 1873f85e193739c953358c865005855253af4f68a497John McCall if (ignored) return 0; 1874f85e193739c953358c865005855253af4f68a497John McCall 1875f85e193739c953358c865005855253af4f68a497John McCall return CGF.Builder.CreateBitCast(result, origType); 1876f85e193739c953358c865005855253af4f68a497John McCall} 1877f85e193739c953358c865005855253af4f68a497John McCall 1878f85e193739c953358c865005855253af4f68a497John McCall/// Perform an operation having the following signature: 1879f85e193739c953358c865005855253af4f68a497John McCall/// void (i8**, i8**) 1880f85e193739c953358c865005855253af4f68a497John McCallstatic void emitARCCopyOperation(CodeGenFunction &CGF, 1881f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *dst, 1882f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *src, 1883f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn, 18845f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef fnName) { 1885f85e193739c953358c865005855253af4f68a497John McCall assert(dst->getType() == src->getType()); 1886f85e193739c953358c865005855253af4f68a497John McCall 1887f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 188876ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::Type *argTypes[] = { CGF.Int8PtrPtrTy, CGF.Int8PtrPtrTy }; 188976ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer 18902acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType 1891f85e193739c953358c865005855253af4f68a497John McCall = llvm::FunctionType::get(CGF.Builder.getVoidTy(), argTypes, false); 1892f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); 1893f85e193739c953358c865005855253af4f68a497John McCall } 1894f85e193739c953358c865005855253af4f68a497John McCall 1895bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *args[] = { 1896bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall CGF.Builder.CreateBitCast(dst, CGF.Int8PtrPtrTy), 1897bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall CGF.Builder.CreateBitCast(src, CGF.Int8PtrPtrTy) 1898bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall }; 1899bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall CGF.EmitNounwindRuntimeCall(fn, args); 1900f85e193739c953358c865005855253af4f68a497John McCall} 1901f85e193739c953358c865005855253af4f68a497John McCall 1902f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do a retain. Based on the type, calls one of: 19039d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retain(i8* %value) 19049d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainBlock(i8* %value) 1905f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCRetain(QualType type, llvm::Value *value) { 1906f85e193739c953358c865005855253af4f68a497John McCall if (type->isBlockPointerType()) 1907348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall return EmitARCRetainBlock(value, /*mandatory*/ false); 1908f85e193739c953358c865005855253af4f68a497John McCall else 1909f85e193739c953358c865005855253af4f68a497John McCall return EmitARCRetainNonBlock(value); 1910f85e193739c953358c865005855253af4f68a497John McCall} 1911f85e193739c953358c865005855253af4f68a497John McCall 1912f85e193739c953358c865005855253af4f68a497John McCall/// Retain the given object, with normal retain semantics. 19139d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retain(i8* %value) 1914f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCRetainNonBlock(llvm::Value *value) { 1915f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 1916f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_retain, 1917f85e193739c953358c865005855253af4f68a497John McCall "objc_retain"); 1918f85e193739c953358c865005855253af4f68a497John McCall} 1919f85e193739c953358c865005855253af4f68a497John McCall 1920f85e193739c953358c865005855253af4f68a497John McCall/// Retain the given block, with _Block_copy semantics. 19219d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainBlock(i8* %value) 1922348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall/// 1923348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall/// \param mandatory - If false, emit the call with metadata 1924348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall/// indicating that it's okay for the optimizer to eliminate this call 1925348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall/// if it can prove that the block never escapes except down the stack. 1926348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCallllvm::Value *CodeGenFunction::EmitARCRetainBlock(llvm::Value *value, 1927348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall bool mandatory) { 1928348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall llvm::Value *result 1929348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall = emitARCValueOperation(*this, value, 1930348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall CGM.getARCEntrypoints().objc_retainBlock, 1931348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall "objc_retainBlock"); 1932348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 1933348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall // If the copy isn't mandatory, add !clang.arc.copy_on_escape to 1934348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall // tell the optimizer that it doesn't need to do this copy if the 1935348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall // block doesn't escape, where being passed as an argument doesn't 1936348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall // count as escaping. 1937348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall if (!mandatory && isa<llvm::Instruction>(result)) { 1938348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall llvm::CallInst *call 1939348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall = cast<llvm::CallInst>(result->stripPointerCasts()); 1940348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall assert(call->getCalledValue() == CGM.getARCEntrypoints().objc_retainBlock); 1941348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 1942348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall SmallVector<llvm::Value*,1> args; 1943348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall call->setMetadata("clang.arc.copy_on_escape", 1944348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall llvm::MDNode::get(Builder.getContext(), args)); 1945348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall } 1946348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 1947348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall return result; 1948f85e193739c953358c865005855253af4f68a497John McCall} 1949f85e193739c953358c865005855253af4f68a497John McCall 1950f85e193739c953358c865005855253af4f68a497John McCall/// Retain the given object which is the result of a function call. 19519d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainAutoreleasedReturnValue(i8* %value) 1952f85e193739c953358c865005855253af4f68a497John McCall/// 1953f85e193739c953358c865005855253af4f68a497John McCall/// Yes, this function name is one character away from a different 1954f85e193739c953358c865005855253af4f68a497John McCall/// call with completely different semantics. 1955f85e193739c953358c865005855253af4f68a497John McCallllvm::Value * 1956f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCRetainAutoreleasedReturnValue(llvm::Value *value) { 1957f85e193739c953358c865005855253af4f68a497John McCall // Fetch the void(void) inline asm which marks that we're going to 1958f85e193739c953358c865005855253af4f68a497John McCall // retain the autoreleased return value. 1959f85e193739c953358c865005855253af4f68a497John McCall llvm::InlineAsm *&marker 1960f85e193739c953358c865005855253af4f68a497John McCall = CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker; 1961f85e193739c953358c865005855253af4f68a497John McCall if (!marker) { 19625f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef assembly 1963f85e193739c953358c865005855253af4f68a497John McCall = CGM.getTargetCodeGenInfo() 1964f85e193739c953358c865005855253af4f68a497John McCall .getARCRetainAutoreleasedReturnValueMarker(); 1965f85e193739c953358c865005855253af4f68a497John McCall 1966f85e193739c953358c865005855253af4f68a497John McCall // If we have an empty assembly string, there's nothing to do. 1967f85e193739c953358c865005855253af4f68a497John McCall if (assembly.empty()) { 1968f85e193739c953358c865005855253af4f68a497John McCall 1969f85e193739c953358c865005855253af4f68a497John McCall // Otherwise, at -O0, build an inline asm that we're going to call 1970f85e193739c953358c865005855253af4f68a497John McCall // in a moment. 1971f85e193739c953358c865005855253af4f68a497John McCall } else if (CGM.getCodeGenOpts().OptimizationLevel == 0) { 1972f85e193739c953358c865005855253af4f68a497John McCall llvm::FunctionType *type = 19738b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner llvm::FunctionType::get(VoidTy, /*variadic*/false); 1974f85e193739c953358c865005855253af4f68a497John McCall 1975f85e193739c953358c865005855253af4f68a497John McCall marker = llvm::InlineAsm::get(type, assembly, "", /*sideeffects*/ true); 1976f85e193739c953358c865005855253af4f68a497John McCall 1977f85e193739c953358c865005855253af4f68a497John McCall // If we're at -O1 and above, we don't want to litter the code 1978f85e193739c953358c865005855253af4f68a497John McCall // with this marker yet, so leave a breadcrumb for the ARC 1979f85e193739c953358c865005855253af4f68a497John McCall // optimizer to pick up. 1980f85e193739c953358c865005855253af4f68a497John McCall } else { 1981f85e193739c953358c865005855253af4f68a497John McCall llvm::NamedMDNode *metadata = 1982f85e193739c953358c865005855253af4f68a497John McCall CGM.getModule().getOrInsertNamedMetadata( 1983f85e193739c953358c865005855253af4f68a497John McCall "clang.arc.retainAutoreleasedReturnValueMarker"); 1984f85e193739c953358c865005855253af4f68a497John McCall assert(metadata->getNumOperands() <= 1); 1985f85e193739c953358c865005855253af4f68a497John McCall if (metadata->getNumOperands() == 0) { 1986f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *string = llvm::MDString::get(getLLVMContext(), assembly); 1987da549e8995c447542d5631b8b67fcc3a9582797aJay Foad metadata->addOperand(llvm::MDNode::get(getLLVMContext(), string)); 1988f85e193739c953358c865005855253af4f68a497John McCall } 1989f85e193739c953358c865005855253af4f68a497John McCall } 1990f85e193739c953358c865005855253af4f68a497John McCall } 1991f85e193739c953358c865005855253af4f68a497John McCall 1992f85e193739c953358c865005855253af4f68a497John McCall // Call the marker asm if we made one, which we do only at -O0. 1993f85e193739c953358c865005855253af4f68a497John McCall if (marker) Builder.CreateCall(marker); 1994f85e193739c953358c865005855253af4f68a497John McCall 1995f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 1996f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_retainAutoreleasedReturnValue, 1997f85e193739c953358c865005855253af4f68a497John McCall "objc_retainAutoreleasedReturnValue"); 1998f85e193739c953358c865005855253af4f68a497John McCall} 1999f85e193739c953358c865005855253af4f68a497John McCall 2000f85e193739c953358c865005855253af4f68a497John McCall/// Release the given object. 20019d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call void \@objc_release(i8* %value) 20025b07e8077a20b80fee90bd76c43c6150c676e4a8John McCallvoid CodeGenFunction::EmitARCRelease(llvm::Value *value, 20035b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall ARCPreciseLifetime_t precise) { 2004f85e193739c953358c865005855253af4f68a497John McCall if (isa<llvm::ConstantPointerNull>(value)) return; 2005f85e193739c953358c865005855253af4f68a497John McCall 2006f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn = CGM.getARCEntrypoints().objc_release; 2007f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 20082acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 200976ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false); 2010f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGM, fnType, "objc_release"); 2011f85e193739c953358c865005855253af4f68a497John McCall } 2012f85e193739c953358c865005855253af4f68a497John McCall 2013f85e193739c953358c865005855253af4f68a497John McCall // Cast the argument to 'id'. 2014f85e193739c953358c865005855253af4f68a497John McCall value = Builder.CreateBitCast(value, Int8PtrTy); 2015f85e193739c953358c865005855253af4f68a497John McCall 2016f85e193739c953358c865005855253af4f68a497John McCall // Call objc_release. 2017bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::CallInst *call = EmitNounwindRuntimeCall(fn, value); 2018f85e193739c953358c865005855253af4f68a497John McCall 20195b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall if (precise == ARCImpreciseLifetime) { 20205f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<llvm::Value*,1> args; 2021f85e193739c953358c865005855253af4f68a497John McCall call->setMetadata("clang.imprecise_release", 2022f85e193739c953358c865005855253af4f68a497John McCall llvm::MDNode::get(Builder.getContext(), args)); 2023f85e193739c953358c865005855253af4f68a497John McCall } 2024f85e193739c953358c865005855253af4f68a497John McCall} 2025f85e193739c953358c865005855253af4f68a497John McCall 2026015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// Destroy a __strong variable. 2027015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// 2028015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// At -O0, emit a call to store 'null' into the address; 2029015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// instrumenting tools prefer this because the address is exposed, 2030015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// but it's relatively cumbersome to optimize. 2031015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// 2032015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// At -O1 and above, just load and call objc_release. 2033015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// 2034015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// call void \@objc_storeStrong(i8** %addr, i8* null) 20355b07e8077a20b80fee90bd76c43c6150c676e4a8John McCallvoid CodeGenFunction::EmitARCDestroyStrong(llvm::Value *addr, 20365b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall ARCPreciseLifetime_t precise) { 2037015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall if (CGM.getCodeGenOpts().OptimizationLevel == 0) { 2038015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall llvm::PointerType *addrTy = cast<llvm::PointerType>(addr->getType()); 2039015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall llvm::Value *null = llvm::ConstantPointerNull::get( 2040015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall cast<llvm::PointerType>(addrTy->getElementType())); 2041015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall EmitARCStoreStrongCall(addr, null, /*ignored*/ true); 2042015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall return; 2043015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall } 2044015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall 2045015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall llvm::Value *value = Builder.CreateLoad(addr); 2046015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall EmitARCRelease(value, precise); 2047015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall} 2048015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall 2049f85e193739c953358c865005855253af4f68a497John McCall/// Store into a strong object. Always calls this: 20509d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call void \@objc_storeStrong(i8** %addr, i8* %value) 2051f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCStoreStrongCall(llvm::Value *addr, 2052f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value, 2053f85e193739c953358c865005855253af4f68a497John McCall bool ignored) { 2054f85e193739c953358c865005855253af4f68a497John McCall assert(cast<llvm::PointerType>(addr->getType())->getElementType() 2055f85e193739c953358c865005855253af4f68a497John McCall == value->getType()); 2056f85e193739c953358c865005855253af4f68a497John McCall 2057f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn = CGM.getARCEntrypoints().objc_storeStrong; 2058f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 20599cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::Type *argTypes[] = { Int8PtrPtrTy, Int8PtrTy }; 20602acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType 2061f85e193739c953358c865005855253af4f68a497John McCall = llvm::FunctionType::get(Builder.getVoidTy(), argTypes, false); 2062f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGM, fnType, "objc_storeStrong"); 2063f85e193739c953358c865005855253af4f68a497John McCall } 2064f85e193739c953358c865005855253af4f68a497John McCall 2065bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *args[] = { 2066bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall Builder.CreateBitCast(addr, Int8PtrPtrTy), 2067bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall Builder.CreateBitCast(value, Int8PtrTy) 2068bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall }; 2069bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall EmitNounwindRuntimeCall(fn, args); 2070f85e193739c953358c865005855253af4f68a497John McCall 2071f85e193739c953358c865005855253af4f68a497John McCall if (ignored) return 0; 2072f85e193739c953358c865005855253af4f68a497John McCall return value; 2073f85e193739c953358c865005855253af4f68a497John McCall} 2074f85e193739c953358c865005855253af4f68a497John McCall 2075f85e193739c953358c865005855253af4f68a497John McCall/// Store into a strong object. Sometimes calls this: 20769d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call void \@objc_storeStrong(i8** %addr, i8* %value) 2077f85e193739c953358c865005855253af4f68a497John McCall/// Other times, breaks it down into components. 2078545d996ec5a3113f046944f11b27cc2d6cb055b4John McCallllvm::Value *CodeGenFunction::EmitARCStoreStrong(LValue dst, 2079f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *newValue, 2080f85e193739c953358c865005855253af4f68a497John McCall bool ignored) { 2081545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall QualType type = dst.getType(); 2082f85e193739c953358c865005855253af4f68a497John McCall bool isBlock = type->isBlockPointerType(); 2083f85e193739c953358c865005855253af4f68a497John McCall 2084f85e193739c953358c865005855253af4f68a497John McCall // Use a store barrier at -O0 unless this is a block type or the 2085f85e193739c953358c865005855253af4f68a497John McCall // lvalue is inadequately aligned. 2086f85e193739c953358c865005855253af4f68a497John McCall if (shouldUseFusedARCCalls() && 2087f85e193739c953358c865005855253af4f68a497John McCall !isBlock && 20886da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman (dst.getAlignment().isZero() || 20896da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman dst.getAlignment() >= CharUnits::fromQuantity(PointerAlignInBytes))) { 2090f85e193739c953358c865005855253af4f68a497John McCall return EmitARCStoreStrongCall(dst.getAddress(), newValue, ignored); 2091f85e193739c953358c865005855253af4f68a497John McCall } 2092f85e193739c953358c865005855253af4f68a497John McCall 2093f85e193739c953358c865005855253af4f68a497John McCall // Otherwise, split it out. 2094f85e193739c953358c865005855253af4f68a497John McCall 2095f85e193739c953358c865005855253af4f68a497John McCall // Retain the new value. 2096f85e193739c953358c865005855253af4f68a497John McCall newValue = EmitARCRetain(type, newValue); 2097f85e193739c953358c865005855253af4f68a497John McCall 2098f85e193739c953358c865005855253af4f68a497John McCall // Read the old value. 20994ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky llvm::Value *oldValue = EmitLoadOfScalar(dst, SourceLocation()); 2100f85e193739c953358c865005855253af4f68a497John McCall 2101f85e193739c953358c865005855253af4f68a497John McCall // Store. We do this before the release so that any deallocs won't 2102f85e193739c953358c865005855253af4f68a497John McCall // see the old value. 2103545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall EmitStoreOfScalar(newValue, dst); 2104f85e193739c953358c865005855253af4f68a497John McCall 2105f85e193739c953358c865005855253af4f68a497John McCall // Finally, release the old value. 21065b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall EmitARCRelease(oldValue, dst.isARCPreciseLifetime()); 2107f85e193739c953358c865005855253af4f68a497John McCall 2108f85e193739c953358c865005855253af4f68a497John McCall return newValue; 2109f85e193739c953358c865005855253af4f68a497John McCall} 2110f85e193739c953358c865005855253af4f68a497John McCall 2111f85e193739c953358c865005855253af4f68a497John McCall/// Autorelease the given object. 21129d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_autorelease(i8* %value) 2113f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCAutorelease(llvm::Value *value) { 2114f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 2115f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_autorelease, 2116f85e193739c953358c865005855253af4f68a497John McCall "objc_autorelease"); 2117f85e193739c953358c865005855253af4f68a497John McCall} 2118f85e193739c953358c865005855253af4f68a497John McCall 2119f85e193739c953358c865005855253af4f68a497John McCall/// Autorelease the given object. 21209d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_autoreleaseReturnValue(i8* %value) 2121f85e193739c953358c865005855253af4f68a497John McCallllvm::Value * 2122f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCAutoreleaseReturnValue(llvm::Value *value) { 2123f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 2124f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_autoreleaseReturnValue, 2125df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier "objc_autoreleaseReturnValue", 2126df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier /*isTailCall*/ true); 2127f85e193739c953358c865005855253af4f68a497John McCall} 2128f85e193739c953358c865005855253af4f68a497John McCall 2129f85e193739c953358c865005855253af4f68a497John McCall/// Do a fused retain/autorelease of the given object. 21309d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainAutoreleaseReturnValue(i8* %value) 2131f85e193739c953358c865005855253af4f68a497John McCallllvm::Value * 2132f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCRetainAutoreleaseReturnValue(llvm::Value *value) { 2133f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 2134f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_retainAutoreleaseReturnValue, 2135df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier "objc_retainAutoreleaseReturnValue", 2136df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier /*isTailCall*/ true); 2137f85e193739c953358c865005855253af4f68a497John McCall} 2138f85e193739c953358c865005855253af4f68a497John McCall 2139f85e193739c953358c865005855253af4f68a497John McCall/// Do a fused retain/autorelease of the given object. 21409d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainAutorelease(i8* %value) 2141f85e193739c953358c865005855253af4f68a497John McCall/// or 21429d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// %retain = call i8* \@objc_retainBlock(i8* %value) 21439d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_autorelease(i8* %retain) 2144f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCRetainAutorelease(QualType type, 2145f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value) { 2146f85e193739c953358c865005855253af4f68a497John McCall if (!type->isBlockPointerType()) 2147f85e193739c953358c865005855253af4f68a497John McCall return EmitARCRetainAutoreleaseNonBlock(value); 2148f85e193739c953358c865005855253af4f68a497John McCall 2149f85e193739c953358c865005855253af4f68a497John McCall if (isa<llvm::ConstantPointerNull>(value)) return value; 2150f85e193739c953358c865005855253af4f68a497John McCall 21512acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *origType = value->getType(); 2152f85e193739c953358c865005855253af4f68a497John McCall value = Builder.CreateBitCast(value, Int8PtrTy); 2153348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall value = EmitARCRetainBlock(value, /*mandatory*/ true); 2154f85e193739c953358c865005855253af4f68a497John McCall value = EmitARCAutorelease(value); 2155f85e193739c953358c865005855253af4f68a497John McCall return Builder.CreateBitCast(value, origType); 2156f85e193739c953358c865005855253af4f68a497John McCall} 2157f85e193739c953358c865005855253af4f68a497John McCall 2158f85e193739c953358c865005855253af4f68a497John McCall/// Do a fused retain/autorelease of the given object. 21599d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainAutorelease(i8* %value) 2160f85e193739c953358c865005855253af4f68a497John McCallllvm::Value * 2161f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCRetainAutoreleaseNonBlock(llvm::Value *value) { 2162f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 2163f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_retainAutorelease, 2164f85e193739c953358c865005855253af4f68a497John McCall "objc_retainAutorelease"); 2165f85e193739c953358c865005855253af4f68a497John McCall} 2166f85e193739c953358c865005855253af4f68a497John McCall 21679d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// i8* \@objc_loadWeak(i8** %addr) 2168f85e193739c953358c865005855253af4f68a497John McCall/// Essentially objc_autorelease(objc_loadWeakRetained(addr)). 2169f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCLoadWeak(llvm::Value *addr) { 2170f85e193739c953358c865005855253af4f68a497John McCall return emitARCLoadOperation(*this, addr, 2171f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_loadWeak, 2172f85e193739c953358c865005855253af4f68a497John McCall "objc_loadWeak"); 2173f85e193739c953358c865005855253af4f68a497John McCall} 2174f85e193739c953358c865005855253af4f68a497John McCall 21759d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// i8* \@objc_loadWeakRetained(i8** %addr) 2176f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCLoadWeakRetained(llvm::Value *addr) { 2177f85e193739c953358c865005855253af4f68a497John McCall return emitARCLoadOperation(*this, addr, 2178f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_loadWeakRetained, 2179f85e193739c953358c865005855253af4f68a497John McCall "objc_loadWeakRetained"); 2180f85e193739c953358c865005855253af4f68a497John McCall} 2181f85e193739c953358c865005855253af4f68a497John McCall 21829d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// i8* \@objc_storeWeak(i8** %addr, i8* %value) 2183f85e193739c953358c865005855253af4f68a497John McCall/// Returns %value. 2184f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCStoreWeak(llvm::Value *addr, 2185f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value, 2186f85e193739c953358c865005855253af4f68a497John McCall bool ignored) { 2187f85e193739c953358c865005855253af4f68a497John McCall return emitARCStoreOperation(*this, addr, value, 2188f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_storeWeak, 2189f85e193739c953358c865005855253af4f68a497John McCall "objc_storeWeak", ignored); 2190f85e193739c953358c865005855253af4f68a497John McCall} 2191f85e193739c953358c865005855253af4f68a497John McCall 21929d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// i8* \@objc_initWeak(i8** %addr, i8* %value) 2193f85e193739c953358c865005855253af4f68a497John McCall/// Returns %value. %addr is known to not have a current weak entry. 2194f85e193739c953358c865005855253af4f68a497John McCall/// Essentially equivalent to: 2195f85e193739c953358c865005855253af4f68a497John McCall/// *addr = nil; objc_storeWeak(addr, value); 2196f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitARCInitWeak(llvm::Value *addr, llvm::Value *value) { 2197f85e193739c953358c865005855253af4f68a497John McCall // If we're initializing to null, just write null to memory; no need 2198f85e193739c953358c865005855253af4f68a497John McCall // to get the runtime involved. But don't do this if optimization 2199f85e193739c953358c865005855253af4f68a497John McCall // is enabled, because accounting for this would make the optimizer 2200f85e193739c953358c865005855253af4f68a497John McCall // much more complicated. 2201f85e193739c953358c865005855253af4f68a497John McCall if (isa<llvm::ConstantPointerNull>(value) && 2202f85e193739c953358c865005855253af4f68a497John McCall CGM.getCodeGenOpts().OptimizationLevel == 0) { 2203f85e193739c953358c865005855253af4f68a497John McCall Builder.CreateStore(value, addr); 2204f85e193739c953358c865005855253af4f68a497John McCall return; 2205f85e193739c953358c865005855253af4f68a497John McCall } 2206f85e193739c953358c865005855253af4f68a497John McCall 2207f85e193739c953358c865005855253af4f68a497John McCall emitARCStoreOperation(*this, addr, value, 2208f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_initWeak, 2209f85e193739c953358c865005855253af4f68a497John McCall "objc_initWeak", /*ignored*/ true); 2210f85e193739c953358c865005855253af4f68a497John McCall} 2211f85e193739c953358c865005855253af4f68a497John McCall 22129d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// void \@objc_destroyWeak(i8** %addr) 2213f85e193739c953358c865005855253af4f68a497John McCall/// Essentially objc_storeWeak(addr, nil). 2214f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitARCDestroyWeak(llvm::Value *addr) { 2215f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn = CGM.getARCEntrypoints().objc_destroyWeak; 2216f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 22172acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 221876ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrPtrTy, false); 2219f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGM, fnType, "objc_destroyWeak"); 2220f85e193739c953358c865005855253af4f68a497John McCall } 2221f85e193739c953358c865005855253af4f68a497John McCall 2222f85e193739c953358c865005855253af4f68a497John McCall // Cast the argument to 'id*'. 2223f85e193739c953358c865005855253af4f68a497John McCall addr = Builder.CreateBitCast(addr, Int8PtrPtrTy); 2224f85e193739c953358c865005855253af4f68a497John McCall 2225bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall EmitNounwindRuntimeCall(fn, addr); 2226f85e193739c953358c865005855253af4f68a497John McCall} 2227f85e193739c953358c865005855253af4f68a497John McCall 22289d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// void \@objc_moveWeak(i8** %dest, i8** %src) 2229f85e193739c953358c865005855253af4f68a497John McCall/// Disregards the current value in %dest. Leaves %src pointing to nothing. 2230f85e193739c953358c865005855253af4f68a497John McCall/// Essentially (objc_copyWeak(dest, src), objc_destroyWeak(src)). 2231f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitARCMoveWeak(llvm::Value *dst, llvm::Value *src) { 2232f85e193739c953358c865005855253af4f68a497John McCall emitARCCopyOperation(*this, dst, src, 2233f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_moveWeak, 2234f85e193739c953358c865005855253af4f68a497John McCall "objc_moveWeak"); 2235f85e193739c953358c865005855253af4f68a497John McCall} 2236f85e193739c953358c865005855253af4f68a497John McCall 22379d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// void \@objc_copyWeak(i8** %dest, i8** %src) 2238f85e193739c953358c865005855253af4f68a497John McCall/// Disregards the current value in %dest. Essentially 2239f85e193739c953358c865005855253af4f68a497John McCall/// objc_release(objc_initWeak(dest, objc_readWeakRetained(src))) 2240f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitARCCopyWeak(llvm::Value *dst, llvm::Value *src) { 2241f85e193739c953358c865005855253af4f68a497John McCall emitARCCopyOperation(*this, dst, src, 2242f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_copyWeak, 2243f85e193739c953358c865005855253af4f68a497John McCall "objc_copyWeak"); 2244f85e193739c953358c865005855253af4f68a497John McCall} 2245f85e193739c953358c865005855253af4f68a497John McCall 2246f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do a objc_autoreleasepool_push. 22479d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_autoreleasePoolPush(void) 2248f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCAutoreleasePoolPush() { 2249f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn = CGM.getRREntrypoints().objc_autoreleasePoolPush; 2250f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 22512acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 2252f85e193739c953358c865005855253af4f68a497John McCall llvm::FunctionType::get(Int8PtrTy, false); 2253f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPush"); 2254f85e193739c953358c865005855253af4f68a497John McCall } 2255f85e193739c953358c865005855253af4f68a497John McCall 2256bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall return EmitNounwindRuntimeCall(fn); 2257f85e193739c953358c865005855253af4f68a497John McCall} 2258f85e193739c953358c865005855253af4f68a497John McCall 2259f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do a primitive release. 22609d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call void \@objc_autoreleasePoolPop(i8* %ptr) 2261f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitObjCAutoreleasePoolPop(llvm::Value *value) { 2262f85e193739c953358c865005855253af4f68a497John McCall assert(value->getType() == Int8PtrTy); 2263f85e193739c953358c865005855253af4f68a497John McCall 2264f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn = CGM.getRREntrypoints().objc_autoreleasePoolPop; 2265f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 22662acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 226776ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false); 2268f85e193739c953358c865005855253af4f68a497John McCall 2269f85e193739c953358c865005855253af4f68a497John McCall // We don't want to use a weak import here; instead we should not 2270f85e193739c953358c865005855253af4f68a497John McCall // fall into this path. 2271f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPop"); 2272f85e193739c953358c865005855253af4f68a497John McCall } 2273f85e193739c953358c865005855253af4f68a497John McCall 2274b57f6b3d8f84e9241fd978e2739d6a9da0870546John McCall // objc_autoreleasePoolPop can throw. 2275b57f6b3d8f84e9241fd978e2739d6a9da0870546John McCall EmitRuntimeCallOrInvoke(fn, value); 2276f85e193739c953358c865005855253af4f68a497John McCall} 2277f85e193739c953358c865005855253af4f68a497John McCall 2278f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do an MRR version objc_autoreleasepool_push. 2279f85e193739c953358c865005855253af4f68a497John McCall/// Which is: [[NSAutoreleasePool alloc] init]; 2280f85e193739c953358c865005855253af4f68a497John McCall/// Where alloc is declared as: + (id) alloc; in NSAutoreleasePool class. 2281f85e193739c953358c865005855253af4f68a497John McCall/// init is declared as: - (id) init; in its NSObject super class. 2282f85e193739c953358c865005855253af4f68a497John McCall/// 2283f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCMRRAutoreleasePoolPush() { 2284f85e193739c953358c865005855253af4f68a497John McCall CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 2285bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *Receiver = Runtime.EmitNSAutoreleasePoolClassRef(*this); 2286f85e193739c953358c865005855253af4f68a497John McCall // [NSAutoreleasePool alloc] 2287f85e193739c953358c865005855253af4f68a497John McCall IdentifierInfo *II = &CGM.getContext().Idents.get("alloc"); 2288f85e193739c953358c865005855253af4f68a497John McCall Selector AllocSel = getContext().Selectors.getSelector(0, &II); 2289f85e193739c953358c865005855253af4f68a497John McCall CallArgList Args; 2290f85e193739c953358c865005855253af4f68a497John McCall RValue AllocRV = 2291f85e193739c953358c865005855253af4f68a497John McCall Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 2292f85e193739c953358c865005855253af4f68a497John McCall getContext().getObjCIdType(), 2293f85e193739c953358c865005855253af4f68a497John McCall AllocSel, Receiver, Args); 2294f85e193739c953358c865005855253af4f68a497John McCall 2295f85e193739c953358c865005855253af4f68a497John McCall // [Receiver init] 2296f85e193739c953358c865005855253af4f68a497John McCall Receiver = AllocRV.getScalarVal(); 2297f85e193739c953358c865005855253af4f68a497John McCall II = &CGM.getContext().Idents.get("init"); 2298f85e193739c953358c865005855253af4f68a497John McCall Selector InitSel = getContext().Selectors.getSelector(0, &II); 2299f85e193739c953358c865005855253af4f68a497John McCall RValue InitRV = 2300f85e193739c953358c865005855253af4f68a497John McCall Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 2301f85e193739c953358c865005855253af4f68a497John McCall getContext().getObjCIdType(), 2302f85e193739c953358c865005855253af4f68a497John McCall InitSel, Receiver, Args); 2303f85e193739c953358c865005855253af4f68a497John McCall return InitRV.getScalarVal(); 2304f85e193739c953358c865005855253af4f68a497John McCall} 2305f85e193739c953358c865005855253af4f68a497John McCall 2306f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do a primitive release. 2307f85e193739c953358c865005855253af4f68a497John McCall/// [tmp drain]; 2308f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitObjCMRRAutoreleasePoolPop(llvm::Value *Arg) { 2309f85e193739c953358c865005855253af4f68a497John McCall IdentifierInfo *II = &CGM.getContext().Idents.get("drain"); 2310f85e193739c953358c865005855253af4f68a497John McCall Selector DrainSel = getContext().Selectors.getSelector(0, &II); 2311f85e193739c953358c865005855253af4f68a497John McCall CallArgList Args; 2312f85e193739c953358c865005855253af4f68a497John McCall CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), 2313f85e193739c953358c865005855253af4f68a497John McCall getContext().VoidTy, DrainSel, Arg, Args); 2314f85e193739c953358c865005855253af4f68a497John McCall} 2315f85e193739c953358c865005855253af4f68a497John McCall 2316bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCallvoid CodeGenFunction::destroyARCStrongPrecise(CodeGenFunction &CGF, 2317bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall llvm::Value *addr, 2318bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall QualType type) { 23195b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall CGF.EmitARCDestroyStrong(addr, ARCPreciseLifetime); 2320bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall} 2321bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 2322bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCallvoid CodeGenFunction::destroyARCStrongImprecise(CodeGenFunction &CGF, 2323bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall llvm::Value *addr, 2324bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall QualType type) { 23255b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall CGF.EmitARCDestroyStrong(addr, ARCImpreciseLifetime); 2326bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall} 2327bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 2328bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCallvoid CodeGenFunction::destroyARCWeak(CodeGenFunction &CGF, 2329bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall llvm::Value *addr, 2330bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall QualType type) { 2331bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall CGF.EmitARCDestroyWeak(addr); 2332bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall} 2333bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 2334f85e193739c953358c865005855253af4f68a497John McCallnamespace { 2335f85e193739c953358c865005855253af4f68a497John McCall struct CallObjCAutoreleasePoolObject : EHScopeStack::Cleanup { 2336f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *Token; 2337f85e193739c953358c865005855253af4f68a497John McCall 2338f85e193739c953358c865005855253af4f68a497John McCall CallObjCAutoreleasePoolObject(llvm::Value *token) : Token(token) {} 2339f85e193739c953358c865005855253af4f68a497John McCall 2340651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines void Emit(CodeGenFunction &CGF, Flags flags) override { 2341f85e193739c953358c865005855253af4f68a497John McCall CGF.EmitObjCAutoreleasePoolPop(Token); 2342f85e193739c953358c865005855253af4f68a497John McCall } 2343f85e193739c953358c865005855253af4f68a497John McCall }; 2344f85e193739c953358c865005855253af4f68a497John McCall struct CallObjCMRRAutoreleasePoolObject : EHScopeStack::Cleanup { 2345f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *Token; 2346f85e193739c953358c865005855253af4f68a497John McCall 2347f85e193739c953358c865005855253af4f68a497John McCall CallObjCMRRAutoreleasePoolObject(llvm::Value *token) : Token(token) {} 2348f85e193739c953358c865005855253af4f68a497John McCall 2349651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines void Emit(CodeGenFunction &CGF, Flags flags) override { 2350f85e193739c953358c865005855253af4f68a497John McCall CGF.EmitObjCMRRAutoreleasePoolPop(Token); 2351f85e193739c953358c865005855253af4f68a497John McCall } 2352f85e193739c953358c865005855253af4f68a497John McCall }; 2353f85e193739c953358c865005855253af4f68a497John McCall} 2354f85e193739c953358c865005855253af4f68a497John McCall 2355f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr) { 23564e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().ObjCAutoRefCount) 2357f85e193739c953358c865005855253af4f68a497John McCall EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, Ptr); 2358f85e193739c953358c865005855253af4f68a497John McCall else 2359f85e193739c953358c865005855253af4f68a497John McCall EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, Ptr); 2360f85e193739c953358c865005855253af4f68a497John McCall} 2361f85e193739c953358c865005855253af4f68a497John McCall 2362f85e193739c953358c865005855253af4f68a497John McCallstatic TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF, 2363f85e193739c953358c865005855253af4f68a497John McCall LValue lvalue, 2364f85e193739c953358c865005855253af4f68a497John McCall QualType type) { 2365f85e193739c953358c865005855253af4f68a497John McCall switch (type.getObjCLifetime()) { 2366f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_None: 2367f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_ExplicitNone: 2368f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_Strong: 2369f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_Autoreleasing: 23704ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky return TryEmitResult(CGF.EmitLoadOfLValue(lvalue, 23714ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky SourceLocation()).getScalarVal(), 2372f85e193739c953358c865005855253af4f68a497John McCall false); 2373f85e193739c953358c865005855253af4f68a497John McCall 2374f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_Weak: 2375f85e193739c953358c865005855253af4f68a497John McCall return TryEmitResult(CGF.EmitARCLoadWeakRetained(lvalue.getAddress()), 2376f85e193739c953358c865005855253af4f68a497John McCall true); 2377f85e193739c953358c865005855253af4f68a497John McCall } 2378f85e193739c953358c865005855253af4f68a497John McCall 2379f85e193739c953358c865005855253af4f68a497John McCall llvm_unreachable("impossible lifetime!"); 2380f85e193739c953358c865005855253af4f68a497John McCall} 2381f85e193739c953358c865005855253af4f68a497John McCall 2382f85e193739c953358c865005855253af4f68a497John McCallstatic TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF, 2383f85e193739c953358c865005855253af4f68a497John McCall const Expr *e) { 2384f85e193739c953358c865005855253af4f68a497John McCall e = e->IgnoreParens(); 2385f85e193739c953358c865005855253af4f68a497John McCall QualType type = e->getType(); 2386f85e193739c953358c865005855253af4f68a497John McCall 23872148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // If we're loading retained from a __strong xvalue, we can avoid 23882148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // an extra retain/release pair by zeroing out the source of this 23892148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // "move" operation. 23902148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall if (e->isXValue() && 23912148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall !type.isConstQualified() && 23922148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall type.getObjCLifetime() == Qualifiers::OCL_Strong) { 23932148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // Emit the lvalue. 23942148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall LValue lv = CGF.EmitLValue(e); 23952148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall 23962148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // Load the object pointer. 23974ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky llvm::Value *result = CGF.EmitLoadOfLValue(lv, 23984ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky SourceLocation()).getScalarVal(); 23992148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall 24002148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // Set the source pointer to NULL. 24012148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall CGF.EmitStoreOfScalar(getNullForVariable(lv.getAddress()), lv); 24022148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall 24032148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall return TryEmitResult(result, true); 24042148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall } 24052148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall 2406f85e193739c953358c865005855253af4f68a497John McCall // As a very special optimization, in ARC++, if the l-value is the 2407f85e193739c953358c865005855253af4f68a497John McCall // result of a non-volatile assignment, do a simple retain of the 2408f85e193739c953358c865005855253af4f68a497John McCall // result of the call to objc_storeWeak instead of reloading. 24094e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGF.getLangOpts().CPlusPlus && 2410f85e193739c953358c865005855253af4f68a497John McCall !type.isVolatileQualified() && 2411f85e193739c953358c865005855253af4f68a497John McCall type.getObjCLifetime() == Qualifiers::OCL_Weak && 2412f85e193739c953358c865005855253af4f68a497John McCall isa<BinaryOperator>(e) && 2413f85e193739c953358c865005855253af4f68a497John McCall cast<BinaryOperator>(e)->getOpcode() == BO_Assign) 2414f85e193739c953358c865005855253af4f68a497John McCall return TryEmitResult(CGF.EmitScalarExpr(e), false); 2415f85e193739c953358c865005855253af4f68a497John McCall 2416f85e193739c953358c865005855253af4f68a497John McCall return tryEmitARCRetainLoadOfScalar(CGF, CGF.EmitLValue(e), type); 2417f85e193739c953358c865005855253af4f68a497John McCall} 2418f85e193739c953358c865005855253af4f68a497John McCall 2419f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainAfterCall(CodeGenFunction &CGF, 2420f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value); 2421f85e193739c953358c865005855253af4f68a497John McCall 2422f85e193739c953358c865005855253af4f68a497John McCall/// Given that the given expression is some sort of call (which does 2423f85e193739c953358c865005855253af4f68a497John McCall/// not return retained), emit a retain following it. 2424f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainCall(CodeGenFunction &CGF, const Expr *e) { 2425f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = CGF.EmitScalarExpr(e); 2426f85e193739c953358c865005855253af4f68a497John McCall return emitARCRetainAfterCall(CGF, value); 2427f85e193739c953358c865005855253af4f68a497John McCall} 2428f85e193739c953358c865005855253af4f68a497John McCall 2429f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainAfterCall(CodeGenFunction &CGF, 2430f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value) { 2431f85e193739c953358c865005855253af4f68a497John McCall if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(value)) { 2432f85e193739c953358c865005855253af4f68a497John McCall CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP(); 2433f85e193739c953358c865005855253af4f68a497John McCall 2434f85e193739c953358c865005855253af4f68a497John McCall // Place the retain immediately following the call. 2435f85e193739c953358c865005855253af4f68a497John McCall CGF.Builder.SetInsertPoint(call->getParent(), 2436f85e193739c953358c865005855253af4f68a497John McCall ++llvm::BasicBlock::iterator(call)); 2437f85e193739c953358c865005855253af4f68a497John McCall value = CGF.EmitARCRetainAutoreleasedReturnValue(value); 2438f85e193739c953358c865005855253af4f68a497John McCall 2439f85e193739c953358c865005855253af4f68a497John McCall CGF.Builder.restoreIP(ip); 2440f85e193739c953358c865005855253af4f68a497John McCall return value; 2441f85e193739c953358c865005855253af4f68a497John McCall } else if (llvm::InvokeInst *invoke = dyn_cast<llvm::InvokeInst>(value)) { 2442f85e193739c953358c865005855253af4f68a497John McCall CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP(); 2443f85e193739c953358c865005855253af4f68a497John McCall 2444f85e193739c953358c865005855253af4f68a497John McCall // Place the retain at the beginning of the normal destination block. 2445f85e193739c953358c865005855253af4f68a497John McCall llvm::BasicBlock *BB = invoke->getNormalDest(); 2446f85e193739c953358c865005855253af4f68a497John McCall CGF.Builder.SetInsertPoint(BB, BB->begin()); 2447f85e193739c953358c865005855253af4f68a497John McCall value = CGF.EmitARCRetainAutoreleasedReturnValue(value); 2448f85e193739c953358c865005855253af4f68a497John McCall 2449f85e193739c953358c865005855253af4f68a497John McCall CGF.Builder.restoreIP(ip); 2450f85e193739c953358c865005855253af4f68a497John McCall return value; 2451f85e193739c953358c865005855253af4f68a497John McCall 2452f85e193739c953358c865005855253af4f68a497John McCall // Bitcasts can arise because of related-result returns. Rewrite 2453f85e193739c953358c865005855253af4f68a497John McCall // the operand. 2454f85e193739c953358c865005855253af4f68a497John McCall } else if (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(value)) { 2455f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *operand = bitcast->getOperand(0); 2456f85e193739c953358c865005855253af4f68a497John McCall operand = emitARCRetainAfterCall(CGF, operand); 2457f85e193739c953358c865005855253af4f68a497John McCall bitcast->setOperand(0, operand); 2458f85e193739c953358c865005855253af4f68a497John McCall return bitcast; 2459f85e193739c953358c865005855253af4f68a497John McCall 2460f85e193739c953358c865005855253af4f68a497John McCall // Generic fall-back case. 2461f85e193739c953358c865005855253af4f68a497John McCall } else { 2462f85e193739c953358c865005855253af4f68a497John McCall // Retain using the non-block variant: we never need to do a copy 2463f85e193739c953358c865005855253af4f68a497John McCall // of a block that's been returned to us. 2464f85e193739c953358c865005855253af4f68a497John McCall return CGF.EmitARCRetainNonBlock(value); 2465f85e193739c953358c865005855253af4f68a497John McCall } 2466f85e193739c953358c865005855253af4f68a497John McCall} 2467f85e193739c953358c865005855253af4f68a497John McCall 2468dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall/// Determine whether it might be important to emit a separate 2469dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall/// objc_retain_block on the result of the given expression, or 2470dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall/// whether it's okay to just emit it in a +1 context. 2471dc05b11c67331016473fbc7909827b1b89c9616bJohn McCallstatic bool shouldEmitSeparateBlockRetain(const Expr *e) { 2472dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall assert(e->getType()->isBlockPointerType()); 2473dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall e = e->IgnoreParens(); 2474dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2475dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // For future goodness, emit block expressions directly in +1 2476dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // contexts if we can. 2477dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall if (isa<BlockExpr>(e)) 2478dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return false; 2479dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2480dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall if (const CastExpr *cast = dyn_cast<CastExpr>(e)) { 2481dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall switch (cast->getCastKind()) { 2482dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // Emitting these operations in +1 contexts is goodness. 2483dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall case CK_LValueToRValue: 248433e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCReclaimReturnedObject: 248533e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCConsumeObject: 248633e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCProduceObject: 2487dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return false; 2488dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2489dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // These operations preserve a block type. 2490dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall case CK_NoOp: 2491dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall case CK_BitCast: 2492dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return shouldEmitSeparateBlockRetain(cast->getSubExpr()); 2493dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2494dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // These operations are known to be bad (or haven't been considered). 2495dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall case CK_AnyPointerToBlockPointerCast: 2496dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall default: 2497dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return true; 2498dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall } 2499dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall } 2500dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2501dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return true; 2502dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall} 2503dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 25044b9c2d235fb9449e249d74f48ecfec601650de93John McCall/// Try to emit a PseudoObjectExpr at +1. 25054b9c2d235fb9449e249d74f48ecfec601650de93John McCall/// 25064b9c2d235fb9449e249d74f48ecfec601650de93John McCall/// This massively duplicates emitPseudoObjectRValue. 25074b9c2d235fb9449e249d74f48ecfec601650de93John McCallstatic TryEmitResult tryEmitARCRetainPseudoObject(CodeGenFunction &CGF, 25084b9c2d235fb9449e249d74f48ecfec601650de93John McCall const PseudoObjectExpr *E) { 2509cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques; 25104b9c2d235fb9449e249d74f48ecfec601650de93John McCall 25114b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Find the result expression. 25124b9c2d235fb9449e249d74f48ecfec601650de93John McCall const Expr *resultExpr = E->getResultExpr(); 25134b9c2d235fb9449e249d74f48ecfec601650de93John McCall assert(resultExpr); 25144b9c2d235fb9449e249d74f48ecfec601650de93John McCall TryEmitResult result; 25154b9c2d235fb9449e249d74f48ecfec601650de93John McCall 25164b9c2d235fb9449e249d74f48ecfec601650de93John McCall for (PseudoObjectExpr::const_semantics_iterator 25174b9c2d235fb9449e249d74f48ecfec601650de93John McCall i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) { 25184b9c2d235fb9449e249d74f48ecfec601650de93John McCall const Expr *semantic = *i; 25194b9c2d235fb9449e249d74f48ecfec601650de93John McCall 25204b9c2d235fb9449e249d74f48ecfec601650de93John McCall // If this semantic expression is an opaque value, bind it 25214b9c2d235fb9449e249d74f48ecfec601650de93John McCall // to the result of its source expression. 25224b9c2d235fb9449e249d74f48ecfec601650de93John McCall if (const OpaqueValueExpr *ov = dyn_cast<OpaqueValueExpr>(semantic)) { 25234b9c2d235fb9449e249d74f48ecfec601650de93John McCall typedef CodeGenFunction::OpaqueValueMappingData OVMA; 25244b9c2d235fb9449e249d74f48ecfec601650de93John McCall OVMA opaqueData; 25254b9c2d235fb9449e249d74f48ecfec601650de93John McCall 25264b9c2d235fb9449e249d74f48ecfec601650de93John McCall // If this semantic is the result of the pseudo-object 25274b9c2d235fb9449e249d74f48ecfec601650de93John McCall // expression, try to evaluate the source as +1. 25284b9c2d235fb9449e249d74f48ecfec601650de93John McCall if (ov == resultExpr) { 25294b9c2d235fb9449e249d74f48ecfec601650de93John McCall assert(!OVMA::shouldBindAsLValue(ov)); 25304b9c2d235fb9449e249d74f48ecfec601650de93John McCall result = tryEmitARCRetainScalarExpr(CGF, ov->getSourceExpr()); 25314b9c2d235fb9449e249d74f48ecfec601650de93John McCall opaqueData = OVMA::bind(CGF, ov, RValue::get(result.getPointer())); 25324b9c2d235fb9449e249d74f48ecfec601650de93John McCall 25334b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Otherwise, just bind it. 25344b9c2d235fb9449e249d74f48ecfec601650de93John McCall } else { 25354b9c2d235fb9449e249d74f48ecfec601650de93John McCall opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr()); 25364b9c2d235fb9449e249d74f48ecfec601650de93John McCall } 25374b9c2d235fb9449e249d74f48ecfec601650de93John McCall opaques.push_back(opaqueData); 25384b9c2d235fb9449e249d74f48ecfec601650de93John McCall 25394b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Otherwise, if the expression is the result, evaluate it 25404b9c2d235fb9449e249d74f48ecfec601650de93John McCall // and remember the result. 25414b9c2d235fb9449e249d74f48ecfec601650de93John McCall } else if (semantic == resultExpr) { 25424b9c2d235fb9449e249d74f48ecfec601650de93John McCall result = tryEmitARCRetainScalarExpr(CGF, semantic); 25434b9c2d235fb9449e249d74f48ecfec601650de93John McCall 25444b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Otherwise, evaluate the expression in an ignored context. 25454b9c2d235fb9449e249d74f48ecfec601650de93John McCall } else { 25464b9c2d235fb9449e249d74f48ecfec601650de93John McCall CGF.EmitIgnoredExpr(semantic); 25474b9c2d235fb9449e249d74f48ecfec601650de93John McCall } 25484b9c2d235fb9449e249d74f48ecfec601650de93John McCall } 25494b9c2d235fb9449e249d74f48ecfec601650de93John McCall 25504b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Unbind all the opaques now. 25514b9c2d235fb9449e249d74f48ecfec601650de93John McCall for (unsigned i = 0, e = opaques.size(); i != e; ++i) 25524b9c2d235fb9449e249d74f48ecfec601650de93John McCall opaques[i].unbind(CGF); 25534b9c2d235fb9449e249d74f48ecfec601650de93John McCall 25544b9c2d235fb9449e249d74f48ecfec601650de93John McCall return result; 25554b9c2d235fb9449e249d74f48ecfec601650de93John McCall} 25564b9c2d235fb9449e249d74f48ecfec601650de93John McCall 2557f85e193739c953358c865005855253af4f68a497John McCallstatic TryEmitResult 2558f85e193739c953358c865005855253af4f68a497John McCalltryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e) { 255972dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // We should *never* see a nested full-expression here, because if 256072dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // we fail to emit at +1, our caller must not retain after we close 256172dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // out the full-expression. 256272dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall assert(!isa<ExprWithCleanups>(e)); 2563990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 2564f85e193739c953358c865005855253af4f68a497John McCall // The desired result type, if it differs from the type of the 2565f85e193739c953358c865005855253af4f68a497John McCall // ultimate opaque expression. 25662acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *resultType = 0; 2567f85e193739c953358c865005855253af4f68a497John McCall 2568f85e193739c953358c865005855253af4f68a497John McCall while (true) { 2569f85e193739c953358c865005855253af4f68a497John McCall e = e->IgnoreParens(); 2570f85e193739c953358c865005855253af4f68a497John McCall 2571f85e193739c953358c865005855253af4f68a497John McCall // There's a break at the end of this if-chain; anything 2572f85e193739c953358c865005855253af4f68a497John McCall // that wants to keep looping has to explicitly continue. 2573f85e193739c953358c865005855253af4f68a497John McCall if (const CastExpr *ce = dyn_cast<CastExpr>(e)) { 2574f85e193739c953358c865005855253af4f68a497John McCall switch (ce->getCastKind()) { 2575f85e193739c953358c865005855253af4f68a497John McCall // No-op casts don't change the type, so we just ignore them. 2576f85e193739c953358c865005855253af4f68a497John McCall case CK_NoOp: 2577f85e193739c953358c865005855253af4f68a497John McCall e = ce->getSubExpr(); 2578f85e193739c953358c865005855253af4f68a497John McCall continue; 2579f85e193739c953358c865005855253af4f68a497John McCall 2580f85e193739c953358c865005855253af4f68a497John McCall case CK_LValueToRValue: { 2581f85e193739c953358c865005855253af4f68a497John McCall TryEmitResult loadResult 2582f85e193739c953358c865005855253af4f68a497John McCall = tryEmitARCRetainLoadOfScalar(CGF, ce->getSubExpr()); 2583f85e193739c953358c865005855253af4f68a497John McCall if (resultType) { 2584f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = loadResult.getPointer(); 2585f85e193739c953358c865005855253af4f68a497John McCall value = CGF.Builder.CreateBitCast(value, resultType); 2586f85e193739c953358c865005855253af4f68a497John McCall loadResult.setPointer(value); 2587f85e193739c953358c865005855253af4f68a497John McCall } 2588f85e193739c953358c865005855253af4f68a497John McCall return loadResult; 2589f85e193739c953358c865005855253af4f68a497John McCall } 2590f85e193739c953358c865005855253af4f68a497John McCall 2591f85e193739c953358c865005855253af4f68a497John McCall // These casts can change the type, so remember that and 2592f85e193739c953358c865005855253af4f68a497John McCall // soldier on. We only need to remember the outermost such 2593f85e193739c953358c865005855253af4f68a497John McCall // cast, though. 25941d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall case CK_CPointerToObjCPointerCast: 25951d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall case CK_BlockPointerToObjCPointerCast: 2596f85e193739c953358c865005855253af4f68a497John McCall case CK_AnyPointerToBlockPointerCast: 2597f85e193739c953358c865005855253af4f68a497John McCall case CK_BitCast: 2598f85e193739c953358c865005855253af4f68a497John McCall if (!resultType) 2599f85e193739c953358c865005855253af4f68a497John McCall resultType = CGF.ConvertType(ce->getType()); 2600f85e193739c953358c865005855253af4f68a497John McCall e = ce->getSubExpr(); 2601f85e193739c953358c865005855253af4f68a497John McCall assert(e->getType()->hasPointerRepresentation()); 2602f85e193739c953358c865005855253af4f68a497John McCall continue; 2603f85e193739c953358c865005855253af4f68a497John McCall 2604f85e193739c953358c865005855253af4f68a497John McCall // For consumptions, just emit the subexpression and thus elide 2605f85e193739c953358c865005855253af4f68a497John McCall // the retain/release pair. 260633e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCConsumeObject: { 2607f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *result = CGF.EmitScalarExpr(ce->getSubExpr()); 2608f85e193739c953358c865005855253af4f68a497John McCall if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); 2609f85e193739c953358c865005855253af4f68a497John McCall return TryEmitResult(result, true); 2610f85e193739c953358c865005855253af4f68a497John McCall } 2611f85e193739c953358c865005855253af4f68a497John McCall 2612dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // Block extends are net +0. Naively, we could just recurse on 2613dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // the subexpression, but actually we need to ensure that the 2614dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // value is copied as a block, so there's a little filter here. 261533e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCExtendBlockObject: { 2616dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall llvm::Value *result; // will be a +0 value 2617dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2618dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // If we can't safely assume the sub-expression will produce a 2619dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // block-copied value, emit the sub-expression at +0. 2620dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall if (shouldEmitSeparateBlockRetain(ce->getSubExpr())) { 2621dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall result = CGF.EmitScalarExpr(ce->getSubExpr()); 2622dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2623dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // Otherwise, try to emit the sub-expression at +1 recursively. 2624dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall } else { 2625dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall TryEmitResult subresult 2626dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall = tryEmitARCRetainScalarExpr(CGF, ce->getSubExpr()); 2627dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall result = subresult.getPointer(); 2628dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2629dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // If that produced a retained value, just use that, 2630dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // possibly casting down. 2631dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall if (subresult.getInt()) { 2632dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall if (resultType) 2633dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall result = CGF.Builder.CreateBitCast(result, resultType); 2634dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return TryEmitResult(result, true); 2635dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall } 2636dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2637dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // Otherwise it's +0. 2638dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall } 2639dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2640dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // Retain the object as a block, then cast down. 2641348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall result = CGF.EmitARCRetainBlock(result, /*mandatory*/ true); 2642dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); 2643dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return TryEmitResult(result, true); 2644dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall } 2645dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 26467e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall // For reclaims, emit the subexpression as a retained call and 26477e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall // skip the consumption. 264833e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCReclaimReturnedObject: { 26497e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall llvm::Value *result = emitARCRetainCall(CGF, ce->getSubExpr()); 26507e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); 26517e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall return TryEmitResult(result, true); 26527e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall } 26537e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall 2654f85e193739c953358c865005855253af4f68a497John McCall default: 2655f85e193739c953358c865005855253af4f68a497John McCall break; 2656f85e193739c953358c865005855253af4f68a497John McCall } 2657f85e193739c953358c865005855253af4f68a497John McCall 2658f85e193739c953358c865005855253af4f68a497John McCall // Skip __extension__. 2659f85e193739c953358c865005855253af4f68a497John McCall } else if (const UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { 2660f85e193739c953358c865005855253af4f68a497John McCall if (op->getOpcode() == UO_Extension) { 2661f85e193739c953358c865005855253af4f68a497John McCall e = op->getSubExpr(); 2662f85e193739c953358c865005855253af4f68a497John McCall continue; 2663f85e193739c953358c865005855253af4f68a497John McCall } 2664f85e193739c953358c865005855253af4f68a497John McCall 2665f85e193739c953358c865005855253af4f68a497John McCall // For calls and message sends, use the retained-call logic. 2666f85e193739c953358c865005855253af4f68a497John McCall // Delegate inits are a special case in that they're the only 2667f85e193739c953358c865005855253af4f68a497John McCall // returns-retained expression that *isn't* surrounded by 2668f85e193739c953358c865005855253af4f68a497John McCall // a consume. 2669f85e193739c953358c865005855253af4f68a497John McCall } else if (isa<CallExpr>(e) || 2670f85e193739c953358c865005855253af4f68a497John McCall (isa<ObjCMessageExpr>(e) && 2671f85e193739c953358c865005855253af4f68a497John McCall !cast<ObjCMessageExpr>(e)->isDelegateInitCall())) { 2672f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *result = emitARCRetainCall(CGF, e); 2673f85e193739c953358c865005855253af4f68a497John McCall if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); 2674f85e193739c953358c865005855253af4f68a497John McCall return TryEmitResult(result, true); 26754b9c2d235fb9449e249d74f48ecfec601650de93John McCall 26764b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Look through pseudo-object expressions. 26774b9c2d235fb9449e249d74f48ecfec601650de93John McCall } else if (const PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) { 26784b9c2d235fb9449e249d74f48ecfec601650de93John McCall TryEmitResult result 26794b9c2d235fb9449e249d74f48ecfec601650de93John McCall = tryEmitARCRetainPseudoObject(CGF, pseudo); 26804b9c2d235fb9449e249d74f48ecfec601650de93John McCall if (resultType) { 26814b9c2d235fb9449e249d74f48ecfec601650de93John McCall llvm::Value *value = result.getPointer(); 26824b9c2d235fb9449e249d74f48ecfec601650de93John McCall value = CGF.Builder.CreateBitCast(value, resultType); 26834b9c2d235fb9449e249d74f48ecfec601650de93John McCall result.setPointer(value); 26844b9c2d235fb9449e249d74f48ecfec601650de93John McCall } 26854b9c2d235fb9449e249d74f48ecfec601650de93John McCall return result; 2686f85e193739c953358c865005855253af4f68a497John McCall } 2687f85e193739c953358c865005855253af4f68a497John McCall 2688f85e193739c953358c865005855253af4f68a497John McCall // Conservatively halt the search at any other expression kind. 2689f85e193739c953358c865005855253af4f68a497John McCall break; 2690f85e193739c953358c865005855253af4f68a497John McCall } 2691f85e193739c953358c865005855253af4f68a497John McCall 2692f85e193739c953358c865005855253af4f68a497John McCall // We didn't find an obvious production, so emit what we've got and 2693f85e193739c953358c865005855253af4f68a497John McCall // tell the caller that we didn't manage to retain. 2694f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *result = CGF.EmitScalarExpr(e); 2695f85e193739c953358c865005855253af4f68a497John McCall if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); 2696f85e193739c953358c865005855253af4f68a497John McCall return TryEmitResult(result, false); 2697f85e193739c953358c865005855253af4f68a497John McCall} 2698f85e193739c953358c865005855253af4f68a497John McCall 2699f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF, 2700f85e193739c953358c865005855253af4f68a497John McCall LValue lvalue, 2701f85e193739c953358c865005855253af4f68a497John McCall QualType type) { 2702f85e193739c953358c865005855253af4f68a497John McCall TryEmitResult result = tryEmitARCRetainLoadOfScalar(CGF, lvalue, type); 2703f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = result.getPointer(); 2704f85e193739c953358c865005855253af4f68a497John McCall if (!result.getInt()) 2705f85e193739c953358c865005855253af4f68a497John McCall value = CGF.EmitARCRetain(type, value); 2706f85e193739c953358c865005855253af4f68a497John McCall return value; 2707f85e193739c953358c865005855253af4f68a497John McCall} 2708f85e193739c953358c865005855253af4f68a497John McCall 2709f85e193739c953358c865005855253af4f68a497John McCall/// EmitARCRetainScalarExpr - Semantically equivalent to 2710f85e193739c953358c865005855253af4f68a497John McCall/// EmitARCRetainObject(e->getType(), EmitScalarExpr(e)), but making a 2711f85e193739c953358c865005855253af4f68a497John McCall/// best-effort attempt to peephole expressions that naturally produce 2712f85e193739c953358c865005855253af4f68a497John McCall/// retained objects. 2713f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCRetainScalarExpr(const Expr *e) { 271472dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // The retain needs to happen within the full-expression. 271572dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) { 271672dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall enterFullExpression(cleanups); 271772dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall RunCleanupsScope scope(*this); 271872dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall return EmitARCRetainScalarExpr(cleanups->getSubExpr()); 271972dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall } 272072dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall 2721f85e193739c953358c865005855253af4f68a497John McCall TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e); 2722f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = result.getPointer(); 2723f85e193739c953358c865005855253af4f68a497John McCall if (!result.getInt()) 2724f85e193739c953358c865005855253af4f68a497John McCall value = EmitARCRetain(e->getType(), value); 2725f85e193739c953358c865005855253af4f68a497John McCall return value; 2726f85e193739c953358c865005855253af4f68a497John McCall} 2727f85e193739c953358c865005855253af4f68a497John McCall 2728f85e193739c953358c865005855253af4f68a497John McCallllvm::Value * 2729f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCRetainAutoreleaseScalarExpr(const Expr *e) { 273072dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // The retain needs to happen within the full-expression. 273172dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) { 273272dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall enterFullExpression(cleanups); 273372dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall RunCleanupsScope scope(*this); 273472dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall return EmitARCRetainAutoreleaseScalarExpr(cleanups->getSubExpr()); 273572dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall } 273672dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall 2737f85e193739c953358c865005855253af4f68a497John McCall TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e); 2738f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = result.getPointer(); 2739f85e193739c953358c865005855253af4f68a497John McCall if (result.getInt()) 2740f85e193739c953358c865005855253af4f68a497John McCall value = EmitARCAutorelease(value); 2741f85e193739c953358c865005855253af4f68a497John McCall else 2742f85e193739c953358c865005855253af4f68a497John McCall value = EmitARCRetainAutorelease(e->getType(), value); 2743f85e193739c953358c865005855253af4f68a497John McCall return value; 2744f85e193739c953358c865005855253af4f68a497John McCall} 2745f85e193739c953358c865005855253af4f68a497John McCall 2746348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCallllvm::Value *CodeGenFunction::EmitARCExtendBlockObject(const Expr *e) { 2747348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall llvm::Value *result; 2748348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall bool doRetain; 2749348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 2750348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall if (shouldEmitSeparateBlockRetain(e)) { 2751348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall result = EmitScalarExpr(e); 2752348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall doRetain = true; 2753348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall } else { 2754348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall TryEmitResult subresult = tryEmitARCRetainScalarExpr(*this, e); 2755348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall result = subresult.getPointer(); 2756348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall doRetain = !subresult.getInt(); 2757348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall } 2758348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 2759348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall if (doRetain) 2760348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall result = EmitARCRetainBlock(result, /*mandatory*/ true); 2761348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall return EmitObjCConsumeObject(e->getType(), result); 2762348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall} 2763348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 27642b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCallllvm::Value *CodeGenFunction::EmitObjCThrowOperand(const Expr *expr) { 27652b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // In ARC, retain and autorelease the expression. 27664e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().ObjCAutoRefCount) { 27672b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // Do so before running any cleanups for the full-expression. 276872dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // EmitARCRetainAutoreleaseScalarExpr does this for us. 27692b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall return EmitARCRetainAutoreleaseScalarExpr(expr); 27702b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall } 27712b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall 27722b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // Otherwise, use the normal scalar-expression emission. The 27732b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // exception machinery doesn't do anything special with the 27742b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // exception like retaining it, so there's no safety associated with 27752b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // only running cleanups after the throw has started, and when it 27762b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // matters it tends to be substantially inferior code. 27772b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall return EmitScalarExpr(expr); 27782b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall} 27792b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall 2780f85e193739c953358c865005855253af4f68a497John McCallstd::pair<LValue,llvm::Value*> 2781f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCStoreStrong(const BinaryOperator *e, 2782f85e193739c953358c865005855253af4f68a497John McCall bool ignored) { 2783f85e193739c953358c865005855253af4f68a497John McCall // Evaluate the RHS first. 2784f85e193739c953358c865005855253af4f68a497John McCall TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e->getRHS()); 2785f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = result.getPointer(); 2786f85e193739c953358c865005855253af4f68a497John McCall 2787fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall bool hasImmediateRetain = result.getInt(); 2788fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall 2789fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall // If we didn't emit a retained object, and the l-value is of block 2790fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall // type, then we need to emit the block-retain immediately in case 2791fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall // it invalidates the l-value. 2792fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall if (!hasImmediateRetain && e->getType()->isBlockPointerType()) { 2793348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall value = EmitARCRetainBlock(value, /*mandatory*/ false); 2794fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall hasImmediateRetain = true; 2795fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall } 2796fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall 2797f85e193739c953358c865005855253af4f68a497John McCall LValue lvalue = EmitLValue(e->getLHS()); 2798f85e193739c953358c865005855253af4f68a497John McCall 2799f85e193739c953358c865005855253af4f68a497John McCall // If the RHS was emitted retained, expand this. 2800fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall if (hasImmediateRetain) { 2801c53143cc5f4edbd0490eefa9ecc1de4f9c24d8ddNick Lewycky llvm::Value *oldValue = EmitLoadOfScalar(lvalue, SourceLocation()); 28026da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman EmitStoreOfScalar(value, lvalue); 28035b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall EmitARCRelease(oldValue, lvalue.isARCPreciseLifetime()); 2804f85e193739c953358c865005855253af4f68a497John McCall } else { 2805545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall value = EmitARCStoreStrong(lvalue, value, ignored); 2806f85e193739c953358c865005855253af4f68a497John McCall } 2807f85e193739c953358c865005855253af4f68a497John McCall 2808f85e193739c953358c865005855253af4f68a497John McCall return std::pair<LValue,llvm::Value*>(lvalue, value); 2809f85e193739c953358c865005855253af4f68a497John McCall} 2810f85e193739c953358c865005855253af4f68a497John McCall 2811f85e193739c953358c865005855253af4f68a497John McCallstd::pair<LValue,llvm::Value*> 2812f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCStoreAutoreleasing(const BinaryOperator *e) { 2813f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = EmitARCRetainAutoreleaseScalarExpr(e->getRHS()); 2814f85e193739c953358c865005855253af4f68a497John McCall LValue lvalue = EmitLValue(e->getLHS()); 2815f85e193739c953358c865005855253af4f68a497John McCall 28166da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman EmitStoreOfScalar(value, lvalue); 2817f85e193739c953358c865005855253af4f68a497John McCall 2818f85e193739c953358c865005855253af4f68a497John McCall return std::pair<LValue,llvm::Value*>(lvalue, value); 2819f85e193739c953358c865005855253af4f68a497John McCall} 2820f85e193739c953358c865005855253af4f68a497John McCall 2821f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitObjCAutoreleasePoolStmt( 282216098f366097305c348b356a44638d6c959c6e60Eric Christopher const ObjCAutoreleasePoolStmt &ARPS) { 2823f85e193739c953358c865005855253af4f68a497John McCall const Stmt *subStmt = ARPS.getSubStmt(); 2824f85e193739c953358c865005855253af4f68a497John McCall const CompoundStmt &S = cast<CompoundStmt>(*subStmt); 2825f85e193739c953358c865005855253af4f68a497John McCall 2826f85e193739c953358c865005855253af4f68a497John McCall CGDebugInfo *DI = getDebugInfo(); 282773fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher if (DI) 282873fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher DI->EmitLexicalBlockStart(Builder, S.getLBracLoc()); 2829f85e193739c953358c865005855253af4f68a497John McCall 2830f85e193739c953358c865005855253af4f68a497John McCall // Keep track of the current cleanup stack depth. 2831f85e193739c953358c865005855253af4f68a497John McCall RunCleanupsScope Scope(*this); 28320a7dd788dbef975f35f273c7ab913f480f7edd60John McCall if (CGM.getLangOpts().ObjCRuntime.hasNativeARC()) { 2833f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *token = EmitObjCAutoreleasePoolPush(); 2834f85e193739c953358c865005855253af4f68a497John McCall EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, token); 2835f85e193739c953358c865005855253af4f68a497John McCall } else { 2836f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *token = EmitObjCMRRAutoreleasePoolPush(); 2837f85e193739c953358c865005855253af4f68a497John McCall EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, token); 2838f85e193739c953358c865005855253af4f68a497John McCall } 2839f85e193739c953358c865005855253af4f68a497John McCall 2840651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines for (const auto *I : S.body()) 2841651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines EmitStmt(I); 2842f85e193739c953358c865005855253af4f68a497John McCall 284373fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher if (DI) 284473fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher DI->EmitLexicalBlockEnd(Builder, S.getRBracLoc()); 2845f85e193739c953358c865005855253af4f68a497John McCall} 28460c24c808e4dce43e88abf2d5f98546b901bd4315John McCall 28470c24c808e4dce43e88abf2d5f98546b901bd4315John McCall/// EmitExtendGCLifetime - Given a pointer to an Objective-C object, 28480c24c808e4dce43e88abf2d5f98546b901bd4315John McCall/// make sure it survives garbage collection until this point. 28490c24c808e4dce43e88abf2d5f98546b901bd4315John McCallvoid CodeGenFunction::EmitExtendGCLifetime(llvm::Value *object) { 28500c24c808e4dce43e88abf2d5f98546b901bd4315John McCall // We just use an inline assembly. 28510c24c808e4dce43e88abf2d5f98546b901bd4315John McCall llvm::FunctionType *extenderType 2852de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall = llvm::FunctionType::get(VoidTy, VoidPtrTy, RequiredArgs::All); 28530c24c808e4dce43e88abf2d5f98546b901bd4315John McCall llvm::Value *extender 28540c24c808e4dce43e88abf2d5f98546b901bd4315John McCall = llvm::InlineAsm::get(extenderType, 28550c24c808e4dce43e88abf2d5f98546b901bd4315John McCall /* assembly */ "", 28560c24c808e4dce43e88abf2d5f98546b901bd4315John McCall /* constraints */ "r", 28570c24c808e4dce43e88abf2d5f98546b901bd4315John McCall /* side effects */ true); 28580c24c808e4dce43e88abf2d5f98546b901bd4315John McCall 28590c24c808e4dce43e88abf2d5f98546b901bd4315John McCall object = Builder.CreateBitCast(object, VoidPtrTy); 2860bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall EmitNounwindRuntimeCall(extender, object); 28610c24c808e4dce43e88abf2d5f98546b901bd4315John McCall} 28620c24c808e4dce43e88abf2d5f98546b901bd4315John McCall 286320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian/// GenerateObjCAtomicSetterCopyHelperFunction - Given a c++ object type with 286484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian/// non-trivial copy assignment function, produce following helper function. 286584e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian/// static void copyHelper(Ty *dest, const Ty *source) { *dest = *source; } 286684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian/// 286784e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanianllvm::Constant * 286820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz JahanianCodeGenFunction::GenerateObjCAtomicSetterCopyHelperFunction( 286920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian const ObjCPropertyImplDecl *PID) { 2870260611a32535c851237926bfcf78869b13c07d5bJohn McCall if (!getLangOpts().CPlusPlus || 287190f692611bd486198ffa67a0e097013a2e6e3d2eRafael Espindola !getLangOpts().ObjCRuntime.hasAtomicCopyHelper()) 287284e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian return 0; 287384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian QualType Ty = PID->getPropertyIvarDecl()->getType(); 287484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian if (!Ty->isRecordType()) 287584e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian return 0; 287684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 287720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if ((!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_atomic))) 287884e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian return 0; 2879b08cfb3c5909752c0e501a4e5c31a507a4cc1f72Fariborz Jahanian llvm::Constant * HelperFn = 0; 288020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if (hasTrivialSetExpr(PID)) 288120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return 0; 288220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian assert(PID->getSetterCXXAssignment() && "SetterCXXAssignment - null"); 288320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if ((HelperFn = CGM.getAtomicSetterHelperFnMap(Ty))) 288420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return HelperFn; 288584e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 288684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian ASTContext &C = getContext(); 288784e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian IdentifierInfo *II 288820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian = &CGM.getContext().Idents.get("__assign_helper_atomic_property_"); 288984e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian FunctionDecl *FD = FunctionDecl::Create(C, 289084e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian C.getTranslationUnitDecl(), 289184e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian SourceLocation(), 289284e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian SourceLocation(), II, C.VoidTy, 0, 289384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian SC_Static, 289484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian false, 2895b92bd4b3271b7892abe9fd8c74fb54a27ad702abEric Christopher false); 289684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 289784e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian QualType DestTy = C.getPointerType(Ty); 289884e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian QualType SrcTy = Ty; 289984e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian SrcTy.addConst(); 290084e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian SrcTy = C.getPointerType(SrcTy); 290184e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 290284e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian FunctionArgList args; 290384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian ImplicitParamDecl dstDecl(FD, SourceLocation(), 0, DestTy); 290484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian args.push_back(&dstDecl); 290584e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian ImplicitParamDecl srcDecl(FD, SourceLocation(), 0, SrcTy); 290684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian args.push_back(&srcDecl); 2907651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 2908651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines const CGFunctionInfo &FI = CGM.getTypes().arrangeFreeFunctionDeclaration( 2909651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines C.VoidTy, args, FunctionType::ExtInfo(), RequiredArgs::All); 2910651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 2911de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI); 291284e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 291384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian llvm::Function *Fn = 291484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 291516098f366097305c348b356a44638d6c959c6e60Eric Christopher "__assign_helper_atomic_property_", 291616098f366097305c348b356a44638d6c959c6e60Eric Christopher &CGM.getModule()); 291784e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 291884e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation()); 291984e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 2920f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr DstExpr(&dstDecl, false, DestTy, 2921f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_RValue, SourceLocation()); 2922f4b88a45902af1802a1cb42ba48b1c474474f228John McCall UnaryOperator DST(&DstExpr, UO_Deref, DestTy->getPointeeType(), 2923f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_LValue, OK_Ordinary, SourceLocation()); 292484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 2925f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr SrcExpr(&srcDecl, false, SrcTy, 2926f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_RValue, SourceLocation()); 2927f4b88a45902af1802a1cb42ba48b1c474474f228John McCall UnaryOperator SRC(&SrcExpr, UO_Deref, SrcTy->getPointeeType(), 2928f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_LValue, OK_Ordinary, SourceLocation()); 292984e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 2930f4b88a45902af1802a1cb42ba48b1c474474f228John McCall Expr *Args[2] = { &DST, &SRC }; 293120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CallExpr *CalleeExp = cast<CallExpr>(PID->getSetterCXXAssignment()); 2932f4b88a45902af1802a1cb42ba48b1c474474f228John McCall CXXOperatorCallExpr TheCall(C, OO_Equal, CalleeExp->getCallee(), 29333b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer Args, DestTy->getPointeeType(), 2934be9af1288881110e406b87914162eaa59f1e5918Lang Hames VK_LValue, SourceLocation(), false); 2935f4b88a45902af1802a1cb42ba48b1c474474f228John McCall 2936f4b88a45902af1802a1cb42ba48b1c474474f228John McCall EmitStmt(&TheCall); 293784e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 293884e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian FinishFunction(); 2939cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); 294020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CGM.setAtomicSetterHelperFnMap(Ty, HelperFn); 2941cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian return HelperFn; 294220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian} 294320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 294420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanianllvm::Constant * 294520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz JahanianCodeGenFunction::GenerateObjCAtomicGetterCopyHelperFunction( 294620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian const ObjCPropertyImplDecl *PID) { 2947260611a32535c851237926bfcf78869b13c07d5bJohn McCall if (!getLangOpts().CPlusPlus || 294890f692611bd486198ffa67a0e097013a2e6e3d2eRafael Espindola !getLangOpts().ObjCRuntime.hasAtomicCopyHelper()) 294920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return 0; 295020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 295120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian QualType Ty = PD->getType(); 295220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if (!Ty->isRecordType()) 295320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return 0; 295420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if ((!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_atomic))) 295520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return 0; 295620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Constant * HelperFn = 0; 295720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 295820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if (hasTrivialGetExpr(PID)) 295920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return 0; 296020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian assert(PID->getGetterCXXConstructor() && "getGetterCXXConstructor - null"); 296120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if ((HelperFn = CGM.getAtomicGetterHelperFnMap(Ty))) 296220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return HelperFn; 296320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 296420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 296520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ASTContext &C = getContext(); 296620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian IdentifierInfo *II 296720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian = &CGM.getContext().Idents.get("__copy_helper_atomic_property_"); 296820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian FunctionDecl *FD = FunctionDecl::Create(C, 296920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian C.getTranslationUnitDecl(), 297020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SourceLocation(), 297120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SourceLocation(), II, C.VoidTy, 0, 297220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SC_Static, 297320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian false, 2974b92bd4b3271b7892abe9fd8c74fb54a27ad702abEric Christopher false); 297520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 297620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian QualType DestTy = C.getPointerType(Ty); 297720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian QualType SrcTy = Ty; 297820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SrcTy.addConst(); 297920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SrcTy = C.getPointerType(SrcTy); 298020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 298120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian FunctionArgList args; 298220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ImplicitParamDecl dstDecl(FD, SourceLocation(), 0, DestTy); 298320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian args.push_back(&dstDecl); 298420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ImplicitParamDecl srcDecl(FD, SourceLocation(), 0, SrcTy); 298520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian args.push_back(&srcDecl); 2986651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 2987651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines const CGFunctionInfo &FI = CGM.getTypes().arrangeFreeFunctionDeclaration( 2988651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines C.VoidTy, args, FunctionType::ExtInfo(), RequiredArgs::All); 2989651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 2990de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI); 299120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 299220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Function *Fn = 299320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 299420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian "__copy_helper_atomic_property_", &CGM.getModule()); 299584e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 299620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation()); 299720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 2998f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr SrcExpr(&srcDecl, false, SrcTy, 299920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian VK_RValue, SourceLocation()); 300020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 3001f4b88a45902af1802a1cb42ba48b1c474474f228John McCall UnaryOperator SRC(&SrcExpr, UO_Deref, SrcTy->getPointeeType(), 3002f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_LValue, OK_Ordinary, SourceLocation()); 300320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 300420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstructExpr *CXXConstExpr = 300520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian cast<CXXConstructExpr>(PID->getGetterCXXConstructor()); 300620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 300720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SmallVector<Expr*, 4> ConstructorArgs; 3008f4b88a45902af1802a1cb42ba48b1c474474f228John McCall ConstructorArgs.push_back(&SRC); 300920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstructExpr::arg_iterator A = CXXConstExpr->arg_begin(); 301020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ++A; 301120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 301220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian for (CXXConstructExpr::arg_iterator AEnd = CXXConstExpr->arg_end(); 301320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian A != AEnd; ++A) 301420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ConstructorArgs.push_back(*A); 301520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 301620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstructExpr *TheCXXConstructExpr = 301720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstructExpr::Create(C, Ty, SourceLocation(), 301820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstExpr->getConstructor(), 301920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstExpr->isElidable(), 30203b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer ConstructorArgs, 30215b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl CXXConstExpr->hadMultipleCandidates(), 30225b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl CXXConstExpr->isListInitialization(), 302320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstExpr->requiresZeroInitialization(), 302416098f366097305c348b356a44638d6c959c6e60Eric Christopher CXXConstExpr->getConstructionKind(), 302516098f366097305c348b356a44638d6c959c6e60Eric Christopher SourceRange()); 302620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 3027f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr DstExpr(&dstDecl, false, DestTy, 3028f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_RValue, SourceLocation()); 302920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 3030f4b88a45902af1802a1cb42ba48b1c474474f228John McCall RValue DV = EmitAnyExpr(&DstExpr); 303116098f366097305c348b356a44638d6c959c6e60Eric Christopher CharUnits Alignment 303216098f366097305c348b356a44638d6c959c6e60Eric Christopher = getContext().getTypeAlignInChars(TheCXXConstructExpr->getType()); 303320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian EmitAggExpr(TheCXXConstructExpr, 303420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian AggValueSlot::forAddr(DV.getScalarVal(), Alignment, Qualifiers(), 303520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian AggValueSlot::IsDestructed, 303620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian AggValueSlot::DoesNotNeedGCBarriers, 3037649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier AggValueSlot::IsNotAliased)); 303820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 303920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian FinishFunction(); 304020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); 304120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CGM.setAtomicGetterHelperFnMap(Ty, HelperFn); 304220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return HelperFn; 304384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian} 304484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 3045cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedmanllvm::Value * 3046cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli FriedmanCodeGenFunction::EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty) { 3047cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman // Get selectors for retain/autorelease. 30488c72a7db34a63c38c6065b73fda4aeb0fe19eb65Eli Friedman IdentifierInfo *CopyID = &getContext().Idents.get("copy"); 30498c72a7db34a63c38c6065b73fda4aeb0fe19eb65Eli Friedman Selector CopySelector = 30508c72a7db34a63c38c6065b73fda4aeb0fe19eb65Eli Friedman getContext().Selectors.getNullarySelector(CopyID); 3051cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman IdentifierInfo *AutoreleaseID = &getContext().Idents.get("autorelease"); 3052cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Selector AutoreleaseSelector = 3053cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman getContext().Selectors.getNullarySelector(AutoreleaseID); 3054cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman 3055cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman // Emit calls to retain/autorelease. 3056cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 3057cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman llvm::Value *Val = Block; 3058cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman RValue Result; 3059cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 30608c72a7db34a63c38c6065b73fda4aeb0fe19eb65Eli Friedman Ty, CopySelector, 3061cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Val, CallArgList(), 0, 0); 3062cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Val = Result.getScalarVal(); 3063cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 3064cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Ty, AutoreleaseSelector, 3065cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Val, CallArgList(), 0, 0); 3066cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Val = Result.getScalarVal(); 3067cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman return Val; 3068cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman} 3069cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman 307084e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 30712979ec73b4f974d85f2ce84167712177a44c6f09Ted KremenekCGObjCRuntime::~CGObjCRuntime() {} 3072