CGObjC.cpp revision c53143cc5f4edbd0490eefa9ecc1de4f9c24d8dd
15508518a2702b00be3b15a26d772bde968972f54Anders Carlsson//===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===// 25508518a2702b00be3b15a26d772bde968972f54Anders Carlsson// 35508518a2702b00be3b15a26d772bde968972f54Anders Carlsson// The LLVM Compiler Infrastructure 45508518a2702b00be3b15a26d772bde968972f54Anders Carlsson// 50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source 60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details. 75508518a2702b00be3b15a26d772bde968972f54Anders Carlsson// 85508518a2702b00be3b15a26d772bde968972f54Anders Carlsson//===----------------------------------------------------------------------===// 95508518a2702b00be3b15a26d772bde968972f54Anders Carlsson// 105508518a2702b00be3b15a26d772bde968972f54Anders Carlsson// This contains code to emit Objective-C code as LLVM code. 115508518a2702b00be3b15a26d772bde968972f54Anders Carlsson// 125508518a2702b00be3b15a26d772bde968972f54Anders Carlsson//===----------------------------------------------------------------------===// 135508518a2702b00be3b15a26d772bde968972f54Anders Carlsson 14bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel#include "CGDebugInfo.h" 152979ec73b4f974d85f2ce84167712177a44c6f09Ted Kremenek#include "CGObjCRuntime.h" 165508518a2702b00be3b15a26d772bde968972f54Anders Carlsson#include "CodeGenFunction.h" 175508518a2702b00be3b15a26d772bde968972f54Anders Carlsson#include "CodeGenModule.h" 18f85e193739c953358c865005855253af4f68a497John McCall#include "TargetInfo.h" 1985c59edda02df48fae8dc85049743319bc6e7e89Daniel Dunbar#include "clang/AST/ASTContext.h" 20c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h" 2116f0049415ec596504891259e2a83e19871c0d52Chris Lattner#include "clang/AST/StmtObjC.h" 22e66f4e3e3ae9d7d11b0c302211066fad69228abaDaniel Dunbar#include "clang/Basic/Diagnostic.h" 233d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson#include "llvm/ADT/STLExtras.h" 24b57f6b3d8f84e9241fd978e2739d6a9da0870546John McCall#include "llvm/Support/CallSite.h" 253b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/DataLayout.h" 263b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/InlineAsm.h" 275508518a2702b00be3b15a26d772bde968972f54Anders Carlssonusing namespace clang; 285508518a2702b00be3b15a26d772bde968972f54Anders Carlssonusing namespace CodeGen; 295508518a2702b00be3b15a26d772bde968972f54Anders Carlsson 30f85e193739c953358c865005855253af4f68a497John McCalltypedef llvm::PointerIntPair<llvm::Value*,1,bool> TryEmitResult; 31f85e193739c953358c865005855253af4f68a497John McCallstatic TryEmitResult 32f85e193739c953358c865005855253af4f68a497John McCalltryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e); 33ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenekstatic RValue AdjustRelatedResultType(CodeGenFunction &CGF, 34490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian QualType ET, 35ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const ObjCMethodDecl *Method, 36ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek RValue Result); 37f85e193739c953358c865005855253af4f68a497John McCall 38f85e193739c953358c865005855253af4f68a497John McCall/// Given the address of a variable of pointer type, find the correct 39f85e193739c953358c865005855253af4f68a497John McCall/// null to store into it. 40f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Constant *getNullForVariable(llvm::Value *addr) { 412acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *type = 42f85e193739c953358c865005855253af4f68a497John McCall cast<llvm::PointerType>(addr->getType())->getElementType(); 43f85e193739c953358c865005855253af4f68a497John McCall return llvm::ConstantPointerNull::get(cast<llvm::PointerType>(type)); 44f85e193739c953358c865005855253af4f68a497John McCall} 45f85e193739c953358c865005855253af4f68a497John McCall 468fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner/// Emits an instance of NSConstantString representing the object. 471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpllvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E) 4871fcec9abf2ce66d5e17a24bd021680e94e42f0dDaniel Dunbar{ 490d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall llvm::Constant *C = 500d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall CGM.getObjCRuntime().GenerateConstantString(E->getString()); 51ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbar // FIXME: This bitcast should just be made an invariant on the Runtime. 523c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType())); 538fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner} 548fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner 55eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard/// EmitObjCBoxedExpr - This routine generates code to call 56eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard/// the appropriate expression boxing method. This will either be 57eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard/// one of +[NSNumber numberWith<Type>:], or +[NSString stringWithUTF8String:]. 58ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek/// 5916098f366097305c348b356a44638d6c959c6e60Eric Christopherllvm::Value * 60eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick BeardCodeGenFunction::EmitObjCBoxedExpr(const ObjCBoxedExpr *E) { 61ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Generate the correct selector for this literal's concrete type. 62eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard const Expr *SubExpr = E->getSubExpr(); 63ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Get the method. 64eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard const ObjCMethodDecl *BoxingMethod = E->getBoxingMethod(); 65eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard assert(BoxingMethod && "BoxingMethod is null"); 66eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard assert(BoxingMethod->isClassMethod() && "BoxingMethod must be a class method"); 67eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard Selector Sel = BoxingMethod->getSelector(); 68ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 69ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Generate a reference to the class pointer, which will be the receiver. 70eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard // Assumes that the method was introduced in the class that should be 71eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard // messaged (avoids pulling it out of the result type). 72ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 73eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard const ObjCInterfaceDecl *ClassDecl = BoxingMethod->getClassInterface(); 74bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *Receiver = Runtime.GetClass(*this, ClassDecl); 75eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard 76eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard const ParmVarDecl *argDecl = *BoxingMethod->param_begin(); 77ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek QualType ArgQT = argDecl->getType().getUnqualifiedType(); 78eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard RValue RV = EmitAnyExpr(SubExpr); 79ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek CallArgList Args; 80ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Args.add(RV, ArgQT); 81eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard 82ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek RValue result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 83eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard BoxingMethod->getResultType(), Sel, Receiver, Args, 84eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard ClassDecl, BoxingMethod); 85ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return Builder.CreateBitCast(result.getScalarVal(), 86ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ConvertType(E->getType())); 87ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek} 88ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 89ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenekllvm::Value *CodeGenFunction::EmitObjCCollectionLiteral(const Expr *E, 90ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const ObjCMethodDecl *MethodWithObjects) { 91ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ASTContext &Context = CGM.getContext(); 92ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const ObjCDictionaryLiteral *DLE = 0; 93ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const ObjCArrayLiteral *ALE = dyn_cast<ObjCArrayLiteral>(E); 94ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (!ALE) 95ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek DLE = cast<ObjCDictionaryLiteral>(E); 96ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 97ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Compute the type of the array we're initializing. 98ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek uint64_t NumElements = 99ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ALE ? ALE->getNumElements() : DLE->getNumElements(); 100ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek llvm::APInt APNumElements(Context.getTypeSize(Context.getSizeType()), 101ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek NumElements); 102ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek QualType ElementType = Context.getObjCIdType().withConst(); 103ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek QualType ElementArrayType 104ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek = Context.getConstantArrayType(ElementType, APNumElements, 105ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ArrayType::Normal, /*IndexTypeQuals=*/0); 106ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 107ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Allocate the temporary array(s). 108ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek llvm::Value *Objects = CreateMemTemp(ElementArrayType, "objects"); 109ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek llvm::Value *Keys = 0; 110ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (DLE) 111ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Keys = CreateMemTemp(ElementArrayType, "keys"); 112ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 113527842f97b684831de2b1a455649131ed381ece4John McCall // In ARC, we may need to do extra work to keep all the keys and 114527842f97b684831de2b1a455649131ed381ece4John McCall // values alive until after the call. 115527842f97b684831de2b1a455649131ed381ece4John McCall SmallVector<llvm::Value *, 16> NeededObjects; 116527842f97b684831de2b1a455649131ed381ece4John McCall bool TrackNeededObjects = 117527842f97b684831de2b1a455649131ed381ece4John McCall (getLangOpts().ObjCAutoRefCount && 118527842f97b684831de2b1a455649131ed381ece4John McCall CGM.getCodeGenOpts().OptimizationLevel != 0); 119527842f97b684831de2b1a455649131ed381ece4John McCall 120ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Perform the actual initialialization of the array(s). 121ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek for (uint64_t i = 0; i < NumElements; i++) { 122ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (ALE) { 123527842f97b684831de2b1a455649131ed381ece4John McCall // Emit the element and store it to the appropriate array slot. 124ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const Expr *Rhs = ALE->getElement(i); 125ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek LValue LV = LValue::MakeAddr(Builder.CreateStructGEP(Objects, i), 126ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ElementType, 127ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Context.getTypeAlignInChars(Rhs->getType()), 128ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Context); 129527842f97b684831de2b1a455649131ed381ece4John McCall 130527842f97b684831de2b1a455649131ed381ece4John McCall llvm::Value *value = EmitScalarExpr(Rhs); 131527842f97b684831de2b1a455649131ed381ece4John McCall EmitStoreThroughLValue(RValue::get(value), LV, true); 132527842f97b684831de2b1a455649131ed381ece4John McCall if (TrackNeededObjects) { 133527842f97b684831de2b1a455649131ed381ece4John McCall NeededObjects.push_back(value); 134527842f97b684831de2b1a455649131ed381ece4John McCall } 135ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } else { 136527842f97b684831de2b1a455649131ed381ece4John McCall // Emit the key and store it to the appropriate array slot. 137ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const Expr *Key = DLE->getKeyValueElement(i).Key; 138ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek LValue KeyLV = LValue::MakeAddr(Builder.CreateStructGEP(Keys, i), 139ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ElementType, 140ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Context.getTypeAlignInChars(Key->getType()), 141ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Context); 142527842f97b684831de2b1a455649131ed381ece4John McCall llvm::Value *keyValue = EmitScalarExpr(Key); 143527842f97b684831de2b1a455649131ed381ece4John McCall EmitStoreThroughLValue(RValue::get(keyValue), KeyLV, /*isInit=*/true); 144ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 145527842f97b684831de2b1a455649131ed381ece4John McCall // Emit the value and store it to the appropriate array slot. 146ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const Expr *Value = DLE->getKeyValueElement(i).Value; 147ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek LValue ValueLV = LValue::MakeAddr(Builder.CreateStructGEP(Objects, i), 148ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ElementType, 149ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Context.getTypeAlignInChars(Value->getType()), 150ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Context); 151527842f97b684831de2b1a455649131ed381ece4John McCall llvm::Value *valueValue = EmitScalarExpr(Value); 152527842f97b684831de2b1a455649131ed381ece4John McCall EmitStoreThroughLValue(RValue::get(valueValue), ValueLV, /*isInit=*/true); 153527842f97b684831de2b1a455649131ed381ece4John McCall if (TrackNeededObjects) { 154527842f97b684831de2b1a455649131ed381ece4John McCall NeededObjects.push_back(keyValue); 155527842f97b684831de2b1a455649131ed381ece4John McCall NeededObjects.push_back(valueValue); 156527842f97b684831de2b1a455649131ed381ece4John McCall } 157ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 158ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 159ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 160ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Generate the argument list. 161ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek CallArgList Args; 162ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ObjCMethodDecl::param_const_iterator PI = MethodWithObjects->param_begin(); 163ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const ParmVarDecl *argDecl = *PI++; 164ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek QualType ArgQT = argDecl->getType().getUnqualifiedType(); 165ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Args.add(RValue::get(Objects), ArgQT); 166ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (DLE) { 167ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek argDecl = *PI++; 168ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ArgQT = argDecl->getType().getUnqualifiedType(); 169ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Args.add(RValue::get(Keys), ArgQT); 170ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 171ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek argDecl = *PI; 172ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ArgQT = argDecl->getType().getUnqualifiedType(); 173ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek llvm::Value *Count = 174ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek llvm::ConstantInt::get(CGM.getTypes().ConvertType(ArgQT), NumElements); 175ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Args.add(RValue::get(Count), ArgQT); 176ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 177ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Generate a reference to the class pointer, which will be the receiver. 178ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek Selector Sel = MethodWithObjects->getSelector(); 179ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek QualType ResultType = E->getType(); 180ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const ObjCObjectPointerType *InterfacePointerType 181ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek = ResultType->getAsObjCInterfacePointerType(); 182ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ObjCInterfaceDecl *Class 183ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek = InterfacePointerType->getObjectType()->getInterface(); 184ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 185bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *Receiver = Runtime.GetClass(*this, Class); 186ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 187ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // Generate the message send. 18816098f366097305c348b356a44638d6c959c6e60Eric Christopher RValue result 18916098f366097305c348b356a44638d6c959c6e60Eric Christopher = Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 19016098f366097305c348b356a44638d6c959c6e60Eric Christopher MethodWithObjects->getResultType(), 19116098f366097305c348b356a44638d6c959c6e60Eric Christopher Sel, 19216098f366097305c348b356a44638d6c959c6e60Eric Christopher Receiver, Args, Class, 19316098f366097305c348b356a44638d6c959c6e60Eric Christopher MethodWithObjects); 194527842f97b684831de2b1a455649131ed381ece4John McCall 195527842f97b684831de2b1a455649131ed381ece4John McCall // The above message send needs these objects, but in ARC they are 196527842f97b684831de2b1a455649131ed381ece4John McCall // passed in a buffer that is essentially __unsafe_unretained. 197527842f97b684831de2b1a455649131ed381ece4John McCall // Therefore we must prevent the optimizer from releasing them until 198527842f97b684831de2b1a455649131ed381ece4John McCall // after the call. 199527842f97b684831de2b1a455649131ed381ece4John McCall if (TrackNeededObjects) { 200527842f97b684831de2b1a455649131ed381ece4John McCall EmitARCIntrinsicUse(NeededObjects); 201527842f97b684831de2b1a455649131ed381ece4John McCall } 202527842f97b684831de2b1a455649131ed381ece4John McCall 203ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return Builder.CreateBitCast(result.getScalarVal(), 204ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek ConvertType(E->getType())); 205ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek} 206ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 207ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenekllvm::Value *CodeGenFunction::EmitObjCArrayLiteral(const ObjCArrayLiteral *E) { 208ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return EmitObjCCollectionLiteral(E, E->getArrayWithObjectsMethod()); 209ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek} 210ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 211ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenekllvm::Value *CodeGenFunction::EmitObjCDictionaryLiteral( 212ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek const ObjCDictionaryLiteral *E) { 213ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return EmitObjCCollectionLiteral(E, E->getDictWithObjectsMethod()); 214ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek} 215ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 2168fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner/// Emit a selector. 2178fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattnerllvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) { 2188fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // Untyped selector. 2198fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // Note that this implementation allows for non-constant strings to be passed 2208fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // as arguments to @selector(). Currently, the only thing preventing this 2218fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // behaviour is the type checking in the front end. 222bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall return CGM.getObjCRuntime().GetSelector(*this, E->getSelector()); 2238fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner} 2248fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner 225ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbarllvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) { 226ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbar // FIXME: This should pass the Decl not the name. 227bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall return CGM.getObjCRuntime().GenerateProtocolRef(*this, E->getProtocol()); 228ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbar} 2298fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner 230926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor/// \brief Adjust the type of the result of an Objective-C message send 231926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor/// expression when the method has a related result type. 232926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregorstatic RValue AdjustRelatedResultType(CodeGenFunction &CGF, 233490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian QualType ExpT, 234926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor const ObjCMethodDecl *Method, 235926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor RValue Result) { 236926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor if (!Method) 237926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor return Result; 238f85e193739c953358c865005855253af4f68a497John McCall 239926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor if (!Method->hasRelatedResultType() || 240490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian CGF.getContext().hasSameType(ExpT, Method->getResultType()) || 241926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor !Result.isScalar()) 242926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor return Result; 243926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor 244926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor // We have applied a related result type. Cast the rvalue appropriately. 245926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor return RValue::get(CGF.Builder.CreateBitCast(Result.getScalarVal(), 246490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian CGF.ConvertType(ExpT))); 247926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor} 2488fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner 249dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall/// Decide whether to extend the lifetime of the receiver of a 250dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall/// returns-inner-pointer message. 251dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCallstatic bool 252dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCallshouldExtendReceiverForInnerPointerMessage(const ObjCMessageExpr *message) { 253dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall switch (message->getReceiverKind()) { 254dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 255dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // For a normal instance message, we should extend unless the 256dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // receiver is loaded from a variable with precise lifetime. 257dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall case ObjCMessageExpr::Instance: { 258dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall const Expr *receiver = message->getInstanceReceiver(); 259dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(receiver); 260dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall if (!ice || ice->getCastKind() != CK_LValueToRValue) return true; 261dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall receiver = ice->getSubExpr()->IgnoreParens(); 262dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 263dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // Only __strong variables. 264dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall if (receiver->getType().getObjCLifetime() != Qualifiers::OCL_Strong) 265dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall return true; 266dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 267dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // All ivars and fields have precise lifetime. 268dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall if (isa<MemberExpr>(receiver) || isa<ObjCIvarRefExpr>(receiver)) 269dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall return false; 270dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 271dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // Otherwise, check for variables. 272dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall const DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(ice->getSubExpr()); 273dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall if (!declRef) return true; 274dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall const VarDecl *var = dyn_cast<VarDecl>(declRef->getDecl()); 275dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall if (!var) return true; 276dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 277dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // All variables have precise lifetime except local variables with 278dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // automatic storage duration that aren't specially marked. 279dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall return (var->hasLocalStorage() && 280dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall !var->hasAttr<ObjCPreciseLifetimeAttr>()); 281dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall } 282dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 283dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall case ObjCMessageExpr::Class: 284dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall case ObjCMessageExpr::SuperClass: 285dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // It's never necessary for class objects. 286dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall return false; 287dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 288dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall case ObjCMessageExpr::SuperInstance: 289dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // We generally assume that 'self' lives throughout a method call. 290dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall return false; 291dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall } 292dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 293dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall llvm_unreachable("invalid receiver kind"); 294dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall} 295dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 296ef072fd2f3347cfd857d6eb787b245b950771430John McCallRValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E, 297ef072fd2f3347cfd857d6eb787b245b950771430John McCall ReturnValueSlot Return) { 2988fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // Only the lookup mechanism and first two arguments of the method 2998fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // implementation vary between runtimes. We can get the receiver and 3008fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // arguments in generic code. 3011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 302f85e193739c953358c865005855253af4f68a497John McCall bool isDelegateInit = E->isDelegateInitCall(); 303f85e193739c953358c865005855253af4f68a497John McCall 304dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall const ObjCMethodDecl *method = E->getMethodDecl(); 3054e1524babb095e70de1da882573eb6fbee98a857Fariborz Jahanian 306f85e193739c953358c865005855253af4f68a497John McCall // We don't retain the receiver in delegate init calls, and this is 307f85e193739c953358c865005855253af4f68a497John McCall // safe because the receiver value is always loaded from 'self', 308f85e193739c953358c865005855253af4f68a497John McCall // which we zero out. We don't want to Block_copy block receivers, 309f85e193739c953358c865005855253af4f68a497John McCall // though. 310f85e193739c953358c865005855253af4f68a497John McCall bool retainSelf = 311f85e193739c953358c865005855253af4f68a497John McCall (!isDelegateInit && 3124e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie CGM.getLangOpts().ObjCAutoRefCount && 313dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall method && 314dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall method->hasAttr<NSConsumesSelfAttr>()); 315f85e193739c953358c865005855253af4f68a497John McCall 316208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 3178fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner bool isSuperMessage = false; 318f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar bool isClassMessage = false; 319c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall ObjCInterfaceDecl *OID = 0; 3208fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner // Find the receiver 321926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor QualType ReceiverType; 3220b647a6ea18151149d624ab373e6fe0e819e4a9aDaniel Dunbar llvm::Value *Receiver = 0; 32304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor switch (E->getReceiverKind()) { 32404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor case ObjCMessageExpr::Instance: 325926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor ReceiverType = E->getInstanceReceiver()->getType(); 326f85e193739c953358c865005855253af4f68a497John McCall if (retainSelf) { 327f85e193739c953358c865005855253af4f68a497John McCall TryEmitResult ter = tryEmitARCRetainScalarExpr(*this, 328f85e193739c953358c865005855253af4f68a497John McCall E->getInstanceReceiver()); 329f85e193739c953358c865005855253af4f68a497John McCall Receiver = ter.getPointer(); 330dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall if (ter.getInt()) retainSelf = false; 331f85e193739c953358c865005855253af4f68a497John McCall } else 332f85e193739c953358c865005855253af4f68a497John McCall Receiver = EmitScalarExpr(E->getInstanceReceiver()); 33304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor break; 3341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 33504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor case ObjCMessageExpr::Class: { 336926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor ReceiverType = E->getClassReceiver(); 337926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor const ObjCObjectType *ObjTy = ReceiverType->getAs<ObjCObjectType>(); 3383031c63f7b5b09d5f64609fa7a1922a05b520fa7John McCall assert(ObjTy && "Invalid Objective-C class message send"); 3393031c63f7b5b09d5f64609fa7a1922a05b520fa7John McCall OID = ObjTy->getInterface(); 3403031c63f7b5b09d5f64609fa7a1922a05b520fa7John McCall assert(OID && "Invalid Objective-C class message send"); 341bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall Receiver = Runtime.GetClass(*this, OID); 342f56f1913e91ad32bed52dd3f6afc26735d336584Daniel Dunbar isClassMessage = true; 34304badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor break; 34404badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor } 34504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor 34604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor case ObjCMessageExpr::SuperInstance: 347926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor ReceiverType = E->getSuperType(); 34804badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor Receiver = LoadObjCSelf(); 3498fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner isSuperMessage = true; 35004badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor break; 35104badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor 35204badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor case ObjCMessageExpr::SuperClass: 353926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor ReceiverType = E->getSuperType(); 3548fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner Receiver = LoadObjCSelf(); 35504badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor isSuperMessage = true; 35604badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor isClassMessage = true; 35704badcf84c8d504d8491c7c7e29b58f52cb16640Douglas Gregor break; 3588fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner } 3598fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner 360dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall if (retainSelf) 361dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall Receiver = EmitARCRetainNonBlock(Receiver); 362dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 363dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // In ARC, we sometimes want to "extend the lifetime" 364dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // (i.e. retain+autorelease) of receivers of returns-inner-pointer 365dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall // messages. 3664e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().ObjCAutoRefCount && method && 367dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall method->hasAttr<ObjCReturnsInnerPointerAttr>() && 368dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall shouldExtendReceiverForInnerPointerMessage(E)) 369dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall Receiver = EmitARCRetainAutorelease(ReceiverType, Receiver); 370dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall 371f85e193739c953358c865005855253af4f68a497John McCall QualType ResultType = 372dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall method ? method->getResultType() : E->getType(); 373f85e193739c953358c865005855253af4f68a497John McCall 37419cd87eb5fb3c197e631ce08fd52c446c4d4e8f1Daniel Dunbar CallArgList Args; 375dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall EmitCallArgs(Args, method, E->arg_begin(), E->arg_end()); 3761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 377f85e193739c953358c865005855253af4f68a497John McCall // For delegate init calls in ARC, do an unsafe store of null into 378f85e193739c953358c865005855253af4f68a497John McCall // self. This represents the call taking direct ownership of that 379f85e193739c953358c865005855253af4f68a497John McCall // value. We have to do this after emitting the other call 380f85e193739c953358c865005855253af4f68a497John McCall // arguments because they might also reference self, but we don't 381f85e193739c953358c865005855253af4f68a497John McCall // have to worry about any of them modifying self because that would 382f85e193739c953358c865005855253af4f68a497John McCall // be an undefined read and write of an object in unordered 383f85e193739c953358c865005855253af4f68a497John McCall // expressions. 384f85e193739c953358c865005855253af4f68a497John McCall if (isDelegateInit) { 3854e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie assert(getLangOpts().ObjCAutoRefCount && 386f85e193739c953358c865005855253af4f68a497John McCall "delegate init calls should only be marked in ARC"); 387f85e193739c953358c865005855253af4f68a497John McCall 388f85e193739c953358c865005855253af4f68a497John McCall // Do an unsafe store of null into self. 389f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *selfAddr = 390f85e193739c953358c865005855253af4f68a497John McCall LocalDeclMap[cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()]; 391f85e193739c953358c865005855253af4f68a497John McCall assert(selfAddr && "no self entry for a delegate init call?"); 392f85e193739c953358c865005855253af4f68a497John McCall 393f85e193739c953358c865005855253af4f68a497John McCall Builder.CreateStore(getNullForVariable(selfAddr), selfAddr); 394f85e193739c953358c865005855253af4f68a497John McCall } 3957e70fb217dcdf96faf34df3e197c3831c86f8089Anders Carlsson 396926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor RValue result; 3978fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner if (isSuperMessage) { 3989384c768e93f270118a30ce96546083a666da284Chris Lattner // super is only valid in an Objective-C method 3999384c768e93f270118a30ce96546083a666da284Chris Lattner const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); 4007ce77920a35060f1c8dd72e541e42ce296ccd168Fariborz Jahanian bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext()); 401926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor result = Runtime.GenerateMessageSendSuper(*this, Return, ResultType, 402926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor E->getSelector(), 403926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor OMD->getClassInterface(), 404926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor isCategoryImpl, 405926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor Receiver, 406926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor isClassMessage, 407926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor Args, 408dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall method); 409926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor } else { 410926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor result = Runtime.GenerateMessageSend(*this, Return, ResultType, 411926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor E->getSelector(), 412926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor Receiver, Args, OID, 413dc7c5ad7a15914b7ae24f31f18a20ad2f8ecd0bcJohn McCall method); 4148fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner } 415f85e193739c953358c865005855253af4f68a497John McCall 416f85e193739c953358c865005855253af4f68a497John McCall // For delegate init calls in ARC, implicitly store the result of 417f85e193739c953358c865005855253af4f68a497John McCall // the call back into self. This takes ownership of the value. 418f85e193739c953358c865005855253af4f68a497John McCall if (isDelegateInit) { 419f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *selfAddr = 420f85e193739c953358c865005855253af4f68a497John McCall LocalDeclMap[cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()]; 421f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *newSelf = result.getScalarVal(); 422f85e193739c953358c865005855253af4f68a497John McCall 423f85e193739c953358c865005855253af4f68a497John McCall // The delegate return type isn't necessarily a matching type; in 424f85e193739c953358c865005855253af4f68a497John McCall // fact, it's quite likely to be 'id'. 4252acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *selfTy = 426f85e193739c953358c865005855253af4f68a497John McCall cast<llvm::PointerType>(selfAddr->getType())->getElementType(); 427f85e193739c953358c865005855253af4f68a497John McCall newSelf = Builder.CreateBitCast(newSelf, selfTy); 428f85e193739c953358c865005855253af4f68a497John McCall 429f85e193739c953358c865005855253af4f68a497John McCall Builder.CreateStore(newSelf, selfAddr); 430f85e193739c953358c865005855253af4f68a497John McCall } 4314e1524babb095e70de1da882573eb6fbee98a857Fariborz Jahanian 432490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian return AdjustRelatedResultType(*this, E->getType(), method, result); 4335508518a2702b00be3b15a26d772bde968972f54Anders Carlsson} 4345508518a2702b00be3b15a26d772bde968972f54Anders Carlsson 435f85e193739c953358c865005855253af4f68a497John McCallnamespace { 436f85e193739c953358c865005855253af4f68a497John McCallstruct FinishARCDealloc : EHScopeStack::Cleanup { 437ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall void Emit(CodeGenFunction &CGF, Flags flags) { 438f85e193739c953358c865005855253af4f68a497John McCall const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CGF.CurCodeDecl); 439799d34e9505a833549c71f2ac5f842da157ea031John McCall 440799d34e9505a833549c71f2ac5f842da157ea031John McCall const ObjCImplDecl *impl = cast<ObjCImplDecl>(method->getDeclContext()); 441f85e193739c953358c865005855253af4f68a497John McCall const ObjCInterfaceDecl *iface = impl->getClassInterface(); 442f85e193739c953358c865005855253af4f68a497John McCall if (!iface->getSuperClass()) return; 443f85e193739c953358c865005855253af4f68a497John McCall 444799d34e9505a833549c71f2ac5f842da157ea031John McCall bool isCategory = isa<ObjCCategoryImplDecl>(impl); 445799d34e9505a833549c71f2ac5f842da157ea031John McCall 446f85e193739c953358c865005855253af4f68a497John McCall // Call [super dealloc] if we have a superclass. 447f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *self = CGF.LoadObjCSelf(); 448f85e193739c953358c865005855253af4f68a497John McCall 449f85e193739c953358c865005855253af4f68a497John McCall CallArgList args; 450f85e193739c953358c865005855253af4f68a497John McCall CGF.CGM.getObjCRuntime().GenerateMessageSendSuper(CGF, ReturnValueSlot(), 451f85e193739c953358c865005855253af4f68a497John McCall CGF.getContext().VoidTy, 452f85e193739c953358c865005855253af4f68a497John McCall method->getSelector(), 453f85e193739c953358c865005855253af4f68a497John McCall iface, 454799d34e9505a833549c71f2ac5f842da157ea031John McCall isCategory, 455f85e193739c953358c865005855253af4f68a497John McCall self, 456f85e193739c953358c865005855253af4f68a497John McCall /*is class msg*/ false, 457f85e193739c953358c865005855253af4f68a497John McCall args, 458f85e193739c953358c865005855253af4f68a497John McCall method); 459f85e193739c953358c865005855253af4f68a497John McCall } 460f85e193739c953358c865005855253af4f68a497John McCall}; 461f85e193739c953358c865005855253af4f68a497John McCall} 462f85e193739c953358c865005855253af4f68a497John McCall 463af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// StartObjCMethod - Begin emission of an ObjCMethod. This generates 464af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// the LLVM function and sets the other context used by 465af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// CodeGenFunction. 466679a502d462ef819e6175b58e255ca3f3391e7cfFariborz Jahanianvoid CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD, 4678d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel const ObjCContainerDecl *CD, 4688d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel SourceLocation StartLoc) { 469d26bc76c98006609002d9930f8840490e88ac5b5John McCall FunctionArgList args; 4704800ea6ff8017cf803c32a5fd63b94c0614014e3Devang Patel // Check if we should generate debug info for this method. 471c3030bc285d90a139fb74629daadef5617283203David Blaikie if (OMD->hasAttr<NoDebugAttr>()) 472c3030bc285d90a139fb74629daadef5617283203David Blaikie DebugInfo = NULL; // disable debug info indefinitely for this function 4734800ea6ff8017cf803c32a5fd63b94c0614014e3Devang Patel 474679a502d462ef819e6175b58e255ca3f3391e7cfFariborz Jahanian llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD); 475f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 476de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall const CGFunctionInfo &FI = CGM.getTypes().arrangeObjCMethodDeclaration(OMD); 4770e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar CGM.SetInternalFunctionAttributes(OMD, Fn, FI); 4784111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner 479d26bc76c98006609002d9930f8840490e88ac5b5John McCall args.push_back(OMD->getSelfDecl()); 480d26bc76c98006609002d9930f8840490e88ac5b5John McCall args.push_back(OMD->getCmdDecl()); 4814111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner 482491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis for (ObjCMethodDecl::param_const_iterator PI = OMD->param_begin(), 48316098f366097305c348b356a44638d6c959c6e60Eric Christopher E = OMD->param_end(); PI != E; ++PI) 484d26bc76c98006609002d9930f8840490e88ac5b5John McCall args.push_back(*PI); 485b7ec246872b412f0e7bb9e93eacfd78cfa6adfb3Daniel Dunbar 48614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne CurGD = OMD; 48714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne 4888d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel StartFunction(OMD, OMD->getResultType(), Fn, FI, args, StartLoc); 489f85e193739c953358c865005855253af4f68a497John McCall 490f85e193739c953358c865005855253af4f68a497John McCall // In ARC, certain methods get an extra cleanup. 4914e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().ObjCAutoRefCount && 492f85e193739c953358c865005855253af4f68a497John McCall OMD->isInstanceMethod() && 493f85e193739c953358c865005855253af4f68a497John McCall OMD->getSelector().isUnarySelector()) { 494f85e193739c953358c865005855253af4f68a497John McCall const IdentifierInfo *ident = 495f85e193739c953358c865005855253af4f68a497John McCall OMD->getSelector().getIdentifierInfoForSlot(0); 496f85e193739c953358c865005855253af4f68a497John McCall if (ident->isStr("dealloc")) 497f85e193739c953358c865005855253af4f68a497John McCall EHStack.pushCleanup<FinishARCDealloc>(getARCCleanupKind()); 498f85e193739c953358c865005855253af4f68a497John McCall } 499af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar} 500af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 501f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF, 502f85e193739c953358c865005855253af4f68a497John McCall LValue lvalue, QualType type); 503f85e193739c953358c865005855253af4f68a497John McCall 504af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// Generate an Objective-C method. An Objective-C method is a C function with 5051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// its pointer, name, and types registered in the class struture. 506af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) { 5078d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel StartObjCMethod(OMD, OMD->getClassInterface(), OMD->getLocStart()); 5086fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis EmitStmt(OMD->getBody()); 5096fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis FinishFunction(OMD->getBodyRBrace()); 510af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar} 511af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 51241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall/// emitStructGetterCall - Call the runtime function to load a property 51341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall/// into the return value slot. 51441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCallstatic void emitStructGetterCall(CodeGenFunction &CGF, ObjCIvarDecl *ivar, 51541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall bool isAtomic, bool hasStrong) { 51641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall ASTContext &Context = CGF.getContext(); 51741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 51841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall llvm::Value *src = 51941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(), 52041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall ivar, 0).getAddress(); 52141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 52241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall // objc_copyStruct (ReturnValue, &structIvar, 52341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall // sizeof (Type of Ivar), isAtomic, false); 52441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall CallArgList args; 52541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 52641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall llvm::Value *dest = CGF.Builder.CreateBitCast(CGF.ReturnValue, CGF.VoidPtrTy); 52741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(dest), Context.VoidPtrTy); 52841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 52941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall src = CGF.Builder.CreateBitCast(src, CGF.VoidPtrTy); 53041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(src), Context.VoidPtrTy); 53141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 53241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall CharUnits size = CGF.getContext().getTypeSizeInChars(ivar->getType()); 53341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(CGF.CGM.getSize(size)), Context.getSizeType()); 53441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(CGF.Builder.getInt1(isAtomic)), Context.BoolTy); 53541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(CGF.Builder.getInt1(hasStrong)), Context.BoolTy); 53641bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 53741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall llvm::Value *fn = CGF.CGM.getObjCRuntime().GetGetStructFunction(); 5380f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(Context.VoidTy, args, 5390f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 5400f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 54141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall fn, ReturnValueSlot(), args); 54241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall} 54341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 5441e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// Determine whether the given architecture supports unaligned atomic 5451e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// accesses. They don't have to be fast, just faster than a function 5461e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// call and a mutex. 5471e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallstatic bool hasUnalignedAtomics(llvm::Triple::ArchType arch) { 548de24d44665486e98df2aeb2ef5bbc163abfe7981Eli Friedman // FIXME: Allow unaligned atomic load/store on x86. (It is not 549de24d44665486e98df2aeb2ef5bbc163abfe7981Eli Friedman // currently supported by the backend.) 550de24d44665486e98df2aeb2ef5bbc163abfe7981Eli Friedman return 0; 5511e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall} 5521e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5531e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// Return the maximum size that permits atomic accesses for the given 5541e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall/// architecture. 5551e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallstatic CharUnits getMaxAtomicAccessSize(CodeGenModule &CGM, 5561e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Triple::ArchType arch) { 5571e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // ARM has 8-byte atomic accesses, but it's not clear whether we 5581e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // want to rely on them here. 5591e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5601e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // In the default case, just assume that any size up to a pointer is 5611e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // fine given adequate alignment. 5621e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return CharUnits::fromQuantity(CGM.PointerSizeInBytes); 5631e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall} 5641e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5651e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallnamespace { 5661e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall class PropertyImplStrategy { 5671e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall public: 5681e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall enum StrategyKind { 5691e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// The 'native' strategy is to use the architecture's provided 5701e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// reads and writes. 5711e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Native, 5721e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5731e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// Use objc_setProperty and objc_getProperty. 5741e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall GetSetProperty, 5751e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5761e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// Use objc_setProperty for the setter, but use expression 5771e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// evaluation for the getter. 5781e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall SetPropertyAndExpressionGet, 5791e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5801e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// Use objc_copyStruct. 5811e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CopyStruct, 5821e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5831e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// The 'expression' strategy is to emit normal assignment or 5841e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall /// lvalue-to-rvalue expressions. 5851e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Expression 5861e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall }; 5871e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5881e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall StrategyKind getKind() const { return StrategyKind(Kind); } 5891e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5901e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall bool hasStrongMember() const { return HasStrong; } 5911e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall bool isAtomic() const { return IsAtomic; } 5921e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall bool isCopy() const { return IsCopy; } 5931e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5941e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CharUnits getIvarSize() const { return IvarSize; } 5951e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CharUnits getIvarAlignment() const { return IvarAlignment; } 5961e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 5971e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall PropertyImplStrategy(CodeGenModule &CGM, 5981e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall const ObjCPropertyImplDecl *propImpl); 5991e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6001e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall private: 6011e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall unsigned Kind : 8; 6021e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall unsigned IsAtomic : 1; 6031e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall unsigned IsCopy : 1; 6041e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall unsigned HasStrong : 1; 6051e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6061e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CharUnits IvarSize; 6071e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CharUnits IvarAlignment; 6081e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall }; 6091e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall} 6101e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 611bed28ac1d1463adca3ecf24fca5c30646fa9dbb2Sylvestre Ledru/// Pick an implementation strategy for the given property synthesis. 6121e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallPropertyImplStrategy::PropertyImplStrategy(CodeGenModule &CGM, 6131e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall const ObjCPropertyImplDecl *propImpl) { 6141e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall const ObjCPropertyDecl *prop = propImpl->getPropertyDecl(); 615265941bc308d65cc270d5c4de5806f37ce405606John McCall ObjCPropertyDecl::SetterKind setterKind = prop->getSetterKind(); 6161e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 617265941bc308d65cc270d5c4de5806f37ce405606John McCall IsCopy = (setterKind == ObjCPropertyDecl::Copy); 618265941bc308d65cc270d5c4de5806f37ce405606John McCall IsAtomic = prop->isAtomic(); 6191e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall HasStrong = false; // doesn't matter here. 6201e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6211e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Evaluate the ivar's size and alignment. 6221e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); 6231e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall QualType ivarType = ivar->getType(); 6241e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::tie(IvarSize, IvarAlignment) 6251e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall = CGM.getContext().getTypeInfoInChars(ivarType); 6261e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6271e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // If we have a copy property, we always have to use getProperty/setProperty. 628265941bc308d65cc270d5c4de5806f37ce405606John McCall // TODO: we could actually use setProperty and an expression for non-atomics. 6291e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (IsCopy) { 6301e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = GetSetProperty; 6311e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 6321e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 6331e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 634265941bc308d65cc270d5c4de5806f37ce405606John McCall // Handle retain. 635265941bc308d65cc270d5c4de5806f37ce405606John McCall if (setterKind == ObjCPropertyDecl::Retain) { 6361e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // In GC-only, there's nothing special that needs to be done. 6374e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) { 6381e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // fallthrough 6391e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6401e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // In ARC, if the property is non-atomic, use expression emission, 6411e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // which translates to objc_storeStrong. This isn't required, but 6421e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // it's slightly nicer. 6434e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie } else if (CGM.getLangOpts().ObjCAutoRefCount && !IsAtomic) { 644d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall // Using standard expression emission for the setter is only 645d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall // acceptable if the ivar is __strong, which won't be true if 646d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall // the property is annotated with __attribute__((NSObject)). 647d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall // TODO: falling all the way back to objc_setProperty here is 648d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall // just laziness, though; we could still use objc_storeStrong 649d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall // if we hacked it right. 650d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall if (ivarType.getObjCLifetime() == Qualifiers::OCL_Strong) 651d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall Kind = Expression; 652d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall else 653d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall Kind = SetPropertyAndExpressionGet; 6541e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 6551e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6561e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Otherwise, we need to at least use setProperty. However, if 6571e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // the property isn't atomic, we can use normal expression 6581e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // emission for the getter. 6591e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } else if (!IsAtomic) { 6601e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = SetPropertyAndExpressionGet; 6611e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 6621e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6631e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Otherwise, we have to use both setProperty and getProperty. 6641e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } else { 6651e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = GetSetProperty; 6661e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 6671e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 6681e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 6691e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6701e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // If we're not atomic, just use expression accesses. 6711e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (!IsAtomic) { 6721e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = Expression; 6731e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 6741e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 6751e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6765889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall // Properties on bitfield ivars need to be emitted using expression 6775889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall // accesses even if they're nominally atomic. 6785889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall if (ivar->isBitField()) { 6795889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall Kind = Expression; 6805889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall return; 6815889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall } 6825889c60d19101156a3a54b8c49bcc60a12cc1fdbJohn McCall 6831e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // GC-qualified or ARC-qualified ivars need to be emitted as 6841e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // expressions. This actually works out to being atomic anyway, 6851e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // except for ARC __strong, but that should trigger the above code. 6861e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (ivarType.hasNonTrivialObjCLifetime() || 6874e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie (CGM.getLangOpts().getGC() && 6881e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CGM.getContext().getObjCGCAttrKind(ivarType))) { 6891e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = Expression; 6901e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 6911e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 6921e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6931e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Compute whether the ivar has strong members. 6944e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().getGC()) 6951e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (const RecordType *recordType = ivarType->getAs<RecordType>()) 6961e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall HasStrong = recordType->getDecl()->hasObjectMember(); 6971e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 6981e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // We can never access structs with object members with a native 6991e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // access, because we need to use write barriers. This is what 7001e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // objc_copyStruct is for. 7011e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (HasStrong) { 7021e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = CopyStruct; 7031e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 7041e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 7051e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7061e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Otherwise, this is target-dependent and based on the size and 7071e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // alignment of the ivar. 708c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall 709c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall // If the size of the ivar is not a power of two, give up. We don't 710c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall // want to get into the business of doing compare-and-swaps. 711c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall if (!IvarSize.isPowerOfTwo()) { 712c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall Kind = CopyStruct; 713c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall return; 714c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall } 715c5d9a90b3a3c16324e0cceeccec3d2993888deb6John McCall 7161e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Triple::ArchType arch = 71764aa4b3ec7e62288e2e66c1935487ece995ca94bJohn McCall CGM.getTarget().getTriple().getArch(); 7181e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7191e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Most architectures require memory to fit within a single cache 7201e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // line, so the alignment has to be at least the size of the access. 7211e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Otherwise we have to grab a lock. 7221e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (IvarAlignment < IvarSize && !hasUnalignedAtomics(arch)) { 7231e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = CopyStruct; 7241e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 7251e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 7261e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7271e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // If the ivar's size exceeds the architecture's maximum atomic 7281e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // access size, we have to use CopyStruct. 7291e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (IvarSize > getMaxAtomicAccessSize(CGM, arch)) { 7301e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = CopyStruct; 7311e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 7321e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 7331e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7341e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Otherwise, we can use native loads and stores. 7351e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Kind = Native; 7361e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall} 737af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 7382ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// \brief Generate an Objective-C property getter function. 7392ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// 7402ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// The given Decl must be an ObjCImplementationDecl. \@synthesize 741489034cf8bde09360e0089f401b2929597b125d8Steve Naroff/// is illegal within a category. 742fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanianvoid CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP, 743fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian const ObjCPropertyImplDecl *PID) { 744b6e5fe3cae9bf456f084c555c3ecc256f4181cc5Fariborz Jahanian llvm::Constant *AtomicHelperFn = 74520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian GenerateObjCAtomicGetterCopyHelperFunction(PID); 746af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 747af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCMethodDecl *OMD = PD->getGetterMethodDecl(); 748af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar assert(OMD && "Invalid call to generate getter (empty method)"); 749ea32047660159811a4c14d008a4b7e3807a705d6Eric Christopher StartObjCMethod(OMD, IMP->getClassInterface(), OMD->getLocStart()); 7501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 751490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian generateObjCGetterBody(IMP, PID, OMD, AtomicHelperFn); 7521e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7531e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall FinishFunction(); 7541e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall} 7551e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7566c11f0b3106263600af2ea0438ebb372821514aaJohn McCallstatic bool hasTrivialGetExpr(const ObjCPropertyImplDecl *propImpl) { 7576c11f0b3106263600af2ea0438ebb372821514aaJohn McCall const Expr *getter = propImpl->getGetterCXXConstructor(); 7581e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (!getter) return true; 7591e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7601e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Sema only makes only of these when the ivar has a C++ class type, 7611e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // so the form is pretty constrained. 7621e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7636c11f0b3106263600af2ea0438ebb372821514aaJohn McCall // If the property has a reference type, we might just be binding a 7646c11f0b3106263600af2ea0438ebb372821514aaJohn McCall // reference, in which case the result will be a gl-value. We should 7656c11f0b3106263600af2ea0438ebb372821514aaJohn McCall // treat this as a non-trivial operation. 7666c11f0b3106263600af2ea0438ebb372821514aaJohn McCall if (getter->isGLValue()) 7676c11f0b3106263600af2ea0438ebb372821514aaJohn McCall return false; 7686c11f0b3106263600af2ea0438ebb372821514aaJohn McCall 7691e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // If we selected a trivial copy-constructor, we're okay. 7701e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (const CXXConstructExpr *construct = dyn_cast<CXXConstructExpr>(getter)) 7711e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return (construct->getConstructor()->isTrivial()); 7721e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 7731e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // The constructor might require cleanups (in which case it's never 7741e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // trivial). 7751e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall assert(isa<ExprWithCleanups>(getter)); 7761e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return false; 7771e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall} 7781e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 77920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian/// emitCPPObjectAtomicGetterCall - Call the runtime function to 78020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian/// copy the ivar into the resturn slot. 78120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanianstatic void emitCPPObjectAtomicGetterCall(CodeGenFunction &CGF, 78220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Value *returnAddr, 78320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ObjCIvarDecl *ivar, 78420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Constant *AtomicHelperFn) { 78520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian // objc_copyCppObjectAtomic (&returnSlot, &CppObjectIvar, 78620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian // AtomicHelperFn); 78720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CallArgList args; 78820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 78920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian // The 1st argument is the return Slot. 79020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian args.add(RValue::get(returnAddr), CGF.getContext().VoidPtrTy); 79120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 79220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian // The 2nd argument is the address of the ivar. 79320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Value *ivarAddr = 79420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), 79520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CGF.LoadObjCSelf(), ivar, 0).getAddress(); 79620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy); 79720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy); 79820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 79920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian // Third argument is the helper function. 80020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy); 80120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 80220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Value *copyCppAtomicObjectFn = 803d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall CGF.CGM.getObjCRuntime().GetCppAtomicObjectGetFunction(); 8040f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(CGF.getContext().VoidTy, 8050f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall args, 8060f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 8070f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 80820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian copyCppAtomicObjectFn, ReturnValueSlot(), args); 80920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian} 81020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 8111e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallvoid 8121e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallCodeGenFunction::generateObjCGetterBody(const ObjCImplementationDecl *classImpl, 813b6e5fe3cae9bf456f084c555c3ecc256f4181cc5Fariborz Jahanian const ObjCPropertyImplDecl *propImpl, 814490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian const ObjCMethodDecl *GetterMethodDecl, 815b6e5fe3cae9bf456f084c555c3ecc256f4181cc5Fariborz Jahanian llvm::Constant *AtomicHelperFn) { 8161e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // If there's a non-trivial 'get' expression, we just have to emit that. 8171e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (!hasTrivialGetExpr(propImpl)) { 81820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if (!AtomicHelperFn) { 81920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ReturnStmt ret(SourceLocation(), propImpl->getGetterCXXConstructor(), 82020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian /*nrvo*/ 0); 82120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian EmitReturnStmt(ret); 82220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian } 82320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian else { 82420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); 82520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian emitCPPObjectAtomicGetterCall(*this, ReturnValue, 82620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ivar, AtomicHelperFn); 82720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian } 8281e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 8291e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 8301e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8311e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall const ObjCPropertyDecl *prop = propImpl->getPropertyDecl(); 8321e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall QualType propType = prop->getType(); 8331e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall ObjCMethodDecl *getterMethod = prop->getGetterMethodDecl(); 8341e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8351e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); 8361e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8371e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Pick an implementation strategy. 8381e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall PropertyImplStrategy strategy(CGM, propImpl); 8391e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall switch (strategy.getKind()) { 8401e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::Native: { 841aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman // We don't need to do anything for a zero-size struct. 842aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman if (strategy.getIvarSize().isZero()) 843aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman return; 844aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman 8451e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0); 8461e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8471e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Currently, all atomic accesses have to be through integer 8481e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // types, so there's no point in trying to pick a prettier type. 8491e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Type *bitcastType = 8501e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Type::getIntNTy(getLLVMContext(), 8511e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall getContext().toBits(strategy.getIvarSize())); 8521e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall bitcastType = bitcastType->getPointerTo(); // addrspace 0 okay 8531e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8541e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Perform an atomic load. This does not impose ordering constraints. 8551e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *ivarAddr = LV.getAddress(); 8561e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall ivarAddr = Builder.CreateBitCast(ivarAddr, bitcastType); 8571e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::LoadInst *load = Builder.CreateLoad(ivarAddr, "load"); 8581e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall load->setAlignment(strategy.getIvarAlignment().getQuantity()); 8591e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall load->setAtomic(llvm::Unordered); 8601e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8611e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Store that value into the return address. Doing this with a 8621e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // bitcast is likely to produce some pretty ugly IR, but it's not 8631e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // the *most* terrible thing in the world. 8641e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Builder.CreateStore(load, Builder.CreateBitCast(ReturnValue, bitcastType)); 8651e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8661e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Make sure we don't do an autorelease. 8671e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall AutoreleaseResult = false; 8681e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 8691e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 8701e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8711e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::GetSetProperty: { 8721e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *getPropertyFn = 8731e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CGM.getObjCRuntime().GetPropertyGetFunction(); 8741e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (!getPropertyFn) { 8751e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CGM.ErrorUnsupported(propImpl, "Obj-C getter requiring atomic copy"); 876c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar return; 877c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar } 878c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar 879c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true). 880c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar // FIXME: Can't this be simpler? This might even be worse than the 881c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar // corresponding gcc code. 8821e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *cmd = 8831e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Builder.CreateLoad(LocalDeclMap[getterMethod->getCmdDecl()], "cmd"); 8841e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *self = Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy); 8851e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *ivarOffset = 8861e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall EmitIvarOffset(classImpl->getClassInterface(), ivar); 8871e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 8881e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall CallArgList args; 8891e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall args.add(RValue::get(self), getContext().getObjCIdType()); 8901e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall args.add(RValue::get(cmd), getContext().getObjCSelType()); 8911e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall args.add(RValue::get(ivarOffset), getContext().getPointerDiffType()); 892265941bc308d65cc270d5c4de5806f37ce405606John McCall args.add(RValue::get(Builder.getInt1(strategy.isAtomic())), 893265941bc308d65cc270d5c4de5806f37ce405606John McCall getContext().BoolTy); 8941e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 895e4be5a66072f7c7618071284c8d2a9c6d8e691cfDaniel Dunbar // FIXME: We shouldn't need to get the function info here, the 896e4be5a66072f7c7618071284c8d2a9c6d8e691cfDaniel Dunbar // runtime already should have computed it to build the function. 8970f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RValue RV = EmitCall(getTypes().arrangeFreeFunctionCall(propType, args, 8980f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 8990f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 9001e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall getPropertyFn, ReturnValueSlot(), args); 9011e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 902c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar // We need to fix the type here. Ivars with copy & retain are 903c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar // always objects so we don't need to worry about complex or 904c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar // aggregates. 9051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(), 90652c18b0933006acaf5c865f6e86a4694b5f288e7Fariborz Jahanian getTypes().ConvertType(getterMethod->getResultType()))); 9071e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 9081e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall EmitReturnOfRValue(RV, propType); 909f85e193739c953358c865005855253af4f68a497John McCall 910f85e193739c953358c865005855253af4f68a497John McCall // objc_getProperty does an autorelease, so we should suppress ours. 911f85e193739c953358c865005855253af4f68a497John McCall AutoreleaseResult = false; 9121e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 9131e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 9141e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 9151e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 9161e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::CopyStruct: 9171e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall emitStructGetterCall(*this, ivar, strategy.isAtomic(), 9181e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall strategy.hasStrongMember()); 9191e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 9201e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 9211e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::Expression: 9221e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::SetPropertyAndExpressionGet: { 9231e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0); 9241e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 9251e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall QualType ivarType = ivar->getType(); 9269d232c884ea9872d6555df0fd7359699819bc1f1John McCall switch (getEvaluationKind(ivarType)) { 9279d232c884ea9872d6555df0fd7359699819bc1f1John McCall case TEK_Complex: { 9284ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky ComplexPairTy pair = EmitLoadOfComplex(LV, SourceLocation()); 9299d232c884ea9872d6555df0fd7359699819bc1f1John McCall EmitStoreOfComplex(pair, 9309d232c884ea9872d6555df0fd7359699819bc1f1John McCall MakeNaturalAlignAddrLValue(ReturnValue, ivarType), 9319d232c884ea9872d6555df0fd7359699819bc1f1John McCall /*init*/ true); 9329d232c884ea9872d6555df0fd7359699819bc1f1John McCall return; 9339d232c884ea9872d6555df0fd7359699819bc1f1John McCall } 9349d232c884ea9872d6555df0fd7359699819bc1f1John McCall case TEK_Aggregate: 9351e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // The return value slot is guaranteed to not be aliased, but 9361e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // that's not necessarily the same as "on the stack", so 9371e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // we still potentially need objc_memmove_collectable. 938649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier EmitAggregateCopy(ReturnValue, LV.getAddress(), ivarType); 9399d232c884ea9872d6555df0fd7359699819bc1f1John McCall return; 9409d232c884ea9872d6555df0fd7359699819bc1f1John McCall case TEK_Scalar: { 941ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall llvm::Value *value; 942ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall if (propType->isReferenceType()) { 943ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall value = LV.getAddress(); 944ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall } else { 945ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall // We want to load and autoreleaseReturnValue ARC __weak ivars. 946ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak) { 9471e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall value = emitARCRetainLoadOfScalar(*this, LV, ivarType); 948ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall 949ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall // Otherwise we want to do a simple load, suppressing the 950ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall // final autorelease. 951f85e193739c953358c865005855253af4f68a497John McCall } else { 9524ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky value = EmitLoadOfLValue(LV, SourceLocation()).getScalarVal(); 953ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall AutoreleaseResult = false; 95414086764e340267e17803d0f8243070ffae2c76eFariborz Jahanian } 955f85e193739c953358c865005855253af4f68a497John McCall 956ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall value = Builder.CreateBitCast(value, ConvertType(propType)); 957490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian value = Builder.CreateBitCast(value, 958490a52b4947381879a47b4251db5fb81cdf5d02bFariborz Jahanian ConvertType(GetterMethodDecl->getResultType())); 959ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall } 960ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall 961ba3dd902d1cde09776a50c1adf2cd40bf0a15a7fJohn McCall EmitReturnOfRValue(RValue::get(value), propType); 9629d232c884ea9872d6555df0fd7359699819bc1f1John McCall return; 963ed1d29d62595a83ccf6ef23eb2759d355206df2eFariborz Jahanian } 9649d232c884ea9872d6555df0fd7359699819bc1f1John McCall } 9659d232c884ea9872d6555df0fd7359699819bc1f1John McCall llvm_unreachable("bad evaluation kind"); 966c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar } 967af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 9681e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 9691e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm_unreachable("bad @property implementation strategy!"); 970af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar} 971af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 97241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall/// emitStructSetterCall - Call the runtime function to store the value 97341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall/// from the first formal parameter into the given ivar. 97441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCallstatic void emitStructSetterCall(CodeGenFunction &CGF, ObjCMethodDecl *OMD, 97541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall ObjCIvarDecl *ivar) { 9762846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian // objc_copyStruct (&structIvar, &Arg, 9772846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian // sizeof (struct something), true, false); 978bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall CallArgList args; 979bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall 980bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall // The first argument is the address of the ivar. 98141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall llvm::Value *ivarAddr = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), 98241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall CGF.LoadObjCSelf(), ivar, 0) 98341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall .getAddress(); 98441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy); 98541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy); 986bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall 987bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall // The second argument is the address of the parameter variable. 98841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall ParmVarDecl *argVar = *OMD->param_begin(); 989f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr argRef(argVar, false, argVar->getType().getNonReferenceType(), 990c3953aab1b6167b7f394ca87096e91ab840026adFariborz Jahanian VK_LValue, SourceLocation()); 99141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall llvm::Value *argAddr = CGF.EmitLValue(&argRef).getAddress(); 99241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy); 99341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy); 994bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall 995bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall // The third argument is the sizeof the type. 996bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall llvm::Value *size = 99741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall CGF.CGM.getSize(CGF.getContext().getTypeSizeInChars(ivar->getType())); 99841bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(size), CGF.getContext().getSizeType()); 99941bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall 100041bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall // The fourth argument is the 'isAtomic' flag. 100141bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(CGF.Builder.getTrue()), CGF.getContext().BoolTy); 1002bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall 100341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall // The fifth argument is the 'hasStrong' flag. 100441bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall // FIXME: should this really always be false? 100541bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall args.add(RValue::get(CGF.Builder.getFalse()), CGF.getContext().BoolTy); 1006bbb253c307bc98fea5af9c9e59003e1c0da4cdc2John McCall 100741bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall llvm::Value *copyStructFn = CGF.CGM.getObjCRuntime().GetSetStructFunction(); 10080f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(CGF.getContext().VoidTy, 10090f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall args, 10100f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 10110f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 101241bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall copyStructFn, ReturnValueSlot(), args); 10132846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian} 10142846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian 1015cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian/// emitCPPObjectAtomicSetterCall - Call the runtime function to store 1016cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian/// the value from the first formal parameter into the given ivar, using 1017cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian/// the Cpp API for atomic Cpp objects with non-trivial copy assignment. 1018cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanianstatic void emitCPPObjectAtomicSetterCall(CodeGenFunction &CGF, 1019cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ObjCMethodDecl *OMD, 1020cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ObjCIvarDecl *ivar, 1021cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian llvm::Constant *AtomicHelperFn) { 1022cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // objc_copyCppObjectAtomic (&CppObjectIvar, &Arg, 1023cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // AtomicHelperFn); 1024cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian CallArgList args; 1025cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 1026cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // The first argument is the address of the ivar. 1027cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian llvm::Value *ivarAddr = 1028cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), 1029cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian CGF.LoadObjCSelf(), ivar, 0).getAddress(); 1030cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy); 1031cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy); 1032cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 1033cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // The second argument is the address of the parameter variable. 1034cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ParmVarDecl *argVar = *OMD->param_begin(); 1035f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr argRef(argVar, false, argVar->getType().getNonReferenceType(), 1036cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian VK_LValue, SourceLocation()); 1037cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian llvm::Value *argAddr = CGF.EmitLValue(&argRef).getAddress(); 1038cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy); 1039cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy); 1040cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 1041cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // Third argument is the helper function. 1042cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy); 1043cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 1044cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian llvm::Value *copyCppAtomicObjectFn = 1045d397cfef01d49a41554309d67ea26340c39e1e94David Chisnall CGF.CGM.getObjCRuntime().GetCppAtomicObjectSetFunction(); 10460f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(CGF.getContext().VoidTy, 10470f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall args, 10480f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 10490f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 1050cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian copyCppAtomicObjectFn, ReturnValueSlot(), args); 1051cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian} 1052cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 105320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 10541e1f4871535f2cadcbf8c9af8cc48a2213192320John McCallstatic bool hasTrivialSetExpr(const ObjCPropertyImplDecl *PID) { 10551e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall Expr *setter = PID->getSetterCXXAssignment(); 10561e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (!setter) return true; 10571e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 10581e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Sema only makes only of these when the ivar has a C++ class type, 10591e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // so the form is pretty constrained. 106071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 106171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // An operator call is trivial if the function it calls is trivial. 10621e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // This also implies that there's nothing non-trivial going on with 10631e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // the arguments, because operator= can only be trivial if it's a 10641e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // synthesized assignment operator and therefore both parameters are 10651e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // references. 10661e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (CallExpr *call = dyn_cast<CallExpr>(setter)) { 106771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (const FunctionDecl *callee 106871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall = dyn_cast_or_null<FunctionDecl>(call->getCalleeDecl())) 106971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (callee->isTrivial()) 107071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return true; 107171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return false; 107201cb307f0b84a368cdbc0738d6680aab8ed7423fFariborz Jahanian } 107371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 10741e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall assert(isa<ExprWithCleanups>(setter)); 107571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return false; 107671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall} 107786957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar 10784e494cf56325f26a6c3eb5a065c35e54835f14e6Benjamin Kramerstatic bool UseOptimizedSetter(CodeGenModule &CGM) { 10794e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().getGC() != LangOptions::NonGC) 1080ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return false; 10817a0c064770355a4918df69f95de8dea6338a59a2Daniel Dunbar return CGM.getLangOpts().ObjCRuntime.hasOptimizedSetter(); 1082ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek} 1083ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 108471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCallvoid 108571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCallCodeGenFunction::generateObjCSetterBody(const ObjCImplementationDecl *classImpl, 1086cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian const ObjCPropertyImplDecl *propImpl, 1087cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian llvm::Constant *AtomicHelperFn) { 1088cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian const ObjCPropertyDecl *prop = propImpl->getPropertyDecl(); 1089cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); 1090cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian ObjCMethodDecl *setterMethod = prop->getSetterMethodDecl(); 1091cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian 109271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // Just use the setter expression if Sema gave us one and it's 1093cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // non-trivial. 10941e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall if (!hasTrivialSetExpr(propImpl)) { 1095cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian if (!AtomicHelperFn) 1096cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // If non-atomic, assignment is called directly. 1097cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian EmitStmt(propImpl->getSetterCXXAssignment()); 1098cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian else 1099cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian // If atomic, assignment is called via a locking api. 1100cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian emitCPPObjectAtomicSetterCall(*this, setterMethod, ivar, 1101cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian AtomicHelperFn); 110271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return; 110371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall } 110471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 11051e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall PropertyImplStrategy strategy(CGM, propImpl); 11061e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall switch (strategy.getKind()) { 11071e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::Native: { 1108aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman // We don't need to do anything for a zero-size struct. 1109aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman if (strategy.getIvarSize().isZero()) 1110aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman return; 1111aa014664a6083de28e255cfd304cae4605cf5e4fEli Friedman 11121e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *argAddr = LocalDeclMap[*setterMethod->param_begin()]; 111371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 11141e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall LValue ivarLValue = 11151e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, /*quals*/ 0); 11161e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *ivarAddr = ivarLValue.getAddress(); 11171e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 11181e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Currently, all atomic accesses have to be through integer 11191e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // types, so there's no point in trying to pick a prettier type. 11201e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Type *bitcastType = 11211e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Type::getIntNTy(getLLVMContext(), 11221e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall getContext().toBits(strategy.getIvarSize())); 11231e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall bitcastType = bitcastType->getPointerTo(); // addrspace 0 okay 11241e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 11251e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Cast both arguments to the chosen operation type. 11261e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall argAddr = Builder.CreateBitCast(argAddr, bitcastType); 11271e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall ivarAddr = Builder.CreateBitCast(ivarAddr, bitcastType); 112871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 11291e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // This bitcast load is likely to cause some nasty IR. 11301e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::Value *load = Builder.CreateLoad(argAddr); 11311e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 11321e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall // Perform an atomic store. There are no memory ordering requirements. 11331e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall llvm::StoreInst *store = Builder.CreateStore(load, ivarAddr); 11341e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall store->setAlignment(strategy.getIvarAlignment().getQuantity()); 11351e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall store->setAtomic(llvm::Unordered); 11361e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall return; 11371e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall } 11381e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 11391e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::GetSetProperty: 11401e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::SetPropertyAndExpressionGet: { 1141ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 1142ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek llvm::Value *setOptimizedPropertyFn = 0; 1143ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek llvm::Value *setPropertyFn = 0; 1144ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (UseOptimizedSetter(CGM)) { 11457a0c064770355a4918df69f95de8dea6338a59a2Daniel Dunbar // 10.8 and iOS 6.0 code and GC is off 1146ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek setOptimizedPropertyFn = 114716098f366097305c348b356a44638d6c959c6e60Eric Christopher CGM.getObjCRuntime() 114816098f366097305c348b356a44638d6c959c6e60Eric Christopher .GetOptimizedPropertySetFunction(strategy.isAtomic(), 114916098f366097305c348b356a44638d6c959c6e60Eric Christopher strategy.isCopy()); 1150ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (!setOptimizedPropertyFn) { 1151ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek CGM.ErrorUnsupported(propImpl, "Obj-C optimized setter - NYI"); 1152ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return; 1153ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 115486957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar } 1155ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek else { 1156ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek setPropertyFn = CGM.getObjCRuntime().GetPropertySetFunction(); 1157ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (!setPropertyFn) { 1158ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek CGM.ErrorUnsupported(propImpl, "Obj-C setter requiring atomic copy"); 1159ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek return; 1160ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 1161ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 1162ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 11631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Emit objc_setProperty((id) self, _cmd, offset, arg, 116486957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar // <is-atomic>, <is-copy>). 116571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall llvm::Value *cmd = 116671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall Builder.CreateLoad(LocalDeclMap[setterMethod->getCmdDecl()]); 116771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall llvm::Value *self = 116871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy); 116971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall llvm::Value *ivarOffset = 117071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall EmitIvarOffset(classImpl->getClassInterface(), ivar); 117171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall llvm::Value *arg = LocalDeclMap[*setterMethod->param_begin()]; 117271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall arg = Builder.CreateBitCast(Builder.CreateLoad(arg, "arg"), VoidPtrTy); 117371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 117471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall CallArgList args; 117571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall args.add(RValue::get(self), getContext().getObjCIdType()); 117671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall args.add(RValue::get(cmd), getContext().getObjCSelType()); 1177ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek if (setOptimizedPropertyFn) { 1178ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(arg), getContext().getObjCIdType()); 1179ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(ivarOffset), getContext().getPointerDiffType()); 11800f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall EmitCall(getTypes().arrangeFreeFunctionCall(getContext().VoidTy, args, 11810f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 11820f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 1183ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek setOptimizedPropertyFn, ReturnValueSlot(), args); 1184ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } else { 1185ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(ivarOffset), getContext().getPointerDiffType()); 1186ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(arg), getContext().getObjCIdType()); 1187ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(Builder.getInt1(strategy.isAtomic())), 1188ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek getContext().BoolTy); 1189ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek args.add(RValue::get(Builder.getInt1(strategy.isCopy())), 1190ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek getContext().BoolTy); 1191ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // FIXME: We shouldn't need to get the function info here, the runtime 1192ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek // already should have computed it to build the function. 11930f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall EmitCall(getTypes().arrangeFreeFunctionCall(getContext().VoidTy, args, 11940f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 11950f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 1196ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek setPropertyFn, ReturnValueSlot(), args); 1197ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek } 1198ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek 119971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return; 120071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall } 120171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 12021e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::CopyStruct: 120341bdde9e961c4d96a94148f20f0b18397f4358ddJohn McCall emitStructSetterCall(*this, setterMethod, ivar); 120471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall return; 12051e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall 12061e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall case PropertyImplStrategy::Expression: 12071e1f4871535f2cadcbf8c9af8cc48a2213192320John McCall break; 120871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall } 120971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 121071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // Otherwise, fake up some ASTs and emit a normal assignment. 121171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ValueDecl *selfDecl = setterMethod->getSelfDecl(); 1212f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr self(selfDecl, false, selfDecl->getType(), 1213f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_LValue, SourceLocation()); 121471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ImplicitCastExpr selfLoad(ImplicitCastExpr::OnStack, 121571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall selfDecl->getType(), CK_LValueToRValue, &self, 121671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall VK_RValue); 121771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ObjCIvarRefExpr ivarRef(ivar, ivar->getType().getNonReferenceType(), 12180c70181854a95fca0e0d56dfa1203eb2216052eaFariborz Jahanian SourceLocation(), SourceLocation(), 12190c70181854a95fca0e0d56dfa1203eb2216052eaFariborz Jahanian &selfLoad, true, true); 122071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 122171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ParmVarDecl *argDecl = *setterMethod->param_begin(); 122271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall QualType argType = argDecl->getType().getNonReferenceType(); 1223f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr arg(argDecl, false, argType, VK_LValue, SourceLocation()); 122471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ImplicitCastExpr argLoad(ImplicitCastExpr::OnStack, 122571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argType.getUnqualifiedType(), CK_LValueToRValue, 122671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall &arg, VK_RValue); 122745e8423d7dcea657c14c55347e8a30ac904d7501Daniel Dunbar 122871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // The property type can differ from the ivar type in some situations with 122971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // Objective-C pointer types, we can always bit cast the RHS in these cases. 123071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall // The following absurdity is just to ensure well-formed IR. 123171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall CastKind argCK = CK_NoOp; 123271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (ivarRef.getType()->isObjCObjectPointerType()) { 123371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (argLoad.getType()->isObjCObjectPointerType()) 123471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_BitCast; 123571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall else if (argLoad.getType()->isBlockPointerType()) 123671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_BlockPointerToObjCPointerCast; 123771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall else 123871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_CPointerToObjCPointerCast; 123971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall } else if (ivarRef.getType()->isBlockPointerType()) { 124071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (argLoad.getType()->isBlockPointerType()) 124171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_BitCast; 124271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall else 124371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_AnyPointerToBlockPointerCast; 124471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall } else if (ivarRef.getType()->isPointerType()) { 124571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argCK = CK_BitCast; 124686957eb200492e95a09bce1b2c76f66345468f84Daniel Dunbar } 124771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ImplicitCastExpr argCast(ImplicitCastExpr::OnStack, 124871c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ivarRef.getType(), argCK, &argLoad, 124971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall VK_RValue); 125071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall Expr *finalArg = &argLoad; 125171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall if (!getContext().hasSameUnqualifiedType(ivarRef.getType(), 125271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall argLoad.getType())) 125371c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall finalArg = &argCast; 125471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 125571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 125671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall BinaryOperator assign(&ivarRef, finalArg, BO_Assign, 125771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ivarRef.getType(), VK_RValue, OK_Ordinary, 1258be9af1288881110e406b87914162eaa59f1e5918Lang Hames SourceLocation(), false); 125971c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall EmitStmt(&assign); 126071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall} 126171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 12622ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// \brief Generate an Objective-C property setter function. 12632ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// 12642ee5ba35febf830d366b65dd0dcbf8e291c41342James Dennett/// The given Decl must be an ObjCImplementationDecl. \@synthesize 126571c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall/// is illegal within a category. 126671c758d3f4ecc8b511e6ae4a7210486aa994c182John McCallvoid CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP, 126771c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall const ObjCPropertyImplDecl *PID) { 1268b6e5fe3cae9bf456f084c555c3ecc256f4181cc5Fariborz Jahanian llvm::Constant *AtomicHelperFn = 126920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian GenerateObjCAtomicSetterCopyHelperFunction(PID); 127071c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 127171c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall ObjCMethodDecl *OMD = PD->getSetterMethodDecl(); 127271c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall assert(OMD && "Invalid call to generate setter (empty method)"); 1273ea32047660159811a4c14d008a4b7e3807a705d6Eric Christopher StartObjCMethod(OMD, IMP->getClassInterface(), OMD->getLocStart()); 127471c758d3f4ecc8b511e6ae4a7210486aa994c182John McCall 1275cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian generateObjCSetterBody(IMP, PID, AtomicHelperFn); 1276af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 1277af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar FinishFunction(); 12784111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner} 12794111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner 1280e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCallnamespace { 12819928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall struct DestroyIvar : EHScopeStack::Cleanup { 12829928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall private: 12839928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall llvm::Value *addr; 1284e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall const ObjCIvarDecl *ivar; 1285516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne CodeGenFunction::Destroyer *destroyer; 12869928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall bool useEHCleanupForArray; 12879928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall public: 12889928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall DestroyIvar(llvm::Value *addr, const ObjCIvarDecl *ivar, 12899928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CodeGenFunction::Destroyer *destroyer, 12909928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall bool useEHCleanupForArray) 1291516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne : addr(addr), ivar(ivar), destroyer(destroyer), 12929928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall useEHCleanupForArray(useEHCleanupForArray) {} 1293e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1294ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall void Emit(CodeGenFunction &CGF, Flags flags) { 12959928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall LValue lvalue 12969928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), addr, ivar, /*CVR*/ 0); 12979928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CGF.emitDestroy(lvalue.getAddress(), ivar->getType(), destroyer, 1298ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall flags.isForNormalCleanup() && useEHCleanupForArray); 1299e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall } 1300e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall }; 1301e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall} 1302e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 13039928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall/// Like CodeGenFunction::destroyARCStrong, but do it with a call. 13049928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCallstatic void destroyARCStrongWithStore(CodeGenFunction &CGF, 13059928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall llvm::Value *addr, 13069928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall QualType type) { 13079928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall llvm::Value *null = getNullForVariable(addr); 13089928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CGF.EmitARCStoreStrongCall(addr, null, /*ignored*/ true); 13099928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall} 1310f85e193739c953358c865005855253af4f68a497John McCall 1311e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCallstatic void emitCXXDestructMethod(CodeGenFunction &CGF, 1312e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall ObjCImplementationDecl *impl) { 1313e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall CodeGenFunction::RunCleanupsScope scope(CGF); 1314e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1315e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall llvm::Value *self = CGF.LoadObjCSelf(); 1316e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1317db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose const ObjCInterfaceDecl *iface = impl->getClassInterface(); 1318db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin(); 1319e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall ivar; ivar = ivar->getNextIvar()) { 1320e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall QualType type = ivar->getType(); 1321e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1322e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall // Check whether the ivar is a destructible type. 13239928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall QualType::DestructionKind dtorKind = type.isDestructedType(); 13249928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall if (!dtorKind) continue; 13259928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall 13269928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CodeGenFunction::Destroyer *destroyer = 0; 13279928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall 13289928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall // Use a call to objc_storeStrong to destroy strong ivars, for the 13299928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall // general benefit of the tools. 13309928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall if (dtorKind == QualType::DK_objc_strong_lifetime) { 1331516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne destroyer = destroyARCStrongWithStore; 13329928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall 13339928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall // Otherwise use the default for the destruction kind. 13349928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall } else { 1335516bbd42e62d709013824d6fb8445a0cfda3129aPeter Collingbourne destroyer = CGF.getDestroyer(dtorKind); 1336e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall } 13379928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall 13389928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CleanupKind cleanupKind = CGF.getCleanupKind(dtorKind); 13399928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall 13409928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall CGF.EHStack.pushCleanup<DestroyIvar>(cleanupKind, self, ivar, destroyer, 13419928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall cleanupKind & EHCleanup); 1342e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall } 1343e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1344e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall assert(scope.requiresCleanups() && "nothing to do in .cxx_destruct?"); 1345e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall} 1346e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1347109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanianvoid CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP, 1348109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian ObjCMethodDecl *MD, 1349109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian bool ctor) { 1350109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface()); 13518d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel StartObjCMethod(MD, IMP->getClassInterface(), MD->getLocStart()); 1352e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1353e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall // Emit .cxx_construct. 1354109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian if (ctor) { 1355f85e193739c953358c865005855253af4f68a497John McCall // Suppress the final autorelease in ARC. 1356f85e193739c953358c865005855253af4f68a497John McCall AutoreleaseResult = false; 1357f85e193739c953358c865005855253af4f68a497John McCall 13585f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<CXXCtorInitializer *, 8> IvarInitializers; 1359e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall for (ObjCImplementationDecl::init_const_iterator B = IMP->init_begin(), 1360e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall E = IMP->init_end(); B != E; ++B) { 1361e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall CXXCtorInitializer *IvarInit = (*B); 136200eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet FieldDecl *Field = IvarInit->getAnyMember(); 1363109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field); 13649b4d4fc49f30f1caa35d680702f1921afad81971Fariborz Jahanian LValue LV = EmitLValueForIvar(TypeOfSelfObject(), 13659b4d4fc49f30f1caa35d680702f1921afad81971Fariborz Jahanian LoadObjCSelf(), Ivar, 0); 13667c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall EmitAggExpr(IvarInit->getInit(), 13677c2349be2d11143a2e59a167fd43362a3bf4585eJohn McCall AggValueSlot::forLValue(LV, AggValueSlot::IsDestructed, 1368410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall AggValueSlot::DoesNotNeedGCBarriers, 1369649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier AggValueSlot::IsNotAliased)); 1370109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian } 1371109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian // constructor returns 'self'. 1372109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian CodeGenTypes &Types = CGM.getTypes(); 1373109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian QualType IdTy(CGM.getContext().getObjCIdType()); 1374109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian llvm::Value *SelfAsId = 1375109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy)); 1376109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian EmitReturnOfRValue(RValue::get(SelfAsId), IdTy); 1377e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall 1378e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall // Emit .cxx_destruct. 1379bc397cf90355f17c974b0bdf3960e8fb38caf5d6Chandler Carruth } else { 1380e81ac69c5da9fadfac33ee76e98d5fb558c4e389John McCall emitCXXDestructMethod(*this, IMP); 1381109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian } 1382109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian FinishFunction(); 1383109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian} 1384109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian 13850b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanianbool CodeGenFunction::IndirectObjCSetterArg(const CGFunctionInfo &FI) { 13860b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian CGFunctionInfo::const_arg_iterator it = FI.arg_begin(); 13870b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian it++; it++; 13880b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian const ABIArgInfo &AI = it->info; 13890b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian // FIXME. Is this sufficient check? 13900b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian return (AI.getKind() == ABIArgInfo::Indirect); 13910b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian} 13920b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian 139315bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanianbool CodeGenFunction::IvarTypeWithAggrGCObjects(QualType Ty) { 13944e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().getGC() == LangOptions::NonGC) 139515bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian return false; 139615bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian if (const RecordType *FDTTy = Ty.getTypePtr()->getAs<RecordType>()) 139715bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian return FDTTy->getDecl()->hasObjectMember(); 139815bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian return false; 139915bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian} 140015bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian 1401c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbarllvm::Value *CodeGenFunction::LoadObjCSelf() { 1402f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall VarDecl *Self = cast<ObjCMethodDecl>(CurFuncDecl)->getSelfDecl(); 1403f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall DeclRefExpr DRE(Self, /*is enclosing local*/ (CurFuncDecl != CurCodeDecl), 1404f5ebf9bf1df10ac15ba32a4b24dfe171b7848c58John McCall Self->getType(), VK_LValue, SourceLocation()); 14054ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky return EmitLoadOfScalar(EmitDeclRefLValue(&DRE), SourceLocation()); 14064111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner} 14074111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner 140845012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz JahanianQualType CodeGenFunction::TypeOfSelfObject() { 140945012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); 141045012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian ImplicitParamDecl *selfDecl = OMD->getSelfDecl(); 141114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>( 141214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff getContext().getCanonicalType(selfDecl->getType())); 141345012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian return PTy->getPointeeType(); 141445012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian} 141545012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian 141674391b48b4791cded373683a3baf67314f358d50Chris Lattnervoid CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){ 14171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Constant *EnumerationMutationFn = 1418c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar CGM.getObjCRuntime().EnumerationMutationFunction(); 14191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1420c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar if (!EnumerationMutationFn) { 1421c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime"); 1422c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar return; 1423c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar } 1424c1cf4a579f8a0a77719deedc1b8f850b77d36ecfDaniel Dunbar 1425bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel CGDebugInfo *DI = getDebugInfo(); 142673fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher if (DI) 142773fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin()); 1428bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel 14299d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel // The local variable comes into scope immediately. 14309d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel AutoVarEmission variable = AutoVarEmission::invalid(); 14319d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) 14329d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel variable = EmitAutoVarAlloca(*cast<VarDecl>(SD->getSingleDecl())); 14339d99f2db9bccc1664d6bbf1fc5346bab293ec0c3Devang Patel 1434d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end"); 14351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1436f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson // Fast enumeration state. 14370815b579b31cb3129f732bb7ea36fd6ba6949e98Douglas Gregor QualType StateTy = CGM.getObjCFastEnumerationStateType(); 1438195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr"); 14391884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson EmitNullInitialization(StatePtr, StateTy); 14401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1441f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson // Number of elements in the items array. 14422abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson static const unsigned NumItems = 16; 14431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1444d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Fetch the countByEnumeratingWithState:objects:count: selector. 1445ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer IdentifierInfo *II[] = { 1446ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer &CGM.getContext().Idents.get("countByEnumeratingWithState"), 1447ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer &CGM.getContext().Idents.get("objects"), 1448ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer &CGM.getContext().Idents.get("count") 1449ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer }; 1450ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer Selector FastEnumSel = 1451ad4688669579d5d7b025137a095be66936d7ea31Benjamin Kramer CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]); 1452f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1453f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson QualType ItemsTy = 1454f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson getContext().getConstantArrayType(getContext().getObjCIdType(), 14551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::APInt(32, NumItems), 1456f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson ArrayType::Normal, 0); 1457195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr"); 14581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1459990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Emit the collection pointer. In ARC, we do a retain. 1460990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall llvm::Value *Collection; 14614e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().ObjCAutoRefCount) { 1462990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall Collection = EmitARCRetainScalarExpr(S.getCollection()); 1463990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1464990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Enter a cleanup to do the release. 1465990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall EmitObjCConsumeObject(S.getCollection()->getType(), Collection); 1466990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall } else { 1467990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall Collection = EmitScalarExpr(S.getCollection()); 1468990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall } 14691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14704b302d32378b364703b212834f908762e570c29cJohn McCall // The 'continue' label needs to appear within the cleanup for the 14714b302d32378b364703b212834f908762e570c29cJohn McCall // collection object. 14724b302d32378b364703b212834f908762e570c29cJohn McCall JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next"); 14734b302d32378b364703b212834f908762e570c29cJohn McCall 1474d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Send it our message: 1475f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson CallArgList Args; 1476d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1477d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The first argument is a temporary of the enumeration-state type. 147804c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman Args.add(RValue::get(StatePtr), getContext().getPointerType(StateTy)); 14791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1480d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The second argument is a temporary array with space for NumItems 1481d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // pointers. We'll actually be loading elements from the array 1482d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // pointer written into the control state; this buffer is so that 1483d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // collections that *aren't* backed by arrays can still queue up 1484d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // batches of elements. 148504c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman Args.add(RValue::get(ItemsPtr), getContext().getPointerType(ItemsTy)); 14861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1487d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The third argument is the capacity of that temporary array. 14882acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy); 14894a28d5deeba33722aa009eab488591fb9055cc7eOwen Anderson llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems); 149004c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman Args.add(RValue::get(Count), getContext().UnsignedLongTy); 14911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1492d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Start the enumeration. 14931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump RValue CountRV = 1494ef072fd2f3347cfd857d6eb787b245b950771430John McCall CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), 1495f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson getContext().UnsignedLongTy, 1496f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson FastEnumSel, 1497c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall Collection, Args); 1498f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1499d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The initial number of objects that were returned in the buffer. 1500d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *initialBufferLimit = CountRV.getScalarVal(); 15011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1502d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty"); 1503d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit"); 1504f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1505d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *zero = llvm::Constant::getNullValue(UnsignedLongLTy); 1506f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1507d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // If the limit pointer was zero to begin with, the collection is 1508d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // empty; skip all this. 1509d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateCondBr(Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"), 1510d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmptyBB, LoopInitBB); 15111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1512d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Otherwise, initialize the loop. 1513d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(LoopInitBB); 15141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1515d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Save the initial mutations value. This is the value at an 1516d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // address that was written into the state object by 1517d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // countByEnumeratingWithState:objects:count:. 15181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Value *StateMutationsPtrPtr = 15192abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr"); 15201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, 15212abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson "mutationsptr"); 15221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1523d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *initialMutations = 1524d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateLoad(StateMutationsPtr, "forcoll.initial-mutations"); 15251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1526d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Start looping. This is the point we return to whenever we have a 1527d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // fresh, non-empty batch of objects. 1528d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody"); 1529d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(LoopBodyBB); 15301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1531d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The current index into the buffer. 1532bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad llvm::PHINode *index = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.index"); 1533d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall index->addIncoming(zero, LoopInitBB); 1534f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1535d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // The current buffer size. 1536bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad llvm::PHINode *count = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.count"); 1537d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall count->addIncoming(initialBufferLimit, LoopInitBB); 1538f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1539d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Check whether the mutations value has changed from where it was 1540d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // at start. StateMutationsPtr should actually be invariant between 1541d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // refreshes. 15422abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr"); 1543d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *currentMutations 1544d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall = Builder.CreateLoad(StateMutationsPtr, "statemutations"); 15451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1546d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated"); 1547361cf980a7d976ef11a37b49567412b6b63a89d7Dan Gohman llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcoll.notmutated"); 15481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1549d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations), 1550d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall WasNotMutatedBB, WasMutatedBB); 15511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1552d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // If so, call the enumeration-mutation function. 1553d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(WasMutatedBB); 15542abd89c039e835e84519a4cd8a7495899a70153dAnders Carlsson llvm::Value *V = 15551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Builder.CreateBitCast(Collection, 1556578faa837b552403e2002b97fdfbfde14f2448e5Benjamin Kramer ConvertType(getContext().getObjCIdType())); 15572b2105e92fc77016992dae3f117f526e73af5ea9Daniel Dunbar CallArgList Args2; 155804c9a49ee251424b11d7c4e8b1c23637684cecb6Eli Friedman Args2.add(RValue::get(V), getContext().getObjCIdType()); 1559f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // FIXME: We shouldn't need to get the function info here, the runtime already 1560f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // should have computed it to build the function. 15610f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall EmitCall(CGM.getTypes().arrangeFreeFunctionCall(getContext().VoidTy, Args2, 15620f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), 15630f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All), 1564f3c47c9525153aea2de0ec4bd615b9cf2d81c103Anders Carlsson EnumerationMutationFn, ReturnValueSlot(), Args2); 15651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1566d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Otherwise, or if the mutation function returns, just continue. 1567d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(WasNotMutatedBB); 15681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1569d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Initialize the element variable. 1570d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall RunCleanupsScope elementVariableScope(*this); 157157b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall bool elementIsVariable; 1572d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall LValue elementLValue; 1573d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall QualType elementType; 1574d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) { 157557b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall // Initialize the variable, in case it's a __block variable or something. 157657b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall EmitAutoVarInit(variable); 1577f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 157857b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall const VarDecl* D = cast<VarDecl>(SD->getSingleDecl()); 1579f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr tempDRE(const_cast<VarDecl*>(D), false, D->getType(), 1580d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall VK_LValue, SourceLocation()); 1581d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementLValue = EmitLValue(&tempDRE); 1582d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementType = D->getType(); 158357b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall elementIsVariable = true; 15847acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall 15857acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall if (D->isARCPseudoStrong()) 15867acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall elementLValue.getQuals().setObjCLifetime(Qualifiers::OCL_ExplicitNone); 1587d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall } else { 1588d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementLValue = LValue(); // suppress warning 1589d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementType = cast<Expr>(S.getElement())->getType(); 159057b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall elementIsVariable = false; 1591d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall } 15922acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *convertedElementType = ConvertType(elementType); 1593f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1594d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Fetch the buffer out of the enumeration state. 1595d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // TODO: this pointer should actually be invariant between 1596d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // refreshes, which would help us do certain loop optimizations. 1597d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *StateItemsPtr = 1598d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr"); 1599d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *EnumStateItems = 1600d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateLoad(StateItemsPtr, "stateitems"); 1601f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1602d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Fetch the value at the current index from the buffer. 16031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Value *CurrentItemPtr = 1604d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateGEP(EnumStateItems, index, "currentitem.ptr"); 1605d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr); 16061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1607d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Cast that value to the right type. 1608d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType, 1609d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall "currentitem"); 16101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1611d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Make sure we have an l-value. Yes, this gets evaluated every 1612d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // time through the loop. 16137acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall if (!elementIsVariable) { 1614d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementLValue = EmitLValue(cast<Expr>(S.getElement())); 1615545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue); 16167acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall } else { 16177acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall EmitScalarInit(CurrentItem, elementLValue); 16187acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall } 16191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 162057b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall // If we do have an element variable, this assignment is the end of 162157b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall // its initialization. 162257b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall if (elementIsVariable) 162357b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall EmitAutoVarCleanups(variable); 162457b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall 1625d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Perform the loop body, setting up break and continue labels. 1626e4b6d342c29d5cb9d311756100df1603810fa892Anders Carlsson BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody)); 1627d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall { 1628d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall RunCleanupsScope Scope(*this); 1629d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitStmt(S.getBody()); 1630d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall } 1631f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson BreakContinueStack.pop_back(); 16321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1633d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Destroy the element variable now. 1634d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementVariableScope.ForceCleanup(); 1635d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1636d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Check whether there are more elements. 1637ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall EmitBlock(AfterBody.getBlock()); 16381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1639d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch"); 1640d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1641d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // First we check in the local buffer. 1642d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *indexPlusOne 1643d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall = Builder.CreateAdd(index, llvm::ConstantInt::get(UnsignedLongLTy, 1)); 1644d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1645d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // If we haven't overrun the buffer yet, we can continue. 1646d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateCondBr(Builder.CreateICmpULT(indexPlusOne, count), 1647d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall LoopBodyBB, FetchMoreBB); 1648f0906c4edb37b20141428ca77fa7dfd00b976eafFariborz Jahanian 1649d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall index->addIncoming(indexPlusOne, AfterBody.getBlock()); 1650d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall count->addIncoming(count, AfterBody.getBlock()); 1651f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1652d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // Otherwise, we have to fetch more elements. 1653d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(FetchMoreBB); 16541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump CountRV = 1656ef072fd2f3347cfd857d6eb787b245b950771430John McCall CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), 1657f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson getContext().UnsignedLongTy, 16581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump FastEnumSel, 1659c6cd5fd3eae71f8841504a396563343cfaaf503eDavid Chisnall Collection, Args); 16601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1661d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // If we got a zero count, we're done. 1662d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *refetchCount = CountRV.getScalarVal(); 1663d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1664d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall // (note that the message send might split FetchMoreBB) 1665d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall index->addIncoming(zero, Builder.GetInsertBlock()); 1666d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall count->addIncoming(refetchCount, Builder.GetInsertBlock()); 1667d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall 1668d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero), 1669d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmptyBB, LoopBodyBB); 16701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1671f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson // No more elements. 1672d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall EmitBlock(EmptyBB); 1673f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 167457b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall if (!elementIsVariable) { 1675f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson // If the element was not a declaration, set it to be null. 1676f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 1677d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall llvm::Value *null = llvm::Constant::getNullValue(convertedElementType); 1678d88687fc8b3b6e0bce1f7cb83d347ef009ab5480John McCall elementLValue = EmitLValue(cast<Expr>(S.getElement())); 1679545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall EmitStoreThroughLValue(RValue::get(null), elementLValue); 1680f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson } 1681f484c31f4d6934f56070c2942d4dfdf3fee84074Anders Carlsson 168273fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher if (DI) 168373fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd()); 1684bcbd03ac0ac0890a436e1a179d3a285e914d41faDevang Patel 1685990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall // Leave the cleanup we entered in ARC. 16864e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().ObjCAutoRefCount) 1687990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall PopCleanupBlock(); 1688990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 1689ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall EmitBlock(LoopEnd.getBlock()); 16903d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson} 16913d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson 16921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) { 1693f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall CGM.getObjCRuntime().EmitTryStmt(*this, S); 169464d5d6c5903157c521af496479d06dc26032d718Anders Carlsson} 169564d5d6c5903157c521af496479d06dc26032d718Anders Carlsson 16961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) { 169764d5d6c5903157c521af496479d06dc26032d718Anders Carlsson CGM.getObjCRuntime().EmitThrowStmt(*this, S); 169864d5d6c5903157c521af496479d06dc26032d718Anders Carlsson} 169964d5d6c5903157c521af496479d06dc26032d718Anders Carlsson 170010cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattnervoid CodeGenFunction::EmitObjCAtSynchronizedStmt( 17011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const ObjCAtSynchronizedStmt &S) { 1702f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S); 170310cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattner} 170410cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattner 170533e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall/// Produce the code for a CK_ARCProduceObject. Just does a 1706f85e193739c953358c865005855253af4f68a497John McCall/// primitive retain. 1707f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCProduceObject(QualType type, 1708f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value) { 1709f85e193739c953358c865005855253af4f68a497John McCall return EmitARCRetain(type, value); 1710f85e193739c953358c865005855253af4f68a497John McCall} 1711f85e193739c953358c865005855253af4f68a497John McCall 1712f85e193739c953358c865005855253af4f68a497John McCallnamespace { 1713f85e193739c953358c865005855253af4f68a497John McCall struct CallObjCRelease : EHScopeStack::Cleanup { 1714bddfd87863bac7aa17d226cdfb228f49b30dd5f2John McCall CallObjCRelease(llvm::Value *object) : object(object) {} 1715bddfd87863bac7aa17d226cdfb228f49b30dd5f2John McCall llvm::Value *object; 1716f85e193739c953358c865005855253af4f68a497John McCall 1717ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall void Emit(CodeGenFunction &CGF, Flags flags) { 17185b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall // Releases at the end of the full-expression are imprecise. 17195b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall CGF.EmitARCRelease(object, ARCImpreciseLifetime); 1720f85e193739c953358c865005855253af4f68a497John McCall } 1721f85e193739c953358c865005855253af4f68a497John McCall }; 1722f85e193739c953358c865005855253af4f68a497John McCall} 1723f85e193739c953358c865005855253af4f68a497John McCall 172433e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall/// Produce the code for a CK_ARCConsumeObject. Does a primitive 1725f85e193739c953358c865005855253af4f68a497John McCall/// release at the end of the full-expression. 1726f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCConsumeObject(QualType type, 1727f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *object) { 1728f85e193739c953358c865005855253af4f68a497John McCall // If we're in a conditional branch, we need to make the cleanup 1729bddfd87863bac7aa17d226cdfb228f49b30dd5f2John McCall // conditional. 1730bddfd87863bac7aa17d226cdfb228f49b30dd5f2John McCall pushFullExprCleanup<CallObjCRelease>(getARCCleanupKind(), object); 1731f85e193739c953358c865005855253af4f68a497John McCall return object; 1732f85e193739c953358c865005855253af4f68a497John McCall} 1733f85e193739c953358c865005855253af4f68a497John McCall 1734f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCExtendObjectLifetime(QualType type, 1735f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value) { 1736f85e193739c953358c865005855253af4f68a497John McCall return EmitARCRetainAutorelease(type, value); 1737f85e193739c953358c865005855253af4f68a497John McCall} 1738f85e193739c953358c865005855253af4f68a497John McCall 1739b6a6079449a5275c283982e19b0c38e165833bb2John McCall/// Given a number of pointers, inform the optimizer that they're 1740b6a6079449a5275c283982e19b0c38e165833bb2John McCall/// being intrinsically used up until this point in the program. 1741b6a6079449a5275c283982e19b0c38e165833bb2John McCallvoid CodeGenFunction::EmitARCIntrinsicUse(ArrayRef<llvm::Value*> values) { 1742b6a6079449a5275c283982e19b0c38e165833bb2John McCall llvm::Constant *&fn = CGM.getARCEntrypoints().clang_arc_use; 1743b6a6079449a5275c283982e19b0c38e165833bb2John McCall if (!fn) { 1744b6a6079449a5275c283982e19b0c38e165833bb2John McCall llvm::FunctionType *fnType = 1745b6a6079449a5275c283982e19b0c38e165833bb2John McCall llvm::FunctionType::get(CGM.VoidTy, ArrayRef<llvm::Type*>(), true); 1746b6a6079449a5275c283982e19b0c38e165833bb2John McCall fn = CGM.CreateRuntimeFunction(fnType, "clang.arc.use"); 1747b6a6079449a5275c283982e19b0c38e165833bb2John McCall } 1748b6a6079449a5275c283982e19b0c38e165833bb2John McCall 1749b6a6079449a5275c283982e19b0c38e165833bb2John McCall // This isn't really a "runtime" function, but as an intrinsic it 1750b6a6079449a5275c283982e19b0c38e165833bb2John McCall // doesn't really matter as long as we align things up. 1751b6a6079449a5275c283982e19b0c38e165833bb2John McCall EmitNounwindRuntimeCall(fn, values); 1752b6a6079449a5275c283982e19b0c38e165833bb2John McCall} 1753b6a6079449a5275c283982e19b0c38e165833bb2John McCall 1754f85e193739c953358c865005855253af4f68a497John McCall 1755f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Constant *createARCRuntimeFunction(CodeGenModule &CGM, 17562acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *type, 17575f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef fnName) { 1758f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *fn = CGM.CreateRuntimeFunction(type, fnName); 1759f85e193739c953358c865005855253af4f68a497John McCall 1760554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman if (llvm::Function *f = dyn_cast<llvm::Function>(fn)) { 1761cfe18a195128002c5861e362db1f11ee1bebbea6Michael Gottesman // If the target runtime doesn't naturally support ARC, emit weak 1762cfe18a195128002c5861e362db1f11ee1bebbea6Michael Gottesman // references to the runtime support library. We don't really 1763cfe18a195128002c5861e362db1f11ee1bebbea6Michael Gottesman // permit this to fail, but we need a particular relocation style. 1764554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman if (!CGM.getLangOpts().ObjCRuntime.hasNativeARC()) { 1765f85e193739c953358c865005855253af4f68a497John McCall f->setLinkage(llvm::Function::ExternalWeakLinkage); 1766554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman } else if (fnName == "objc_retain" || fnName == "objc_release") { 1767554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman // If we have Native ARC, set nonlazybind attribute for these APIs for 1768554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman // performance. 176972390b39c545426023ec104afe8706395d732badBill Wendling f->addFnAttr(llvm::Attribute::NonLazyBind); 1770db99e8b72ae2b66d3c2f8d896f49513b8d8e2633Michael Gottesman } 1771554b07d30484185a313dbe2c5e2fd5798ac3f998Michael Gottesman } 1772f85e193739c953358c865005855253af4f68a497John McCall 1773f85e193739c953358c865005855253af4f68a497John McCall return fn; 1774f85e193739c953358c865005855253af4f68a497John McCall} 1775f85e193739c953358c865005855253af4f68a497John McCall 1776f85e193739c953358c865005855253af4f68a497John McCall/// Perform an operation having the signature 1777f85e193739c953358c865005855253af4f68a497John McCall/// i8* (i8*) 1778f85e193739c953358c865005855253af4f68a497John McCall/// where a null input causes a no-op and returns null. 1779f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCValueOperation(CodeGenFunction &CGF, 1780f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value, 1781f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn, 1782df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier StringRef fnName, 1783df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier bool isTailCall = false) { 1784f85e193739c953358c865005855253af4f68a497John McCall if (isa<llvm::ConstantPointerNull>(value)) return value; 1785f85e193739c953358c865005855253af4f68a497John McCall 1786f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 17872acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 178876ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrTy, false); 1789f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); 1790f85e193739c953358c865005855253af4f68a497John McCall } 1791f85e193739c953358c865005855253af4f68a497John McCall 1792f85e193739c953358c865005855253af4f68a497John McCall // Cast the argument to 'id'. 17932acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *origType = value->getType(); 1794f85e193739c953358c865005855253af4f68a497John McCall value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy); 1795f85e193739c953358c865005855253af4f68a497John McCall 1796f85e193739c953358c865005855253af4f68a497John McCall // Call the function. 1797bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::CallInst *call = CGF.EmitNounwindRuntimeCall(fn, value); 1798df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier if (isTailCall) 1799df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier call->setTailCall(); 1800f85e193739c953358c865005855253af4f68a497John McCall 1801f85e193739c953358c865005855253af4f68a497John McCall // Cast the result back to the original type. 1802f85e193739c953358c865005855253af4f68a497John McCall return CGF.Builder.CreateBitCast(call, origType); 1803f85e193739c953358c865005855253af4f68a497John McCall} 1804f85e193739c953358c865005855253af4f68a497John McCall 1805f85e193739c953358c865005855253af4f68a497John McCall/// Perform an operation having the following signature: 1806f85e193739c953358c865005855253af4f68a497John McCall/// i8* (i8**) 1807f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCLoadOperation(CodeGenFunction &CGF, 1808f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *addr, 1809f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn, 18105f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef fnName) { 1811f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 18122acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 181376ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrPtrTy, false); 1814f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); 1815f85e193739c953358c865005855253af4f68a497John McCall } 1816f85e193739c953358c865005855253af4f68a497John McCall 1817f85e193739c953358c865005855253af4f68a497John McCall // Cast the argument to 'id*'. 18182acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *origType = addr->getType(); 1819f85e193739c953358c865005855253af4f68a497John McCall addr = CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy); 1820f85e193739c953358c865005855253af4f68a497John McCall 1821f85e193739c953358c865005855253af4f68a497John McCall // Call the function. 1822bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *result = CGF.EmitNounwindRuntimeCall(fn, addr); 1823f85e193739c953358c865005855253af4f68a497John McCall 1824f85e193739c953358c865005855253af4f68a497John McCall // Cast the result back to a dereference of the original type. 1825f85e193739c953358c865005855253af4f68a497John McCall if (origType != CGF.Int8PtrPtrTy) 1826f85e193739c953358c865005855253af4f68a497John McCall result = CGF.Builder.CreateBitCast(result, 1827f85e193739c953358c865005855253af4f68a497John McCall cast<llvm::PointerType>(origType)->getElementType()); 1828f85e193739c953358c865005855253af4f68a497John McCall 1829f85e193739c953358c865005855253af4f68a497John McCall return result; 1830f85e193739c953358c865005855253af4f68a497John McCall} 1831f85e193739c953358c865005855253af4f68a497John McCall 1832f85e193739c953358c865005855253af4f68a497John McCall/// Perform an operation having the following signature: 1833f85e193739c953358c865005855253af4f68a497John McCall/// i8* (i8**, i8*) 1834f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCStoreOperation(CodeGenFunction &CGF, 1835f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *addr, 1836f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value, 1837f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn, 18385f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef fnName, 1839f85e193739c953358c865005855253af4f68a497John McCall bool ignored) { 1840f85e193739c953358c865005855253af4f68a497John McCall assert(cast<llvm::PointerType>(addr->getType())->getElementType() 1841f85e193739c953358c865005855253af4f68a497John McCall == value->getType()); 1842f85e193739c953358c865005855253af4f68a497John McCall 1843f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 18441d236ab930816f5da27bade92904914c44b73b4cBenjamin Kramer llvm::Type *argTypes[] = { CGF.Int8PtrPtrTy, CGF.Int8PtrTy }; 1845f85e193739c953358c865005855253af4f68a497John McCall 18462acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType 1847f85e193739c953358c865005855253af4f68a497John McCall = llvm::FunctionType::get(CGF.Int8PtrTy, argTypes, false); 1848f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); 1849f85e193739c953358c865005855253af4f68a497John McCall } 1850f85e193739c953358c865005855253af4f68a497John McCall 18512acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *origType = value->getType(); 1852f85e193739c953358c865005855253af4f68a497John McCall 1853bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *args[] = { 1854bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy), 1855bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy) 1856bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall }; 1857bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::CallInst *result = CGF.EmitNounwindRuntimeCall(fn, args); 1858f85e193739c953358c865005855253af4f68a497John McCall 1859f85e193739c953358c865005855253af4f68a497John McCall if (ignored) return 0; 1860f85e193739c953358c865005855253af4f68a497John McCall 1861f85e193739c953358c865005855253af4f68a497John McCall return CGF.Builder.CreateBitCast(result, origType); 1862f85e193739c953358c865005855253af4f68a497John McCall} 1863f85e193739c953358c865005855253af4f68a497John McCall 1864f85e193739c953358c865005855253af4f68a497John McCall/// Perform an operation having the following signature: 1865f85e193739c953358c865005855253af4f68a497John McCall/// void (i8**, i8**) 1866f85e193739c953358c865005855253af4f68a497John McCallstatic void emitARCCopyOperation(CodeGenFunction &CGF, 1867f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *dst, 1868f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *src, 1869f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn, 18705f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef fnName) { 1871f85e193739c953358c865005855253af4f68a497John McCall assert(dst->getType() == src->getType()); 1872f85e193739c953358c865005855253af4f68a497John McCall 1873f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 187476ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::Type *argTypes[] = { CGF.Int8PtrPtrTy, CGF.Int8PtrPtrTy }; 187576ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer 18762acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType 1877f85e193739c953358c865005855253af4f68a497John McCall = llvm::FunctionType::get(CGF.Builder.getVoidTy(), argTypes, false); 1878f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); 1879f85e193739c953358c865005855253af4f68a497John McCall } 1880f85e193739c953358c865005855253af4f68a497John McCall 1881bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *args[] = { 1882bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall CGF.Builder.CreateBitCast(dst, CGF.Int8PtrPtrTy), 1883bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall CGF.Builder.CreateBitCast(src, CGF.Int8PtrPtrTy) 1884bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall }; 1885bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall CGF.EmitNounwindRuntimeCall(fn, args); 1886f85e193739c953358c865005855253af4f68a497John McCall} 1887f85e193739c953358c865005855253af4f68a497John McCall 1888f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do a retain. Based on the type, calls one of: 18899d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retain(i8* %value) 18909d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainBlock(i8* %value) 1891f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCRetain(QualType type, llvm::Value *value) { 1892f85e193739c953358c865005855253af4f68a497John McCall if (type->isBlockPointerType()) 1893348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall return EmitARCRetainBlock(value, /*mandatory*/ false); 1894f85e193739c953358c865005855253af4f68a497John McCall else 1895f85e193739c953358c865005855253af4f68a497John McCall return EmitARCRetainNonBlock(value); 1896f85e193739c953358c865005855253af4f68a497John McCall} 1897f85e193739c953358c865005855253af4f68a497John McCall 1898f85e193739c953358c865005855253af4f68a497John McCall/// Retain the given object, with normal retain semantics. 18999d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retain(i8* %value) 1900f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCRetainNonBlock(llvm::Value *value) { 1901f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 1902f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_retain, 1903f85e193739c953358c865005855253af4f68a497John McCall "objc_retain"); 1904f85e193739c953358c865005855253af4f68a497John McCall} 1905f85e193739c953358c865005855253af4f68a497John McCall 1906f85e193739c953358c865005855253af4f68a497John McCall/// Retain the given block, with _Block_copy semantics. 19079d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainBlock(i8* %value) 1908348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall/// 1909348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall/// \param mandatory - If false, emit the call with metadata 1910348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall/// indicating that it's okay for the optimizer to eliminate this call 1911348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall/// if it can prove that the block never escapes except down the stack. 1912348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCallllvm::Value *CodeGenFunction::EmitARCRetainBlock(llvm::Value *value, 1913348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall bool mandatory) { 1914348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall llvm::Value *result 1915348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall = emitARCValueOperation(*this, value, 1916348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall CGM.getARCEntrypoints().objc_retainBlock, 1917348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall "objc_retainBlock"); 1918348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 1919348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall // If the copy isn't mandatory, add !clang.arc.copy_on_escape to 1920348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall // tell the optimizer that it doesn't need to do this copy if the 1921348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall // block doesn't escape, where being passed as an argument doesn't 1922348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall // count as escaping. 1923348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall if (!mandatory && isa<llvm::Instruction>(result)) { 1924348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall llvm::CallInst *call 1925348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall = cast<llvm::CallInst>(result->stripPointerCasts()); 1926348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall assert(call->getCalledValue() == CGM.getARCEntrypoints().objc_retainBlock); 1927348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 1928348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall SmallVector<llvm::Value*,1> args; 1929348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall call->setMetadata("clang.arc.copy_on_escape", 1930348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall llvm::MDNode::get(Builder.getContext(), args)); 1931348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall } 1932348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 1933348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall return result; 1934f85e193739c953358c865005855253af4f68a497John McCall} 1935f85e193739c953358c865005855253af4f68a497John McCall 1936f85e193739c953358c865005855253af4f68a497John McCall/// Retain the given object which is the result of a function call. 19379d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainAutoreleasedReturnValue(i8* %value) 1938f85e193739c953358c865005855253af4f68a497John McCall/// 1939f85e193739c953358c865005855253af4f68a497John McCall/// Yes, this function name is one character away from a different 1940f85e193739c953358c865005855253af4f68a497John McCall/// call with completely different semantics. 1941f85e193739c953358c865005855253af4f68a497John McCallllvm::Value * 1942f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCRetainAutoreleasedReturnValue(llvm::Value *value) { 1943f85e193739c953358c865005855253af4f68a497John McCall // Fetch the void(void) inline asm which marks that we're going to 1944f85e193739c953358c865005855253af4f68a497John McCall // retain the autoreleased return value. 1945f85e193739c953358c865005855253af4f68a497John McCall llvm::InlineAsm *&marker 1946f85e193739c953358c865005855253af4f68a497John McCall = CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker; 1947f85e193739c953358c865005855253af4f68a497John McCall if (!marker) { 19485f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef assembly 1949f85e193739c953358c865005855253af4f68a497John McCall = CGM.getTargetCodeGenInfo() 1950f85e193739c953358c865005855253af4f68a497John McCall .getARCRetainAutoreleasedReturnValueMarker(); 1951f85e193739c953358c865005855253af4f68a497John McCall 1952f85e193739c953358c865005855253af4f68a497John McCall // If we have an empty assembly string, there's nothing to do. 1953f85e193739c953358c865005855253af4f68a497John McCall if (assembly.empty()) { 1954f85e193739c953358c865005855253af4f68a497John McCall 1955f85e193739c953358c865005855253af4f68a497John McCall // Otherwise, at -O0, build an inline asm that we're going to call 1956f85e193739c953358c865005855253af4f68a497John McCall // in a moment. 1957f85e193739c953358c865005855253af4f68a497John McCall } else if (CGM.getCodeGenOpts().OptimizationLevel == 0) { 1958f85e193739c953358c865005855253af4f68a497John McCall llvm::FunctionType *type = 19598b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner llvm::FunctionType::get(VoidTy, /*variadic*/false); 1960f85e193739c953358c865005855253af4f68a497John McCall 1961f85e193739c953358c865005855253af4f68a497John McCall marker = llvm::InlineAsm::get(type, assembly, "", /*sideeffects*/ true); 1962f85e193739c953358c865005855253af4f68a497John McCall 1963f85e193739c953358c865005855253af4f68a497John McCall // If we're at -O1 and above, we don't want to litter the code 1964f85e193739c953358c865005855253af4f68a497John McCall // with this marker yet, so leave a breadcrumb for the ARC 1965f85e193739c953358c865005855253af4f68a497John McCall // optimizer to pick up. 1966f85e193739c953358c865005855253af4f68a497John McCall } else { 1967f85e193739c953358c865005855253af4f68a497John McCall llvm::NamedMDNode *metadata = 1968f85e193739c953358c865005855253af4f68a497John McCall CGM.getModule().getOrInsertNamedMetadata( 1969f85e193739c953358c865005855253af4f68a497John McCall "clang.arc.retainAutoreleasedReturnValueMarker"); 1970f85e193739c953358c865005855253af4f68a497John McCall assert(metadata->getNumOperands() <= 1); 1971f85e193739c953358c865005855253af4f68a497John McCall if (metadata->getNumOperands() == 0) { 1972f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *string = llvm::MDString::get(getLLVMContext(), assembly); 1973da549e8995c447542d5631b8b67fcc3a9582797aJay Foad metadata->addOperand(llvm::MDNode::get(getLLVMContext(), string)); 1974f85e193739c953358c865005855253af4f68a497John McCall } 1975f85e193739c953358c865005855253af4f68a497John McCall } 1976f85e193739c953358c865005855253af4f68a497John McCall } 1977f85e193739c953358c865005855253af4f68a497John McCall 1978f85e193739c953358c865005855253af4f68a497John McCall // Call the marker asm if we made one, which we do only at -O0. 1979f85e193739c953358c865005855253af4f68a497John McCall if (marker) Builder.CreateCall(marker); 1980f85e193739c953358c865005855253af4f68a497John McCall 1981f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 1982f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_retainAutoreleasedReturnValue, 1983f85e193739c953358c865005855253af4f68a497John McCall "objc_retainAutoreleasedReturnValue"); 1984f85e193739c953358c865005855253af4f68a497John McCall} 1985f85e193739c953358c865005855253af4f68a497John McCall 1986f85e193739c953358c865005855253af4f68a497John McCall/// Release the given object. 19879d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call void \@objc_release(i8* %value) 19885b07e8077a20b80fee90bd76c43c6150c676e4a8John McCallvoid CodeGenFunction::EmitARCRelease(llvm::Value *value, 19895b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall ARCPreciseLifetime_t precise) { 1990f85e193739c953358c865005855253af4f68a497John McCall if (isa<llvm::ConstantPointerNull>(value)) return; 1991f85e193739c953358c865005855253af4f68a497John McCall 1992f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn = CGM.getARCEntrypoints().objc_release; 1993f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 19942acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 199576ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false); 1996f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGM, fnType, "objc_release"); 1997f85e193739c953358c865005855253af4f68a497John McCall } 1998f85e193739c953358c865005855253af4f68a497John McCall 1999f85e193739c953358c865005855253af4f68a497John McCall // Cast the argument to 'id'. 2000f85e193739c953358c865005855253af4f68a497John McCall value = Builder.CreateBitCast(value, Int8PtrTy); 2001f85e193739c953358c865005855253af4f68a497John McCall 2002f85e193739c953358c865005855253af4f68a497John McCall // Call objc_release. 2003bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::CallInst *call = EmitNounwindRuntimeCall(fn, value); 2004f85e193739c953358c865005855253af4f68a497John McCall 20055b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall if (precise == ARCImpreciseLifetime) { 20065f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<llvm::Value*,1> args; 2007f85e193739c953358c865005855253af4f68a497John McCall call->setMetadata("clang.imprecise_release", 2008f85e193739c953358c865005855253af4f68a497John McCall llvm::MDNode::get(Builder.getContext(), args)); 2009f85e193739c953358c865005855253af4f68a497John McCall } 2010f85e193739c953358c865005855253af4f68a497John McCall} 2011f85e193739c953358c865005855253af4f68a497John McCall 2012015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// Destroy a __strong variable. 2013015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// 2014015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// At -O0, emit a call to store 'null' into the address; 2015015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// instrumenting tools prefer this because the address is exposed, 2016015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// but it's relatively cumbersome to optimize. 2017015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// 2018015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// At -O1 and above, just load and call objc_release. 2019015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// 2020015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall/// call void \@objc_storeStrong(i8** %addr, i8* null) 20215b07e8077a20b80fee90bd76c43c6150c676e4a8John McCallvoid CodeGenFunction::EmitARCDestroyStrong(llvm::Value *addr, 20225b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall ARCPreciseLifetime_t precise) { 2023015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall if (CGM.getCodeGenOpts().OptimizationLevel == 0) { 2024015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall llvm::PointerType *addrTy = cast<llvm::PointerType>(addr->getType()); 2025015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall llvm::Value *null = llvm::ConstantPointerNull::get( 2026015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall cast<llvm::PointerType>(addrTy->getElementType())); 2027015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall EmitARCStoreStrongCall(addr, null, /*ignored*/ true); 2028015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall return; 2029015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall } 2030015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall 2031015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall llvm::Value *value = Builder.CreateLoad(addr); 2032015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall EmitARCRelease(value, precise); 2033015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall} 2034015f33b6741ffceba3a71ee2d71d46418a7dc34cJohn McCall 2035f85e193739c953358c865005855253af4f68a497John McCall/// Store into a strong object. Always calls this: 20369d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call void \@objc_storeStrong(i8** %addr, i8* %value) 2037f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCStoreStrongCall(llvm::Value *addr, 2038f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value, 2039f85e193739c953358c865005855253af4f68a497John McCall bool ignored) { 2040f85e193739c953358c865005855253af4f68a497John McCall assert(cast<llvm::PointerType>(addr->getType())->getElementType() 2041f85e193739c953358c865005855253af4f68a497John McCall == value->getType()); 2042f85e193739c953358c865005855253af4f68a497John McCall 2043f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn = CGM.getARCEntrypoints().objc_storeStrong; 2044f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 20459cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::Type *argTypes[] = { Int8PtrPtrTy, Int8PtrTy }; 20462acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType 2047f85e193739c953358c865005855253af4f68a497John McCall = llvm::FunctionType::get(Builder.getVoidTy(), argTypes, false); 2048f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGM, fnType, "objc_storeStrong"); 2049f85e193739c953358c865005855253af4f68a497John McCall } 2050f85e193739c953358c865005855253af4f68a497John McCall 2051bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *args[] = { 2052bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall Builder.CreateBitCast(addr, Int8PtrPtrTy), 2053bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall Builder.CreateBitCast(value, Int8PtrTy) 2054bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall }; 2055bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall EmitNounwindRuntimeCall(fn, args); 2056f85e193739c953358c865005855253af4f68a497John McCall 2057f85e193739c953358c865005855253af4f68a497John McCall if (ignored) return 0; 2058f85e193739c953358c865005855253af4f68a497John McCall return value; 2059f85e193739c953358c865005855253af4f68a497John McCall} 2060f85e193739c953358c865005855253af4f68a497John McCall 2061f85e193739c953358c865005855253af4f68a497John McCall/// Store into a strong object. Sometimes calls this: 20629d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call void \@objc_storeStrong(i8** %addr, i8* %value) 2063f85e193739c953358c865005855253af4f68a497John McCall/// Other times, breaks it down into components. 2064545d996ec5a3113f046944f11b27cc2d6cb055b4John McCallllvm::Value *CodeGenFunction::EmitARCStoreStrong(LValue dst, 2065f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *newValue, 2066f85e193739c953358c865005855253af4f68a497John McCall bool ignored) { 2067545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall QualType type = dst.getType(); 2068f85e193739c953358c865005855253af4f68a497John McCall bool isBlock = type->isBlockPointerType(); 2069f85e193739c953358c865005855253af4f68a497John McCall 2070f85e193739c953358c865005855253af4f68a497John McCall // Use a store barrier at -O0 unless this is a block type or the 2071f85e193739c953358c865005855253af4f68a497John McCall // lvalue is inadequately aligned. 2072f85e193739c953358c865005855253af4f68a497John McCall if (shouldUseFusedARCCalls() && 2073f85e193739c953358c865005855253af4f68a497John McCall !isBlock && 20746da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman (dst.getAlignment().isZero() || 20756da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman dst.getAlignment() >= CharUnits::fromQuantity(PointerAlignInBytes))) { 2076f85e193739c953358c865005855253af4f68a497John McCall return EmitARCStoreStrongCall(dst.getAddress(), newValue, ignored); 2077f85e193739c953358c865005855253af4f68a497John McCall } 2078f85e193739c953358c865005855253af4f68a497John McCall 2079f85e193739c953358c865005855253af4f68a497John McCall // Otherwise, split it out. 2080f85e193739c953358c865005855253af4f68a497John McCall 2081f85e193739c953358c865005855253af4f68a497John McCall // Retain the new value. 2082f85e193739c953358c865005855253af4f68a497John McCall newValue = EmitARCRetain(type, newValue); 2083f85e193739c953358c865005855253af4f68a497John McCall 2084f85e193739c953358c865005855253af4f68a497John McCall // Read the old value. 20854ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky llvm::Value *oldValue = EmitLoadOfScalar(dst, SourceLocation()); 2086f85e193739c953358c865005855253af4f68a497John McCall 2087f85e193739c953358c865005855253af4f68a497John McCall // Store. We do this before the release so that any deallocs won't 2088f85e193739c953358c865005855253af4f68a497John McCall // see the old value. 2089545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall EmitStoreOfScalar(newValue, dst); 2090f85e193739c953358c865005855253af4f68a497John McCall 2091f85e193739c953358c865005855253af4f68a497John McCall // Finally, release the old value. 20925b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall EmitARCRelease(oldValue, dst.isARCPreciseLifetime()); 2093f85e193739c953358c865005855253af4f68a497John McCall 2094f85e193739c953358c865005855253af4f68a497John McCall return newValue; 2095f85e193739c953358c865005855253af4f68a497John McCall} 2096f85e193739c953358c865005855253af4f68a497John McCall 2097f85e193739c953358c865005855253af4f68a497John McCall/// Autorelease the given object. 20989d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_autorelease(i8* %value) 2099f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCAutorelease(llvm::Value *value) { 2100f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 2101f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_autorelease, 2102f85e193739c953358c865005855253af4f68a497John McCall "objc_autorelease"); 2103f85e193739c953358c865005855253af4f68a497John McCall} 2104f85e193739c953358c865005855253af4f68a497John McCall 2105f85e193739c953358c865005855253af4f68a497John McCall/// Autorelease the given object. 21069d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_autoreleaseReturnValue(i8* %value) 2107f85e193739c953358c865005855253af4f68a497John McCallllvm::Value * 2108f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCAutoreleaseReturnValue(llvm::Value *value) { 2109f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 2110f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_autoreleaseReturnValue, 2111df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier "objc_autoreleaseReturnValue", 2112df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier /*isTailCall*/ true); 2113f85e193739c953358c865005855253af4f68a497John McCall} 2114f85e193739c953358c865005855253af4f68a497John McCall 2115f85e193739c953358c865005855253af4f68a497John McCall/// Do a fused retain/autorelease of the given object. 21169d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainAutoreleaseReturnValue(i8* %value) 2117f85e193739c953358c865005855253af4f68a497John McCallllvm::Value * 2118f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCRetainAutoreleaseReturnValue(llvm::Value *value) { 2119f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 2120f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_retainAutoreleaseReturnValue, 2121df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier "objc_retainAutoreleaseReturnValue", 2122df76f1ea801a60a422812b20bd0bfa6ab51cecf8Chad Rosier /*isTailCall*/ true); 2123f85e193739c953358c865005855253af4f68a497John McCall} 2124f85e193739c953358c865005855253af4f68a497John McCall 2125f85e193739c953358c865005855253af4f68a497John McCall/// Do a fused retain/autorelease of the given object. 21269d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainAutorelease(i8* %value) 2127f85e193739c953358c865005855253af4f68a497John McCall/// or 21289d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// %retain = call i8* \@objc_retainBlock(i8* %value) 21299d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_autorelease(i8* %retain) 2130f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCRetainAutorelease(QualType type, 2131f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value) { 2132f85e193739c953358c865005855253af4f68a497John McCall if (!type->isBlockPointerType()) 2133f85e193739c953358c865005855253af4f68a497John McCall return EmitARCRetainAutoreleaseNonBlock(value); 2134f85e193739c953358c865005855253af4f68a497John McCall 2135f85e193739c953358c865005855253af4f68a497John McCall if (isa<llvm::ConstantPointerNull>(value)) return value; 2136f85e193739c953358c865005855253af4f68a497John McCall 21372acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *origType = value->getType(); 2138f85e193739c953358c865005855253af4f68a497John McCall value = Builder.CreateBitCast(value, Int8PtrTy); 2139348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall value = EmitARCRetainBlock(value, /*mandatory*/ true); 2140f85e193739c953358c865005855253af4f68a497John McCall value = EmitARCAutorelease(value); 2141f85e193739c953358c865005855253af4f68a497John McCall return Builder.CreateBitCast(value, origType); 2142f85e193739c953358c865005855253af4f68a497John McCall} 2143f85e193739c953358c865005855253af4f68a497John McCall 2144f85e193739c953358c865005855253af4f68a497John McCall/// Do a fused retain/autorelease of the given object. 21459d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_retainAutorelease(i8* %value) 2146f85e193739c953358c865005855253af4f68a497John McCallllvm::Value * 2147f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCRetainAutoreleaseNonBlock(llvm::Value *value) { 2148f85e193739c953358c865005855253af4f68a497John McCall return emitARCValueOperation(*this, value, 2149f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_retainAutorelease, 2150f85e193739c953358c865005855253af4f68a497John McCall "objc_retainAutorelease"); 2151f85e193739c953358c865005855253af4f68a497John McCall} 2152f85e193739c953358c865005855253af4f68a497John McCall 21539d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// i8* \@objc_loadWeak(i8** %addr) 2154f85e193739c953358c865005855253af4f68a497John McCall/// Essentially objc_autorelease(objc_loadWeakRetained(addr)). 2155f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCLoadWeak(llvm::Value *addr) { 2156f85e193739c953358c865005855253af4f68a497John McCall return emitARCLoadOperation(*this, addr, 2157f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_loadWeak, 2158f85e193739c953358c865005855253af4f68a497John McCall "objc_loadWeak"); 2159f85e193739c953358c865005855253af4f68a497John McCall} 2160f85e193739c953358c865005855253af4f68a497John McCall 21619d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// i8* \@objc_loadWeakRetained(i8** %addr) 2162f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCLoadWeakRetained(llvm::Value *addr) { 2163f85e193739c953358c865005855253af4f68a497John McCall return emitARCLoadOperation(*this, addr, 2164f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_loadWeakRetained, 2165f85e193739c953358c865005855253af4f68a497John McCall "objc_loadWeakRetained"); 2166f85e193739c953358c865005855253af4f68a497John McCall} 2167f85e193739c953358c865005855253af4f68a497John McCall 21689d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// i8* \@objc_storeWeak(i8** %addr, i8* %value) 2169f85e193739c953358c865005855253af4f68a497John McCall/// Returns %value. 2170f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCStoreWeak(llvm::Value *addr, 2171f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value, 2172f85e193739c953358c865005855253af4f68a497John McCall bool ignored) { 2173f85e193739c953358c865005855253af4f68a497John McCall return emitARCStoreOperation(*this, addr, value, 2174f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_storeWeak, 2175f85e193739c953358c865005855253af4f68a497John McCall "objc_storeWeak", ignored); 2176f85e193739c953358c865005855253af4f68a497John McCall} 2177f85e193739c953358c865005855253af4f68a497John McCall 21789d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// i8* \@objc_initWeak(i8** %addr, i8* %value) 2179f85e193739c953358c865005855253af4f68a497John McCall/// Returns %value. %addr is known to not have a current weak entry. 2180f85e193739c953358c865005855253af4f68a497John McCall/// Essentially equivalent to: 2181f85e193739c953358c865005855253af4f68a497John McCall/// *addr = nil; objc_storeWeak(addr, value); 2182f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitARCInitWeak(llvm::Value *addr, llvm::Value *value) { 2183f85e193739c953358c865005855253af4f68a497John McCall // If we're initializing to null, just write null to memory; no need 2184f85e193739c953358c865005855253af4f68a497John McCall // to get the runtime involved. But don't do this if optimization 2185f85e193739c953358c865005855253af4f68a497John McCall // is enabled, because accounting for this would make the optimizer 2186f85e193739c953358c865005855253af4f68a497John McCall // much more complicated. 2187f85e193739c953358c865005855253af4f68a497John McCall if (isa<llvm::ConstantPointerNull>(value) && 2188f85e193739c953358c865005855253af4f68a497John McCall CGM.getCodeGenOpts().OptimizationLevel == 0) { 2189f85e193739c953358c865005855253af4f68a497John McCall Builder.CreateStore(value, addr); 2190f85e193739c953358c865005855253af4f68a497John McCall return; 2191f85e193739c953358c865005855253af4f68a497John McCall } 2192f85e193739c953358c865005855253af4f68a497John McCall 2193f85e193739c953358c865005855253af4f68a497John McCall emitARCStoreOperation(*this, addr, value, 2194f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_initWeak, 2195f85e193739c953358c865005855253af4f68a497John McCall "objc_initWeak", /*ignored*/ true); 2196f85e193739c953358c865005855253af4f68a497John McCall} 2197f85e193739c953358c865005855253af4f68a497John McCall 21989d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// void \@objc_destroyWeak(i8** %addr) 2199f85e193739c953358c865005855253af4f68a497John McCall/// Essentially objc_storeWeak(addr, nil). 2200f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitARCDestroyWeak(llvm::Value *addr) { 2201f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn = CGM.getARCEntrypoints().objc_destroyWeak; 2202f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 22032acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 220476ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrPtrTy, false); 2205f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGM, fnType, "objc_destroyWeak"); 2206f85e193739c953358c865005855253af4f68a497John McCall } 2207f85e193739c953358c865005855253af4f68a497John McCall 2208f85e193739c953358c865005855253af4f68a497John McCall // Cast the argument to 'id*'. 2209f85e193739c953358c865005855253af4f68a497John McCall addr = Builder.CreateBitCast(addr, Int8PtrPtrTy); 2210f85e193739c953358c865005855253af4f68a497John McCall 2211bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall EmitNounwindRuntimeCall(fn, addr); 2212f85e193739c953358c865005855253af4f68a497John McCall} 2213f85e193739c953358c865005855253af4f68a497John McCall 22149d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// void \@objc_moveWeak(i8** %dest, i8** %src) 2215f85e193739c953358c865005855253af4f68a497John McCall/// Disregards the current value in %dest. Leaves %src pointing to nothing. 2216f85e193739c953358c865005855253af4f68a497John McCall/// Essentially (objc_copyWeak(dest, src), objc_destroyWeak(src)). 2217f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitARCMoveWeak(llvm::Value *dst, llvm::Value *src) { 2218f85e193739c953358c865005855253af4f68a497John McCall emitARCCopyOperation(*this, dst, src, 2219f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_moveWeak, 2220f85e193739c953358c865005855253af4f68a497John McCall "objc_moveWeak"); 2221f85e193739c953358c865005855253af4f68a497John McCall} 2222f85e193739c953358c865005855253af4f68a497John McCall 22239d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// void \@objc_copyWeak(i8** %dest, i8** %src) 2224f85e193739c953358c865005855253af4f68a497John McCall/// Disregards the current value in %dest. Essentially 2225f85e193739c953358c865005855253af4f68a497John McCall/// objc_release(objc_initWeak(dest, objc_readWeakRetained(src))) 2226f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitARCCopyWeak(llvm::Value *dst, llvm::Value *src) { 2227f85e193739c953358c865005855253af4f68a497John McCall emitARCCopyOperation(*this, dst, src, 2228f85e193739c953358c865005855253af4f68a497John McCall CGM.getARCEntrypoints().objc_copyWeak, 2229f85e193739c953358c865005855253af4f68a497John McCall "objc_copyWeak"); 2230f85e193739c953358c865005855253af4f68a497John McCall} 2231f85e193739c953358c865005855253af4f68a497John McCall 2232f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do a objc_autoreleasepool_push. 22339d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call i8* \@objc_autoreleasePoolPush(void) 2234f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCAutoreleasePoolPush() { 2235f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn = CGM.getRREntrypoints().objc_autoreleasePoolPush; 2236f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 22372acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 2238f85e193739c953358c865005855253af4f68a497John McCall llvm::FunctionType::get(Int8PtrTy, false); 2239f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPush"); 2240f85e193739c953358c865005855253af4f68a497John McCall } 2241f85e193739c953358c865005855253af4f68a497John McCall 2242bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall return EmitNounwindRuntimeCall(fn); 2243f85e193739c953358c865005855253af4f68a497John McCall} 2244f85e193739c953358c865005855253af4f68a497John McCall 2245f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do a primitive release. 22469d96e9c8a602d55a59bade0a20c05c52242db70eJames Dennett/// call void \@objc_autoreleasePoolPop(i8* %ptr) 2247f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitObjCAutoreleasePoolPop(llvm::Value *value) { 2248f85e193739c953358c865005855253af4f68a497John McCall assert(value->getType() == Int8PtrTy); 2249f85e193739c953358c865005855253af4f68a497John McCall 2250f85e193739c953358c865005855253af4f68a497John McCall llvm::Constant *&fn = CGM.getRREntrypoints().objc_autoreleasePoolPop; 2251f85e193739c953358c865005855253af4f68a497John McCall if (!fn) { 22522acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *fnType = 225376ecdfc53778a8468b7177c556fdbfe797fdbb29Benjamin Kramer llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false); 2254f85e193739c953358c865005855253af4f68a497John McCall 2255f85e193739c953358c865005855253af4f68a497John McCall // We don't want to use a weak import here; instead we should not 2256f85e193739c953358c865005855253af4f68a497John McCall // fall into this path. 2257f85e193739c953358c865005855253af4f68a497John McCall fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPop"); 2258f85e193739c953358c865005855253af4f68a497John McCall } 2259f85e193739c953358c865005855253af4f68a497John McCall 2260b57f6b3d8f84e9241fd978e2739d6a9da0870546John McCall // objc_autoreleasePoolPop can throw. 2261b57f6b3d8f84e9241fd978e2739d6a9da0870546John McCall EmitRuntimeCallOrInvoke(fn, value); 2262f85e193739c953358c865005855253af4f68a497John McCall} 2263f85e193739c953358c865005855253af4f68a497John McCall 2264f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do an MRR version objc_autoreleasepool_push. 2265f85e193739c953358c865005855253af4f68a497John McCall/// Which is: [[NSAutoreleasePool alloc] init]; 2266f85e193739c953358c865005855253af4f68a497John McCall/// Where alloc is declared as: + (id) alloc; in NSAutoreleasePool class. 2267f85e193739c953358c865005855253af4f68a497John McCall/// init is declared as: - (id) init; in its NSObject super class. 2268f85e193739c953358c865005855253af4f68a497John McCall/// 2269f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitObjCMRRAutoreleasePoolPush() { 2270f85e193739c953358c865005855253af4f68a497John McCall CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 2271bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall llvm::Value *Receiver = Runtime.EmitNSAutoreleasePoolClassRef(*this); 2272f85e193739c953358c865005855253af4f68a497John McCall // [NSAutoreleasePool alloc] 2273f85e193739c953358c865005855253af4f68a497John McCall IdentifierInfo *II = &CGM.getContext().Idents.get("alloc"); 2274f85e193739c953358c865005855253af4f68a497John McCall Selector AllocSel = getContext().Selectors.getSelector(0, &II); 2275f85e193739c953358c865005855253af4f68a497John McCall CallArgList Args; 2276f85e193739c953358c865005855253af4f68a497John McCall RValue AllocRV = 2277f85e193739c953358c865005855253af4f68a497John McCall Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 2278f85e193739c953358c865005855253af4f68a497John McCall getContext().getObjCIdType(), 2279f85e193739c953358c865005855253af4f68a497John McCall AllocSel, Receiver, Args); 2280f85e193739c953358c865005855253af4f68a497John McCall 2281f85e193739c953358c865005855253af4f68a497John McCall // [Receiver init] 2282f85e193739c953358c865005855253af4f68a497John McCall Receiver = AllocRV.getScalarVal(); 2283f85e193739c953358c865005855253af4f68a497John McCall II = &CGM.getContext().Idents.get("init"); 2284f85e193739c953358c865005855253af4f68a497John McCall Selector InitSel = getContext().Selectors.getSelector(0, &II); 2285f85e193739c953358c865005855253af4f68a497John McCall RValue InitRV = 2286f85e193739c953358c865005855253af4f68a497John McCall Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 2287f85e193739c953358c865005855253af4f68a497John McCall getContext().getObjCIdType(), 2288f85e193739c953358c865005855253af4f68a497John McCall InitSel, Receiver, Args); 2289f85e193739c953358c865005855253af4f68a497John McCall return InitRV.getScalarVal(); 2290f85e193739c953358c865005855253af4f68a497John McCall} 2291f85e193739c953358c865005855253af4f68a497John McCall 2292f85e193739c953358c865005855253af4f68a497John McCall/// Produce the code to do a primitive release. 2293f85e193739c953358c865005855253af4f68a497John McCall/// [tmp drain]; 2294f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitObjCMRRAutoreleasePoolPop(llvm::Value *Arg) { 2295f85e193739c953358c865005855253af4f68a497John McCall IdentifierInfo *II = &CGM.getContext().Idents.get("drain"); 2296f85e193739c953358c865005855253af4f68a497John McCall Selector DrainSel = getContext().Selectors.getSelector(0, &II); 2297f85e193739c953358c865005855253af4f68a497John McCall CallArgList Args; 2298f85e193739c953358c865005855253af4f68a497John McCall CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), 2299f85e193739c953358c865005855253af4f68a497John McCall getContext().VoidTy, DrainSel, Arg, Args); 2300f85e193739c953358c865005855253af4f68a497John McCall} 2301f85e193739c953358c865005855253af4f68a497John McCall 2302bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCallvoid CodeGenFunction::destroyARCStrongPrecise(CodeGenFunction &CGF, 2303bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall llvm::Value *addr, 2304bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall QualType type) { 23055b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall CGF.EmitARCDestroyStrong(addr, ARCPreciseLifetime); 2306bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall} 2307bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 2308bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCallvoid CodeGenFunction::destroyARCStrongImprecise(CodeGenFunction &CGF, 2309bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall llvm::Value *addr, 2310bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall QualType type) { 23115b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall CGF.EmitARCDestroyStrong(addr, ARCImpreciseLifetime); 2312bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall} 2313bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 2314bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCallvoid CodeGenFunction::destroyARCWeak(CodeGenFunction &CGF, 2315bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall llvm::Value *addr, 2316bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall QualType type) { 2317bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall CGF.EmitARCDestroyWeak(addr); 2318bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall} 2319bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall 2320f85e193739c953358c865005855253af4f68a497John McCallnamespace { 2321f85e193739c953358c865005855253af4f68a497John McCall struct CallObjCAutoreleasePoolObject : EHScopeStack::Cleanup { 2322f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *Token; 2323f85e193739c953358c865005855253af4f68a497John McCall 2324f85e193739c953358c865005855253af4f68a497John McCall CallObjCAutoreleasePoolObject(llvm::Value *token) : Token(token) {} 2325f85e193739c953358c865005855253af4f68a497John McCall 2326ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall void Emit(CodeGenFunction &CGF, Flags flags) { 2327f85e193739c953358c865005855253af4f68a497John McCall CGF.EmitObjCAutoreleasePoolPop(Token); 2328f85e193739c953358c865005855253af4f68a497John McCall } 2329f85e193739c953358c865005855253af4f68a497John McCall }; 2330f85e193739c953358c865005855253af4f68a497John McCall struct CallObjCMRRAutoreleasePoolObject : EHScopeStack::Cleanup { 2331f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *Token; 2332f85e193739c953358c865005855253af4f68a497John McCall 2333f85e193739c953358c865005855253af4f68a497John McCall CallObjCMRRAutoreleasePoolObject(llvm::Value *token) : Token(token) {} 2334f85e193739c953358c865005855253af4f68a497John McCall 2335ad346f4f678ab1c3222425641d851dc63e9dfa1aJohn McCall void Emit(CodeGenFunction &CGF, Flags flags) { 2336f85e193739c953358c865005855253af4f68a497John McCall CGF.EmitObjCMRRAutoreleasePoolPop(Token); 2337f85e193739c953358c865005855253af4f68a497John McCall } 2338f85e193739c953358c865005855253af4f68a497John McCall }; 2339f85e193739c953358c865005855253af4f68a497John McCall} 2340f85e193739c953358c865005855253af4f68a497John McCall 2341f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr) { 23424e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().ObjCAutoRefCount) 2343f85e193739c953358c865005855253af4f68a497John McCall EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, Ptr); 2344f85e193739c953358c865005855253af4f68a497John McCall else 2345f85e193739c953358c865005855253af4f68a497John McCall EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, Ptr); 2346f85e193739c953358c865005855253af4f68a497John McCall} 2347f85e193739c953358c865005855253af4f68a497John McCall 2348f85e193739c953358c865005855253af4f68a497John McCallstatic TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF, 2349f85e193739c953358c865005855253af4f68a497John McCall LValue lvalue, 2350f85e193739c953358c865005855253af4f68a497John McCall QualType type) { 2351f85e193739c953358c865005855253af4f68a497John McCall switch (type.getObjCLifetime()) { 2352f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_None: 2353f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_ExplicitNone: 2354f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_Strong: 2355f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_Autoreleasing: 23564ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky return TryEmitResult(CGF.EmitLoadOfLValue(lvalue, 23574ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky SourceLocation()).getScalarVal(), 2358f85e193739c953358c865005855253af4f68a497John McCall false); 2359f85e193739c953358c865005855253af4f68a497John McCall 2360f85e193739c953358c865005855253af4f68a497John McCall case Qualifiers::OCL_Weak: 2361f85e193739c953358c865005855253af4f68a497John McCall return TryEmitResult(CGF.EmitARCLoadWeakRetained(lvalue.getAddress()), 2362f85e193739c953358c865005855253af4f68a497John McCall true); 2363f85e193739c953358c865005855253af4f68a497John McCall } 2364f85e193739c953358c865005855253af4f68a497John McCall 2365f85e193739c953358c865005855253af4f68a497John McCall llvm_unreachable("impossible lifetime!"); 2366f85e193739c953358c865005855253af4f68a497John McCall} 2367f85e193739c953358c865005855253af4f68a497John McCall 2368f85e193739c953358c865005855253af4f68a497John McCallstatic TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF, 2369f85e193739c953358c865005855253af4f68a497John McCall const Expr *e) { 2370f85e193739c953358c865005855253af4f68a497John McCall e = e->IgnoreParens(); 2371f85e193739c953358c865005855253af4f68a497John McCall QualType type = e->getType(); 2372f85e193739c953358c865005855253af4f68a497John McCall 23732148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // If we're loading retained from a __strong xvalue, we can avoid 23742148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // an extra retain/release pair by zeroing out the source of this 23752148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // "move" operation. 23762148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall if (e->isXValue() && 23772148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall !type.isConstQualified() && 23782148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall type.getObjCLifetime() == Qualifiers::OCL_Strong) { 23792148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // Emit the lvalue. 23802148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall LValue lv = CGF.EmitLValue(e); 23812148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall 23822148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // Load the object pointer. 23834ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky llvm::Value *result = CGF.EmitLoadOfLValue(lv, 23844ee7dc2369c1f0257a73b2e83a7d38fdebdd9176Nick Lewycky SourceLocation()).getScalarVal(); 23852148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall 23862148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall // Set the source pointer to NULL. 23872148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall CGF.EmitStoreOfScalar(getNullForVariable(lv.getAddress()), lv); 23882148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall 23892148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall return TryEmitResult(result, true); 23902148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall } 23912148011bffc011f8e5f5b6dc1e312fa4afbc10a9John McCall 2392f85e193739c953358c865005855253af4f68a497John McCall // As a very special optimization, in ARC++, if the l-value is the 2393f85e193739c953358c865005855253af4f68a497John McCall // result of a non-volatile assignment, do a simple retain of the 2394f85e193739c953358c865005855253af4f68a497John McCall // result of the call to objc_storeWeak instead of reloading. 23954e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGF.getLangOpts().CPlusPlus && 2396f85e193739c953358c865005855253af4f68a497John McCall !type.isVolatileQualified() && 2397f85e193739c953358c865005855253af4f68a497John McCall type.getObjCLifetime() == Qualifiers::OCL_Weak && 2398f85e193739c953358c865005855253af4f68a497John McCall isa<BinaryOperator>(e) && 2399f85e193739c953358c865005855253af4f68a497John McCall cast<BinaryOperator>(e)->getOpcode() == BO_Assign) 2400f85e193739c953358c865005855253af4f68a497John McCall return TryEmitResult(CGF.EmitScalarExpr(e), false); 2401f85e193739c953358c865005855253af4f68a497John McCall 2402f85e193739c953358c865005855253af4f68a497John McCall return tryEmitARCRetainLoadOfScalar(CGF, CGF.EmitLValue(e), type); 2403f85e193739c953358c865005855253af4f68a497John McCall} 2404f85e193739c953358c865005855253af4f68a497John McCall 2405f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainAfterCall(CodeGenFunction &CGF, 2406f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value); 2407f85e193739c953358c865005855253af4f68a497John McCall 2408f85e193739c953358c865005855253af4f68a497John McCall/// Given that the given expression is some sort of call (which does 2409f85e193739c953358c865005855253af4f68a497John McCall/// not return retained), emit a retain following it. 2410f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainCall(CodeGenFunction &CGF, const Expr *e) { 2411f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = CGF.EmitScalarExpr(e); 2412f85e193739c953358c865005855253af4f68a497John McCall return emitARCRetainAfterCall(CGF, value); 2413f85e193739c953358c865005855253af4f68a497John McCall} 2414f85e193739c953358c865005855253af4f68a497John McCall 2415f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainAfterCall(CodeGenFunction &CGF, 2416f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value) { 2417f85e193739c953358c865005855253af4f68a497John McCall if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(value)) { 2418f85e193739c953358c865005855253af4f68a497John McCall CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP(); 2419f85e193739c953358c865005855253af4f68a497John McCall 2420f85e193739c953358c865005855253af4f68a497John McCall // Place the retain immediately following the call. 2421f85e193739c953358c865005855253af4f68a497John McCall CGF.Builder.SetInsertPoint(call->getParent(), 2422f85e193739c953358c865005855253af4f68a497John McCall ++llvm::BasicBlock::iterator(call)); 2423f85e193739c953358c865005855253af4f68a497John McCall value = CGF.EmitARCRetainAutoreleasedReturnValue(value); 2424f85e193739c953358c865005855253af4f68a497John McCall 2425f85e193739c953358c865005855253af4f68a497John McCall CGF.Builder.restoreIP(ip); 2426f85e193739c953358c865005855253af4f68a497John McCall return value; 2427f85e193739c953358c865005855253af4f68a497John McCall } else if (llvm::InvokeInst *invoke = dyn_cast<llvm::InvokeInst>(value)) { 2428f85e193739c953358c865005855253af4f68a497John McCall CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP(); 2429f85e193739c953358c865005855253af4f68a497John McCall 2430f85e193739c953358c865005855253af4f68a497John McCall // Place the retain at the beginning of the normal destination block. 2431f85e193739c953358c865005855253af4f68a497John McCall llvm::BasicBlock *BB = invoke->getNormalDest(); 2432f85e193739c953358c865005855253af4f68a497John McCall CGF.Builder.SetInsertPoint(BB, BB->begin()); 2433f85e193739c953358c865005855253af4f68a497John McCall value = CGF.EmitARCRetainAutoreleasedReturnValue(value); 2434f85e193739c953358c865005855253af4f68a497John McCall 2435f85e193739c953358c865005855253af4f68a497John McCall CGF.Builder.restoreIP(ip); 2436f85e193739c953358c865005855253af4f68a497John McCall return value; 2437f85e193739c953358c865005855253af4f68a497John McCall 2438f85e193739c953358c865005855253af4f68a497John McCall // Bitcasts can arise because of related-result returns. Rewrite 2439f85e193739c953358c865005855253af4f68a497John McCall // the operand. 2440f85e193739c953358c865005855253af4f68a497John McCall } else if (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(value)) { 2441f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *operand = bitcast->getOperand(0); 2442f85e193739c953358c865005855253af4f68a497John McCall operand = emitARCRetainAfterCall(CGF, operand); 2443f85e193739c953358c865005855253af4f68a497John McCall bitcast->setOperand(0, operand); 2444f85e193739c953358c865005855253af4f68a497John McCall return bitcast; 2445f85e193739c953358c865005855253af4f68a497John McCall 2446f85e193739c953358c865005855253af4f68a497John McCall // Generic fall-back case. 2447f85e193739c953358c865005855253af4f68a497John McCall } else { 2448f85e193739c953358c865005855253af4f68a497John McCall // Retain using the non-block variant: we never need to do a copy 2449f85e193739c953358c865005855253af4f68a497John McCall // of a block that's been returned to us. 2450f85e193739c953358c865005855253af4f68a497John McCall return CGF.EmitARCRetainNonBlock(value); 2451f85e193739c953358c865005855253af4f68a497John McCall } 2452f85e193739c953358c865005855253af4f68a497John McCall} 2453f85e193739c953358c865005855253af4f68a497John McCall 2454dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall/// Determine whether it might be important to emit a separate 2455dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall/// objc_retain_block on the result of the given expression, or 2456dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall/// whether it's okay to just emit it in a +1 context. 2457dc05b11c67331016473fbc7909827b1b89c9616bJohn McCallstatic bool shouldEmitSeparateBlockRetain(const Expr *e) { 2458dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall assert(e->getType()->isBlockPointerType()); 2459dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall e = e->IgnoreParens(); 2460dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2461dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // For future goodness, emit block expressions directly in +1 2462dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // contexts if we can. 2463dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall if (isa<BlockExpr>(e)) 2464dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return false; 2465dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2466dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall if (const CastExpr *cast = dyn_cast<CastExpr>(e)) { 2467dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall switch (cast->getCastKind()) { 2468dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // Emitting these operations in +1 contexts is goodness. 2469dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall case CK_LValueToRValue: 247033e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCReclaimReturnedObject: 247133e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCConsumeObject: 247233e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCProduceObject: 2473dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return false; 2474dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2475dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // These operations preserve a block type. 2476dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall case CK_NoOp: 2477dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall case CK_BitCast: 2478dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return shouldEmitSeparateBlockRetain(cast->getSubExpr()); 2479dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2480dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // These operations are known to be bad (or haven't been considered). 2481dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall case CK_AnyPointerToBlockPointerCast: 2482dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall default: 2483dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return true; 2484dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall } 2485dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall } 2486dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2487dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return true; 2488dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall} 2489dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 24904b9c2d235fb9449e249d74f48ecfec601650de93John McCall/// Try to emit a PseudoObjectExpr at +1. 24914b9c2d235fb9449e249d74f48ecfec601650de93John McCall/// 24924b9c2d235fb9449e249d74f48ecfec601650de93John McCall/// This massively duplicates emitPseudoObjectRValue. 24934b9c2d235fb9449e249d74f48ecfec601650de93John McCallstatic TryEmitResult tryEmitARCRetainPseudoObject(CodeGenFunction &CGF, 24944b9c2d235fb9449e249d74f48ecfec601650de93John McCall const PseudoObjectExpr *E) { 2495cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques; 24964b9c2d235fb9449e249d74f48ecfec601650de93John McCall 24974b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Find the result expression. 24984b9c2d235fb9449e249d74f48ecfec601650de93John McCall const Expr *resultExpr = E->getResultExpr(); 24994b9c2d235fb9449e249d74f48ecfec601650de93John McCall assert(resultExpr); 25004b9c2d235fb9449e249d74f48ecfec601650de93John McCall TryEmitResult result; 25014b9c2d235fb9449e249d74f48ecfec601650de93John McCall 25024b9c2d235fb9449e249d74f48ecfec601650de93John McCall for (PseudoObjectExpr::const_semantics_iterator 25034b9c2d235fb9449e249d74f48ecfec601650de93John McCall i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) { 25044b9c2d235fb9449e249d74f48ecfec601650de93John McCall const Expr *semantic = *i; 25054b9c2d235fb9449e249d74f48ecfec601650de93John McCall 25064b9c2d235fb9449e249d74f48ecfec601650de93John McCall // If this semantic expression is an opaque value, bind it 25074b9c2d235fb9449e249d74f48ecfec601650de93John McCall // to the result of its source expression. 25084b9c2d235fb9449e249d74f48ecfec601650de93John McCall if (const OpaqueValueExpr *ov = dyn_cast<OpaqueValueExpr>(semantic)) { 25094b9c2d235fb9449e249d74f48ecfec601650de93John McCall typedef CodeGenFunction::OpaqueValueMappingData OVMA; 25104b9c2d235fb9449e249d74f48ecfec601650de93John McCall OVMA opaqueData; 25114b9c2d235fb9449e249d74f48ecfec601650de93John McCall 25124b9c2d235fb9449e249d74f48ecfec601650de93John McCall // If this semantic is the result of the pseudo-object 25134b9c2d235fb9449e249d74f48ecfec601650de93John McCall // expression, try to evaluate the source as +1. 25144b9c2d235fb9449e249d74f48ecfec601650de93John McCall if (ov == resultExpr) { 25154b9c2d235fb9449e249d74f48ecfec601650de93John McCall assert(!OVMA::shouldBindAsLValue(ov)); 25164b9c2d235fb9449e249d74f48ecfec601650de93John McCall result = tryEmitARCRetainScalarExpr(CGF, ov->getSourceExpr()); 25174b9c2d235fb9449e249d74f48ecfec601650de93John McCall opaqueData = OVMA::bind(CGF, ov, RValue::get(result.getPointer())); 25184b9c2d235fb9449e249d74f48ecfec601650de93John McCall 25194b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Otherwise, just bind it. 25204b9c2d235fb9449e249d74f48ecfec601650de93John McCall } else { 25214b9c2d235fb9449e249d74f48ecfec601650de93John McCall opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr()); 25224b9c2d235fb9449e249d74f48ecfec601650de93John McCall } 25234b9c2d235fb9449e249d74f48ecfec601650de93John McCall opaques.push_back(opaqueData); 25244b9c2d235fb9449e249d74f48ecfec601650de93John McCall 25254b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Otherwise, if the expression is the result, evaluate it 25264b9c2d235fb9449e249d74f48ecfec601650de93John McCall // and remember the result. 25274b9c2d235fb9449e249d74f48ecfec601650de93John McCall } else if (semantic == resultExpr) { 25284b9c2d235fb9449e249d74f48ecfec601650de93John McCall result = tryEmitARCRetainScalarExpr(CGF, semantic); 25294b9c2d235fb9449e249d74f48ecfec601650de93John McCall 25304b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Otherwise, evaluate the expression in an ignored context. 25314b9c2d235fb9449e249d74f48ecfec601650de93John McCall } else { 25324b9c2d235fb9449e249d74f48ecfec601650de93John McCall CGF.EmitIgnoredExpr(semantic); 25334b9c2d235fb9449e249d74f48ecfec601650de93John McCall } 25344b9c2d235fb9449e249d74f48ecfec601650de93John McCall } 25354b9c2d235fb9449e249d74f48ecfec601650de93John McCall 25364b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Unbind all the opaques now. 25374b9c2d235fb9449e249d74f48ecfec601650de93John McCall for (unsigned i = 0, e = opaques.size(); i != e; ++i) 25384b9c2d235fb9449e249d74f48ecfec601650de93John McCall opaques[i].unbind(CGF); 25394b9c2d235fb9449e249d74f48ecfec601650de93John McCall 25404b9c2d235fb9449e249d74f48ecfec601650de93John McCall return result; 25414b9c2d235fb9449e249d74f48ecfec601650de93John McCall} 25424b9c2d235fb9449e249d74f48ecfec601650de93John McCall 2543f85e193739c953358c865005855253af4f68a497John McCallstatic TryEmitResult 2544f85e193739c953358c865005855253af4f68a497John McCalltryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e) { 254572dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // We should *never* see a nested full-expression here, because if 254672dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // we fail to emit at +1, our caller must not retain after we close 254772dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // out the full-expression. 254872dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall assert(!isa<ExprWithCleanups>(e)); 2549990567cb60e8530ba01b41d4e056e32b44b95ec0John McCall 2550f85e193739c953358c865005855253af4f68a497John McCall // The desired result type, if it differs from the type of the 2551f85e193739c953358c865005855253af4f68a497John McCall // ultimate opaque expression. 25522acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *resultType = 0; 2553f85e193739c953358c865005855253af4f68a497John McCall 2554f85e193739c953358c865005855253af4f68a497John McCall while (true) { 2555f85e193739c953358c865005855253af4f68a497John McCall e = e->IgnoreParens(); 2556f85e193739c953358c865005855253af4f68a497John McCall 2557f85e193739c953358c865005855253af4f68a497John McCall // There's a break at the end of this if-chain; anything 2558f85e193739c953358c865005855253af4f68a497John McCall // that wants to keep looping has to explicitly continue. 2559f85e193739c953358c865005855253af4f68a497John McCall if (const CastExpr *ce = dyn_cast<CastExpr>(e)) { 2560f85e193739c953358c865005855253af4f68a497John McCall switch (ce->getCastKind()) { 2561f85e193739c953358c865005855253af4f68a497John McCall // No-op casts don't change the type, so we just ignore them. 2562f85e193739c953358c865005855253af4f68a497John McCall case CK_NoOp: 2563f85e193739c953358c865005855253af4f68a497John McCall e = ce->getSubExpr(); 2564f85e193739c953358c865005855253af4f68a497John McCall continue; 2565f85e193739c953358c865005855253af4f68a497John McCall 2566f85e193739c953358c865005855253af4f68a497John McCall case CK_LValueToRValue: { 2567f85e193739c953358c865005855253af4f68a497John McCall TryEmitResult loadResult 2568f85e193739c953358c865005855253af4f68a497John McCall = tryEmitARCRetainLoadOfScalar(CGF, ce->getSubExpr()); 2569f85e193739c953358c865005855253af4f68a497John McCall if (resultType) { 2570f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = loadResult.getPointer(); 2571f85e193739c953358c865005855253af4f68a497John McCall value = CGF.Builder.CreateBitCast(value, resultType); 2572f85e193739c953358c865005855253af4f68a497John McCall loadResult.setPointer(value); 2573f85e193739c953358c865005855253af4f68a497John McCall } 2574f85e193739c953358c865005855253af4f68a497John McCall return loadResult; 2575f85e193739c953358c865005855253af4f68a497John McCall } 2576f85e193739c953358c865005855253af4f68a497John McCall 2577f85e193739c953358c865005855253af4f68a497John McCall // These casts can change the type, so remember that and 2578f85e193739c953358c865005855253af4f68a497John McCall // soldier on. We only need to remember the outermost such 2579f85e193739c953358c865005855253af4f68a497John McCall // cast, though. 25801d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall case CK_CPointerToObjCPointerCast: 25811d9b3b25f7ac0d0195bba6b507a684fe5e7943eeJohn McCall case CK_BlockPointerToObjCPointerCast: 2582f85e193739c953358c865005855253af4f68a497John McCall case CK_AnyPointerToBlockPointerCast: 2583f85e193739c953358c865005855253af4f68a497John McCall case CK_BitCast: 2584f85e193739c953358c865005855253af4f68a497John McCall if (!resultType) 2585f85e193739c953358c865005855253af4f68a497John McCall resultType = CGF.ConvertType(ce->getType()); 2586f85e193739c953358c865005855253af4f68a497John McCall e = ce->getSubExpr(); 2587f85e193739c953358c865005855253af4f68a497John McCall assert(e->getType()->hasPointerRepresentation()); 2588f85e193739c953358c865005855253af4f68a497John McCall continue; 2589f85e193739c953358c865005855253af4f68a497John McCall 2590f85e193739c953358c865005855253af4f68a497John McCall // For consumptions, just emit the subexpression and thus elide 2591f85e193739c953358c865005855253af4f68a497John McCall // the retain/release pair. 259233e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCConsumeObject: { 2593f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *result = CGF.EmitScalarExpr(ce->getSubExpr()); 2594f85e193739c953358c865005855253af4f68a497John McCall if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); 2595f85e193739c953358c865005855253af4f68a497John McCall return TryEmitResult(result, true); 2596f85e193739c953358c865005855253af4f68a497John McCall } 2597f85e193739c953358c865005855253af4f68a497John McCall 2598dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // Block extends are net +0. Naively, we could just recurse on 2599dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // the subexpression, but actually we need to ensure that the 2600dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // value is copied as a block, so there's a little filter here. 260133e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCExtendBlockObject: { 2602dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall llvm::Value *result; // will be a +0 value 2603dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2604dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // If we can't safely assume the sub-expression will produce a 2605dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // block-copied value, emit the sub-expression at +0. 2606dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall if (shouldEmitSeparateBlockRetain(ce->getSubExpr())) { 2607dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall result = CGF.EmitScalarExpr(ce->getSubExpr()); 2608dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2609dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // Otherwise, try to emit the sub-expression at +1 recursively. 2610dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall } else { 2611dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall TryEmitResult subresult 2612dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall = tryEmitARCRetainScalarExpr(CGF, ce->getSubExpr()); 2613dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall result = subresult.getPointer(); 2614dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2615dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // If that produced a retained value, just use that, 2616dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // possibly casting down. 2617dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall if (subresult.getInt()) { 2618dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall if (resultType) 2619dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall result = CGF.Builder.CreateBitCast(result, resultType); 2620dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return TryEmitResult(result, true); 2621dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall } 2622dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2623dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // Otherwise it's +0. 2624dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall } 2625dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 2626dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall // Retain the object as a block, then cast down. 2627348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall result = CGF.EmitARCRetainBlock(result, /*mandatory*/ true); 2628dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); 2629dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall return TryEmitResult(result, true); 2630dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall } 2631dc05b11c67331016473fbc7909827b1b89c9616bJohn McCall 26327e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall // For reclaims, emit the subexpression as a retained call and 26337e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall // skip the consumption. 263433e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall case CK_ARCReclaimReturnedObject: { 26357e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall llvm::Value *result = emitARCRetainCall(CGF, ce->getSubExpr()); 26367e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); 26377e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall return TryEmitResult(result, true); 26387e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall } 26397e5e5f4cc36fe50f46ad76dca7a266434c94f475John McCall 2640f85e193739c953358c865005855253af4f68a497John McCall default: 2641f85e193739c953358c865005855253af4f68a497John McCall break; 2642f85e193739c953358c865005855253af4f68a497John McCall } 2643f85e193739c953358c865005855253af4f68a497John McCall 2644f85e193739c953358c865005855253af4f68a497John McCall // Skip __extension__. 2645f85e193739c953358c865005855253af4f68a497John McCall } else if (const UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { 2646f85e193739c953358c865005855253af4f68a497John McCall if (op->getOpcode() == UO_Extension) { 2647f85e193739c953358c865005855253af4f68a497John McCall e = op->getSubExpr(); 2648f85e193739c953358c865005855253af4f68a497John McCall continue; 2649f85e193739c953358c865005855253af4f68a497John McCall } 2650f85e193739c953358c865005855253af4f68a497John McCall 2651f85e193739c953358c865005855253af4f68a497John McCall // For calls and message sends, use the retained-call logic. 2652f85e193739c953358c865005855253af4f68a497John McCall // Delegate inits are a special case in that they're the only 2653f85e193739c953358c865005855253af4f68a497John McCall // returns-retained expression that *isn't* surrounded by 2654f85e193739c953358c865005855253af4f68a497John McCall // a consume. 2655f85e193739c953358c865005855253af4f68a497John McCall } else if (isa<CallExpr>(e) || 2656f85e193739c953358c865005855253af4f68a497John McCall (isa<ObjCMessageExpr>(e) && 2657f85e193739c953358c865005855253af4f68a497John McCall !cast<ObjCMessageExpr>(e)->isDelegateInitCall())) { 2658f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *result = emitARCRetainCall(CGF, e); 2659f85e193739c953358c865005855253af4f68a497John McCall if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); 2660f85e193739c953358c865005855253af4f68a497John McCall return TryEmitResult(result, true); 26614b9c2d235fb9449e249d74f48ecfec601650de93John McCall 26624b9c2d235fb9449e249d74f48ecfec601650de93John McCall // Look through pseudo-object expressions. 26634b9c2d235fb9449e249d74f48ecfec601650de93John McCall } else if (const PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) { 26644b9c2d235fb9449e249d74f48ecfec601650de93John McCall TryEmitResult result 26654b9c2d235fb9449e249d74f48ecfec601650de93John McCall = tryEmitARCRetainPseudoObject(CGF, pseudo); 26664b9c2d235fb9449e249d74f48ecfec601650de93John McCall if (resultType) { 26674b9c2d235fb9449e249d74f48ecfec601650de93John McCall llvm::Value *value = result.getPointer(); 26684b9c2d235fb9449e249d74f48ecfec601650de93John McCall value = CGF.Builder.CreateBitCast(value, resultType); 26694b9c2d235fb9449e249d74f48ecfec601650de93John McCall result.setPointer(value); 26704b9c2d235fb9449e249d74f48ecfec601650de93John McCall } 26714b9c2d235fb9449e249d74f48ecfec601650de93John McCall return result; 2672f85e193739c953358c865005855253af4f68a497John McCall } 2673f85e193739c953358c865005855253af4f68a497John McCall 2674f85e193739c953358c865005855253af4f68a497John McCall // Conservatively halt the search at any other expression kind. 2675f85e193739c953358c865005855253af4f68a497John McCall break; 2676f85e193739c953358c865005855253af4f68a497John McCall } 2677f85e193739c953358c865005855253af4f68a497John McCall 2678f85e193739c953358c865005855253af4f68a497John McCall // We didn't find an obvious production, so emit what we've got and 2679f85e193739c953358c865005855253af4f68a497John McCall // tell the caller that we didn't manage to retain. 2680f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *result = CGF.EmitScalarExpr(e); 2681f85e193739c953358c865005855253af4f68a497John McCall if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); 2682f85e193739c953358c865005855253af4f68a497John McCall return TryEmitResult(result, false); 2683f85e193739c953358c865005855253af4f68a497John McCall} 2684f85e193739c953358c865005855253af4f68a497John McCall 2685f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF, 2686f85e193739c953358c865005855253af4f68a497John McCall LValue lvalue, 2687f85e193739c953358c865005855253af4f68a497John McCall QualType type) { 2688f85e193739c953358c865005855253af4f68a497John McCall TryEmitResult result = tryEmitARCRetainLoadOfScalar(CGF, lvalue, type); 2689f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = result.getPointer(); 2690f85e193739c953358c865005855253af4f68a497John McCall if (!result.getInt()) 2691f85e193739c953358c865005855253af4f68a497John McCall value = CGF.EmitARCRetain(type, value); 2692f85e193739c953358c865005855253af4f68a497John McCall return value; 2693f85e193739c953358c865005855253af4f68a497John McCall} 2694f85e193739c953358c865005855253af4f68a497John McCall 2695f85e193739c953358c865005855253af4f68a497John McCall/// EmitARCRetainScalarExpr - Semantically equivalent to 2696f85e193739c953358c865005855253af4f68a497John McCall/// EmitARCRetainObject(e->getType(), EmitScalarExpr(e)), but making a 2697f85e193739c953358c865005855253af4f68a497John McCall/// best-effort attempt to peephole expressions that naturally produce 2698f85e193739c953358c865005855253af4f68a497John McCall/// retained objects. 2699f85e193739c953358c865005855253af4f68a497John McCallllvm::Value *CodeGenFunction::EmitARCRetainScalarExpr(const Expr *e) { 270072dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // The retain needs to happen within the full-expression. 270172dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) { 270272dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall enterFullExpression(cleanups); 270372dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall RunCleanupsScope scope(*this); 270472dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall return EmitARCRetainScalarExpr(cleanups->getSubExpr()); 270572dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall } 270672dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall 2707f85e193739c953358c865005855253af4f68a497John McCall TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e); 2708f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = result.getPointer(); 2709f85e193739c953358c865005855253af4f68a497John McCall if (!result.getInt()) 2710f85e193739c953358c865005855253af4f68a497John McCall value = EmitARCRetain(e->getType(), value); 2711f85e193739c953358c865005855253af4f68a497John McCall return value; 2712f85e193739c953358c865005855253af4f68a497John McCall} 2713f85e193739c953358c865005855253af4f68a497John McCall 2714f85e193739c953358c865005855253af4f68a497John McCallllvm::Value * 2715f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCRetainAutoreleaseScalarExpr(const Expr *e) { 271672dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // The retain needs to happen within the full-expression. 271772dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) { 271872dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall enterFullExpression(cleanups); 271972dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall RunCleanupsScope scope(*this); 272072dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall return EmitARCRetainAutoreleaseScalarExpr(cleanups->getSubExpr()); 272172dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall } 272272dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall 2723f85e193739c953358c865005855253af4f68a497John McCall TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e); 2724f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = result.getPointer(); 2725f85e193739c953358c865005855253af4f68a497John McCall if (result.getInt()) 2726f85e193739c953358c865005855253af4f68a497John McCall value = EmitARCAutorelease(value); 2727f85e193739c953358c865005855253af4f68a497John McCall else 2728f85e193739c953358c865005855253af4f68a497John McCall value = EmitARCRetainAutorelease(e->getType(), value); 2729f85e193739c953358c865005855253af4f68a497John McCall return value; 2730f85e193739c953358c865005855253af4f68a497John McCall} 2731f85e193739c953358c865005855253af4f68a497John McCall 2732348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCallllvm::Value *CodeGenFunction::EmitARCExtendBlockObject(const Expr *e) { 2733348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall llvm::Value *result; 2734348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall bool doRetain; 2735348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 2736348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall if (shouldEmitSeparateBlockRetain(e)) { 2737348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall result = EmitScalarExpr(e); 2738348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall doRetain = true; 2739348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall } else { 2740348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall TryEmitResult subresult = tryEmitARCRetainScalarExpr(*this, e); 2741348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall result = subresult.getPointer(); 2742348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall doRetain = !subresult.getInt(); 2743348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall } 2744348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 2745348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall if (doRetain) 2746348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall result = EmitARCRetainBlock(result, /*mandatory*/ true); 2747348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall return EmitObjCConsumeObject(e->getType(), result); 2748348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall} 2749348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall 27502b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCallllvm::Value *CodeGenFunction::EmitObjCThrowOperand(const Expr *expr) { 27512b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // In ARC, retain and autorelease the expression. 27524e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().ObjCAutoRefCount) { 27532b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // Do so before running any cleanups for the full-expression. 275472dcecc8c2f9cdd4f0faacc62676d3bb6dba4d02John McCall // EmitARCRetainAutoreleaseScalarExpr does this for us. 27552b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall return EmitARCRetainAutoreleaseScalarExpr(expr); 27562b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall } 27572b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall 27582b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // Otherwise, use the normal scalar-expression emission. The 27592b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // exception machinery doesn't do anything special with the 27602b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // exception like retaining it, so there's no safety associated with 27612b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // only running cleanups after the throw has started, and when it 27622b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall // matters it tends to be substantially inferior code. 27632b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall return EmitScalarExpr(expr); 27642b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall} 27652b014d6c0c6b8ac94b416ac37dfc7931f20777a7John McCall 2766f85e193739c953358c865005855253af4f68a497John McCallstd::pair<LValue,llvm::Value*> 2767f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCStoreStrong(const BinaryOperator *e, 2768f85e193739c953358c865005855253af4f68a497John McCall bool ignored) { 2769f85e193739c953358c865005855253af4f68a497John McCall // Evaluate the RHS first. 2770f85e193739c953358c865005855253af4f68a497John McCall TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e->getRHS()); 2771f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = result.getPointer(); 2772f85e193739c953358c865005855253af4f68a497John McCall 2773fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall bool hasImmediateRetain = result.getInt(); 2774fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall 2775fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall // If we didn't emit a retained object, and the l-value is of block 2776fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall // type, then we need to emit the block-retain immediately in case 2777fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall // it invalidates the l-value. 2778fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall if (!hasImmediateRetain && e->getType()->isBlockPointerType()) { 2779348f16fc7c71f0d9b651cb79fd1012843073493fJohn McCall value = EmitARCRetainBlock(value, /*mandatory*/ false); 2780fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall hasImmediateRetain = true; 2781fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall } 2782fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall 2783f85e193739c953358c865005855253af4f68a497John McCall LValue lvalue = EmitLValue(e->getLHS()); 2784f85e193739c953358c865005855253af4f68a497John McCall 2785f85e193739c953358c865005855253af4f68a497John McCall // If the RHS was emitted retained, expand this. 2786fb7208119a60f68c87e7d4b6e4b87371871abb49John McCall if (hasImmediateRetain) { 2787c53143cc5f4edbd0490eefa9ecc1de4f9c24d8ddNick Lewycky llvm::Value *oldValue = EmitLoadOfScalar(lvalue, SourceLocation()); 27886da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman EmitStoreOfScalar(value, lvalue); 27895b07e8077a20b80fee90bd76c43c6150c676e4a8John McCall EmitARCRelease(oldValue, lvalue.isARCPreciseLifetime()); 2790f85e193739c953358c865005855253af4f68a497John McCall } else { 2791545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall value = EmitARCStoreStrong(lvalue, value, ignored); 2792f85e193739c953358c865005855253af4f68a497John McCall } 2793f85e193739c953358c865005855253af4f68a497John McCall 2794f85e193739c953358c865005855253af4f68a497John McCall return std::pair<LValue,llvm::Value*>(lvalue, value); 2795f85e193739c953358c865005855253af4f68a497John McCall} 2796f85e193739c953358c865005855253af4f68a497John McCall 2797f85e193739c953358c865005855253af4f68a497John McCallstd::pair<LValue,llvm::Value*> 2798f85e193739c953358c865005855253af4f68a497John McCallCodeGenFunction::EmitARCStoreAutoreleasing(const BinaryOperator *e) { 2799f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = EmitARCRetainAutoreleaseScalarExpr(e->getRHS()); 2800f85e193739c953358c865005855253af4f68a497John McCall LValue lvalue = EmitLValue(e->getLHS()); 2801f85e193739c953358c865005855253af4f68a497John McCall 28026da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman EmitStoreOfScalar(value, lvalue); 2803f85e193739c953358c865005855253af4f68a497John McCall 2804f85e193739c953358c865005855253af4f68a497John McCall return std::pair<LValue,llvm::Value*>(lvalue, value); 2805f85e193739c953358c865005855253af4f68a497John McCall} 2806f85e193739c953358c865005855253af4f68a497John McCall 2807f85e193739c953358c865005855253af4f68a497John McCallvoid CodeGenFunction::EmitObjCAutoreleasePoolStmt( 280816098f366097305c348b356a44638d6c959c6e60Eric Christopher const ObjCAutoreleasePoolStmt &ARPS) { 2809f85e193739c953358c865005855253af4f68a497John McCall const Stmt *subStmt = ARPS.getSubStmt(); 2810f85e193739c953358c865005855253af4f68a497John McCall const CompoundStmt &S = cast<CompoundStmt>(*subStmt); 2811f85e193739c953358c865005855253af4f68a497John McCall 2812f85e193739c953358c865005855253af4f68a497John McCall CGDebugInfo *DI = getDebugInfo(); 281373fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher if (DI) 281473fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher DI->EmitLexicalBlockStart(Builder, S.getLBracLoc()); 2815f85e193739c953358c865005855253af4f68a497John McCall 2816f85e193739c953358c865005855253af4f68a497John McCall // Keep track of the current cleanup stack depth. 2817f85e193739c953358c865005855253af4f68a497John McCall RunCleanupsScope Scope(*this); 28180a7dd788dbef975f35f273c7ab913f480f7edd60John McCall if (CGM.getLangOpts().ObjCRuntime.hasNativeARC()) { 2819f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *token = EmitObjCAutoreleasePoolPush(); 2820f85e193739c953358c865005855253af4f68a497John McCall EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, token); 2821f85e193739c953358c865005855253af4f68a497John McCall } else { 2822f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *token = EmitObjCMRRAutoreleasePoolPush(); 2823f85e193739c953358c865005855253af4f68a497John McCall EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, token); 2824f85e193739c953358c865005855253af4f68a497John McCall } 2825f85e193739c953358c865005855253af4f68a497John McCall 2826f85e193739c953358c865005855253af4f68a497John McCall for (CompoundStmt::const_body_iterator I = S.body_begin(), 2827f85e193739c953358c865005855253af4f68a497John McCall E = S.body_end(); I != E; ++I) 2828f85e193739c953358c865005855253af4f68a497John McCall EmitStmt(*I); 2829f85e193739c953358c865005855253af4f68a497John McCall 283073fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher if (DI) 283173fb35003aad027492e661a3749e921b5d1ecaf9Eric Christopher DI->EmitLexicalBlockEnd(Builder, S.getRBracLoc()); 2832f85e193739c953358c865005855253af4f68a497John McCall} 28330c24c808e4dce43e88abf2d5f98546b901bd4315John McCall 28340c24c808e4dce43e88abf2d5f98546b901bd4315John McCall/// EmitExtendGCLifetime - Given a pointer to an Objective-C object, 28350c24c808e4dce43e88abf2d5f98546b901bd4315John McCall/// make sure it survives garbage collection until this point. 28360c24c808e4dce43e88abf2d5f98546b901bd4315John McCallvoid CodeGenFunction::EmitExtendGCLifetime(llvm::Value *object) { 28370c24c808e4dce43e88abf2d5f98546b901bd4315John McCall // We just use an inline assembly. 28380c24c808e4dce43e88abf2d5f98546b901bd4315John McCall llvm::FunctionType *extenderType 2839de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall = llvm::FunctionType::get(VoidTy, VoidPtrTy, RequiredArgs::All); 28400c24c808e4dce43e88abf2d5f98546b901bd4315John McCall llvm::Value *extender 28410c24c808e4dce43e88abf2d5f98546b901bd4315John McCall = llvm::InlineAsm::get(extenderType, 28420c24c808e4dce43e88abf2d5f98546b901bd4315John McCall /* assembly */ "", 28430c24c808e4dce43e88abf2d5f98546b901bd4315John McCall /* constraints */ "r", 28440c24c808e4dce43e88abf2d5f98546b901bd4315John McCall /* side effects */ true); 28450c24c808e4dce43e88abf2d5f98546b901bd4315John McCall 28460c24c808e4dce43e88abf2d5f98546b901bd4315John McCall object = Builder.CreateBitCast(object, VoidPtrTy); 2847bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall EmitNounwindRuntimeCall(extender, object); 28480c24c808e4dce43e88abf2d5f98546b901bd4315John McCall} 28490c24c808e4dce43e88abf2d5f98546b901bd4315John McCall 285020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian/// GenerateObjCAtomicSetterCopyHelperFunction - Given a c++ object type with 285184e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian/// non-trivial copy assignment function, produce following helper function. 285284e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian/// static void copyHelper(Ty *dest, const Ty *source) { *dest = *source; } 285384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian/// 285484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanianllvm::Constant * 285520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz JahanianCodeGenFunction::GenerateObjCAtomicSetterCopyHelperFunction( 285620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian const ObjCPropertyImplDecl *PID) { 2857260611a32535c851237926bfcf78869b13c07d5bJohn McCall if (!getLangOpts().CPlusPlus || 285890f692611bd486198ffa67a0e097013a2e6e3d2eRafael Espindola !getLangOpts().ObjCRuntime.hasAtomicCopyHelper()) 285984e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian return 0; 286084e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian QualType Ty = PID->getPropertyIvarDecl()->getType(); 286184e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian if (!Ty->isRecordType()) 286284e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian return 0; 286384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 286420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if ((!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_atomic))) 286584e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian return 0; 2866b08cfb3c5909752c0e501a4e5c31a507a4cc1f72Fariborz Jahanian llvm::Constant * HelperFn = 0; 286720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if (hasTrivialSetExpr(PID)) 286820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return 0; 286920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian assert(PID->getSetterCXXAssignment() && "SetterCXXAssignment - null"); 287020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if ((HelperFn = CGM.getAtomicSetterHelperFnMap(Ty))) 287120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return HelperFn; 287284e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 287384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian ASTContext &C = getContext(); 287484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian IdentifierInfo *II 287520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian = &CGM.getContext().Idents.get("__assign_helper_atomic_property_"); 287684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian FunctionDecl *FD = FunctionDecl::Create(C, 287784e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian C.getTranslationUnitDecl(), 287884e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian SourceLocation(), 287984e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian SourceLocation(), II, C.VoidTy, 0, 288084e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian SC_Static, 288184e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian false, 2882b92bd4b3271b7892abe9fd8c74fb54a27ad702abEric Christopher false); 288384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 288484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian QualType DestTy = C.getPointerType(Ty); 288584e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian QualType SrcTy = Ty; 288684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian SrcTy.addConst(); 288784e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian SrcTy = C.getPointerType(SrcTy); 288884e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 288984e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian FunctionArgList args; 289084e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian ImplicitParamDecl dstDecl(FD, SourceLocation(), 0, DestTy); 289184e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian args.push_back(&dstDecl); 289284e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian ImplicitParamDecl srcDecl(FD, SourceLocation(), 0, SrcTy); 289384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian args.push_back(&srcDecl); 289484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 289584e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian const CGFunctionInfo &FI = 2896de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall CGM.getTypes().arrangeFunctionDeclaration(C.VoidTy, args, 2897de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall FunctionType::ExtInfo(), 2898de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall RequiredArgs::All); 289984e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 2900de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI); 290184e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 290284e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian llvm::Function *Fn = 290384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 290416098f366097305c348b356a44638d6c959c6e60Eric Christopher "__assign_helper_atomic_property_", 290516098f366097305c348b356a44638d6c959c6e60Eric Christopher &CGM.getModule()); 290684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 290784e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation()); 290884e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 2909f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr DstExpr(&dstDecl, false, DestTy, 2910f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_RValue, SourceLocation()); 2911f4b88a45902af1802a1cb42ba48b1c474474f228John McCall UnaryOperator DST(&DstExpr, UO_Deref, DestTy->getPointeeType(), 2912f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_LValue, OK_Ordinary, SourceLocation()); 291384e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 2914f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr SrcExpr(&srcDecl, false, SrcTy, 2915f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_RValue, SourceLocation()); 2916f4b88a45902af1802a1cb42ba48b1c474474f228John McCall UnaryOperator SRC(&SrcExpr, UO_Deref, SrcTy->getPointeeType(), 2917f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_LValue, OK_Ordinary, SourceLocation()); 291884e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 2919f4b88a45902af1802a1cb42ba48b1c474474f228John McCall Expr *Args[2] = { &DST, &SRC }; 292020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CallExpr *CalleeExp = cast<CallExpr>(PID->getSetterCXXAssignment()); 2921f4b88a45902af1802a1cb42ba48b1c474474f228John McCall CXXOperatorCallExpr TheCall(C, OO_Equal, CalleeExp->getCallee(), 29223b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer Args, DestTy->getPointeeType(), 2923be9af1288881110e406b87914162eaa59f1e5918Lang Hames VK_LValue, SourceLocation(), false); 2924f4b88a45902af1802a1cb42ba48b1c474474f228John McCall 2925f4b88a45902af1802a1cb42ba48b1c474474f228John McCall EmitStmt(&TheCall); 292684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 292784e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian FinishFunction(); 2928cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); 292920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CGM.setAtomicSetterHelperFnMap(Ty, HelperFn); 2930cd93b96bc51c255d104095bc6a6d8eac74a84b1eFariborz Jahanian return HelperFn; 293120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian} 293220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 293320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanianllvm::Constant * 293420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz JahanianCodeGenFunction::GenerateObjCAtomicGetterCopyHelperFunction( 293520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian const ObjCPropertyImplDecl *PID) { 2936260611a32535c851237926bfcf78869b13c07d5bJohn McCall if (!getLangOpts().CPlusPlus || 293790f692611bd486198ffa67a0e097013a2e6e3d2eRafael Espindola !getLangOpts().ObjCRuntime.hasAtomicCopyHelper()) 293820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return 0; 293920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 294020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian QualType Ty = PD->getType(); 294120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if (!Ty->isRecordType()) 294220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return 0; 294320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if ((!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_atomic))) 294420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return 0; 294520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Constant * HelperFn = 0; 294620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 294720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if (hasTrivialGetExpr(PID)) 294820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return 0; 294920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian assert(PID->getGetterCXXConstructor() && "getGetterCXXConstructor - null"); 295020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian if ((HelperFn = CGM.getAtomicGetterHelperFnMap(Ty))) 295120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return HelperFn; 295220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 295320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 295420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ASTContext &C = getContext(); 295520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian IdentifierInfo *II 295620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian = &CGM.getContext().Idents.get("__copy_helper_atomic_property_"); 295720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian FunctionDecl *FD = FunctionDecl::Create(C, 295820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian C.getTranslationUnitDecl(), 295920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SourceLocation(), 296020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SourceLocation(), II, C.VoidTy, 0, 296120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SC_Static, 296220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian false, 2963b92bd4b3271b7892abe9fd8c74fb54a27ad702abEric Christopher false); 296420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 296520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian QualType DestTy = C.getPointerType(Ty); 296620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian QualType SrcTy = Ty; 296720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SrcTy.addConst(); 296820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SrcTy = C.getPointerType(SrcTy); 296920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 297020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian FunctionArgList args; 297120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ImplicitParamDecl dstDecl(FD, SourceLocation(), 0, DestTy); 297220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian args.push_back(&dstDecl); 297320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ImplicitParamDecl srcDecl(FD, SourceLocation(), 0, SrcTy); 297420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian args.push_back(&srcDecl); 297520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 297620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian const CGFunctionInfo &FI = 2977de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall CGM.getTypes().arrangeFunctionDeclaration(C.VoidTy, args, 2978de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall FunctionType::ExtInfo(), 2979de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall RequiredArgs::All); 298020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 2981de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI); 298220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 298320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Function *Fn = 298420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 298520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian "__copy_helper_atomic_property_", &CGM.getModule()); 298684e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 298720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation()); 298820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 2989f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr SrcExpr(&srcDecl, false, SrcTy, 299020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian VK_RValue, SourceLocation()); 299120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 2992f4b88a45902af1802a1cb42ba48b1c474474f228John McCall UnaryOperator SRC(&SrcExpr, UO_Deref, SrcTy->getPointeeType(), 2993f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_LValue, OK_Ordinary, SourceLocation()); 299420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 299520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstructExpr *CXXConstExpr = 299620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian cast<CXXConstructExpr>(PID->getGetterCXXConstructor()); 299720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 299820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian SmallVector<Expr*, 4> ConstructorArgs; 2999f4b88a45902af1802a1cb42ba48b1c474474f228John McCall ConstructorArgs.push_back(&SRC); 300020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstructExpr::arg_iterator A = CXXConstExpr->arg_begin(); 300120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ++A; 300220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 300320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian for (CXXConstructExpr::arg_iterator AEnd = CXXConstExpr->arg_end(); 300420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian A != AEnd; ++A) 300520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian ConstructorArgs.push_back(*A); 300620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 300720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstructExpr *TheCXXConstructExpr = 300820abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstructExpr::Create(C, Ty, SourceLocation(), 300920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstExpr->getConstructor(), 301020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstExpr->isElidable(), 30113b6bef9a213249c6ab6d67c07b1ac6380961be3eBenjamin Kramer ConstructorArgs, 30125b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl CXXConstExpr->hadMultipleCandidates(), 30135b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl CXXConstExpr->isListInitialization(), 301420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CXXConstExpr->requiresZeroInitialization(), 301516098f366097305c348b356a44638d6c959c6e60Eric Christopher CXXConstExpr->getConstructionKind(), 301616098f366097305c348b356a44638d6c959c6e60Eric Christopher SourceRange()); 301720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 3018f4b88a45902af1802a1cb42ba48b1c474474f228John McCall DeclRefExpr DstExpr(&dstDecl, false, DestTy, 3019f4b88a45902af1802a1cb42ba48b1c474474f228John McCall VK_RValue, SourceLocation()); 302020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 3021f4b88a45902af1802a1cb42ba48b1c474474f228John McCall RValue DV = EmitAnyExpr(&DstExpr); 302216098f366097305c348b356a44638d6c959c6e60Eric Christopher CharUnits Alignment 302316098f366097305c348b356a44638d6c959c6e60Eric Christopher = getContext().getTypeAlignInChars(TheCXXConstructExpr->getType()); 302420abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian EmitAggExpr(TheCXXConstructExpr, 302520abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian AggValueSlot::forAddr(DV.getScalarVal(), Alignment, Qualifiers(), 302620abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian AggValueSlot::IsDestructed, 302720abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian AggValueSlot::DoesNotNeedGCBarriers, 3028649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier AggValueSlot::IsNotAliased)); 302920abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian 303020abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian FinishFunction(); 303120abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); 303220abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian CGM.setAtomicGetterHelperFnMap(Ty, HelperFn); 303320abee6b95c4f5a61e471b4b616439280ca4a81bFariborz Jahanian return HelperFn; 303484e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian} 303584e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 3036cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedmanllvm::Value * 3037cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli FriedmanCodeGenFunction::EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty) { 3038cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman // Get selectors for retain/autorelease. 30398c72a7db34a63c38c6065b73fda4aeb0fe19eb65Eli Friedman IdentifierInfo *CopyID = &getContext().Idents.get("copy"); 30408c72a7db34a63c38c6065b73fda4aeb0fe19eb65Eli Friedman Selector CopySelector = 30418c72a7db34a63c38c6065b73fda4aeb0fe19eb65Eli Friedman getContext().Selectors.getNullarySelector(CopyID); 3042cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman IdentifierInfo *AutoreleaseID = &getContext().Idents.get("autorelease"); 3043cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Selector AutoreleaseSelector = 3044cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman getContext().Selectors.getNullarySelector(AutoreleaseID); 3045cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman 3046cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman // Emit calls to retain/autorelease. 3047cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 3048cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman llvm::Value *Val = Block; 3049cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman RValue Result; 3050cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 30518c72a7db34a63c38c6065b73fda4aeb0fe19eb65Eli Friedman Ty, CopySelector, 3052cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Val, CallArgList(), 0, 0); 3053cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Val = Result.getScalarVal(); 3054cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 3055cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Ty, AutoreleaseSelector, 3056cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Val, CallArgList(), 0, 0); 3057cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman Val = Result.getScalarVal(); 3058cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman return Val; 3059cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman} 3060cae40c4d448529fe65de3d87bdbe97d3b54b4d98Eli Friedman 306184e49865cbc2392787efcf2211ec53a947128a9eFariborz Jahanian 30622979ec73b4f974d85f2ce84167712177a44c6f09Ted KremenekCGObjCRuntime::~CGObjCRuntime() {} 3063