1193326Sed//===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===// 2193326Sed// 3193326Sed// The LLVM Compiler Infrastructure 4193326Sed// 5193326Sed// This file is distributed under the University of Illinois Open Source 6193326Sed// License. See LICENSE.TXT for details. 7193326Sed// 8193326Sed//===----------------------------------------------------------------------===// 9193326Sed// 10193326Sed// This contains code to emit Objective-C code as LLVM code. 11193326Sed// 12193326Sed//===----------------------------------------------------------------------===// 13193326Sed 14218893Sdim#include "CGDebugInfo.h" 15193326Sed#include "CGObjCRuntime.h" 16193326Sed#include "CodeGenFunction.h" 17193326Sed#include "CodeGenModule.h" 18224145Sdim#include "TargetInfo.h" 19193326Sed#include "clang/AST/ASTContext.h" 20193326Sed#include "clang/AST/DeclObjC.h" 21193326Sed#include "clang/AST/StmtObjC.h" 22193326Sed#include "clang/Basic/Diagnostic.h" 23193326Sed#include "llvm/ADT/STLExtras.h" 24251662Sdim#include "llvm/Support/CallSite.h" 25249423Sdim#include "llvm/IR/DataLayout.h" 26249423Sdim#include "llvm/IR/InlineAsm.h" 27193326Sedusing namespace clang; 28193326Sedusing namespace CodeGen; 29193326Sed 30224145Sdimtypedef llvm::PointerIntPair<llvm::Value*,1,bool> TryEmitResult; 31224145Sdimstatic TryEmitResult 32224145SdimtryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e); 33234353Sdimstatic RValue AdjustRelatedResultType(CodeGenFunction &CGF, 34239462Sdim QualType ET, 35234353Sdim const ObjCMethodDecl *Method, 36234353Sdim RValue Result); 37224145Sdim 38224145Sdim/// Given the address of a variable of pointer type, find the correct 39224145Sdim/// null to store into it. 40224145Sdimstatic llvm::Constant *getNullForVariable(llvm::Value *addr) { 41226633Sdim llvm::Type *type = 42224145Sdim cast<llvm::PointerType>(addr->getType())->getElementType(); 43224145Sdim return llvm::ConstantPointerNull::get(cast<llvm::PointerType>(type)); 44224145Sdim} 45224145Sdim 46193326Sed/// Emits an instance of NSConstantString representing the object. 47198092Srdivackyllvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E) 48193326Sed{ 49202879Srdivacky llvm::Constant *C = 50202879Srdivacky CGM.getObjCRuntime().GenerateConstantString(E->getString()); 51193326Sed // FIXME: This bitcast should just be made an invariant on the Runtime. 52193326Sed return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType())); 53193326Sed} 54193326Sed 55239462Sdim/// EmitObjCBoxedExpr - This routine generates code to call 56239462Sdim/// the appropriate expression boxing method. This will either be 57239462Sdim/// one of +[NSNumber numberWith<Type>:], or +[NSString stringWithUTF8String:]. 58234353Sdim/// 59234353Sdimllvm::Value * 60239462SdimCodeGenFunction::EmitObjCBoxedExpr(const ObjCBoxedExpr *E) { 61234353Sdim // Generate the correct selector for this literal's concrete type. 62239462Sdim const Expr *SubExpr = E->getSubExpr(); 63234353Sdim // Get the method. 64239462Sdim const ObjCMethodDecl *BoxingMethod = E->getBoxingMethod(); 65239462Sdim assert(BoxingMethod && "BoxingMethod is null"); 66239462Sdim assert(BoxingMethod->isClassMethod() && "BoxingMethod must be a class method"); 67239462Sdim Selector Sel = BoxingMethod->getSelector(); 68234353Sdim 69234353Sdim // Generate a reference to the class pointer, which will be the receiver. 70239462Sdim // Assumes that the method was introduced in the class that should be 71239462Sdim // messaged (avoids pulling it out of the result type). 72234353Sdim CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 73239462Sdim const ObjCInterfaceDecl *ClassDecl = BoxingMethod->getClassInterface(); 74249423Sdim llvm::Value *Receiver = Runtime.GetClass(*this, ClassDecl); 75239462Sdim 76239462Sdim const ParmVarDecl *argDecl = *BoxingMethod->param_begin(); 77234353Sdim QualType ArgQT = argDecl->getType().getUnqualifiedType(); 78239462Sdim RValue RV = EmitAnyExpr(SubExpr); 79234353Sdim CallArgList Args; 80234353Sdim Args.add(RV, ArgQT); 81239462Sdim 82234353Sdim RValue result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 83239462Sdim BoxingMethod->getResultType(), Sel, Receiver, Args, 84239462Sdim ClassDecl, BoxingMethod); 85234353Sdim return Builder.CreateBitCast(result.getScalarVal(), 86234353Sdim ConvertType(E->getType())); 87234353Sdim} 88234353Sdim 89234353Sdimllvm::Value *CodeGenFunction::EmitObjCCollectionLiteral(const Expr *E, 90234353Sdim const ObjCMethodDecl *MethodWithObjects) { 91234353Sdim ASTContext &Context = CGM.getContext(); 92234353Sdim const ObjCDictionaryLiteral *DLE = 0; 93234353Sdim const ObjCArrayLiteral *ALE = dyn_cast<ObjCArrayLiteral>(E); 94234353Sdim if (!ALE) 95234353Sdim DLE = cast<ObjCDictionaryLiteral>(E); 96234353Sdim 97234353Sdim // Compute the type of the array we're initializing. 98234353Sdim uint64_t NumElements = 99234353Sdim ALE ? ALE->getNumElements() : DLE->getNumElements(); 100234353Sdim llvm::APInt APNumElements(Context.getTypeSize(Context.getSizeType()), 101234353Sdim NumElements); 102234353Sdim QualType ElementType = Context.getObjCIdType().withConst(); 103234353Sdim QualType ElementArrayType 104234353Sdim = Context.getConstantArrayType(ElementType, APNumElements, 105234353Sdim ArrayType::Normal, /*IndexTypeQuals=*/0); 106234353Sdim 107234353Sdim // Allocate the temporary array(s). 108234353Sdim llvm::Value *Objects = CreateMemTemp(ElementArrayType, "objects"); 109234353Sdim llvm::Value *Keys = 0; 110234353Sdim if (DLE) 111234353Sdim Keys = CreateMemTemp(ElementArrayType, "keys"); 112234353Sdim 113249423Sdim // In ARC, we may need to do extra work to keep all the keys and 114249423Sdim // values alive until after the call. 115249423Sdim SmallVector<llvm::Value *, 16> NeededObjects; 116249423Sdim bool TrackNeededObjects = 117249423Sdim (getLangOpts().ObjCAutoRefCount && 118249423Sdim CGM.getCodeGenOpts().OptimizationLevel != 0); 119249423Sdim 120234353Sdim // Perform the actual initialialization of the array(s). 121234353Sdim for (uint64_t i = 0; i < NumElements; i++) { 122234353Sdim if (ALE) { 123249423Sdim // Emit the element and store it to the appropriate array slot. 124234353Sdim const Expr *Rhs = ALE->getElement(i); 125234353Sdim LValue LV = LValue::MakeAddr(Builder.CreateStructGEP(Objects, i), 126234353Sdim ElementType, 127234353Sdim Context.getTypeAlignInChars(Rhs->getType()), 128234353Sdim Context); 129249423Sdim 130249423Sdim llvm::Value *value = EmitScalarExpr(Rhs); 131249423Sdim EmitStoreThroughLValue(RValue::get(value), LV, true); 132249423Sdim if (TrackNeededObjects) { 133249423Sdim NeededObjects.push_back(value); 134249423Sdim } 135234353Sdim } else { 136249423Sdim // Emit the key and store it to the appropriate array slot. 137234353Sdim const Expr *Key = DLE->getKeyValueElement(i).Key; 138234353Sdim LValue KeyLV = LValue::MakeAddr(Builder.CreateStructGEP(Keys, i), 139234353Sdim ElementType, 140234353Sdim Context.getTypeAlignInChars(Key->getType()), 141234353Sdim Context); 142249423Sdim llvm::Value *keyValue = EmitScalarExpr(Key); 143249423Sdim EmitStoreThroughLValue(RValue::get(keyValue), KeyLV, /*isInit=*/true); 144234353Sdim 145249423Sdim // Emit the value and store it to the appropriate array slot. 146234353Sdim const Expr *Value = DLE->getKeyValueElement(i).Value; 147234353Sdim LValue ValueLV = LValue::MakeAddr(Builder.CreateStructGEP(Objects, i), 148234353Sdim ElementType, 149234353Sdim Context.getTypeAlignInChars(Value->getType()), 150234353Sdim Context); 151249423Sdim llvm::Value *valueValue = EmitScalarExpr(Value); 152249423Sdim EmitStoreThroughLValue(RValue::get(valueValue), ValueLV, /*isInit=*/true); 153249423Sdim if (TrackNeededObjects) { 154249423Sdim NeededObjects.push_back(keyValue); 155249423Sdim NeededObjects.push_back(valueValue); 156249423Sdim } 157234353Sdim } 158234353Sdim } 159234353Sdim 160234353Sdim // Generate the argument list. 161234353Sdim CallArgList Args; 162234353Sdim ObjCMethodDecl::param_const_iterator PI = MethodWithObjects->param_begin(); 163234353Sdim const ParmVarDecl *argDecl = *PI++; 164234353Sdim QualType ArgQT = argDecl->getType().getUnqualifiedType(); 165234353Sdim Args.add(RValue::get(Objects), ArgQT); 166234353Sdim if (DLE) { 167234353Sdim argDecl = *PI++; 168234353Sdim ArgQT = argDecl->getType().getUnqualifiedType(); 169234353Sdim Args.add(RValue::get(Keys), ArgQT); 170234353Sdim } 171234353Sdim argDecl = *PI; 172234353Sdim ArgQT = argDecl->getType().getUnqualifiedType(); 173234353Sdim llvm::Value *Count = 174234353Sdim llvm::ConstantInt::get(CGM.getTypes().ConvertType(ArgQT), NumElements); 175234353Sdim Args.add(RValue::get(Count), ArgQT); 176234353Sdim 177234353Sdim // Generate a reference to the class pointer, which will be the receiver. 178234353Sdim Selector Sel = MethodWithObjects->getSelector(); 179234353Sdim QualType ResultType = E->getType(); 180234353Sdim const ObjCObjectPointerType *InterfacePointerType 181234353Sdim = ResultType->getAsObjCInterfacePointerType(); 182234353Sdim ObjCInterfaceDecl *Class 183234353Sdim = InterfacePointerType->getObjectType()->getInterface(); 184234353Sdim CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 185249423Sdim llvm::Value *Receiver = Runtime.GetClass(*this, Class); 186234353Sdim 187234353Sdim // Generate the message send. 188234353Sdim RValue result 189234353Sdim = Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 190234353Sdim MethodWithObjects->getResultType(), 191234353Sdim Sel, 192234353Sdim Receiver, Args, Class, 193234353Sdim MethodWithObjects); 194249423Sdim 195249423Sdim // The above message send needs these objects, but in ARC they are 196249423Sdim // passed in a buffer that is essentially __unsafe_unretained. 197249423Sdim // Therefore we must prevent the optimizer from releasing them until 198249423Sdim // after the call. 199249423Sdim if (TrackNeededObjects) { 200249423Sdim EmitARCIntrinsicUse(NeededObjects); 201249423Sdim } 202249423Sdim 203234353Sdim return Builder.CreateBitCast(result.getScalarVal(), 204234353Sdim ConvertType(E->getType())); 205234353Sdim} 206234353Sdim 207234353Sdimllvm::Value *CodeGenFunction::EmitObjCArrayLiteral(const ObjCArrayLiteral *E) { 208234353Sdim return EmitObjCCollectionLiteral(E, E->getArrayWithObjectsMethod()); 209234353Sdim} 210234353Sdim 211234353Sdimllvm::Value *CodeGenFunction::EmitObjCDictionaryLiteral( 212234353Sdim const ObjCDictionaryLiteral *E) { 213234353Sdim return EmitObjCCollectionLiteral(E, E->getDictWithObjectsMethod()); 214234353Sdim} 215234353Sdim 216193326Sed/// Emit a selector. 217193326Sedllvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) { 218193326Sed // Untyped selector. 219193326Sed // Note that this implementation allows for non-constant strings to be passed 220193326Sed // as arguments to @selector(). Currently, the only thing preventing this 221193326Sed // behaviour is the type checking in the front end. 222249423Sdim return CGM.getObjCRuntime().GetSelector(*this, E->getSelector()); 223193326Sed} 224193326Sed 225193326Sedllvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) { 226193326Sed // FIXME: This should pass the Decl not the name. 227249423Sdim return CGM.getObjCRuntime().GenerateProtocolRef(*this, E->getProtocol()); 228193326Sed} 229193326Sed 230223017Sdim/// \brief Adjust the type of the result of an Objective-C message send 231223017Sdim/// expression when the method has a related result type. 232223017Sdimstatic RValue AdjustRelatedResultType(CodeGenFunction &CGF, 233239462Sdim QualType ExpT, 234223017Sdim const ObjCMethodDecl *Method, 235223017Sdim RValue Result) { 236223017Sdim if (!Method) 237223017Sdim return Result; 238224145Sdim 239223017Sdim if (!Method->hasRelatedResultType() || 240239462Sdim CGF.getContext().hasSameType(ExpT, Method->getResultType()) || 241223017Sdim !Result.isScalar()) 242223017Sdim return Result; 243223017Sdim 244223017Sdim // We have applied a related result type. Cast the rvalue appropriately. 245223017Sdim return RValue::get(CGF.Builder.CreateBitCast(Result.getScalarVal(), 246239462Sdim CGF.ConvertType(ExpT))); 247223017Sdim} 248193326Sed 249226633Sdim/// Decide whether to extend the lifetime of the receiver of a 250226633Sdim/// returns-inner-pointer message. 251226633Sdimstatic bool 252226633SdimshouldExtendReceiverForInnerPointerMessage(const ObjCMessageExpr *message) { 253226633Sdim switch (message->getReceiverKind()) { 254226633Sdim 255226633Sdim // For a normal instance message, we should extend unless the 256226633Sdim // receiver is loaded from a variable with precise lifetime. 257226633Sdim case ObjCMessageExpr::Instance: { 258226633Sdim const Expr *receiver = message->getInstanceReceiver(); 259226633Sdim const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(receiver); 260226633Sdim if (!ice || ice->getCastKind() != CK_LValueToRValue) return true; 261226633Sdim receiver = ice->getSubExpr()->IgnoreParens(); 262226633Sdim 263226633Sdim // Only __strong variables. 264226633Sdim if (receiver->getType().getObjCLifetime() != Qualifiers::OCL_Strong) 265226633Sdim return true; 266226633Sdim 267226633Sdim // All ivars and fields have precise lifetime. 268226633Sdim if (isa<MemberExpr>(receiver) || isa<ObjCIvarRefExpr>(receiver)) 269226633Sdim return false; 270226633Sdim 271226633Sdim // Otherwise, check for variables. 272226633Sdim const DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(ice->getSubExpr()); 273226633Sdim if (!declRef) return true; 274226633Sdim const VarDecl *var = dyn_cast<VarDecl>(declRef->getDecl()); 275226633Sdim if (!var) return true; 276226633Sdim 277226633Sdim // All variables have precise lifetime except local variables with 278226633Sdim // automatic storage duration that aren't specially marked. 279226633Sdim return (var->hasLocalStorage() && 280226633Sdim !var->hasAttr<ObjCPreciseLifetimeAttr>()); 281226633Sdim } 282226633Sdim 283226633Sdim case ObjCMessageExpr::Class: 284226633Sdim case ObjCMessageExpr::SuperClass: 285226633Sdim // It's never necessary for class objects. 286226633Sdim return false; 287226633Sdim 288226633Sdim case ObjCMessageExpr::SuperInstance: 289226633Sdim // We generally assume that 'self' lives throughout a method call. 290226633Sdim return false; 291226633Sdim } 292226633Sdim 293226633Sdim llvm_unreachable("invalid receiver kind"); 294226633Sdim} 295226633Sdim 296208600SrdivackyRValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E, 297208600Srdivacky ReturnValueSlot Return) { 298193326Sed // Only the lookup mechanism and first two arguments of the method 299193326Sed // implementation vary between runtimes. We can get the receiver and 300193326Sed // arguments in generic code. 301198092Srdivacky 302224145Sdim bool isDelegateInit = E->isDelegateInitCall(); 303224145Sdim 304226633Sdim const ObjCMethodDecl *method = E->getMethodDecl(); 305226633Sdim 306224145Sdim // We don't retain the receiver in delegate init calls, and this is 307224145Sdim // safe because the receiver value is always loaded from 'self', 308224145Sdim // which we zero out. We don't want to Block_copy block receivers, 309224145Sdim // though. 310224145Sdim bool retainSelf = 311224145Sdim (!isDelegateInit && 312234353Sdim CGM.getLangOpts().ObjCAutoRefCount && 313226633Sdim method && 314226633Sdim method->hasAttr<NSConsumesSelfAttr>()); 315224145Sdim 316193326Sed CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 317193326Sed bool isSuperMessage = false; 318193326Sed bool isClassMessage = false; 319207619Srdivacky ObjCInterfaceDecl *OID = 0; 320193326Sed // Find the receiver 321223017Sdim QualType ReceiverType; 322207619Srdivacky llvm::Value *Receiver = 0; 323207619Srdivacky switch (E->getReceiverKind()) { 324207619Srdivacky case ObjCMessageExpr::Instance: 325223017Sdim ReceiverType = E->getInstanceReceiver()->getType(); 326224145Sdim if (retainSelf) { 327224145Sdim TryEmitResult ter = tryEmitARCRetainScalarExpr(*this, 328224145Sdim E->getInstanceReceiver()); 329224145Sdim Receiver = ter.getPointer(); 330226633Sdim if (ter.getInt()) retainSelf = false; 331224145Sdim } else 332224145Sdim Receiver = EmitScalarExpr(E->getInstanceReceiver()); 333207619Srdivacky break; 334193326Sed 335207619Srdivacky case ObjCMessageExpr::Class: { 336223017Sdim ReceiverType = E->getClassReceiver(); 337223017Sdim const ObjCObjectType *ObjTy = ReceiverType->getAs<ObjCObjectType>(); 338208600Srdivacky assert(ObjTy && "Invalid Objective-C class message send"); 339208600Srdivacky OID = ObjTy->getInterface(); 340208600Srdivacky assert(OID && "Invalid Objective-C class message send"); 341249423Sdim Receiver = Runtime.GetClass(*this, OID); 342207619Srdivacky isClassMessage = true; 343207619Srdivacky break; 344207619Srdivacky } 345198092Srdivacky 346207619Srdivacky case ObjCMessageExpr::SuperInstance: 347223017Sdim ReceiverType = E->getSuperType(); 348207619Srdivacky Receiver = LoadObjCSelf(); 349193326Sed isSuperMessage = true; 350207619Srdivacky break; 351207619Srdivacky 352207619Srdivacky case ObjCMessageExpr::SuperClass: 353223017Sdim ReceiverType = E->getSuperType(); 354193326Sed Receiver = LoadObjCSelf(); 355207619Srdivacky isSuperMessage = true; 356207619Srdivacky isClassMessage = true; 357207619Srdivacky break; 358193326Sed } 359193326Sed 360226633Sdim if (retainSelf) 361226633Sdim Receiver = EmitARCRetainNonBlock(Receiver); 362226633Sdim 363226633Sdim // In ARC, we sometimes want to "extend the lifetime" 364226633Sdim // (i.e. retain+autorelease) of receivers of returns-inner-pointer 365226633Sdim // messages. 366234353Sdim if (getLangOpts().ObjCAutoRefCount && method && 367226633Sdim method->hasAttr<ObjCReturnsInnerPointerAttr>() && 368226633Sdim shouldExtendReceiverForInnerPointerMessage(E)) 369226633Sdim Receiver = EmitARCRetainAutorelease(ReceiverType, Receiver); 370226633Sdim 371224145Sdim QualType ResultType = 372226633Sdim method ? method->getResultType() : E->getType(); 373224145Sdim 374193326Sed CallArgList Args; 375226633Sdim EmitCallArgs(Args, method, E->arg_begin(), E->arg_end()); 376198092Srdivacky 377224145Sdim // For delegate init calls in ARC, do an unsafe store of null into 378224145Sdim // self. This represents the call taking direct ownership of that 379224145Sdim // value. We have to do this after emitting the other call 380224145Sdim // arguments because they might also reference self, but we don't 381224145Sdim // have to worry about any of them modifying self because that would 382224145Sdim // be an undefined read and write of an object in unordered 383224145Sdim // expressions. 384224145Sdim if (isDelegateInit) { 385234353Sdim assert(getLangOpts().ObjCAutoRefCount && 386224145Sdim "delegate init calls should only be marked in ARC"); 387210299Sed 388224145Sdim // Do an unsafe store of null into self. 389224145Sdim llvm::Value *selfAddr = 390224145Sdim LocalDeclMap[cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()]; 391224145Sdim assert(selfAddr && "no self entry for a delegate init call?"); 392224145Sdim 393224145Sdim Builder.CreateStore(getNullForVariable(selfAddr), selfAddr); 394224145Sdim } 395224145Sdim 396223017Sdim RValue result; 397193326Sed if (isSuperMessage) { 398193326Sed // super is only valid in an Objective-C method 399193326Sed const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); 400193326Sed bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext()); 401223017Sdim result = Runtime.GenerateMessageSendSuper(*this, Return, ResultType, 402223017Sdim E->getSelector(), 403223017Sdim OMD->getClassInterface(), 404223017Sdim isCategoryImpl, 405223017Sdim Receiver, 406223017Sdim isClassMessage, 407223017Sdim Args, 408226633Sdim method); 409223017Sdim } else { 410223017Sdim result = Runtime.GenerateMessageSend(*this, Return, ResultType, 411223017Sdim E->getSelector(), 412223017Sdim Receiver, Args, OID, 413226633Sdim method); 414193326Sed } 415224145Sdim 416224145Sdim // For delegate init calls in ARC, implicitly store the result of 417224145Sdim // the call back into self. This takes ownership of the value. 418224145Sdim if (isDelegateInit) { 419224145Sdim llvm::Value *selfAddr = 420224145Sdim LocalDeclMap[cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()]; 421224145Sdim llvm::Value *newSelf = result.getScalarVal(); 422224145Sdim 423224145Sdim // The delegate return type isn't necessarily a matching type; in 424224145Sdim // fact, it's quite likely to be 'id'. 425226633Sdim llvm::Type *selfTy = 426224145Sdim cast<llvm::PointerType>(selfAddr->getType())->getElementType(); 427224145Sdim newSelf = Builder.CreateBitCast(newSelf, selfTy); 428224145Sdim 429224145Sdim Builder.CreateStore(newSelf, selfAddr); 430224145Sdim } 431224145Sdim 432239462Sdim return AdjustRelatedResultType(*this, E->getType(), method, result); 433193326Sed} 434193326Sed 435224145Sdimnamespace { 436224145Sdimstruct FinishARCDealloc : EHScopeStack::Cleanup { 437224145Sdim void Emit(CodeGenFunction &CGF, Flags flags) { 438224145Sdim const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CGF.CurCodeDecl); 439224145Sdim 440224145Sdim const ObjCImplDecl *impl = cast<ObjCImplDecl>(method->getDeclContext()); 441224145Sdim const ObjCInterfaceDecl *iface = impl->getClassInterface(); 442224145Sdim if (!iface->getSuperClass()) return; 443224145Sdim 444224145Sdim bool isCategory = isa<ObjCCategoryImplDecl>(impl); 445224145Sdim 446224145Sdim // Call [super dealloc] if we have a superclass. 447224145Sdim llvm::Value *self = CGF.LoadObjCSelf(); 448224145Sdim 449224145Sdim CallArgList args; 450224145Sdim CGF.CGM.getObjCRuntime().GenerateMessageSendSuper(CGF, ReturnValueSlot(), 451224145Sdim CGF.getContext().VoidTy, 452224145Sdim method->getSelector(), 453224145Sdim iface, 454224145Sdim isCategory, 455224145Sdim self, 456224145Sdim /*is class msg*/ false, 457224145Sdim args, 458224145Sdim method); 459224145Sdim } 460224145Sdim}; 461224145Sdim} 462224145Sdim 463193326Sed/// StartObjCMethod - Begin emission of an ObjCMethod. This generates 464193326Sed/// the LLVM function and sets the other context used by 465193326Sed/// CodeGenFunction. 466193326Sedvoid CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD, 467223017Sdim const ObjCContainerDecl *CD, 468223017Sdim SourceLocation StartLoc) { 469221345Sdim FunctionArgList args; 470206275Srdivacky // Check if we should generate debug info for this method. 471243830Sdim if (!OMD->hasAttr<NoDebugAttr>()) 472243830Sdim maybeInitializeDebugInfo(); 473206275Srdivacky 474193326Sed llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD); 475193326Sed 476234353Sdim const CGFunctionInfo &FI = CGM.getTypes().arrangeObjCMethodDeclaration(OMD); 477193326Sed CGM.SetInternalFunctionAttributes(OMD, Fn, FI); 478193326Sed 479221345Sdim args.push_back(OMD->getSelfDecl()); 480221345Sdim args.push_back(OMD->getCmdDecl()); 481193326Sed 482226633Sdim for (ObjCMethodDecl::param_const_iterator PI = OMD->param_begin(), 483234353Sdim E = OMD->param_end(); PI != E; ++PI) 484221345Sdim args.push_back(*PI); 485193326Sed 486218893Sdim CurGD = OMD; 487218893Sdim 488223017Sdim StartFunction(OMD, OMD->getResultType(), Fn, FI, args, StartLoc); 489224145Sdim 490224145Sdim // In ARC, certain methods get an extra cleanup. 491234353Sdim if (CGM.getLangOpts().ObjCAutoRefCount && 492224145Sdim OMD->isInstanceMethod() && 493224145Sdim OMD->getSelector().isUnarySelector()) { 494224145Sdim const IdentifierInfo *ident = 495224145Sdim OMD->getSelector().getIdentifierInfoForSlot(0); 496224145Sdim if (ident->isStr("dealloc")) 497224145Sdim EHStack.pushCleanup<FinishARCDealloc>(getARCCleanupKind()); 498224145Sdim } 499193326Sed} 500193326Sed 501224145Sdimstatic llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF, 502224145Sdim LValue lvalue, QualType type); 503224145Sdim 504193326Sed/// Generate an Objective-C method. An Objective-C method is a C function with 505198092Srdivacky/// its pointer, name, and types registered in the class struture. 506193326Sedvoid CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) { 507223017Sdim StartObjCMethod(OMD, OMD->getClassInterface(), OMD->getLocStart()); 508195341Sed EmitStmt(OMD->getBody()); 509195341Sed FinishFunction(OMD->getBodyRBrace()); 510193326Sed} 511193326Sed 512226633Sdim/// emitStructGetterCall - Call the runtime function to load a property 513226633Sdim/// into the return value slot. 514226633Sdimstatic void emitStructGetterCall(CodeGenFunction &CGF, ObjCIvarDecl *ivar, 515226633Sdim bool isAtomic, bool hasStrong) { 516226633Sdim ASTContext &Context = CGF.getContext(); 517193326Sed 518226633Sdim llvm::Value *src = 519226633Sdim CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(), 520226633Sdim ivar, 0).getAddress(); 521226633Sdim 522226633Sdim // objc_copyStruct (ReturnValue, &structIvar, 523226633Sdim // sizeof (Type of Ivar), isAtomic, false); 524226633Sdim CallArgList args; 525226633Sdim 526226633Sdim llvm::Value *dest = CGF.Builder.CreateBitCast(CGF.ReturnValue, CGF.VoidPtrTy); 527226633Sdim args.add(RValue::get(dest), Context.VoidPtrTy); 528226633Sdim 529226633Sdim src = CGF.Builder.CreateBitCast(src, CGF.VoidPtrTy); 530226633Sdim args.add(RValue::get(src), Context.VoidPtrTy); 531226633Sdim 532226633Sdim CharUnits size = CGF.getContext().getTypeSizeInChars(ivar->getType()); 533226633Sdim args.add(RValue::get(CGF.CGM.getSize(size)), Context.getSizeType()); 534226633Sdim args.add(RValue::get(CGF.Builder.getInt1(isAtomic)), Context.BoolTy); 535226633Sdim args.add(RValue::get(CGF.Builder.getInt1(hasStrong)), Context.BoolTy); 536226633Sdim 537226633Sdim llvm::Value *fn = CGF.CGM.getObjCRuntime().GetGetStructFunction(); 538239462Sdim CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(Context.VoidTy, args, 539239462Sdim FunctionType::ExtInfo(), 540239462Sdim RequiredArgs::All), 541226633Sdim fn, ReturnValueSlot(), args); 542226633Sdim} 543226633Sdim 544226633Sdim/// Determine whether the given architecture supports unaligned atomic 545226633Sdim/// accesses. They don't have to be fast, just faster than a function 546226633Sdim/// call and a mutex. 547226633Sdimstatic bool hasUnalignedAtomics(llvm::Triple::ArchType arch) { 548226633Sdim // FIXME: Allow unaligned atomic load/store on x86. (It is not 549226633Sdim // currently supported by the backend.) 550226633Sdim return 0; 551226633Sdim} 552226633Sdim 553226633Sdim/// Return the maximum size that permits atomic accesses for the given 554226633Sdim/// architecture. 555226633Sdimstatic CharUnits getMaxAtomicAccessSize(CodeGenModule &CGM, 556226633Sdim llvm::Triple::ArchType arch) { 557226633Sdim // ARM has 8-byte atomic accesses, but it's not clear whether we 558226633Sdim // want to rely on them here. 559226633Sdim 560226633Sdim // In the default case, just assume that any size up to a pointer is 561226633Sdim // fine given adequate alignment. 562226633Sdim return CharUnits::fromQuantity(CGM.PointerSizeInBytes); 563226633Sdim} 564226633Sdim 565226633Sdimnamespace { 566226633Sdim class PropertyImplStrategy { 567226633Sdim public: 568226633Sdim enum StrategyKind { 569226633Sdim /// The 'native' strategy is to use the architecture's provided 570226633Sdim /// reads and writes. 571226633Sdim Native, 572226633Sdim 573226633Sdim /// Use objc_setProperty and objc_getProperty. 574226633Sdim GetSetProperty, 575226633Sdim 576226633Sdim /// Use objc_setProperty for the setter, but use expression 577226633Sdim /// evaluation for the getter. 578226633Sdim SetPropertyAndExpressionGet, 579226633Sdim 580226633Sdim /// Use objc_copyStruct. 581226633Sdim CopyStruct, 582226633Sdim 583226633Sdim /// The 'expression' strategy is to emit normal assignment or 584226633Sdim /// lvalue-to-rvalue expressions. 585226633Sdim Expression 586226633Sdim }; 587226633Sdim 588226633Sdim StrategyKind getKind() const { return StrategyKind(Kind); } 589226633Sdim 590226633Sdim bool hasStrongMember() const { return HasStrong; } 591226633Sdim bool isAtomic() const { return IsAtomic; } 592226633Sdim bool isCopy() const { return IsCopy; } 593226633Sdim 594226633Sdim CharUnits getIvarSize() const { return IvarSize; } 595226633Sdim CharUnits getIvarAlignment() const { return IvarAlignment; } 596226633Sdim 597226633Sdim PropertyImplStrategy(CodeGenModule &CGM, 598226633Sdim const ObjCPropertyImplDecl *propImpl); 599226633Sdim 600226633Sdim private: 601226633Sdim unsigned Kind : 8; 602226633Sdim unsigned IsAtomic : 1; 603226633Sdim unsigned IsCopy : 1; 604226633Sdim unsigned HasStrong : 1; 605226633Sdim 606226633Sdim CharUnits IvarSize; 607226633Sdim CharUnits IvarAlignment; 608226633Sdim }; 609226633Sdim} 610226633Sdim 611239462Sdim/// Pick an implementation strategy for the given property synthesis. 612226633SdimPropertyImplStrategy::PropertyImplStrategy(CodeGenModule &CGM, 613226633Sdim const ObjCPropertyImplDecl *propImpl) { 614226633Sdim const ObjCPropertyDecl *prop = propImpl->getPropertyDecl(); 615226633Sdim ObjCPropertyDecl::SetterKind setterKind = prop->getSetterKind(); 616226633Sdim 617226633Sdim IsCopy = (setterKind == ObjCPropertyDecl::Copy); 618226633Sdim IsAtomic = prop->isAtomic(); 619226633Sdim HasStrong = false; // doesn't matter here. 620226633Sdim 621226633Sdim // Evaluate the ivar's size and alignment. 622226633Sdim ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); 623226633Sdim QualType ivarType = ivar->getType(); 624226633Sdim llvm::tie(IvarSize, IvarAlignment) 625226633Sdim = CGM.getContext().getTypeInfoInChars(ivarType); 626226633Sdim 627226633Sdim // If we have a copy property, we always have to use getProperty/setProperty. 628226633Sdim // TODO: we could actually use setProperty and an expression for non-atomics. 629226633Sdim if (IsCopy) { 630226633Sdim Kind = GetSetProperty; 631226633Sdim return; 632226633Sdim } 633226633Sdim 634226633Sdim // Handle retain. 635226633Sdim if (setterKind == ObjCPropertyDecl::Retain) { 636226633Sdim // In GC-only, there's nothing special that needs to be done. 637234353Sdim if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) { 638226633Sdim // fallthrough 639226633Sdim 640226633Sdim // In ARC, if the property is non-atomic, use expression emission, 641226633Sdim // which translates to objc_storeStrong. This isn't required, but 642226633Sdim // it's slightly nicer. 643234353Sdim } else if (CGM.getLangOpts().ObjCAutoRefCount && !IsAtomic) { 644243830Sdim // Using standard expression emission for the setter is only 645243830Sdim // acceptable if the ivar is __strong, which won't be true if 646243830Sdim // the property is annotated with __attribute__((NSObject)). 647243830Sdim // TODO: falling all the way back to objc_setProperty here is 648243830Sdim // just laziness, though; we could still use objc_storeStrong 649243830Sdim // if we hacked it right. 650243830Sdim if (ivarType.getObjCLifetime() == Qualifiers::OCL_Strong) 651243830Sdim Kind = Expression; 652243830Sdim else 653243830Sdim Kind = SetPropertyAndExpressionGet; 654226633Sdim return; 655226633Sdim 656226633Sdim // Otherwise, we need to at least use setProperty. However, if 657226633Sdim // the property isn't atomic, we can use normal expression 658226633Sdim // emission for the getter. 659226633Sdim } else if (!IsAtomic) { 660226633Sdim Kind = SetPropertyAndExpressionGet; 661226633Sdim return; 662226633Sdim 663226633Sdim // Otherwise, we have to use both setProperty and getProperty. 664226633Sdim } else { 665226633Sdim Kind = GetSetProperty; 666226633Sdim return; 667226633Sdim } 668226633Sdim } 669226633Sdim 670226633Sdim // If we're not atomic, just use expression accesses. 671226633Sdim if (!IsAtomic) { 672226633Sdim Kind = Expression; 673226633Sdim return; 674226633Sdim } 675226633Sdim 676226633Sdim // Properties on bitfield ivars need to be emitted using expression 677226633Sdim // accesses even if they're nominally atomic. 678226633Sdim if (ivar->isBitField()) { 679226633Sdim Kind = Expression; 680226633Sdim return; 681226633Sdim } 682226633Sdim 683226633Sdim // GC-qualified or ARC-qualified ivars need to be emitted as 684226633Sdim // expressions. This actually works out to being atomic anyway, 685226633Sdim // except for ARC __strong, but that should trigger the above code. 686226633Sdim if (ivarType.hasNonTrivialObjCLifetime() || 687234353Sdim (CGM.getLangOpts().getGC() && 688226633Sdim CGM.getContext().getObjCGCAttrKind(ivarType))) { 689226633Sdim Kind = Expression; 690226633Sdim return; 691226633Sdim } 692226633Sdim 693226633Sdim // Compute whether the ivar has strong members. 694234353Sdim if (CGM.getLangOpts().getGC()) 695226633Sdim if (const RecordType *recordType = ivarType->getAs<RecordType>()) 696226633Sdim HasStrong = recordType->getDecl()->hasObjectMember(); 697226633Sdim 698226633Sdim // We can never access structs with object members with a native 699226633Sdim // access, because we need to use write barriers. This is what 700226633Sdim // objc_copyStruct is for. 701226633Sdim if (HasStrong) { 702226633Sdim Kind = CopyStruct; 703226633Sdim return; 704226633Sdim } 705226633Sdim 706226633Sdim // Otherwise, this is target-dependent and based on the size and 707226633Sdim // alignment of the ivar. 708226633Sdim 709226633Sdim // If the size of the ivar is not a power of two, give up. We don't 710226633Sdim // want to get into the business of doing compare-and-swaps. 711226633Sdim if (!IvarSize.isPowerOfTwo()) { 712226633Sdim Kind = CopyStruct; 713226633Sdim return; 714226633Sdim } 715226633Sdim 716226633Sdim llvm::Triple::ArchType arch = 717251662Sdim CGM.getTarget().getTriple().getArch(); 718226633Sdim 719226633Sdim // Most architectures require memory to fit within a single cache 720226633Sdim // line, so the alignment has to be at least the size of the access. 721226633Sdim // Otherwise we have to grab a lock. 722226633Sdim if (IvarAlignment < IvarSize && !hasUnalignedAtomics(arch)) { 723226633Sdim Kind = CopyStruct; 724226633Sdim return; 725226633Sdim } 726226633Sdim 727226633Sdim // If the ivar's size exceeds the architecture's maximum atomic 728226633Sdim // access size, we have to use CopyStruct. 729226633Sdim if (IvarSize > getMaxAtomicAccessSize(CGM, arch)) { 730226633Sdim Kind = CopyStruct; 731226633Sdim return; 732226633Sdim } 733226633Sdim 734226633Sdim // Otherwise, we can use native loads and stores. 735226633Sdim Kind = Native; 736226633Sdim} 737226633Sdim 738239462Sdim/// \brief Generate an Objective-C property getter function. 739239462Sdim/// 740239462Sdim/// The given Decl must be an ObjCImplementationDecl. \@synthesize 741193326Sed/// is illegal within a category. 742193326Sedvoid CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP, 743193326Sed const ObjCPropertyImplDecl *PID) { 744234353Sdim llvm::Constant *AtomicHelperFn = 745234353Sdim GenerateObjCAtomicGetterCopyHelperFunction(PID); 746193326Sed const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 747193326Sed ObjCMethodDecl *OMD = PD->getGetterMethodDecl(); 748193326Sed assert(OMD && "Invalid call to generate getter (empty method)"); 749234353Sdim StartObjCMethod(OMD, IMP->getClassInterface(), OMD->getLocStart()); 750226633Sdim 751239462Sdim generateObjCGetterBody(IMP, PID, OMD, AtomicHelperFn); 752226633Sdim 753226633Sdim FinishFunction(); 754226633Sdim} 755226633Sdim 756226633Sdimstatic bool hasTrivialGetExpr(const ObjCPropertyImplDecl *propImpl) { 757226633Sdim const Expr *getter = propImpl->getGetterCXXConstructor(); 758226633Sdim if (!getter) return true; 759226633Sdim 760226633Sdim // Sema only makes only of these when the ivar has a C++ class type, 761226633Sdim // so the form is pretty constrained. 762226633Sdim 763226633Sdim // If the property has a reference type, we might just be binding a 764226633Sdim // reference, in which case the result will be a gl-value. We should 765226633Sdim // treat this as a non-trivial operation. 766226633Sdim if (getter->isGLValue()) 767226633Sdim return false; 768226633Sdim 769226633Sdim // If we selected a trivial copy-constructor, we're okay. 770226633Sdim if (const CXXConstructExpr *construct = dyn_cast<CXXConstructExpr>(getter)) 771226633Sdim return (construct->getConstructor()->isTrivial()); 772226633Sdim 773226633Sdim // The constructor might require cleanups (in which case it's never 774226633Sdim // trivial). 775226633Sdim assert(isa<ExprWithCleanups>(getter)); 776226633Sdim return false; 777226633Sdim} 778226633Sdim 779234353Sdim/// emitCPPObjectAtomicGetterCall - Call the runtime function to 780234353Sdim/// copy the ivar into the resturn slot. 781234353Sdimstatic void emitCPPObjectAtomicGetterCall(CodeGenFunction &CGF, 782234353Sdim llvm::Value *returnAddr, 783234353Sdim ObjCIvarDecl *ivar, 784234353Sdim llvm::Constant *AtomicHelperFn) { 785234353Sdim // objc_copyCppObjectAtomic (&returnSlot, &CppObjectIvar, 786234353Sdim // AtomicHelperFn); 787234353Sdim CallArgList args; 788234353Sdim 789234353Sdim // The 1st argument is the return Slot. 790234353Sdim args.add(RValue::get(returnAddr), CGF.getContext().VoidPtrTy); 791234353Sdim 792234353Sdim // The 2nd argument is the address of the ivar. 793234353Sdim llvm::Value *ivarAddr = 794234353Sdim CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), 795234353Sdim CGF.LoadObjCSelf(), ivar, 0).getAddress(); 796234353Sdim ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy); 797234353Sdim args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy); 798234353Sdim 799234353Sdim // Third argument is the helper function. 800234353Sdim args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy); 801234353Sdim 802234353Sdim llvm::Value *copyCppAtomicObjectFn = 803249423Sdim CGF.CGM.getObjCRuntime().GetCppAtomicObjectGetFunction(); 804239462Sdim CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(CGF.getContext().VoidTy, 805239462Sdim args, 806239462Sdim FunctionType::ExtInfo(), 807239462Sdim RequiredArgs::All), 808234353Sdim copyCppAtomicObjectFn, ReturnValueSlot(), args); 809234353Sdim} 810234353Sdim 811226633Sdimvoid 812226633SdimCodeGenFunction::generateObjCGetterBody(const ObjCImplementationDecl *classImpl, 813234353Sdim const ObjCPropertyImplDecl *propImpl, 814239462Sdim const ObjCMethodDecl *GetterMethodDecl, 815234353Sdim llvm::Constant *AtomicHelperFn) { 816226633Sdim // If there's a non-trivial 'get' expression, we just have to emit that. 817226633Sdim if (!hasTrivialGetExpr(propImpl)) { 818234353Sdim if (!AtomicHelperFn) { 819234353Sdim ReturnStmt ret(SourceLocation(), propImpl->getGetterCXXConstructor(), 820234353Sdim /*nrvo*/ 0); 821234353Sdim EmitReturnStmt(ret); 822234353Sdim } 823234353Sdim else { 824234353Sdim ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); 825234353Sdim emitCPPObjectAtomicGetterCall(*this, ReturnValue, 826234353Sdim ivar, AtomicHelperFn); 827234353Sdim } 828226633Sdim return; 829226633Sdim } 830226633Sdim 831226633Sdim const ObjCPropertyDecl *prop = propImpl->getPropertyDecl(); 832226633Sdim QualType propType = prop->getType(); 833226633Sdim ObjCMethodDecl *getterMethod = prop->getGetterMethodDecl(); 834226633Sdim 835226633Sdim ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); 836226633Sdim 837226633Sdim // Pick an implementation strategy. 838226633Sdim PropertyImplStrategy strategy(CGM, propImpl); 839226633Sdim switch (strategy.getKind()) { 840226633Sdim case PropertyImplStrategy::Native: { 841243830Sdim // We don't need to do anything for a zero-size struct. 842243830Sdim if (strategy.getIvarSize().isZero()) 843243830Sdim return; 844243830Sdim 845226633Sdim LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0); 846226633Sdim 847226633Sdim // Currently, all atomic accesses have to be through integer 848226633Sdim // types, so there's no point in trying to pick a prettier type. 849226633Sdim llvm::Type *bitcastType = 850226633Sdim llvm::Type::getIntNTy(getLLVMContext(), 851226633Sdim getContext().toBits(strategy.getIvarSize())); 852226633Sdim bitcastType = bitcastType->getPointerTo(); // addrspace 0 okay 853226633Sdim 854226633Sdim // Perform an atomic load. This does not impose ordering constraints. 855226633Sdim llvm::Value *ivarAddr = LV.getAddress(); 856226633Sdim ivarAddr = Builder.CreateBitCast(ivarAddr, bitcastType); 857226633Sdim llvm::LoadInst *load = Builder.CreateLoad(ivarAddr, "load"); 858226633Sdim load->setAlignment(strategy.getIvarAlignment().getQuantity()); 859226633Sdim load->setAtomic(llvm::Unordered); 860226633Sdim 861226633Sdim // Store that value into the return address. Doing this with a 862226633Sdim // bitcast is likely to produce some pretty ugly IR, but it's not 863226633Sdim // the *most* terrible thing in the world. 864226633Sdim Builder.CreateStore(load, Builder.CreateBitCast(ReturnValue, bitcastType)); 865226633Sdim 866226633Sdim // Make sure we don't do an autorelease. 867226633Sdim AutoreleaseResult = false; 868226633Sdim return; 869226633Sdim } 870226633Sdim 871226633Sdim case PropertyImplStrategy::GetSetProperty: { 872226633Sdim llvm::Value *getPropertyFn = 873193326Sed CGM.getObjCRuntime().GetPropertyGetFunction(); 874226633Sdim if (!getPropertyFn) { 875226633Sdim CGM.ErrorUnsupported(propImpl, "Obj-C getter requiring atomic copy"); 876193326Sed return; 877193326Sed } 878193326Sed 879193326Sed // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true). 880193326Sed // FIXME: Can't this be simpler? This might even be worse than the 881193326Sed // corresponding gcc code. 882226633Sdim llvm::Value *cmd = 883226633Sdim Builder.CreateLoad(LocalDeclMap[getterMethod->getCmdDecl()], "cmd"); 884226633Sdim llvm::Value *self = Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy); 885226633Sdim llvm::Value *ivarOffset = 886226633Sdim EmitIvarOffset(classImpl->getClassInterface(), ivar); 887226633Sdim 888226633Sdim CallArgList args; 889226633Sdim args.add(RValue::get(self), getContext().getObjCIdType()); 890226633Sdim args.add(RValue::get(cmd), getContext().getObjCSelType()); 891226633Sdim args.add(RValue::get(ivarOffset), getContext().getPointerDiffType()); 892226633Sdim args.add(RValue::get(Builder.getInt1(strategy.isAtomic())), 893226633Sdim getContext().BoolTy); 894226633Sdim 895193326Sed // FIXME: We shouldn't need to get the function info here, the 896193326Sed // runtime already should have computed it to build the function. 897239462Sdim RValue RV = EmitCall(getTypes().arrangeFreeFunctionCall(propType, args, 898239462Sdim FunctionType::ExtInfo(), 899239462Sdim RequiredArgs::All), 900226633Sdim getPropertyFn, ReturnValueSlot(), args); 901226633Sdim 902193326Sed // We need to fix the type here. Ivars with copy & retain are 903193326Sed // always objects so we don't need to worry about complex or 904193326Sed // aggregates. 905198092Srdivacky RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(), 906239462Sdim getTypes().ConvertType(getterMethod->getResultType()))); 907224145Sdim 908226633Sdim EmitReturnOfRValue(RV, propType); 909226633Sdim 910224145Sdim // objc_getProperty does an autorelease, so we should suppress ours. 911224145Sdim AutoreleaseResult = false; 912226633Sdim 913226633Sdim return; 914226633Sdim } 915226633Sdim 916226633Sdim case PropertyImplStrategy::CopyStruct: 917226633Sdim emitStructGetterCall(*this, ivar, strategy.isAtomic(), 918226633Sdim strategy.hasStrongMember()); 919226633Sdim return; 920226633Sdim 921226633Sdim case PropertyImplStrategy::Expression: 922226633Sdim case PropertyImplStrategy::SetPropertyAndExpressionGet: { 923226633Sdim LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0); 924226633Sdim 925226633Sdim QualType ivarType = ivar->getType(); 926249423Sdim switch (getEvaluationKind(ivarType)) { 927249423Sdim case TEK_Complex: { 928249423Sdim ComplexPairTy pair = EmitLoadOfComplex(LV); 929249423Sdim EmitStoreOfComplex(pair, 930249423Sdim MakeNaturalAlignAddrLValue(ReturnValue, ivarType), 931249423Sdim /*init*/ true); 932249423Sdim return; 933249423Sdim } 934249423Sdim case TEK_Aggregate: 935226633Sdim // The return value slot is guaranteed to not be aliased, but 936226633Sdim // that's not necessarily the same as "on the stack", so 937226633Sdim // we still potentially need objc_memmove_collectable. 938226633Sdim EmitAggregateCopy(ReturnValue, LV.getAddress(), ivarType); 939249423Sdim return; 940249423Sdim case TEK_Scalar: { 941226633Sdim llvm::Value *value; 942226633Sdim if (propType->isReferenceType()) { 943226633Sdim value = LV.getAddress(); 944226633Sdim } else { 945226633Sdim // We want to load and autoreleaseReturnValue ARC __weak ivars. 946226633Sdim if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak) { 947226633Sdim value = emitARCRetainLoadOfScalar(*this, LV, ivarType); 948224145Sdim 949226633Sdim // Otherwise we want to do a simple load, suppressing the 950226633Sdim // final autorelease. 951224145Sdim } else { 952226633Sdim value = EmitLoadOfLValue(LV).getScalarVal(); 953226633Sdim AutoreleaseResult = false; 954221345Sdim } 955224145Sdim 956226633Sdim value = Builder.CreateBitCast(value, ConvertType(propType)); 957239462Sdim value = Builder.CreateBitCast(value, 958239462Sdim ConvertType(GetterMethodDecl->getResultType())); 959226633Sdim } 960226633Sdim 961226633Sdim EmitReturnOfRValue(RValue::get(value), propType); 962249423Sdim return; 963193326Sed } 964249423Sdim } 965249423Sdim llvm_unreachable("bad evaluation kind"); 966193326Sed } 967193326Sed 968226633Sdim } 969226633Sdim llvm_unreachable("bad @property implementation strategy!"); 970193326Sed} 971193326Sed 972226633Sdim/// emitStructSetterCall - Call the runtime function to store the value 973226633Sdim/// from the first formal parameter into the given ivar. 974226633Sdimstatic void emitStructSetterCall(CodeGenFunction &CGF, ObjCMethodDecl *OMD, 975226633Sdim ObjCIvarDecl *ivar) { 976218893Sdim // objc_copyStruct (&structIvar, &Arg, 977218893Sdim // sizeof (struct something), true, false); 978226633Sdim CallArgList args; 979226633Sdim 980226633Sdim // The first argument is the address of the ivar. 981226633Sdim llvm::Value *ivarAddr = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), 982226633Sdim CGF.LoadObjCSelf(), ivar, 0) 983226633Sdim .getAddress(); 984226633Sdim ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy); 985226633Sdim args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy); 986226633Sdim 987226633Sdim // The second argument is the address of the parameter variable. 988226633Sdim ParmVarDecl *argVar = *OMD->param_begin(); 989234353Sdim DeclRefExpr argRef(argVar, false, argVar->getType().getNonReferenceType(), 990234353Sdim VK_LValue, SourceLocation()); 991226633Sdim llvm::Value *argAddr = CGF.EmitLValue(&argRef).getAddress(); 992226633Sdim argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy); 993226633Sdim args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy); 994226633Sdim 995226633Sdim // The third argument is the sizeof the type. 996226633Sdim llvm::Value *size = 997226633Sdim CGF.CGM.getSize(CGF.getContext().getTypeSizeInChars(ivar->getType())); 998226633Sdim args.add(RValue::get(size), CGF.getContext().getSizeType()); 999226633Sdim 1000226633Sdim // The fourth argument is the 'isAtomic' flag. 1001226633Sdim args.add(RValue::get(CGF.Builder.getTrue()), CGF.getContext().BoolTy); 1002226633Sdim 1003226633Sdim // The fifth argument is the 'hasStrong' flag. 1004226633Sdim // FIXME: should this really always be false? 1005226633Sdim args.add(RValue::get(CGF.Builder.getFalse()), CGF.getContext().BoolTy); 1006226633Sdim 1007226633Sdim llvm::Value *copyStructFn = CGF.CGM.getObjCRuntime().GetSetStructFunction(); 1008239462Sdim CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(CGF.getContext().VoidTy, 1009239462Sdim args, 1010239462Sdim FunctionType::ExtInfo(), 1011239462Sdim RequiredArgs::All), 1012226633Sdim copyStructFn, ReturnValueSlot(), args); 1013218893Sdim} 1014218893Sdim 1015234353Sdim/// emitCPPObjectAtomicSetterCall - Call the runtime function to store 1016234353Sdim/// the value from the first formal parameter into the given ivar, using 1017234353Sdim/// the Cpp API for atomic Cpp objects with non-trivial copy assignment. 1018234353Sdimstatic void emitCPPObjectAtomicSetterCall(CodeGenFunction &CGF, 1019234353Sdim ObjCMethodDecl *OMD, 1020234353Sdim ObjCIvarDecl *ivar, 1021234353Sdim llvm::Constant *AtomicHelperFn) { 1022234353Sdim // objc_copyCppObjectAtomic (&CppObjectIvar, &Arg, 1023234353Sdim // AtomicHelperFn); 1024234353Sdim CallArgList args; 1025234353Sdim 1026234353Sdim // The first argument is the address of the ivar. 1027234353Sdim llvm::Value *ivarAddr = 1028234353Sdim CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), 1029234353Sdim CGF.LoadObjCSelf(), ivar, 0).getAddress(); 1030234353Sdim ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy); 1031234353Sdim args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy); 1032234353Sdim 1033234353Sdim // The second argument is the address of the parameter variable. 1034234353Sdim ParmVarDecl *argVar = *OMD->param_begin(); 1035234353Sdim DeclRefExpr argRef(argVar, false, argVar->getType().getNonReferenceType(), 1036234353Sdim VK_LValue, SourceLocation()); 1037234353Sdim llvm::Value *argAddr = CGF.EmitLValue(&argRef).getAddress(); 1038234353Sdim argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy); 1039234353Sdim args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy); 1040234353Sdim 1041234353Sdim // Third argument is the helper function. 1042234353Sdim args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy); 1043234353Sdim 1044234353Sdim llvm::Value *copyCppAtomicObjectFn = 1045249423Sdim CGF.CGM.getObjCRuntime().GetCppAtomicObjectSetFunction(); 1046239462Sdim CGF.EmitCall(CGF.getTypes().arrangeFreeFunctionCall(CGF.getContext().VoidTy, 1047239462Sdim args, 1048239462Sdim FunctionType::ExtInfo(), 1049239462Sdim RequiredArgs::All), 1050234353Sdim copyCppAtomicObjectFn, ReturnValueSlot(), args); 1051234353Sdim 1052234353Sdim 1053234353Sdim} 1054234353Sdim 1055234353Sdim 1056226633Sdimstatic bool hasTrivialSetExpr(const ObjCPropertyImplDecl *PID) { 1057226633Sdim Expr *setter = PID->getSetterCXXAssignment(); 1058226633Sdim if (!setter) return true; 1059226633Sdim 1060226633Sdim // Sema only makes only of these when the ivar has a C++ class type, 1061226633Sdim // so the form is pretty constrained. 1062226633Sdim 1063226633Sdim // An operator call is trivial if the function it calls is trivial. 1064226633Sdim // This also implies that there's nothing non-trivial going on with 1065226633Sdim // the arguments, because operator= can only be trivial if it's a 1066226633Sdim // synthesized assignment operator and therefore both parameters are 1067226633Sdim // references. 1068226633Sdim if (CallExpr *call = dyn_cast<CallExpr>(setter)) { 1069226633Sdim if (const FunctionDecl *callee 1070226633Sdim = dyn_cast_or_null<FunctionDecl>(call->getCalleeDecl())) 1071226633Sdim if (callee->isTrivial()) 1072226633Sdim return true; 1073226633Sdim return false; 1074221345Sdim } 1075226633Sdim 1076226633Sdim assert(isa<ExprWithCleanups>(setter)); 1077226633Sdim return false; 1078221345Sdim} 1079221345Sdim 1080234353Sdimstatic bool UseOptimizedSetter(CodeGenModule &CGM) { 1081234353Sdim if (CGM.getLangOpts().getGC() != LangOptions::NonGC) 1082234353Sdim return false; 1083243830Sdim return CGM.getLangOpts().ObjCRuntime.hasOptimizedSetter(); 1084234353Sdim} 1085234353Sdim 1086226633Sdimvoid 1087226633SdimCodeGenFunction::generateObjCSetterBody(const ObjCImplementationDecl *classImpl, 1088234353Sdim const ObjCPropertyImplDecl *propImpl, 1089234353Sdim llvm::Constant *AtomicHelperFn) { 1090234353Sdim const ObjCPropertyDecl *prop = propImpl->getPropertyDecl(); 1091234353Sdim ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); 1092234353Sdim ObjCMethodDecl *setterMethod = prop->getSetterMethodDecl(); 1093234353Sdim 1094226633Sdim // Just use the setter expression if Sema gave us one and it's 1095234353Sdim // non-trivial. 1096226633Sdim if (!hasTrivialSetExpr(propImpl)) { 1097234353Sdim if (!AtomicHelperFn) 1098234353Sdim // If non-atomic, assignment is called directly. 1099234353Sdim EmitStmt(propImpl->getSetterCXXAssignment()); 1100234353Sdim else 1101234353Sdim // If atomic, assignment is called via a locking api. 1102234353Sdim emitCPPObjectAtomicSetterCall(*this, setterMethod, ivar, 1103234353Sdim AtomicHelperFn); 1104226633Sdim return; 1105226633Sdim } 1106193326Sed 1107226633Sdim PropertyImplStrategy strategy(CGM, propImpl); 1108226633Sdim switch (strategy.getKind()) { 1109226633Sdim case PropertyImplStrategy::Native: { 1110243830Sdim // We don't need to do anything for a zero-size struct. 1111243830Sdim if (strategy.getIvarSize().isZero()) 1112243830Sdim return; 1113243830Sdim 1114226633Sdim llvm::Value *argAddr = LocalDeclMap[*setterMethod->param_begin()]; 1115226633Sdim 1116226633Sdim LValue ivarLValue = 1117226633Sdim EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, /*quals*/ 0); 1118226633Sdim llvm::Value *ivarAddr = ivarLValue.getAddress(); 1119226633Sdim 1120226633Sdim // Currently, all atomic accesses have to be through integer 1121226633Sdim // types, so there's no point in trying to pick a prettier type. 1122226633Sdim llvm::Type *bitcastType = 1123226633Sdim llvm::Type::getIntNTy(getLLVMContext(), 1124226633Sdim getContext().toBits(strategy.getIvarSize())); 1125226633Sdim bitcastType = bitcastType->getPointerTo(); // addrspace 0 okay 1126226633Sdim 1127226633Sdim // Cast both arguments to the chosen operation type. 1128226633Sdim argAddr = Builder.CreateBitCast(argAddr, bitcastType); 1129226633Sdim ivarAddr = Builder.CreateBitCast(ivarAddr, bitcastType); 1130226633Sdim 1131226633Sdim // This bitcast load is likely to cause some nasty IR. 1132226633Sdim llvm::Value *load = Builder.CreateLoad(argAddr); 1133226633Sdim 1134226633Sdim // Perform an atomic store. There are no memory ordering requirements. 1135226633Sdim llvm::StoreInst *store = Builder.CreateStore(load, ivarAddr); 1136226633Sdim store->setAlignment(strategy.getIvarAlignment().getQuantity()); 1137226633Sdim store->setAtomic(llvm::Unordered); 1138226633Sdim return; 1139226633Sdim } 1140226633Sdim 1141226633Sdim case PropertyImplStrategy::GetSetProperty: 1142226633Sdim case PropertyImplStrategy::SetPropertyAndExpressionGet: { 1143234353Sdim 1144234353Sdim llvm::Value *setOptimizedPropertyFn = 0; 1145234353Sdim llvm::Value *setPropertyFn = 0; 1146234353Sdim if (UseOptimizedSetter(CGM)) { 1147243830Sdim // 10.8 and iOS 6.0 code and GC is off 1148234353Sdim setOptimizedPropertyFn = 1149234353Sdim CGM.getObjCRuntime() 1150234353Sdim .GetOptimizedPropertySetFunction(strategy.isAtomic(), 1151234353Sdim strategy.isCopy()); 1152234353Sdim if (!setOptimizedPropertyFn) { 1153234353Sdim CGM.ErrorUnsupported(propImpl, "Obj-C optimized setter - NYI"); 1154234353Sdim return; 1155234353Sdim } 1156193326Sed } 1157234353Sdim else { 1158234353Sdim setPropertyFn = CGM.getObjCRuntime().GetPropertySetFunction(); 1159234353Sdim if (!setPropertyFn) { 1160234353Sdim CGM.ErrorUnsupported(propImpl, "Obj-C setter requiring atomic copy"); 1161234353Sdim return; 1162234353Sdim } 1163234353Sdim } 1164234353Sdim 1165198092Srdivacky // Emit objc_setProperty((id) self, _cmd, offset, arg, 1166193326Sed // <is-atomic>, <is-copy>). 1167226633Sdim llvm::Value *cmd = 1168226633Sdim Builder.CreateLoad(LocalDeclMap[setterMethod->getCmdDecl()]); 1169226633Sdim llvm::Value *self = 1170226633Sdim Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy); 1171226633Sdim llvm::Value *ivarOffset = 1172226633Sdim EmitIvarOffset(classImpl->getClassInterface(), ivar); 1173226633Sdim llvm::Value *arg = LocalDeclMap[*setterMethod->param_begin()]; 1174226633Sdim arg = Builder.CreateBitCast(Builder.CreateLoad(arg, "arg"), VoidPtrTy); 1175226633Sdim 1176226633Sdim CallArgList args; 1177226633Sdim args.add(RValue::get(self), getContext().getObjCIdType()); 1178226633Sdim args.add(RValue::get(cmd), getContext().getObjCSelType()); 1179234353Sdim if (setOptimizedPropertyFn) { 1180234353Sdim args.add(RValue::get(arg), getContext().getObjCIdType()); 1181234353Sdim args.add(RValue::get(ivarOffset), getContext().getPointerDiffType()); 1182239462Sdim EmitCall(getTypes().arrangeFreeFunctionCall(getContext().VoidTy, args, 1183239462Sdim FunctionType::ExtInfo(), 1184239462Sdim RequiredArgs::All), 1185234353Sdim setOptimizedPropertyFn, ReturnValueSlot(), args); 1186234353Sdim } else { 1187234353Sdim args.add(RValue::get(ivarOffset), getContext().getPointerDiffType()); 1188234353Sdim args.add(RValue::get(arg), getContext().getObjCIdType()); 1189234353Sdim args.add(RValue::get(Builder.getInt1(strategy.isAtomic())), 1190234353Sdim getContext().BoolTy); 1191234353Sdim args.add(RValue::get(Builder.getInt1(strategy.isCopy())), 1192234353Sdim getContext().BoolTy); 1193234353Sdim // FIXME: We shouldn't need to get the function info here, the runtime 1194234353Sdim // already should have computed it to build the function. 1195239462Sdim EmitCall(getTypes().arrangeFreeFunctionCall(getContext().VoidTy, args, 1196239462Sdim FunctionType::ExtInfo(), 1197239462Sdim RequiredArgs::All), 1198234353Sdim setPropertyFn, ReturnValueSlot(), args); 1199234353Sdim } 1200234353Sdim 1201226633Sdim return; 1202226633Sdim } 1203226633Sdim 1204226633Sdim case PropertyImplStrategy::CopyStruct: 1205226633Sdim emitStructSetterCall(*this, setterMethod, ivar); 1206226633Sdim return; 1207226633Sdim 1208226633Sdim case PropertyImplStrategy::Expression: 1209226633Sdim break; 1210226633Sdim } 1211226633Sdim 1212226633Sdim // Otherwise, fake up some ASTs and emit a normal assignment. 1213226633Sdim ValueDecl *selfDecl = setterMethod->getSelfDecl(); 1214234353Sdim DeclRefExpr self(selfDecl, false, selfDecl->getType(), 1215234353Sdim VK_LValue, SourceLocation()); 1216226633Sdim ImplicitCastExpr selfLoad(ImplicitCastExpr::OnStack, 1217226633Sdim selfDecl->getType(), CK_LValueToRValue, &self, 1218226633Sdim VK_RValue); 1219226633Sdim ObjCIvarRefExpr ivarRef(ivar, ivar->getType().getNonReferenceType(), 1220249423Sdim SourceLocation(), SourceLocation(), 1221249423Sdim &selfLoad, true, true); 1222226633Sdim 1223226633Sdim ParmVarDecl *argDecl = *setterMethod->param_begin(); 1224226633Sdim QualType argType = argDecl->getType().getNonReferenceType(); 1225234353Sdim DeclRefExpr arg(argDecl, false, argType, VK_LValue, SourceLocation()); 1226226633Sdim ImplicitCastExpr argLoad(ImplicitCastExpr::OnStack, 1227226633Sdim argType.getUnqualifiedType(), CK_LValueToRValue, 1228226633Sdim &arg, VK_RValue); 1229198893Srdivacky 1230226633Sdim // The property type can differ from the ivar type in some situations with 1231226633Sdim // Objective-C pointer types, we can always bit cast the RHS in these cases. 1232226633Sdim // The following absurdity is just to ensure well-formed IR. 1233226633Sdim CastKind argCK = CK_NoOp; 1234226633Sdim if (ivarRef.getType()->isObjCObjectPointerType()) { 1235226633Sdim if (argLoad.getType()->isObjCObjectPointerType()) 1236226633Sdim argCK = CK_BitCast; 1237226633Sdim else if (argLoad.getType()->isBlockPointerType()) 1238226633Sdim argCK = CK_BlockPointerToObjCPointerCast; 1239226633Sdim else 1240226633Sdim argCK = CK_CPointerToObjCPointerCast; 1241226633Sdim } else if (ivarRef.getType()->isBlockPointerType()) { 1242226633Sdim if (argLoad.getType()->isBlockPointerType()) 1243226633Sdim argCK = CK_BitCast; 1244226633Sdim else 1245226633Sdim argCK = CK_AnyPointerToBlockPointerCast; 1246226633Sdim } else if (ivarRef.getType()->isPointerType()) { 1247226633Sdim argCK = CK_BitCast; 1248193326Sed } 1249226633Sdim ImplicitCastExpr argCast(ImplicitCastExpr::OnStack, 1250226633Sdim ivarRef.getType(), argCK, &argLoad, 1251226633Sdim VK_RValue); 1252226633Sdim Expr *finalArg = &argLoad; 1253226633Sdim if (!getContext().hasSameUnqualifiedType(ivarRef.getType(), 1254226633Sdim argLoad.getType())) 1255226633Sdim finalArg = &argCast; 1256193326Sed 1257226633Sdim 1258226633Sdim BinaryOperator assign(&ivarRef, finalArg, BO_Assign, 1259226633Sdim ivarRef.getType(), VK_RValue, OK_Ordinary, 1260243830Sdim SourceLocation(), false); 1261226633Sdim EmitStmt(&assign); 1262226633Sdim} 1263226633Sdim 1264239462Sdim/// \brief Generate an Objective-C property setter function. 1265239462Sdim/// 1266239462Sdim/// The given Decl must be an ObjCImplementationDecl. \@synthesize 1267226633Sdim/// is illegal within a category. 1268226633Sdimvoid CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP, 1269226633Sdim const ObjCPropertyImplDecl *PID) { 1270234353Sdim llvm::Constant *AtomicHelperFn = 1271234353Sdim GenerateObjCAtomicSetterCopyHelperFunction(PID); 1272226633Sdim const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 1273226633Sdim ObjCMethodDecl *OMD = PD->getSetterMethodDecl(); 1274226633Sdim assert(OMD && "Invalid call to generate setter (empty method)"); 1275234353Sdim StartObjCMethod(OMD, IMP->getClassInterface(), OMD->getLocStart()); 1276226633Sdim 1277234353Sdim generateObjCSetterBody(IMP, PID, AtomicHelperFn); 1278226633Sdim 1279193326Sed FinishFunction(); 1280193326Sed} 1281193326Sed 1282221345Sdimnamespace { 1283224145Sdim struct DestroyIvar : EHScopeStack::Cleanup { 1284224145Sdim private: 1285224145Sdim llvm::Value *addr; 1286221345Sdim const ObjCIvarDecl *ivar; 1287234353Sdim CodeGenFunction::Destroyer *destroyer; 1288224145Sdim bool useEHCleanupForArray; 1289224145Sdim public: 1290224145Sdim DestroyIvar(llvm::Value *addr, const ObjCIvarDecl *ivar, 1291224145Sdim CodeGenFunction::Destroyer *destroyer, 1292224145Sdim bool useEHCleanupForArray) 1293234353Sdim : addr(addr), ivar(ivar), destroyer(destroyer), 1294224145Sdim useEHCleanupForArray(useEHCleanupForArray) {} 1295221345Sdim 1296224145Sdim void Emit(CodeGenFunction &CGF, Flags flags) { 1297224145Sdim LValue lvalue 1298224145Sdim = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), addr, ivar, /*CVR*/ 0); 1299224145Sdim CGF.emitDestroy(lvalue.getAddress(), ivar->getType(), destroyer, 1300224145Sdim flags.isForNormalCleanup() && useEHCleanupForArray); 1301221345Sdim } 1302221345Sdim }; 1303224145Sdim} 1304221345Sdim 1305224145Sdim/// Like CodeGenFunction::destroyARCStrong, but do it with a call. 1306224145Sdimstatic void destroyARCStrongWithStore(CodeGenFunction &CGF, 1307224145Sdim llvm::Value *addr, 1308224145Sdim QualType type) { 1309224145Sdim llvm::Value *null = getNullForVariable(addr); 1310224145Sdim CGF.EmitARCStoreStrongCall(addr, null, /*ignored*/ true); 1311221345Sdim} 1312221345Sdim 1313221345Sdimstatic void emitCXXDestructMethod(CodeGenFunction &CGF, 1314221345Sdim ObjCImplementationDecl *impl) { 1315221345Sdim CodeGenFunction::RunCleanupsScope scope(CGF); 1316221345Sdim 1317221345Sdim llvm::Value *self = CGF.LoadObjCSelf(); 1318221345Sdim 1319226633Sdim const ObjCInterfaceDecl *iface = impl->getClassInterface(); 1320226633Sdim for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin(); 1321221345Sdim ivar; ivar = ivar->getNextIvar()) { 1322221345Sdim QualType type = ivar->getType(); 1323221345Sdim 1324221345Sdim // Check whether the ivar is a destructible type. 1325224145Sdim QualType::DestructionKind dtorKind = type.isDestructedType(); 1326224145Sdim if (!dtorKind) continue; 1327221345Sdim 1328224145Sdim CodeGenFunction::Destroyer *destroyer = 0; 1329221345Sdim 1330224145Sdim // Use a call to objc_storeStrong to destroy strong ivars, for the 1331224145Sdim // general benefit of the tools. 1332224145Sdim if (dtorKind == QualType::DK_objc_strong_lifetime) { 1333234353Sdim destroyer = destroyARCStrongWithStore; 1334224145Sdim 1335224145Sdim // Otherwise use the default for the destruction kind. 1336224145Sdim } else { 1337234353Sdim destroyer = CGF.getDestroyer(dtorKind); 1338221345Sdim } 1339224145Sdim 1340224145Sdim CleanupKind cleanupKind = CGF.getCleanupKind(dtorKind); 1341224145Sdim 1342224145Sdim CGF.EHStack.pushCleanup<DestroyIvar>(cleanupKind, self, ivar, destroyer, 1343224145Sdim cleanupKind & EHCleanup); 1344221345Sdim } 1345221345Sdim 1346221345Sdim assert(scope.requiresCleanups() && "nothing to do in .cxx_destruct?"); 1347221345Sdim} 1348221345Sdim 1349207619Srdivackyvoid CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP, 1350207619Srdivacky ObjCMethodDecl *MD, 1351207619Srdivacky bool ctor) { 1352207619Srdivacky MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface()); 1353223017Sdim StartObjCMethod(MD, IMP->getClassInterface(), MD->getLocStart()); 1354221345Sdim 1355221345Sdim // Emit .cxx_construct. 1356207619Srdivacky if (ctor) { 1357224145Sdim // Suppress the final autorelease in ARC. 1358224145Sdim AutoreleaseResult = false; 1359224145Sdim 1360226633Sdim SmallVector<CXXCtorInitializer *, 8> IvarInitializers; 1361221345Sdim for (ObjCImplementationDecl::init_const_iterator B = IMP->init_begin(), 1362221345Sdim E = IMP->init_end(); B != E; ++B) { 1363221345Sdim CXXCtorInitializer *IvarInit = (*B); 1364218893Sdim FieldDecl *Field = IvarInit->getAnyMember(); 1365207619Srdivacky ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field); 1366207619Srdivacky LValue LV = EmitLValueForIvar(TypeOfSelfObject(), 1367207619Srdivacky LoadObjCSelf(), Ivar, 0); 1368226633Sdim EmitAggExpr(IvarInit->getInit(), 1369226633Sdim AggValueSlot::forLValue(LV, AggValueSlot::IsDestructed, 1370226633Sdim AggValueSlot::DoesNotNeedGCBarriers, 1371226633Sdim AggValueSlot::IsNotAliased)); 1372207619Srdivacky } 1373207619Srdivacky // constructor returns 'self'. 1374207619Srdivacky CodeGenTypes &Types = CGM.getTypes(); 1375207619Srdivacky QualType IdTy(CGM.getContext().getObjCIdType()); 1376207619Srdivacky llvm::Value *SelfAsId = 1377207619Srdivacky Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy)); 1378207619Srdivacky EmitReturnOfRValue(RValue::get(SelfAsId), IdTy); 1379221345Sdim 1380221345Sdim // Emit .cxx_destruct. 1381208600Srdivacky } else { 1382221345Sdim emitCXXDestructMethod(*this, IMP); 1383207619Srdivacky } 1384207619Srdivacky FinishFunction(); 1385207619Srdivacky} 1386207619Srdivacky 1387207619Srdivackybool CodeGenFunction::IndirectObjCSetterArg(const CGFunctionInfo &FI) { 1388207619Srdivacky CGFunctionInfo::const_arg_iterator it = FI.arg_begin(); 1389207619Srdivacky it++; it++; 1390207619Srdivacky const ABIArgInfo &AI = it->info; 1391207619Srdivacky // FIXME. Is this sufficient check? 1392207619Srdivacky return (AI.getKind() == ABIArgInfo::Indirect); 1393207619Srdivacky} 1394207619Srdivacky 1395207619Srdivackybool CodeGenFunction::IvarTypeWithAggrGCObjects(QualType Ty) { 1396234353Sdim if (CGM.getLangOpts().getGC() == LangOptions::NonGC) 1397207619Srdivacky return false; 1398207619Srdivacky if (const RecordType *FDTTy = Ty.getTypePtr()->getAs<RecordType>()) 1399207619Srdivacky return FDTTy->getDecl()->hasObjectMember(); 1400207619Srdivacky return false; 1401207619Srdivacky} 1402207619Srdivacky 1403193326Sedllvm::Value *CodeGenFunction::LoadObjCSelf() { 1404251662Sdim VarDecl *Self = cast<ObjCMethodDecl>(CurFuncDecl)->getSelfDecl(); 1405251662Sdim DeclRefExpr DRE(Self, /*is enclosing local*/ (CurFuncDecl != CurCodeDecl), 1406251662Sdim Self->getType(), VK_LValue, SourceLocation()); 1407251662Sdim return EmitLoadOfScalar(EmitDeclRefLValue(&DRE)); 1408193326Sed} 1409193326Sed 1410193326SedQualType CodeGenFunction::TypeOfSelfObject() { 1411193326Sed const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); 1412193326Sed ImplicitParamDecl *selfDecl = OMD->getSelfDecl(); 1413198092Srdivacky const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>( 1414198092Srdivacky getContext().getCanonicalType(selfDecl->getType())); 1415193326Sed return PTy->getPointeeType(); 1416193326Sed} 1417193326Sed 1418193326Sedvoid CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){ 1419198092Srdivacky llvm::Constant *EnumerationMutationFn = 1420193326Sed CGM.getObjCRuntime().EnumerationMutationFunction(); 1421198092Srdivacky 1422193326Sed if (!EnumerationMutationFn) { 1423193326Sed CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime"); 1424193326Sed return; 1425193326Sed } 1426193326Sed 1427218893Sdim CGDebugInfo *DI = getDebugInfo(); 1428226633Sdim if (DI) 1429226633Sdim DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin()); 1430198092Srdivacky 1431224145Sdim // The local variable comes into scope immediately. 1432224145Sdim AutoVarEmission variable = AutoVarEmission::invalid(); 1433224145Sdim if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) 1434224145Sdim variable = EmitAutoVarAlloca(*cast<VarDecl>(SD->getSingleDecl())); 1435224145Sdim 1436218893Sdim JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end"); 1437218893Sdim 1438193326Sed // Fast enumeration state. 1439226633Sdim QualType StateTy = CGM.getObjCFastEnumerationStateType(); 1440203955Srdivacky llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr"); 1441208600Srdivacky EmitNullInitialization(StatePtr, StateTy); 1442198092Srdivacky 1443193326Sed // Number of elements in the items array. 1444193326Sed static const unsigned NumItems = 16; 1445198092Srdivacky 1446218893Sdim // Fetch the countByEnumeratingWithState:objects:count: selector. 1447206084Srdivacky IdentifierInfo *II[] = { 1448206084Srdivacky &CGM.getContext().Idents.get("countByEnumeratingWithState"), 1449206084Srdivacky &CGM.getContext().Idents.get("objects"), 1450206084Srdivacky &CGM.getContext().Idents.get("count") 1451206084Srdivacky }; 1452206084Srdivacky Selector FastEnumSel = 1453206084Srdivacky CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]); 1454193326Sed 1455193326Sed QualType ItemsTy = 1456193326Sed getContext().getConstantArrayType(getContext().getObjCIdType(), 1457198092Srdivacky llvm::APInt(32, NumItems), 1458193326Sed ArrayType::Normal, 0); 1459203955Srdivacky llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr"); 1460198092Srdivacky 1461226633Sdim // Emit the collection pointer. In ARC, we do a retain. 1462226633Sdim llvm::Value *Collection; 1463234353Sdim if (getLangOpts().ObjCAutoRefCount) { 1464226633Sdim Collection = EmitARCRetainScalarExpr(S.getCollection()); 1465198092Srdivacky 1466226633Sdim // Enter a cleanup to do the release. 1467226633Sdim EmitObjCConsumeObject(S.getCollection()->getType(), Collection); 1468226633Sdim } else { 1469226633Sdim Collection = EmitScalarExpr(S.getCollection()); 1470226633Sdim } 1471226633Sdim 1472226633Sdim // The 'continue' label needs to appear within the cleanup for the 1473226633Sdim // collection object. 1474226633Sdim JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next"); 1475226633Sdim 1476218893Sdim // Send it our message: 1477193326Sed CallArgList Args; 1478218893Sdim 1479218893Sdim // The first argument is a temporary of the enumeration-state type. 1480221345Sdim Args.add(RValue::get(StatePtr), getContext().getPointerType(StateTy)); 1481198092Srdivacky 1482218893Sdim // The second argument is a temporary array with space for NumItems 1483218893Sdim // pointers. We'll actually be loading elements from the array 1484218893Sdim // pointer written into the control state; this buffer is so that 1485218893Sdim // collections that *aren't* backed by arrays can still queue up 1486218893Sdim // batches of elements. 1487221345Sdim Args.add(RValue::get(ItemsPtr), getContext().getPointerType(ItemsTy)); 1488198092Srdivacky 1489218893Sdim // The third argument is the capacity of that temporary array. 1490226633Sdim llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy); 1491193326Sed llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems); 1492221345Sdim Args.add(RValue::get(Count), getContext().UnsignedLongTy); 1493198092Srdivacky 1494218893Sdim // Start the enumeration. 1495198092Srdivacky RValue CountRV = 1496208600Srdivacky CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), 1497193326Sed getContext().UnsignedLongTy, 1498193326Sed FastEnumSel, 1499207619Srdivacky Collection, Args); 1500193326Sed 1501218893Sdim // The initial number of objects that were returned in the buffer. 1502218893Sdim llvm::Value *initialBufferLimit = CountRV.getScalarVal(); 1503198092Srdivacky 1504218893Sdim llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty"); 1505218893Sdim llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit"); 1506198092Srdivacky 1507218893Sdim llvm::Value *zero = llvm::Constant::getNullValue(UnsignedLongLTy); 1508193326Sed 1509218893Sdim // If the limit pointer was zero to begin with, the collection is 1510218893Sdim // empty; skip all this. 1511218893Sdim Builder.CreateCondBr(Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"), 1512218893Sdim EmptyBB, LoopInitBB); 1513193326Sed 1514218893Sdim // Otherwise, initialize the loop. 1515218893Sdim EmitBlock(LoopInitBB); 1516198092Srdivacky 1517218893Sdim // Save the initial mutations value. This is the value at an 1518218893Sdim // address that was written into the state object by 1519218893Sdim // countByEnumeratingWithState:objects:count:. 1520198092Srdivacky llvm::Value *StateMutationsPtrPtr = 1521193326Sed Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr"); 1522198092Srdivacky llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, 1523193326Sed "mutationsptr"); 1524198092Srdivacky 1525218893Sdim llvm::Value *initialMutations = 1526218893Sdim Builder.CreateLoad(StateMutationsPtr, "forcoll.initial-mutations"); 1527198092Srdivacky 1528218893Sdim // Start looping. This is the point we return to whenever we have a 1529218893Sdim // fresh, non-empty batch of objects. 1530218893Sdim llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody"); 1531218893Sdim EmitBlock(LoopBodyBB); 1532198092Srdivacky 1533218893Sdim // The current index into the buffer. 1534221345Sdim llvm::PHINode *index = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.index"); 1535218893Sdim index->addIncoming(zero, LoopInitBB); 1536193326Sed 1537218893Sdim // The current buffer size. 1538221345Sdim llvm::PHINode *count = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.count"); 1539218893Sdim count->addIncoming(initialBufferLimit, LoopInitBB); 1540198092Srdivacky 1541218893Sdim // Check whether the mutations value has changed from where it was 1542218893Sdim // at start. StateMutationsPtr should actually be invariant between 1543218893Sdim // refreshes. 1544193326Sed StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr"); 1545218893Sdim llvm::Value *currentMutations 1546218893Sdim = Builder.CreateLoad(StateMutationsPtr, "statemutations"); 1547193326Sed 1548218893Sdim llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated"); 1549221345Sdim llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcoll.notmutated"); 1550198092Srdivacky 1551218893Sdim Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations), 1552218893Sdim WasNotMutatedBB, WasMutatedBB); 1553198092Srdivacky 1554218893Sdim // If so, call the enumeration-mutation function. 1555218893Sdim EmitBlock(WasMutatedBB); 1556193326Sed llvm::Value *V = 1557198092Srdivacky Builder.CreateBitCast(Collection, 1558226633Sdim ConvertType(getContext().getObjCIdType())); 1559193326Sed CallArgList Args2; 1560221345Sdim Args2.add(RValue::get(V), getContext().getObjCIdType()); 1561193326Sed // FIXME: We shouldn't need to get the function info here, the runtime already 1562193326Sed // should have computed it to build the function. 1563239462Sdim EmitCall(CGM.getTypes().arrangeFreeFunctionCall(getContext().VoidTy, Args2, 1564239462Sdim FunctionType::ExtInfo(), 1565239462Sdim RequiredArgs::All), 1566201361Srdivacky EnumerationMutationFn, ReturnValueSlot(), Args2); 1567198092Srdivacky 1568218893Sdim // Otherwise, or if the mutation function returns, just continue. 1569218893Sdim EmitBlock(WasNotMutatedBB); 1570198092Srdivacky 1571218893Sdim // Initialize the element variable. 1572218893Sdim RunCleanupsScope elementVariableScope(*this); 1573219077Sdim bool elementIsVariable; 1574218893Sdim LValue elementLValue; 1575218893Sdim QualType elementType; 1576218893Sdim if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) { 1577219077Sdim // Initialize the variable, in case it's a __block variable or something. 1578219077Sdim EmitAutoVarInit(variable); 1579219077Sdim 1580218893Sdim const VarDecl* D = cast<VarDecl>(SD->getSingleDecl()); 1581234353Sdim DeclRefExpr tempDRE(const_cast<VarDecl*>(D), false, D->getType(), 1582218893Sdim VK_LValue, SourceLocation()); 1583218893Sdim elementLValue = EmitLValue(&tempDRE); 1584218893Sdim elementType = D->getType(); 1585219077Sdim elementIsVariable = true; 1586224145Sdim 1587224145Sdim if (D->isARCPseudoStrong()) 1588224145Sdim elementLValue.getQuals().setObjCLifetime(Qualifiers::OCL_ExplicitNone); 1589218893Sdim } else { 1590218893Sdim elementLValue = LValue(); // suppress warning 1591218893Sdim elementType = cast<Expr>(S.getElement())->getType(); 1592219077Sdim elementIsVariable = false; 1593218893Sdim } 1594226633Sdim llvm::Type *convertedElementType = ConvertType(elementType); 1595218893Sdim 1596218893Sdim // Fetch the buffer out of the enumeration state. 1597218893Sdim // TODO: this pointer should actually be invariant between 1598218893Sdim // refreshes, which would help us do certain loop optimizations. 1599198092Srdivacky llvm::Value *StateItemsPtr = 1600193326Sed Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr"); 1601218893Sdim llvm::Value *EnumStateItems = 1602218893Sdim Builder.CreateLoad(StateItemsPtr, "stateitems"); 1603193326Sed 1604218893Sdim // Fetch the value at the current index from the buffer. 1605198092Srdivacky llvm::Value *CurrentItemPtr = 1606218893Sdim Builder.CreateGEP(EnumStateItems, index, "currentitem.ptr"); 1607218893Sdim llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr); 1608198092Srdivacky 1609218893Sdim // Cast that value to the right type. 1610218893Sdim CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType, 1611218893Sdim "currentitem"); 1612198092Srdivacky 1613218893Sdim // Make sure we have an l-value. Yes, this gets evaluated every 1614218893Sdim // time through the loop. 1615224145Sdim if (!elementIsVariable) { 1616218893Sdim elementLValue = EmitLValue(cast<Expr>(S.getElement())); 1617224145Sdim EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue); 1618224145Sdim } else { 1619224145Sdim EmitScalarInit(CurrentItem, elementLValue); 1620224145Sdim } 1621198092Srdivacky 1622219077Sdim // If we do have an element variable, this assignment is the end of 1623219077Sdim // its initialization. 1624219077Sdim if (elementIsVariable) 1625219077Sdim EmitAutoVarCleanups(variable); 1626219077Sdim 1627218893Sdim // Perform the loop body, setting up break and continue labels. 1628218893Sdim BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody)); 1629218893Sdim { 1630218893Sdim RunCleanupsScope Scope(*this); 1631218893Sdim EmitStmt(S.getBody()); 1632218893Sdim } 1633218893Sdim BreakContinueStack.pop_back(); 1634198092Srdivacky 1635218893Sdim // Destroy the element variable now. 1636218893Sdim elementVariableScope.ForceCleanup(); 1637198092Srdivacky 1638218893Sdim // Check whether there are more elements. 1639218893Sdim EmitBlock(AfterBody.getBlock()); 1640198092Srdivacky 1641218893Sdim llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch"); 1642193326Sed 1643218893Sdim // First we check in the local buffer. 1644218893Sdim llvm::Value *indexPlusOne 1645218893Sdim = Builder.CreateAdd(index, llvm::ConstantInt::get(UnsignedLongLTy, 1)); 1646198092Srdivacky 1647218893Sdim // If we haven't overrun the buffer yet, we can continue. 1648218893Sdim Builder.CreateCondBr(Builder.CreateICmpULT(indexPlusOne, count), 1649218893Sdim LoopBodyBB, FetchMoreBB); 1650198092Srdivacky 1651218893Sdim index->addIncoming(indexPlusOne, AfterBody.getBlock()); 1652218893Sdim count->addIncoming(count, AfterBody.getBlock()); 1653198092Srdivacky 1654218893Sdim // Otherwise, we have to fetch more elements. 1655218893Sdim EmitBlock(FetchMoreBB); 1656193326Sed 1657198092Srdivacky CountRV = 1658208600Srdivacky CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), 1659193326Sed getContext().UnsignedLongTy, 1660198092Srdivacky FastEnumSel, 1661207619Srdivacky Collection, Args); 1662198092Srdivacky 1663218893Sdim // If we got a zero count, we're done. 1664218893Sdim llvm::Value *refetchCount = CountRV.getScalarVal(); 1665198092Srdivacky 1666218893Sdim // (note that the message send might split FetchMoreBB) 1667218893Sdim index->addIncoming(zero, Builder.GetInsertBlock()); 1668218893Sdim count->addIncoming(refetchCount, Builder.GetInsertBlock()); 1669218893Sdim 1670218893Sdim Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero), 1671218893Sdim EmptyBB, LoopBodyBB); 1672218893Sdim 1673193326Sed // No more elements. 1674218893Sdim EmitBlock(EmptyBB); 1675193326Sed 1676219077Sdim if (!elementIsVariable) { 1677193326Sed // If the element was not a declaration, set it to be null. 1678193326Sed 1679218893Sdim llvm::Value *null = llvm::Constant::getNullValue(convertedElementType); 1680218893Sdim elementLValue = EmitLValue(cast<Expr>(S.getElement())); 1681224145Sdim EmitStoreThroughLValue(RValue::get(null), elementLValue); 1682218893Sdim } 1683198092Srdivacky 1684226633Sdim if (DI) 1685226633Sdim DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd()); 1686193326Sed 1687226633Sdim // Leave the cleanup we entered in ARC. 1688234353Sdim if (getLangOpts().ObjCAutoRefCount) 1689226633Sdim PopCleanupBlock(); 1690226633Sdim 1691212904Sdim EmitBlock(LoopEnd.getBlock()); 1692193326Sed} 1693193326Sed 1694198092Srdivackyvoid CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) { 1695210299Sed CGM.getObjCRuntime().EmitTryStmt(*this, S); 1696193326Sed} 1697193326Sed 1698198092Srdivackyvoid CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) { 1699193326Sed CGM.getObjCRuntime().EmitThrowStmt(*this, S); 1700193326Sed} 1701193326Sed 1702193326Sedvoid CodeGenFunction::EmitObjCAtSynchronizedStmt( 1703198092Srdivacky const ObjCAtSynchronizedStmt &S) { 1704210299Sed CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S); 1705193326Sed} 1706193326Sed 1707226633Sdim/// Produce the code for a CK_ARCProduceObject. Just does a 1708224145Sdim/// primitive retain. 1709224145Sdimllvm::Value *CodeGenFunction::EmitObjCProduceObject(QualType type, 1710224145Sdim llvm::Value *value) { 1711224145Sdim return EmitARCRetain(type, value); 1712224145Sdim} 1713224145Sdim 1714224145Sdimnamespace { 1715224145Sdim struct CallObjCRelease : EHScopeStack::Cleanup { 1716226633Sdim CallObjCRelease(llvm::Value *object) : object(object) {} 1717226633Sdim llvm::Value *object; 1718224145Sdim 1719224145Sdim void Emit(CodeGenFunction &CGF, Flags flags) { 1720249423Sdim // Releases at the end of the full-expression are imprecise. 1721249423Sdim CGF.EmitARCRelease(object, ARCImpreciseLifetime); 1722224145Sdim } 1723224145Sdim }; 1724224145Sdim} 1725224145Sdim 1726226633Sdim/// Produce the code for a CK_ARCConsumeObject. Does a primitive 1727224145Sdim/// release at the end of the full-expression. 1728224145Sdimllvm::Value *CodeGenFunction::EmitObjCConsumeObject(QualType type, 1729224145Sdim llvm::Value *object) { 1730224145Sdim // If we're in a conditional branch, we need to make the cleanup 1731226633Sdim // conditional. 1732226633Sdim pushFullExprCleanup<CallObjCRelease>(getARCCleanupKind(), object); 1733224145Sdim return object; 1734224145Sdim} 1735224145Sdim 1736224145Sdimllvm::Value *CodeGenFunction::EmitObjCExtendObjectLifetime(QualType type, 1737224145Sdim llvm::Value *value) { 1738224145Sdim return EmitARCRetainAutorelease(type, value); 1739224145Sdim} 1740224145Sdim 1741249423Sdim/// Given a number of pointers, inform the optimizer that they're 1742249423Sdim/// being intrinsically used up until this point in the program. 1743249423Sdimvoid CodeGenFunction::EmitARCIntrinsicUse(ArrayRef<llvm::Value*> values) { 1744249423Sdim llvm::Constant *&fn = CGM.getARCEntrypoints().clang_arc_use; 1745249423Sdim if (!fn) { 1746249423Sdim llvm::FunctionType *fnType = 1747249423Sdim llvm::FunctionType::get(CGM.VoidTy, ArrayRef<llvm::Type*>(), true); 1748249423Sdim fn = CGM.CreateRuntimeFunction(fnType, "clang.arc.use"); 1749249423Sdim } 1750224145Sdim 1751249423Sdim // This isn't really a "runtime" function, but as an intrinsic it 1752249423Sdim // doesn't really matter as long as we align things up. 1753249423Sdim EmitNounwindRuntimeCall(fn, values); 1754249423Sdim} 1755249423Sdim 1756249423Sdim 1757224145Sdimstatic llvm::Constant *createARCRuntimeFunction(CodeGenModule &CGM, 1758226633Sdim llvm::FunctionType *type, 1759226633Sdim StringRef fnName) { 1760224145Sdim llvm::Constant *fn = CGM.CreateRuntimeFunction(type, fnName); 1761224145Sdim 1762239462Sdim if (llvm::Function *f = dyn_cast<llvm::Function>(fn)) { 1763249423Sdim // If the target runtime doesn't naturally support ARC, emit weak 1764249423Sdim // references to the runtime support library. We don't really 1765249423Sdim // permit this to fail, but we need a particular relocation style. 1766249423Sdim if (!CGM.getLangOpts().ObjCRuntime.hasNativeARC()) { 1767224145Sdim f->setLinkage(llvm::Function::ExternalWeakLinkage); 1768249423Sdim } else if (fnName == "objc_retain" || fnName == "objc_release") { 1769249423Sdim // If we have Native ARC, set nonlazybind attribute for these APIs for 1770249423Sdim // performance. 1771249423Sdim f->addFnAttr(llvm::Attribute::NonLazyBind); 1772249423Sdim } 1773239462Sdim } 1774224145Sdim 1775224145Sdim return fn; 1776224145Sdim} 1777224145Sdim 1778224145Sdim/// Perform an operation having the signature 1779224145Sdim/// i8* (i8*) 1780224145Sdim/// where a null input causes a no-op and returns null. 1781224145Sdimstatic llvm::Value *emitARCValueOperation(CodeGenFunction &CGF, 1782224145Sdim llvm::Value *value, 1783224145Sdim llvm::Constant *&fn, 1784249423Sdim StringRef fnName, 1785249423Sdim bool isTailCall = false) { 1786224145Sdim if (isa<llvm::ConstantPointerNull>(value)) return value; 1787224145Sdim 1788224145Sdim if (!fn) { 1789226633Sdim llvm::FunctionType *fnType = 1790249423Sdim llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrTy, false); 1791224145Sdim fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); 1792224145Sdim } 1793224145Sdim 1794224145Sdim // Cast the argument to 'id'. 1795226633Sdim llvm::Type *origType = value->getType(); 1796224145Sdim value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy); 1797224145Sdim 1798224145Sdim // Call the function. 1799249423Sdim llvm::CallInst *call = CGF.EmitNounwindRuntimeCall(fn, value); 1800249423Sdim if (isTailCall) 1801249423Sdim call->setTailCall(); 1802224145Sdim 1803224145Sdim // Cast the result back to the original type. 1804224145Sdim return CGF.Builder.CreateBitCast(call, origType); 1805224145Sdim} 1806224145Sdim 1807224145Sdim/// Perform an operation having the following signature: 1808224145Sdim/// i8* (i8**) 1809224145Sdimstatic llvm::Value *emitARCLoadOperation(CodeGenFunction &CGF, 1810224145Sdim llvm::Value *addr, 1811224145Sdim llvm::Constant *&fn, 1812226633Sdim StringRef fnName) { 1813224145Sdim if (!fn) { 1814226633Sdim llvm::FunctionType *fnType = 1815249423Sdim llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrPtrTy, false); 1816224145Sdim fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); 1817224145Sdim } 1818224145Sdim 1819224145Sdim // Cast the argument to 'id*'. 1820226633Sdim llvm::Type *origType = addr->getType(); 1821224145Sdim addr = CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy); 1822224145Sdim 1823224145Sdim // Call the function. 1824249423Sdim llvm::Value *result = CGF.EmitNounwindRuntimeCall(fn, addr); 1825224145Sdim 1826224145Sdim // Cast the result back to a dereference of the original type. 1827224145Sdim if (origType != CGF.Int8PtrPtrTy) 1828224145Sdim result = CGF.Builder.CreateBitCast(result, 1829224145Sdim cast<llvm::PointerType>(origType)->getElementType()); 1830224145Sdim 1831224145Sdim return result; 1832224145Sdim} 1833224145Sdim 1834224145Sdim/// Perform an operation having the following signature: 1835224145Sdim/// i8* (i8**, i8*) 1836224145Sdimstatic llvm::Value *emitARCStoreOperation(CodeGenFunction &CGF, 1837224145Sdim llvm::Value *addr, 1838224145Sdim llvm::Value *value, 1839224145Sdim llvm::Constant *&fn, 1840226633Sdim StringRef fnName, 1841224145Sdim bool ignored) { 1842224145Sdim assert(cast<llvm::PointerType>(addr->getType())->getElementType() 1843224145Sdim == value->getType()); 1844224145Sdim 1845224145Sdim if (!fn) { 1846234353Sdim llvm::Type *argTypes[] = { CGF.Int8PtrPtrTy, CGF.Int8PtrTy }; 1847224145Sdim 1848226633Sdim llvm::FunctionType *fnType 1849224145Sdim = llvm::FunctionType::get(CGF.Int8PtrTy, argTypes, false); 1850224145Sdim fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); 1851224145Sdim } 1852224145Sdim 1853226633Sdim llvm::Type *origType = value->getType(); 1854224145Sdim 1855249423Sdim llvm::Value *args[] = { 1856249423Sdim CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy), 1857249423Sdim CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy) 1858249423Sdim }; 1859249423Sdim llvm::CallInst *result = CGF.EmitNounwindRuntimeCall(fn, args); 1860224145Sdim 1861224145Sdim if (ignored) return 0; 1862224145Sdim 1863224145Sdim return CGF.Builder.CreateBitCast(result, origType); 1864224145Sdim} 1865224145Sdim 1866224145Sdim/// Perform an operation having the following signature: 1867224145Sdim/// void (i8**, i8**) 1868224145Sdimstatic void emitARCCopyOperation(CodeGenFunction &CGF, 1869224145Sdim llvm::Value *dst, 1870224145Sdim llvm::Value *src, 1871224145Sdim llvm::Constant *&fn, 1872226633Sdim StringRef fnName) { 1873224145Sdim assert(dst->getType() == src->getType()); 1874224145Sdim 1875224145Sdim if (!fn) { 1876249423Sdim llvm::Type *argTypes[] = { CGF.Int8PtrPtrTy, CGF.Int8PtrPtrTy }; 1877249423Sdim 1878226633Sdim llvm::FunctionType *fnType 1879224145Sdim = llvm::FunctionType::get(CGF.Builder.getVoidTy(), argTypes, false); 1880224145Sdim fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); 1881224145Sdim } 1882224145Sdim 1883249423Sdim llvm::Value *args[] = { 1884249423Sdim CGF.Builder.CreateBitCast(dst, CGF.Int8PtrPtrTy), 1885249423Sdim CGF.Builder.CreateBitCast(src, CGF.Int8PtrPtrTy) 1886249423Sdim }; 1887249423Sdim CGF.EmitNounwindRuntimeCall(fn, args); 1888224145Sdim} 1889224145Sdim 1890224145Sdim/// Produce the code to do a retain. Based on the type, calls one of: 1891239462Sdim/// call i8* \@objc_retain(i8* %value) 1892239462Sdim/// call i8* \@objc_retainBlock(i8* %value) 1893224145Sdimllvm::Value *CodeGenFunction::EmitARCRetain(QualType type, llvm::Value *value) { 1894224145Sdim if (type->isBlockPointerType()) 1895226633Sdim return EmitARCRetainBlock(value, /*mandatory*/ false); 1896224145Sdim else 1897224145Sdim return EmitARCRetainNonBlock(value); 1898224145Sdim} 1899224145Sdim 1900224145Sdim/// Retain the given object, with normal retain semantics. 1901239462Sdim/// call i8* \@objc_retain(i8* %value) 1902224145Sdimllvm::Value *CodeGenFunction::EmitARCRetainNonBlock(llvm::Value *value) { 1903224145Sdim return emitARCValueOperation(*this, value, 1904224145Sdim CGM.getARCEntrypoints().objc_retain, 1905224145Sdim "objc_retain"); 1906224145Sdim} 1907224145Sdim 1908224145Sdim/// Retain the given block, with _Block_copy semantics. 1909239462Sdim/// call i8* \@objc_retainBlock(i8* %value) 1910226633Sdim/// 1911226633Sdim/// \param mandatory - If false, emit the call with metadata 1912226633Sdim/// indicating that it's okay for the optimizer to eliminate this call 1913226633Sdim/// if it can prove that the block never escapes except down the stack. 1914226633Sdimllvm::Value *CodeGenFunction::EmitARCRetainBlock(llvm::Value *value, 1915226633Sdim bool mandatory) { 1916226633Sdim llvm::Value *result 1917226633Sdim = emitARCValueOperation(*this, value, 1918226633Sdim CGM.getARCEntrypoints().objc_retainBlock, 1919226633Sdim "objc_retainBlock"); 1920226633Sdim 1921226633Sdim // If the copy isn't mandatory, add !clang.arc.copy_on_escape to 1922226633Sdim // tell the optimizer that it doesn't need to do this copy if the 1923226633Sdim // block doesn't escape, where being passed as an argument doesn't 1924226633Sdim // count as escaping. 1925226633Sdim if (!mandatory && isa<llvm::Instruction>(result)) { 1926226633Sdim llvm::CallInst *call 1927226633Sdim = cast<llvm::CallInst>(result->stripPointerCasts()); 1928226633Sdim assert(call->getCalledValue() == CGM.getARCEntrypoints().objc_retainBlock); 1929226633Sdim 1930226633Sdim SmallVector<llvm::Value*,1> args; 1931226633Sdim call->setMetadata("clang.arc.copy_on_escape", 1932226633Sdim llvm::MDNode::get(Builder.getContext(), args)); 1933226633Sdim } 1934226633Sdim 1935226633Sdim return result; 1936224145Sdim} 1937224145Sdim 1938224145Sdim/// Retain the given object which is the result of a function call. 1939239462Sdim/// call i8* \@objc_retainAutoreleasedReturnValue(i8* %value) 1940224145Sdim/// 1941224145Sdim/// Yes, this function name is one character away from a different 1942224145Sdim/// call with completely different semantics. 1943224145Sdimllvm::Value * 1944224145SdimCodeGenFunction::EmitARCRetainAutoreleasedReturnValue(llvm::Value *value) { 1945224145Sdim // Fetch the void(void) inline asm which marks that we're going to 1946224145Sdim // retain the autoreleased return value. 1947224145Sdim llvm::InlineAsm *&marker 1948224145Sdim = CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker; 1949224145Sdim if (!marker) { 1950226633Sdim StringRef assembly 1951224145Sdim = CGM.getTargetCodeGenInfo() 1952224145Sdim .getARCRetainAutoreleasedReturnValueMarker(); 1953224145Sdim 1954224145Sdim // If we have an empty assembly string, there's nothing to do. 1955224145Sdim if (assembly.empty()) { 1956224145Sdim 1957224145Sdim // Otherwise, at -O0, build an inline asm that we're going to call 1958224145Sdim // in a moment. 1959224145Sdim } else if (CGM.getCodeGenOpts().OptimizationLevel == 0) { 1960224145Sdim llvm::FunctionType *type = 1961234353Sdim llvm::FunctionType::get(VoidTy, /*variadic*/false); 1962224145Sdim 1963224145Sdim marker = llvm::InlineAsm::get(type, assembly, "", /*sideeffects*/ true); 1964224145Sdim 1965224145Sdim // If we're at -O1 and above, we don't want to litter the code 1966224145Sdim // with this marker yet, so leave a breadcrumb for the ARC 1967224145Sdim // optimizer to pick up. 1968224145Sdim } else { 1969224145Sdim llvm::NamedMDNode *metadata = 1970224145Sdim CGM.getModule().getOrInsertNamedMetadata( 1971224145Sdim "clang.arc.retainAutoreleasedReturnValueMarker"); 1972224145Sdim assert(metadata->getNumOperands() <= 1); 1973224145Sdim if (metadata->getNumOperands() == 0) { 1974224145Sdim llvm::Value *string = llvm::MDString::get(getLLVMContext(), assembly); 1975226633Sdim metadata->addOperand(llvm::MDNode::get(getLLVMContext(), string)); 1976224145Sdim } 1977224145Sdim } 1978224145Sdim } 1979224145Sdim 1980224145Sdim // Call the marker asm if we made one, which we do only at -O0. 1981224145Sdim if (marker) Builder.CreateCall(marker); 1982224145Sdim 1983224145Sdim return emitARCValueOperation(*this, value, 1984224145Sdim CGM.getARCEntrypoints().objc_retainAutoreleasedReturnValue, 1985224145Sdim "objc_retainAutoreleasedReturnValue"); 1986224145Sdim} 1987224145Sdim 1988224145Sdim/// Release the given object. 1989239462Sdim/// call void \@objc_release(i8* %value) 1990249423Sdimvoid CodeGenFunction::EmitARCRelease(llvm::Value *value, 1991249423Sdim ARCPreciseLifetime_t precise) { 1992224145Sdim if (isa<llvm::ConstantPointerNull>(value)) return; 1993224145Sdim 1994224145Sdim llvm::Constant *&fn = CGM.getARCEntrypoints().objc_release; 1995224145Sdim if (!fn) { 1996226633Sdim llvm::FunctionType *fnType = 1997249423Sdim llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false); 1998224145Sdim fn = createARCRuntimeFunction(CGM, fnType, "objc_release"); 1999224145Sdim } 2000224145Sdim 2001224145Sdim // Cast the argument to 'id'. 2002224145Sdim value = Builder.CreateBitCast(value, Int8PtrTy); 2003224145Sdim 2004224145Sdim // Call objc_release. 2005249423Sdim llvm::CallInst *call = EmitNounwindRuntimeCall(fn, value); 2006224145Sdim 2007249423Sdim if (precise == ARCImpreciseLifetime) { 2008226633Sdim SmallVector<llvm::Value*,1> args; 2009224145Sdim call->setMetadata("clang.imprecise_release", 2010224145Sdim llvm::MDNode::get(Builder.getContext(), args)); 2011224145Sdim } 2012224145Sdim} 2013224145Sdim 2014243830Sdim/// Destroy a __strong variable. 2015243830Sdim/// 2016243830Sdim/// At -O0, emit a call to store 'null' into the address; 2017243830Sdim/// instrumenting tools prefer this because the address is exposed, 2018243830Sdim/// but it's relatively cumbersome to optimize. 2019243830Sdim/// 2020243830Sdim/// At -O1 and above, just load and call objc_release. 2021243830Sdim/// 2022243830Sdim/// call void \@objc_storeStrong(i8** %addr, i8* null) 2023249423Sdimvoid CodeGenFunction::EmitARCDestroyStrong(llvm::Value *addr, 2024249423Sdim ARCPreciseLifetime_t precise) { 2025243830Sdim if (CGM.getCodeGenOpts().OptimizationLevel == 0) { 2026243830Sdim llvm::PointerType *addrTy = cast<llvm::PointerType>(addr->getType()); 2027243830Sdim llvm::Value *null = llvm::ConstantPointerNull::get( 2028243830Sdim cast<llvm::PointerType>(addrTy->getElementType())); 2029243830Sdim EmitARCStoreStrongCall(addr, null, /*ignored*/ true); 2030243830Sdim return; 2031243830Sdim } 2032243830Sdim 2033243830Sdim llvm::Value *value = Builder.CreateLoad(addr); 2034243830Sdim EmitARCRelease(value, precise); 2035243830Sdim} 2036243830Sdim 2037224145Sdim/// Store into a strong object. Always calls this: 2038239462Sdim/// call void \@objc_storeStrong(i8** %addr, i8* %value) 2039224145Sdimllvm::Value *CodeGenFunction::EmitARCStoreStrongCall(llvm::Value *addr, 2040224145Sdim llvm::Value *value, 2041224145Sdim bool ignored) { 2042224145Sdim assert(cast<llvm::PointerType>(addr->getType())->getElementType() 2043224145Sdim == value->getType()); 2044224145Sdim 2045224145Sdim llvm::Constant *&fn = CGM.getARCEntrypoints().objc_storeStrong; 2046224145Sdim if (!fn) { 2047224145Sdim llvm::Type *argTypes[] = { Int8PtrPtrTy, Int8PtrTy }; 2048226633Sdim llvm::FunctionType *fnType 2049224145Sdim = llvm::FunctionType::get(Builder.getVoidTy(), argTypes, false); 2050224145Sdim fn = createARCRuntimeFunction(CGM, fnType, "objc_storeStrong"); 2051224145Sdim } 2052224145Sdim 2053249423Sdim llvm::Value *args[] = { 2054249423Sdim Builder.CreateBitCast(addr, Int8PtrPtrTy), 2055249423Sdim Builder.CreateBitCast(value, Int8PtrTy) 2056249423Sdim }; 2057249423Sdim EmitNounwindRuntimeCall(fn, args); 2058224145Sdim 2059224145Sdim if (ignored) return 0; 2060224145Sdim return value; 2061224145Sdim} 2062224145Sdim 2063224145Sdim/// Store into a strong object. Sometimes calls this: 2064239462Sdim/// call void \@objc_storeStrong(i8** %addr, i8* %value) 2065224145Sdim/// Other times, breaks it down into components. 2066224145Sdimllvm::Value *CodeGenFunction::EmitARCStoreStrong(LValue dst, 2067224145Sdim llvm::Value *newValue, 2068224145Sdim bool ignored) { 2069224145Sdim QualType type = dst.getType(); 2070224145Sdim bool isBlock = type->isBlockPointerType(); 2071224145Sdim 2072224145Sdim // Use a store barrier at -O0 unless this is a block type or the 2073224145Sdim // lvalue is inadequately aligned. 2074224145Sdim if (shouldUseFusedARCCalls() && 2075224145Sdim !isBlock && 2076234353Sdim (dst.getAlignment().isZero() || 2077234353Sdim dst.getAlignment() >= CharUnits::fromQuantity(PointerAlignInBytes))) { 2078224145Sdim return EmitARCStoreStrongCall(dst.getAddress(), newValue, ignored); 2079224145Sdim } 2080224145Sdim 2081224145Sdim // Otherwise, split it out. 2082224145Sdim 2083224145Sdim // Retain the new value. 2084224145Sdim newValue = EmitARCRetain(type, newValue); 2085224145Sdim 2086224145Sdim // Read the old value. 2087224145Sdim llvm::Value *oldValue = EmitLoadOfScalar(dst); 2088224145Sdim 2089224145Sdim // Store. We do this before the release so that any deallocs won't 2090224145Sdim // see the old value. 2091224145Sdim EmitStoreOfScalar(newValue, dst); 2092224145Sdim 2093224145Sdim // Finally, release the old value. 2094249423Sdim EmitARCRelease(oldValue, dst.isARCPreciseLifetime()); 2095224145Sdim 2096224145Sdim return newValue; 2097224145Sdim} 2098224145Sdim 2099224145Sdim/// Autorelease the given object. 2100239462Sdim/// call i8* \@objc_autorelease(i8* %value) 2101224145Sdimllvm::Value *CodeGenFunction::EmitARCAutorelease(llvm::Value *value) { 2102224145Sdim return emitARCValueOperation(*this, value, 2103224145Sdim CGM.getARCEntrypoints().objc_autorelease, 2104224145Sdim "objc_autorelease"); 2105224145Sdim} 2106224145Sdim 2107224145Sdim/// Autorelease the given object. 2108239462Sdim/// call i8* \@objc_autoreleaseReturnValue(i8* %value) 2109224145Sdimllvm::Value * 2110224145SdimCodeGenFunction::EmitARCAutoreleaseReturnValue(llvm::Value *value) { 2111224145Sdim return emitARCValueOperation(*this, value, 2112224145Sdim CGM.getARCEntrypoints().objc_autoreleaseReturnValue, 2113249423Sdim "objc_autoreleaseReturnValue", 2114249423Sdim /*isTailCall*/ true); 2115224145Sdim} 2116224145Sdim 2117224145Sdim/// Do a fused retain/autorelease of the given object. 2118239462Sdim/// call i8* \@objc_retainAutoreleaseReturnValue(i8* %value) 2119224145Sdimllvm::Value * 2120224145SdimCodeGenFunction::EmitARCRetainAutoreleaseReturnValue(llvm::Value *value) { 2121224145Sdim return emitARCValueOperation(*this, value, 2122224145Sdim CGM.getARCEntrypoints().objc_retainAutoreleaseReturnValue, 2123249423Sdim "objc_retainAutoreleaseReturnValue", 2124249423Sdim /*isTailCall*/ true); 2125224145Sdim} 2126224145Sdim 2127224145Sdim/// Do a fused retain/autorelease of the given object. 2128239462Sdim/// call i8* \@objc_retainAutorelease(i8* %value) 2129224145Sdim/// or 2130239462Sdim/// %retain = call i8* \@objc_retainBlock(i8* %value) 2131239462Sdim/// call i8* \@objc_autorelease(i8* %retain) 2132224145Sdimllvm::Value *CodeGenFunction::EmitARCRetainAutorelease(QualType type, 2133224145Sdim llvm::Value *value) { 2134224145Sdim if (!type->isBlockPointerType()) 2135224145Sdim return EmitARCRetainAutoreleaseNonBlock(value); 2136224145Sdim 2137224145Sdim if (isa<llvm::ConstantPointerNull>(value)) return value; 2138224145Sdim 2139226633Sdim llvm::Type *origType = value->getType(); 2140224145Sdim value = Builder.CreateBitCast(value, Int8PtrTy); 2141226633Sdim value = EmitARCRetainBlock(value, /*mandatory*/ true); 2142224145Sdim value = EmitARCAutorelease(value); 2143224145Sdim return Builder.CreateBitCast(value, origType); 2144224145Sdim} 2145224145Sdim 2146224145Sdim/// Do a fused retain/autorelease of the given object. 2147239462Sdim/// call i8* \@objc_retainAutorelease(i8* %value) 2148224145Sdimllvm::Value * 2149224145SdimCodeGenFunction::EmitARCRetainAutoreleaseNonBlock(llvm::Value *value) { 2150224145Sdim return emitARCValueOperation(*this, value, 2151224145Sdim CGM.getARCEntrypoints().objc_retainAutorelease, 2152224145Sdim "objc_retainAutorelease"); 2153224145Sdim} 2154224145Sdim 2155239462Sdim/// i8* \@objc_loadWeak(i8** %addr) 2156224145Sdim/// Essentially objc_autorelease(objc_loadWeakRetained(addr)). 2157224145Sdimllvm::Value *CodeGenFunction::EmitARCLoadWeak(llvm::Value *addr) { 2158224145Sdim return emitARCLoadOperation(*this, addr, 2159224145Sdim CGM.getARCEntrypoints().objc_loadWeak, 2160224145Sdim "objc_loadWeak"); 2161224145Sdim} 2162224145Sdim 2163239462Sdim/// i8* \@objc_loadWeakRetained(i8** %addr) 2164224145Sdimllvm::Value *CodeGenFunction::EmitARCLoadWeakRetained(llvm::Value *addr) { 2165224145Sdim return emitARCLoadOperation(*this, addr, 2166224145Sdim CGM.getARCEntrypoints().objc_loadWeakRetained, 2167224145Sdim "objc_loadWeakRetained"); 2168224145Sdim} 2169224145Sdim 2170239462Sdim/// i8* \@objc_storeWeak(i8** %addr, i8* %value) 2171224145Sdim/// Returns %value. 2172224145Sdimllvm::Value *CodeGenFunction::EmitARCStoreWeak(llvm::Value *addr, 2173224145Sdim llvm::Value *value, 2174224145Sdim bool ignored) { 2175224145Sdim return emitARCStoreOperation(*this, addr, value, 2176224145Sdim CGM.getARCEntrypoints().objc_storeWeak, 2177224145Sdim "objc_storeWeak", ignored); 2178224145Sdim} 2179224145Sdim 2180239462Sdim/// i8* \@objc_initWeak(i8** %addr, i8* %value) 2181224145Sdim/// Returns %value. %addr is known to not have a current weak entry. 2182224145Sdim/// Essentially equivalent to: 2183224145Sdim/// *addr = nil; objc_storeWeak(addr, value); 2184224145Sdimvoid CodeGenFunction::EmitARCInitWeak(llvm::Value *addr, llvm::Value *value) { 2185224145Sdim // If we're initializing to null, just write null to memory; no need 2186224145Sdim // to get the runtime involved. But don't do this if optimization 2187224145Sdim // is enabled, because accounting for this would make the optimizer 2188224145Sdim // much more complicated. 2189224145Sdim if (isa<llvm::ConstantPointerNull>(value) && 2190224145Sdim CGM.getCodeGenOpts().OptimizationLevel == 0) { 2191224145Sdim Builder.CreateStore(value, addr); 2192224145Sdim return; 2193224145Sdim } 2194224145Sdim 2195224145Sdim emitARCStoreOperation(*this, addr, value, 2196224145Sdim CGM.getARCEntrypoints().objc_initWeak, 2197224145Sdim "objc_initWeak", /*ignored*/ true); 2198224145Sdim} 2199224145Sdim 2200239462Sdim/// void \@objc_destroyWeak(i8** %addr) 2201224145Sdim/// Essentially objc_storeWeak(addr, nil). 2202224145Sdimvoid CodeGenFunction::EmitARCDestroyWeak(llvm::Value *addr) { 2203224145Sdim llvm::Constant *&fn = CGM.getARCEntrypoints().objc_destroyWeak; 2204224145Sdim if (!fn) { 2205226633Sdim llvm::FunctionType *fnType = 2206249423Sdim llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrPtrTy, false); 2207224145Sdim fn = createARCRuntimeFunction(CGM, fnType, "objc_destroyWeak"); 2208224145Sdim } 2209224145Sdim 2210224145Sdim // Cast the argument to 'id*'. 2211224145Sdim addr = Builder.CreateBitCast(addr, Int8PtrPtrTy); 2212224145Sdim 2213249423Sdim EmitNounwindRuntimeCall(fn, addr); 2214224145Sdim} 2215224145Sdim 2216239462Sdim/// void \@objc_moveWeak(i8** %dest, i8** %src) 2217224145Sdim/// Disregards the current value in %dest. Leaves %src pointing to nothing. 2218224145Sdim/// Essentially (objc_copyWeak(dest, src), objc_destroyWeak(src)). 2219224145Sdimvoid CodeGenFunction::EmitARCMoveWeak(llvm::Value *dst, llvm::Value *src) { 2220224145Sdim emitARCCopyOperation(*this, dst, src, 2221224145Sdim CGM.getARCEntrypoints().objc_moveWeak, 2222224145Sdim "objc_moveWeak"); 2223224145Sdim} 2224224145Sdim 2225239462Sdim/// void \@objc_copyWeak(i8** %dest, i8** %src) 2226224145Sdim/// Disregards the current value in %dest. Essentially 2227224145Sdim/// objc_release(objc_initWeak(dest, objc_readWeakRetained(src))) 2228224145Sdimvoid CodeGenFunction::EmitARCCopyWeak(llvm::Value *dst, llvm::Value *src) { 2229224145Sdim emitARCCopyOperation(*this, dst, src, 2230224145Sdim CGM.getARCEntrypoints().objc_copyWeak, 2231224145Sdim "objc_copyWeak"); 2232224145Sdim} 2233224145Sdim 2234224145Sdim/// Produce the code to do a objc_autoreleasepool_push. 2235239462Sdim/// call i8* \@objc_autoreleasePoolPush(void) 2236224145Sdimllvm::Value *CodeGenFunction::EmitObjCAutoreleasePoolPush() { 2237224145Sdim llvm::Constant *&fn = CGM.getRREntrypoints().objc_autoreleasePoolPush; 2238224145Sdim if (!fn) { 2239226633Sdim llvm::FunctionType *fnType = 2240224145Sdim llvm::FunctionType::get(Int8PtrTy, false); 2241224145Sdim fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPush"); 2242224145Sdim } 2243224145Sdim 2244249423Sdim return EmitNounwindRuntimeCall(fn); 2245224145Sdim} 2246224145Sdim 2247224145Sdim/// Produce the code to do a primitive release. 2248239462Sdim/// call void \@objc_autoreleasePoolPop(i8* %ptr) 2249224145Sdimvoid CodeGenFunction::EmitObjCAutoreleasePoolPop(llvm::Value *value) { 2250224145Sdim assert(value->getType() == Int8PtrTy); 2251224145Sdim 2252224145Sdim llvm::Constant *&fn = CGM.getRREntrypoints().objc_autoreleasePoolPop; 2253224145Sdim if (!fn) { 2254226633Sdim llvm::FunctionType *fnType = 2255249423Sdim llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false); 2256224145Sdim 2257224145Sdim // We don't want to use a weak import here; instead we should not 2258224145Sdim // fall into this path. 2259224145Sdim fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPop"); 2260224145Sdim } 2261224145Sdim 2262251662Sdim // objc_autoreleasePoolPop can throw. 2263251662Sdim EmitRuntimeCallOrInvoke(fn, value); 2264224145Sdim} 2265224145Sdim 2266224145Sdim/// Produce the code to do an MRR version objc_autoreleasepool_push. 2267224145Sdim/// Which is: [[NSAutoreleasePool alloc] init]; 2268224145Sdim/// Where alloc is declared as: + (id) alloc; in NSAutoreleasePool class. 2269224145Sdim/// init is declared as: - (id) init; in its NSObject super class. 2270224145Sdim/// 2271224145Sdimllvm::Value *CodeGenFunction::EmitObjCMRRAutoreleasePoolPush() { 2272224145Sdim CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 2273249423Sdim llvm::Value *Receiver = Runtime.EmitNSAutoreleasePoolClassRef(*this); 2274224145Sdim // [NSAutoreleasePool alloc] 2275224145Sdim IdentifierInfo *II = &CGM.getContext().Idents.get("alloc"); 2276224145Sdim Selector AllocSel = getContext().Selectors.getSelector(0, &II); 2277224145Sdim CallArgList Args; 2278224145Sdim RValue AllocRV = 2279224145Sdim Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 2280224145Sdim getContext().getObjCIdType(), 2281224145Sdim AllocSel, Receiver, Args); 2282224145Sdim 2283224145Sdim // [Receiver init] 2284224145Sdim Receiver = AllocRV.getScalarVal(); 2285224145Sdim II = &CGM.getContext().Idents.get("init"); 2286224145Sdim Selector InitSel = getContext().Selectors.getSelector(0, &II); 2287224145Sdim RValue InitRV = 2288224145Sdim Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 2289224145Sdim getContext().getObjCIdType(), 2290224145Sdim InitSel, Receiver, Args); 2291224145Sdim return InitRV.getScalarVal(); 2292224145Sdim} 2293224145Sdim 2294224145Sdim/// Produce the code to do a primitive release. 2295224145Sdim/// [tmp drain]; 2296224145Sdimvoid CodeGenFunction::EmitObjCMRRAutoreleasePoolPop(llvm::Value *Arg) { 2297224145Sdim IdentifierInfo *II = &CGM.getContext().Idents.get("drain"); 2298224145Sdim Selector DrainSel = getContext().Selectors.getSelector(0, &II); 2299224145Sdim CallArgList Args; 2300224145Sdim CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), 2301224145Sdim getContext().VoidTy, DrainSel, Arg, Args); 2302224145Sdim} 2303224145Sdim 2304224145Sdimvoid CodeGenFunction::destroyARCStrongPrecise(CodeGenFunction &CGF, 2305224145Sdim llvm::Value *addr, 2306224145Sdim QualType type) { 2307249423Sdim CGF.EmitARCDestroyStrong(addr, ARCPreciseLifetime); 2308224145Sdim} 2309224145Sdim 2310224145Sdimvoid CodeGenFunction::destroyARCStrongImprecise(CodeGenFunction &CGF, 2311224145Sdim llvm::Value *addr, 2312224145Sdim QualType type) { 2313249423Sdim CGF.EmitARCDestroyStrong(addr, ARCImpreciseLifetime); 2314224145Sdim} 2315224145Sdim 2316224145Sdimvoid CodeGenFunction::destroyARCWeak(CodeGenFunction &CGF, 2317224145Sdim llvm::Value *addr, 2318224145Sdim QualType type) { 2319224145Sdim CGF.EmitARCDestroyWeak(addr); 2320224145Sdim} 2321224145Sdim 2322224145Sdimnamespace { 2323224145Sdim struct CallObjCAutoreleasePoolObject : EHScopeStack::Cleanup { 2324224145Sdim llvm::Value *Token; 2325224145Sdim 2326224145Sdim CallObjCAutoreleasePoolObject(llvm::Value *token) : Token(token) {} 2327224145Sdim 2328224145Sdim void Emit(CodeGenFunction &CGF, Flags flags) { 2329224145Sdim CGF.EmitObjCAutoreleasePoolPop(Token); 2330224145Sdim } 2331224145Sdim }; 2332224145Sdim struct CallObjCMRRAutoreleasePoolObject : EHScopeStack::Cleanup { 2333224145Sdim llvm::Value *Token; 2334224145Sdim 2335224145Sdim CallObjCMRRAutoreleasePoolObject(llvm::Value *token) : Token(token) {} 2336224145Sdim 2337224145Sdim void Emit(CodeGenFunction &CGF, Flags flags) { 2338224145Sdim CGF.EmitObjCMRRAutoreleasePoolPop(Token); 2339224145Sdim } 2340224145Sdim }; 2341224145Sdim} 2342224145Sdim 2343224145Sdimvoid CodeGenFunction::EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr) { 2344234353Sdim if (CGM.getLangOpts().ObjCAutoRefCount) 2345224145Sdim EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, Ptr); 2346224145Sdim else 2347224145Sdim EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, Ptr); 2348224145Sdim} 2349224145Sdim 2350224145Sdimstatic TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF, 2351224145Sdim LValue lvalue, 2352224145Sdim QualType type) { 2353224145Sdim switch (type.getObjCLifetime()) { 2354224145Sdim case Qualifiers::OCL_None: 2355224145Sdim case Qualifiers::OCL_ExplicitNone: 2356224145Sdim case Qualifiers::OCL_Strong: 2357224145Sdim case Qualifiers::OCL_Autoreleasing: 2358224145Sdim return TryEmitResult(CGF.EmitLoadOfLValue(lvalue).getScalarVal(), 2359224145Sdim false); 2360224145Sdim 2361224145Sdim case Qualifiers::OCL_Weak: 2362224145Sdim return TryEmitResult(CGF.EmitARCLoadWeakRetained(lvalue.getAddress()), 2363224145Sdim true); 2364224145Sdim } 2365224145Sdim 2366224145Sdim llvm_unreachable("impossible lifetime!"); 2367224145Sdim} 2368224145Sdim 2369224145Sdimstatic TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF, 2370224145Sdim const Expr *e) { 2371224145Sdim e = e->IgnoreParens(); 2372224145Sdim QualType type = e->getType(); 2373224145Sdim 2374226633Sdim // If we're loading retained from a __strong xvalue, we can avoid 2375226633Sdim // an extra retain/release pair by zeroing out the source of this 2376226633Sdim // "move" operation. 2377226633Sdim if (e->isXValue() && 2378226633Sdim !type.isConstQualified() && 2379226633Sdim type.getObjCLifetime() == Qualifiers::OCL_Strong) { 2380226633Sdim // Emit the lvalue. 2381226633Sdim LValue lv = CGF.EmitLValue(e); 2382226633Sdim 2383226633Sdim // Load the object pointer. 2384226633Sdim llvm::Value *result = CGF.EmitLoadOfLValue(lv).getScalarVal(); 2385226633Sdim 2386226633Sdim // Set the source pointer to NULL. 2387226633Sdim CGF.EmitStoreOfScalar(getNullForVariable(lv.getAddress()), lv); 2388226633Sdim 2389226633Sdim return TryEmitResult(result, true); 2390226633Sdim } 2391226633Sdim 2392224145Sdim // As a very special optimization, in ARC++, if the l-value is the 2393224145Sdim // result of a non-volatile assignment, do a simple retain of the 2394224145Sdim // result of the call to objc_storeWeak instead of reloading. 2395234353Sdim if (CGF.getLangOpts().CPlusPlus && 2396224145Sdim !type.isVolatileQualified() && 2397224145Sdim type.getObjCLifetime() == Qualifiers::OCL_Weak && 2398224145Sdim isa<BinaryOperator>(e) && 2399224145Sdim cast<BinaryOperator>(e)->getOpcode() == BO_Assign) 2400224145Sdim return TryEmitResult(CGF.EmitScalarExpr(e), false); 2401224145Sdim 2402224145Sdim return tryEmitARCRetainLoadOfScalar(CGF, CGF.EmitLValue(e), type); 2403224145Sdim} 2404224145Sdim 2405224145Sdimstatic llvm::Value *emitARCRetainAfterCall(CodeGenFunction &CGF, 2406224145Sdim llvm::Value *value); 2407224145Sdim 2408224145Sdim/// Given that the given expression is some sort of call (which does 2409224145Sdim/// not return retained), emit a retain following it. 2410224145Sdimstatic llvm::Value *emitARCRetainCall(CodeGenFunction &CGF, const Expr *e) { 2411224145Sdim llvm::Value *value = CGF.EmitScalarExpr(e); 2412224145Sdim return emitARCRetainAfterCall(CGF, value); 2413224145Sdim} 2414224145Sdim 2415224145Sdimstatic llvm::Value *emitARCRetainAfterCall(CodeGenFunction &CGF, 2416224145Sdim llvm::Value *value) { 2417224145Sdim if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(value)) { 2418224145Sdim CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP(); 2419224145Sdim 2420224145Sdim // Place the retain immediately following the call. 2421224145Sdim CGF.Builder.SetInsertPoint(call->getParent(), 2422224145Sdim ++llvm::BasicBlock::iterator(call)); 2423224145Sdim value = CGF.EmitARCRetainAutoreleasedReturnValue(value); 2424224145Sdim 2425224145Sdim CGF.Builder.restoreIP(ip); 2426224145Sdim return value; 2427224145Sdim } else if (llvm::InvokeInst *invoke = dyn_cast<llvm::InvokeInst>(value)) { 2428224145Sdim CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP(); 2429224145Sdim 2430224145Sdim // Place the retain at the beginning of the normal destination block. 2431224145Sdim llvm::BasicBlock *BB = invoke->getNormalDest(); 2432224145Sdim CGF.Builder.SetInsertPoint(BB, BB->begin()); 2433224145Sdim value = CGF.EmitARCRetainAutoreleasedReturnValue(value); 2434224145Sdim 2435224145Sdim CGF.Builder.restoreIP(ip); 2436224145Sdim return value; 2437224145Sdim 2438224145Sdim // Bitcasts can arise because of related-result returns. Rewrite 2439224145Sdim // the operand. 2440224145Sdim } else if (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(value)) { 2441224145Sdim llvm::Value *operand = bitcast->getOperand(0); 2442224145Sdim operand = emitARCRetainAfterCall(CGF, operand); 2443224145Sdim bitcast->setOperand(0, operand); 2444224145Sdim return bitcast; 2445224145Sdim 2446224145Sdim // Generic fall-back case. 2447224145Sdim } else { 2448224145Sdim // Retain using the non-block variant: we never need to do a copy 2449224145Sdim // of a block that's been returned to us. 2450224145Sdim return CGF.EmitARCRetainNonBlock(value); 2451224145Sdim } 2452224145Sdim} 2453224145Sdim 2454226633Sdim/// Determine whether it might be important to emit a separate 2455226633Sdim/// objc_retain_block on the result of the given expression, or 2456226633Sdim/// whether it's okay to just emit it in a +1 context. 2457226633Sdimstatic bool shouldEmitSeparateBlockRetain(const Expr *e) { 2458226633Sdim assert(e->getType()->isBlockPointerType()); 2459226633Sdim e = e->IgnoreParens(); 2460226633Sdim 2461226633Sdim // For future goodness, emit block expressions directly in +1 2462226633Sdim // contexts if we can. 2463226633Sdim if (isa<BlockExpr>(e)) 2464226633Sdim return false; 2465226633Sdim 2466226633Sdim if (const CastExpr *cast = dyn_cast<CastExpr>(e)) { 2467226633Sdim switch (cast->getCastKind()) { 2468226633Sdim // Emitting these operations in +1 contexts is goodness. 2469226633Sdim case CK_LValueToRValue: 2470226633Sdim case CK_ARCReclaimReturnedObject: 2471226633Sdim case CK_ARCConsumeObject: 2472226633Sdim case CK_ARCProduceObject: 2473226633Sdim return false; 2474226633Sdim 2475226633Sdim // These operations preserve a block type. 2476226633Sdim case CK_NoOp: 2477226633Sdim case CK_BitCast: 2478226633Sdim return shouldEmitSeparateBlockRetain(cast->getSubExpr()); 2479226633Sdim 2480226633Sdim // These operations are known to be bad (or haven't been considered). 2481226633Sdim case CK_AnyPointerToBlockPointerCast: 2482226633Sdim default: 2483226633Sdim return true; 2484226633Sdim } 2485226633Sdim } 2486226633Sdim 2487226633Sdim return true; 2488226633Sdim} 2489226633Sdim 2490234353Sdim/// Try to emit a PseudoObjectExpr at +1. 2491234353Sdim/// 2492234353Sdim/// This massively duplicates emitPseudoObjectRValue. 2493234353Sdimstatic TryEmitResult tryEmitARCRetainPseudoObject(CodeGenFunction &CGF, 2494234353Sdim const PseudoObjectExpr *E) { 2495249423Sdim SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques; 2496234353Sdim 2497234353Sdim // Find the result expression. 2498234353Sdim const Expr *resultExpr = E->getResultExpr(); 2499234353Sdim assert(resultExpr); 2500234353Sdim TryEmitResult result; 2501234353Sdim 2502234353Sdim for (PseudoObjectExpr::const_semantics_iterator 2503234353Sdim i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) { 2504234353Sdim const Expr *semantic = *i; 2505234353Sdim 2506234353Sdim // If this semantic expression is an opaque value, bind it 2507234353Sdim // to the result of its source expression. 2508234353Sdim if (const OpaqueValueExpr *ov = dyn_cast<OpaqueValueExpr>(semantic)) { 2509234353Sdim typedef CodeGenFunction::OpaqueValueMappingData OVMA; 2510234353Sdim OVMA opaqueData; 2511234353Sdim 2512234353Sdim // If this semantic is the result of the pseudo-object 2513234353Sdim // expression, try to evaluate the source as +1. 2514234353Sdim if (ov == resultExpr) { 2515234353Sdim assert(!OVMA::shouldBindAsLValue(ov)); 2516234353Sdim result = tryEmitARCRetainScalarExpr(CGF, ov->getSourceExpr()); 2517234353Sdim opaqueData = OVMA::bind(CGF, ov, RValue::get(result.getPointer())); 2518234353Sdim 2519234353Sdim // Otherwise, just bind it. 2520234353Sdim } else { 2521234353Sdim opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr()); 2522234353Sdim } 2523234353Sdim opaques.push_back(opaqueData); 2524234353Sdim 2525234353Sdim // Otherwise, if the expression is the result, evaluate it 2526234353Sdim // and remember the result. 2527234353Sdim } else if (semantic == resultExpr) { 2528234353Sdim result = tryEmitARCRetainScalarExpr(CGF, semantic); 2529234353Sdim 2530234353Sdim // Otherwise, evaluate the expression in an ignored context. 2531234353Sdim } else { 2532234353Sdim CGF.EmitIgnoredExpr(semantic); 2533234353Sdim } 2534234353Sdim } 2535234353Sdim 2536234353Sdim // Unbind all the opaques now. 2537234353Sdim for (unsigned i = 0, e = opaques.size(); i != e; ++i) 2538234353Sdim opaques[i].unbind(CGF); 2539234353Sdim 2540234353Sdim return result; 2541234353Sdim} 2542234353Sdim 2543224145Sdimstatic TryEmitResult 2544224145SdimtryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e) { 2545249423Sdim // We should *never* see a nested full-expression here, because if 2546249423Sdim // we fail to emit at +1, our caller must not retain after we close 2547249423Sdim // out the full-expression. 2548249423Sdim assert(!isa<ExprWithCleanups>(e)); 2549226633Sdim 2550224145Sdim // The desired result type, if it differs from the type of the 2551224145Sdim // ultimate opaque expression. 2552226633Sdim llvm::Type *resultType = 0; 2553224145Sdim 2554224145Sdim while (true) { 2555224145Sdim e = e->IgnoreParens(); 2556224145Sdim 2557224145Sdim // There's a break at the end of this if-chain; anything 2558224145Sdim // that wants to keep looping has to explicitly continue. 2559224145Sdim if (const CastExpr *ce = dyn_cast<CastExpr>(e)) { 2560224145Sdim switch (ce->getCastKind()) { 2561224145Sdim // No-op casts don't change the type, so we just ignore them. 2562224145Sdim case CK_NoOp: 2563224145Sdim e = ce->getSubExpr(); 2564224145Sdim continue; 2565224145Sdim 2566224145Sdim case CK_LValueToRValue: { 2567224145Sdim TryEmitResult loadResult 2568224145Sdim = tryEmitARCRetainLoadOfScalar(CGF, ce->getSubExpr()); 2569224145Sdim if (resultType) { 2570224145Sdim llvm::Value *value = loadResult.getPointer(); 2571224145Sdim value = CGF.Builder.CreateBitCast(value, resultType); 2572224145Sdim loadResult.setPointer(value); 2573224145Sdim } 2574224145Sdim return loadResult; 2575224145Sdim } 2576224145Sdim 2577224145Sdim // These casts can change the type, so remember that and 2578224145Sdim // soldier on. We only need to remember the outermost such 2579224145Sdim // cast, though. 2580226633Sdim case CK_CPointerToObjCPointerCast: 2581226633Sdim case CK_BlockPointerToObjCPointerCast: 2582224145Sdim case CK_AnyPointerToBlockPointerCast: 2583224145Sdim case CK_BitCast: 2584224145Sdim if (!resultType) 2585224145Sdim resultType = CGF.ConvertType(ce->getType()); 2586224145Sdim e = ce->getSubExpr(); 2587224145Sdim assert(e->getType()->hasPointerRepresentation()); 2588224145Sdim continue; 2589224145Sdim 2590224145Sdim // For consumptions, just emit the subexpression and thus elide 2591224145Sdim // the retain/release pair. 2592226633Sdim case CK_ARCConsumeObject: { 2593224145Sdim llvm::Value *result = CGF.EmitScalarExpr(ce->getSubExpr()); 2594224145Sdim if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); 2595224145Sdim return TryEmitResult(result, true); 2596224145Sdim } 2597224145Sdim 2598226633Sdim // Block extends are net +0. Naively, we could just recurse on 2599226633Sdim // the subexpression, but actually we need to ensure that the 2600226633Sdim // value is copied as a block, so there's a little filter here. 2601226633Sdim case CK_ARCExtendBlockObject: { 2602226633Sdim llvm::Value *result; // will be a +0 value 2603226633Sdim 2604226633Sdim // If we can't safely assume the sub-expression will produce a 2605226633Sdim // block-copied value, emit the sub-expression at +0. 2606226633Sdim if (shouldEmitSeparateBlockRetain(ce->getSubExpr())) { 2607226633Sdim result = CGF.EmitScalarExpr(ce->getSubExpr()); 2608226633Sdim 2609226633Sdim // Otherwise, try to emit the sub-expression at +1 recursively. 2610226633Sdim } else { 2611226633Sdim TryEmitResult subresult 2612226633Sdim = tryEmitARCRetainScalarExpr(CGF, ce->getSubExpr()); 2613226633Sdim result = subresult.getPointer(); 2614226633Sdim 2615226633Sdim // If that produced a retained value, just use that, 2616226633Sdim // possibly casting down. 2617226633Sdim if (subresult.getInt()) { 2618226633Sdim if (resultType) 2619226633Sdim result = CGF.Builder.CreateBitCast(result, resultType); 2620226633Sdim return TryEmitResult(result, true); 2621226633Sdim } 2622226633Sdim 2623226633Sdim // Otherwise it's +0. 2624226633Sdim } 2625226633Sdim 2626226633Sdim // Retain the object as a block, then cast down. 2627226633Sdim result = CGF.EmitARCRetainBlock(result, /*mandatory*/ true); 2628226633Sdim if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); 2629226633Sdim return TryEmitResult(result, true); 2630226633Sdim } 2631226633Sdim 2632224145Sdim // For reclaims, emit the subexpression as a retained call and 2633224145Sdim // skip the consumption. 2634226633Sdim case CK_ARCReclaimReturnedObject: { 2635224145Sdim llvm::Value *result = emitARCRetainCall(CGF, ce->getSubExpr()); 2636224145Sdim if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); 2637224145Sdim return TryEmitResult(result, true); 2638224145Sdim } 2639224145Sdim 2640224145Sdim default: 2641224145Sdim break; 2642224145Sdim } 2643224145Sdim 2644224145Sdim // Skip __extension__. 2645224145Sdim } else if (const UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { 2646224145Sdim if (op->getOpcode() == UO_Extension) { 2647224145Sdim e = op->getSubExpr(); 2648224145Sdim continue; 2649224145Sdim } 2650224145Sdim 2651224145Sdim // For calls and message sends, use the retained-call logic. 2652224145Sdim // Delegate inits are a special case in that they're the only 2653224145Sdim // returns-retained expression that *isn't* surrounded by 2654224145Sdim // a consume. 2655224145Sdim } else if (isa<CallExpr>(e) || 2656224145Sdim (isa<ObjCMessageExpr>(e) && 2657224145Sdim !cast<ObjCMessageExpr>(e)->isDelegateInitCall())) { 2658224145Sdim llvm::Value *result = emitARCRetainCall(CGF, e); 2659224145Sdim if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); 2660224145Sdim return TryEmitResult(result, true); 2661234353Sdim 2662234353Sdim // Look through pseudo-object expressions. 2663234353Sdim } else if (const PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) { 2664234353Sdim TryEmitResult result 2665234353Sdim = tryEmitARCRetainPseudoObject(CGF, pseudo); 2666234353Sdim if (resultType) { 2667234353Sdim llvm::Value *value = result.getPointer(); 2668234353Sdim value = CGF.Builder.CreateBitCast(value, resultType); 2669234353Sdim result.setPointer(value); 2670234353Sdim } 2671234353Sdim return result; 2672224145Sdim } 2673224145Sdim 2674224145Sdim // Conservatively halt the search at any other expression kind. 2675224145Sdim break; 2676224145Sdim } 2677224145Sdim 2678224145Sdim // We didn't find an obvious production, so emit what we've got and 2679224145Sdim // tell the caller that we didn't manage to retain. 2680224145Sdim llvm::Value *result = CGF.EmitScalarExpr(e); 2681224145Sdim if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); 2682224145Sdim return TryEmitResult(result, false); 2683224145Sdim} 2684224145Sdim 2685224145Sdimstatic llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF, 2686224145Sdim LValue lvalue, 2687224145Sdim QualType type) { 2688224145Sdim TryEmitResult result = tryEmitARCRetainLoadOfScalar(CGF, lvalue, type); 2689224145Sdim llvm::Value *value = result.getPointer(); 2690224145Sdim if (!result.getInt()) 2691224145Sdim value = CGF.EmitARCRetain(type, value); 2692224145Sdim return value; 2693224145Sdim} 2694224145Sdim 2695224145Sdim/// EmitARCRetainScalarExpr - Semantically equivalent to 2696224145Sdim/// EmitARCRetainObject(e->getType(), EmitScalarExpr(e)), but making a 2697224145Sdim/// best-effort attempt to peephole expressions that naturally produce 2698224145Sdim/// retained objects. 2699224145Sdimllvm::Value *CodeGenFunction::EmitARCRetainScalarExpr(const Expr *e) { 2700249423Sdim // The retain needs to happen within the full-expression. 2701249423Sdim if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) { 2702249423Sdim enterFullExpression(cleanups); 2703249423Sdim RunCleanupsScope scope(*this); 2704249423Sdim return EmitARCRetainScalarExpr(cleanups->getSubExpr()); 2705249423Sdim } 2706249423Sdim 2707224145Sdim TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e); 2708224145Sdim llvm::Value *value = result.getPointer(); 2709224145Sdim if (!result.getInt()) 2710224145Sdim value = EmitARCRetain(e->getType(), value); 2711224145Sdim return value; 2712224145Sdim} 2713224145Sdim 2714224145Sdimllvm::Value * 2715224145SdimCodeGenFunction::EmitARCRetainAutoreleaseScalarExpr(const Expr *e) { 2716249423Sdim // The retain needs to happen within the full-expression. 2717249423Sdim if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) { 2718249423Sdim enterFullExpression(cleanups); 2719249423Sdim RunCleanupsScope scope(*this); 2720249423Sdim return EmitARCRetainAutoreleaseScalarExpr(cleanups->getSubExpr()); 2721249423Sdim } 2722249423Sdim 2723224145Sdim TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e); 2724224145Sdim llvm::Value *value = result.getPointer(); 2725224145Sdim if (result.getInt()) 2726224145Sdim value = EmitARCAutorelease(value); 2727224145Sdim else 2728224145Sdim value = EmitARCRetainAutorelease(e->getType(), value); 2729224145Sdim return value; 2730224145Sdim} 2731224145Sdim 2732226633Sdimllvm::Value *CodeGenFunction::EmitARCExtendBlockObject(const Expr *e) { 2733226633Sdim llvm::Value *result; 2734226633Sdim bool doRetain; 2735226633Sdim 2736226633Sdim if (shouldEmitSeparateBlockRetain(e)) { 2737226633Sdim result = EmitScalarExpr(e); 2738226633Sdim doRetain = true; 2739226633Sdim } else { 2740226633Sdim TryEmitResult subresult = tryEmitARCRetainScalarExpr(*this, e); 2741226633Sdim result = subresult.getPointer(); 2742226633Sdim doRetain = !subresult.getInt(); 2743226633Sdim } 2744226633Sdim 2745226633Sdim if (doRetain) 2746226633Sdim result = EmitARCRetainBlock(result, /*mandatory*/ true); 2747226633Sdim return EmitObjCConsumeObject(e->getType(), result); 2748226633Sdim} 2749226633Sdim 2750226633Sdimllvm::Value *CodeGenFunction::EmitObjCThrowOperand(const Expr *expr) { 2751226633Sdim // In ARC, retain and autorelease the expression. 2752234353Sdim if (getLangOpts().ObjCAutoRefCount) { 2753226633Sdim // Do so before running any cleanups for the full-expression. 2754249423Sdim // EmitARCRetainAutoreleaseScalarExpr does this for us. 2755226633Sdim return EmitARCRetainAutoreleaseScalarExpr(expr); 2756226633Sdim } 2757226633Sdim 2758226633Sdim // Otherwise, use the normal scalar-expression emission. The 2759226633Sdim // exception machinery doesn't do anything special with the 2760226633Sdim // exception like retaining it, so there's no safety associated with 2761226633Sdim // only running cleanups after the throw has started, and when it 2762226633Sdim // matters it tends to be substantially inferior code. 2763226633Sdim return EmitScalarExpr(expr); 2764226633Sdim} 2765226633Sdim 2766224145Sdimstd::pair<LValue,llvm::Value*> 2767224145SdimCodeGenFunction::EmitARCStoreStrong(const BinaryOperator *e, 2768224145Sdim bool ignored) { 2769224145Sdim // Evaluate the RHS first. 2770224145Sdim TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e->getRHS()); 2771224145Sdim llvm::Value *value = result.getPointer(); 2772224145Sdim 2773226633Sdim bool hasImmediateRetain = result.getInt(); 2774226633Sdim 2775226633Sdim // If we didn't emit a retained object, and the l-value is of block 2776226633Sdim // type, then we need to emit the block-retain immediately in case 2777226633Sdim // it invalidates the l-value. 2778226633Sdim if (!hasImmediateRetain && e->getType()->isBlockPointerType()) { 2779226633Sdim value = EmitARCRetainBlock(value, /*mandatory*/ false); 2780226633Sdim hasImmediateRetain = true; 2781226633Sdim } 2782226633Sdim 2783224145Sdim LValue lvalue = EmitLValue(e->getLHS()); 2784224145Sdim 2785224145Sdim // If the RHS was emitted retained, expand this. 2786226633Sdim if (hasImmediateRetain) { 2787224145Sdim llvm::Value *oldValue = 2788234353Sdim EmitLoadOfScalar(lvalue); 2789234353Sdim EmitStoreOfScalar(value, lvalue); 2790249423Sdim EmitARCRelease(oldValue, lvalue.isARCPreciseLifetime()); 2791224145Sdim } else { 2792224145Sdim value = EmitARCStoreStrong(lvalue, value, ignored); 2793224145Sdim } 2794224145Sdim 2795224145Sdim return std::pair<LValue,llvm::Value*>(lvalue, value); 2796224145Sdim} 2797224145Sdim 2798224145Sdimstd::pair<LValue,llvm::Value*> 2799224145SdimCodeGenFunction::EmitARCStoreAutoreleasing(const BinaryOperator *e) { 2800224145Sdim llvm::Value *value = EmitARCRetainAutoreleaseScalarExpr(e->getRHS()); 2801224145Sdim LValue lvalue = EmitLValue(e->getLHS()); 2802224145Sdim 2803234353Sdim EmitStoreOfScalar(value, lvalue); 2804224145Sdim 2805224145Sdim return std::pair<LValue,llvm::Value*>(lvalue, value); 2806224145Sdim} 2807224145Sdim 2808224145Sdimvoid CodeGenFunction::EmitObjCAutoreleasePoolStmt( 2809234353Sdim const ObjCAutoreleasePoolStmt &ARPS) { 2810224145Sdim const Stmt *subStmt = ARPS.getSubStmt(); 2811224145Sdim const CompoundStmt &S = cast<CompoundStmt>(*subStmt); 2812224145Sdim 2813224145Sdim CGDebugInfo *DI = getDebugInfo(); 2814226633Sdim if (DI) 2815226633Sdim DI->EmitLexicalBlockStart(Builder, S.getLBracLoc()); 2816224145Sdim 2817224145Sdim // Keep track of the current cleanup stack depth. 2818224145Sdim RunCleanupsScope Scope(*this); 2819243830Sdim if (CGM.getLangOpts().ObjCRuntime.hasNativeARC()) { 2820224145Sdim llvm::Value *token = EmitObjCAutoreleasePoolPush(); 2821224145Sdim EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, token); 2822224145Sdim } else { 2823224145Sdim llvm::Value *token = EmitObjCMRRAutoreleasePoolPush(); 2824224145Sdim EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, token); 2825224145Sdim } 2826224145Sdim 2827224145Sdim for (CompoundStmt::const_body_iterator I = S.body_begin(), 2828224145Sdim E = S.body_end(); I != E; ++I) 2829224145Sdim EmitStmt(*I); 2830224145Sdim 2831226633Sdim if (DI) 2832226633Sdim DI->EmitLexicalBlockEnd(Builder, S.getRBracLoc()); 2833224145Sdim} 2834224145Sdim 2835224145Sdim/// EmitExtendGCLifetime - Given a pointer to an Objective-C object, 2836224145Sdim/// make sure it survives garbage collection until this point. 2837224145Sdimvoid CodeGenFunction::EmitExtendGCLifetime(llvm::Value *object) { 2838224145Sdim // We just use an inline assembly. 2839224145Sdim llvm::FunctionType *extenderType 2840234353Sdim = llvm::FunctionType::get(VoidTy, VoidPtrTy, RequiredArgs::All); 2841224145Sdim llvm::Value *extender 2842224145Sdim = llvm::InlineAsm::get(extenderType, 2843224145Sdim /* assembly */ "", 2844224145Sdim /* constraints */ "r", 2845224145Sdim /* side effects */ true); 2846224145Sdim 2847224145Sdim object = Builder.CreateBitCast(object, VoidPtrTy); 2848249423Sdim EmitNounwindRuntimeCall(extender, object); 2849224145Sdim} 2850224145Sdim 2851234353Sdim/// GenerateObjCAtomicSetterCopyHelperFunction - Given a c++ object type with 2852234353Sdim/// non-trivial copy assignment function, produce following helper function. 2853234353Sdim/// static void copyHelper(Ty *dest, const Ty *source) { *dest = *source; } 2854234353Sdim/// 2855234353Sdimllvm::Constant * 2856234353SdimCodeGenFunction::GenerateObjCAtomicSetterCopyHelperFunction( 2857234353Sdim const ObjCPropertyImplDecl *PID) { 2858239462Sdim if (!getLangOpts().CPlusPlus || 2859249423Sdim !getLangOpts().ObjCRuntime.hasAtomicCopyHelper()) 2860234353Sdim return 0; 2861234353Sdim QualType Ty = PID->getPropertyIvarDecl()->getType(); 2862234353Sdim if (!Ty->isRecordType()) 2863234353Sdim return 0; 2864234353Sdim const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 2865234353Sdim if ((!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_atomic))) 2866234353Sdim return 0; 2867234353Sdim llvm::Constant * HelperFn = 0; 2868234353Sdim if (hasTrivialSetExpr(PID)) 2869234353Sdim return 0; 2870234353Sdim assert(PID->getSetterCXXAssignment() && "SetterCXXAssignment - null"); 2871234353Sdim if ((HelperFn = CGM.getAtomicSetterHelperFnMap(Ty))) 2872234353Sdim return HelperFn; 2873234353Sdim 2874234353Sdim ASTContext &C = getContext(); 2875234353Sdim IdentifierInfo *II 2876234353Sdim = &CGM.getContext().Idents.get("__assign_helper_atomic_property_"); 2877234353Sdim FunctionDecl *FD = FunctionDecl::Create(C, 2878234353Sdim C.getTranslationUnitDecl(), 2879234353Sdim SourceLocation(), 2880234353Sdim SourceLocation(), II, C.VoidTy, 0, 2881234353Sdim SC_Static, 2882234353Sdim false, 2883234353Sdim false); 2884234353Sdim 2885234353Sdim QualType DestTy = C.getPointerType(Ty); 2886234353Sdim QualType SrcTy = Ty; 2887234353Sdim SrcTy.addConst(); 2888234353Sdim SrcTy = C.getPointerType(SrcTy); 2889234353Sdim 2890234353Sdim FunctionArgList args; 2891234353Sdim ImplicitParamDecl dstDecl(FD, SourceLocation(), 0, DestTy); 2892234353Sdim args.push_back(&dstDecl); 2893234353Sdim ImplicitParamDecl srcDecl(FD, SourceLocation(), 0, SrcTy); 2894234353Sdim args.push_back(&srcDecl); 2895234353Sdim 2896234353Sdim const CGFunctionInfo &FI = 2897234353Sdim CGM.getTypes().arrangeFunctionDeclaration(C.VoidTy, args, 2898234353Sdim FunctionType::ExtInfo(), 2899234353Sdim RequiredArgs::All); 2900234353Sdim 2901234353Sdim llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI); 2902234353Sdim 2903234353Sdim llvm::Function *Fn = 2904234353Sdim llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 2905234353Sdim "__assign_helper_atomic_property_", 2906234353Sdim &CGM.getModule()); 2907234353Sdim 2908243830Sdim // Initialize debug info if needed. 2909243830Sdim maybeInitializeDebugInfo(); 2910234353Sdim 2911234353Sdim StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation()); 2912234353Sdim 2913234353Sdim DeclRefExpr DstExpr(&dstDecl, false, DestTy, 2914234353Sdim VK_RValue, SourceLocation()); 2915234353Sdim UnaryOperator DST(&DstExpr, UO_Deref, DestTy->getPointeeType(), 2916234353Sdim VK_LValue, OK_Ordinary, SourceLocation()); 2917234353Sdim 2918234353Sdim DeclRefExpr SrcExpr(&srcDecl, false, SrcTy, 2919234353Sdim VK_RValue, SourceLocation()); 2920234353Sdim UnaryOperator SRC(&SrcExpr, UO_Deref, SrcTy->getPointeeType(), 2921234353Sdim VK_LValue, OK_Ordinary, SourceLocation()); 2922234353Sdim 2923234353Sdim Expr *Args[2] = { &DST, &SRC }; 2924234353Sdim CallExpr *CalleeExp = cast<CallExpr>(PID->getSetterCXXAssignment()); 2925234353Sdim CXXOperatorCallExpr TheCall(C, OO_Equal, CalleeExp->getCallee(), 2926243830Sdim Args, DestTy->getPointeeType(), 2927243830Sdim VK_LValue, SourceLocation(), false); 2928234353Sdim 2929234353Sdim EmitStmt(&TheCall); 2930234353Sdim 2931234353Sdim FinishFunction(); 2932234353Sdim HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); 2933234353Sdim CGM.setAtomicSetterHelperFnMap(Ty, HelperFn); 2934234353Sdim return HelperFn; 2935234353Sdim} 2936234353Sdim 2937234353Sdimllvm::Constant * 2938234353SdimCodeGenFunction::GenerateObjCAtomicGetterCopyHelperFunction( 2939234353Sdim const ObjCPropertyImplDecl *PID) { 2940239462Sdim if (!getLangOpts().CPlusPlus || 2941249423Sdim !getLangOpts().ObjCRuntime.hasAtomicCopyHelper()) 2942234353Sdim return 0; 2943234353Sdim const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 2944234353Sdim QualType Ty = PD->getType(); 2945234353Sdim if (!Ty->isRecordType()) 2946234353Sdim return 0; 2947234353Sdim if ((!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_atomic))) 2948234353Sdim return 0; 2949234353Sdim llvm::Constant * HelperFn = 0; 2950234353Sdim 2951234353Sdim if (hasTrivialGetExpr(PID)) 2952234353Sdim return 0; 2953234353Sdim assert(PID->getGetterCXXConstructor() && "getGetterCXXConstructor - null"); 2954234353Sdim if ((HelperFn = CGM.getAtomicGetterHelperFnMap(Ty))) 2955234353Sdim return HelperFn; 2956234353Sdim 2957234353Sdim 2958234353Sdim ASTContext &C = getContext(); 2959234353Sdim IdentifierInfo *II 2960234353Sdim = &CGM.getContext().Idents.get("__copy_helper_atomic_property_"); 2961234353Sdim FunctionDecl *FD = FunctionDecl::Create(C, 2962234353Sdim C.getTranslationUnitDecl(), 2963234353Sdim SourceLocation(), 2964234353Sdim SourceLocation(), II, C.VoidTy, 0, 2965234353Sdim SC_Static, 2966234353Sdim false, 2967234353Sdim false); 2968234353Sdim 2969234353Sdim QualType DestTy = C.getPointerType(Ty); 2970234353Sdim QualType SrcTy = Ty; 2971234353Sdim SrcTy.addConst(); 2972234353Sdim SrcTy = C.getPointerType(SrcTy); 2973234353Sdim 2974234353Sdim FunctionArgList args; 2975234353Sdim ImplicitParamDecl dstDecl(FD, SourceLocation(), 0, DestTy); 2976234353Sdim args.push_back(&dstDecl); 2977234353Sdim ImplicitParamDecl srcDecl(FD, SourceLocation(), 0, SrcTy); 2978234353Sdim args.push_back(&srcDecl); 2979234353Sdim 2980234353Sdim const CGFunctionInfo &FI = 2981234353Sdim CGM.getTypes().arrangeFunctionDeclaration(C.VoidTy, args, 2982234353Sdim FunctionType::ExtInfo(), 2983234353Sdim RequiredArgs::All); 2984234353Sdim 2985234353Sdim llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI); 2986234353Sdim 2987234353Sdim llvm::Function *Fn = 2988234353Sdim llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 2989234353Sdim "__copy_helper_atomic_property_", &CGM.getModule()); 2990234353Sdim 2991243830Sdim // Initialize debug info if needed. 2992243830Sdim maybeInitializeDebugInfo(); 2993234353Sdim 2994234353Sdim StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation()); 2995234353Sdim 2996234353Sdim DeclRefExpr SrcExpr(&srcDecl, false, SrcTy, 2997234353Sdim VK_RValue, SourceLocation()); 2998234353Sdim 2999234353Sdim UnaryOperator SRC(&SrcExpr, UO_Deref, SrcTy->getPointeeType(), 3000234353Sdim VK_LValue, OK_Ordinary, SourceLocation()); 3001234353Sdim 3002234353Sdim CXXConstructExpr *CXXConstExpr = 3003234353Sdim cast<CXXConstructExpr>(PID->getGetterCXXConstructor()); 3004234353Sdim 3005234353Sdim SmallVector<Expr*, 4> ConstructorArgs; 3006234353Sdim ConstructorArgs.push_back(&SRC); 3007234353Sdim CXXConstructExpr::arg_iterator A = CXXConstExpr->arg_begin(); 3008234353Sdim ++A; 3009234353Sdim 3010234353Sdim for (CXXConstructExpr::arg_iterator AEnd = CXXConstExpr->arg_end(); 3011234353Sdim A != AEnd; ++A) 3012234353Sdim ConstructorArgs.push_back(*A); 3013234353Sdim 3014234353Sdim CXXConstructExpr *TheCXXConstructExpr = 3015234353Sdim CXXConstructExpr::Create(C, Ty, SourceLocation(), 3016234353Sdim CXXConstExpr->getConstructor(), 3017234353Sdim CXXConstExpr->isElidable(), 3018243830Sdim ConstructorArgs, 3019234353Sdim CXXConstExpr->hadMultipleCandidates(), 3020234353Sdim CXXConstExpr->isListInitialization(), 3021234353Sdim CXXConstExpr->requiresZeroInitialization(), 3022234353Sdim CXXConstExpr->getConstructionKind(), 3023234353Sdim SourceRange()); 3024234353Sdim 3025234353Sdim DeclRefExpr DstExpr(&dstDecl, false, DestTy, 3026234353Sdim VK_RValue, SourceLocation()); 3027234353Sdim 3028234353Sdim RValue DV = EmitAnyExpr(&DstExpr); 3029234353Sdim CharUnits Alignment 3030234353Sdim = getContext().getTypeAlignInChars(TheCXXConstructExpr->getType()); 3031234353Sdim EmitAggExpr(TheCXXConstructExpr, 3032234353Sdim AggValueSlot::forAddr(DV.getScalarVal(), Alignment, Qualifiers(), 3033234353Sdim AggValueSlot::IsDestructed, 3034234353Sdim AggValueSlot::DoesNotNeedGCBarriers, 3035234353Sdim AggValueSlot::IsNotAliased)); 3036234353Sdim 3037234353Sdim FinishFunction(); 3038234353Sdim HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); 3039234353Sdim CGM.setAtomicGetterHelperFnMap(Ty, HelperFn); 3040234353Sdim return HelperFn; 3041234353Sdim} 3042234353Sdim 3043234353Sdimllvm::Value * 3044234353SdimCodeGenFunction::EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty) { 3045234353Sdim // Get selectors for retain/autorelease. 3046234353Sdim IdentifierInfo *CopyID = &getContext().Idents.get("copy"); 3047234353Sdim Selector CopySelector = 3048234353Sdim getContext().Selectors.getNullarySelector(CopyID); 3049234353Sdim IdentifierInfo *AutoreleaseID = &getContext().Idents.get("autorelease"); 3050234353Sdim Selector AutoreleaseSelector = 3051234353Sdim getContext().Selectors.getNullarySelector(AutoreleaseID); 3052234353Sdim 3053234353Sdim // Emit calls to retain/autorelease. 3054234353Sdim CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 3055234353Sdim llvm::Value *Val = Block; 3056234353Sdim RValue Result; 3057234353Sdim Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 3058234353Sdim Ty, CopySelector, 3059234353Sdim Val, CallArgList(), 0, 0); 3060234353Sdim Val = Result.getScalarVal(); 3061234353Sdim Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 3062234353Sdim Ty, AutoreleaseSelector, 3063234353Sdim Val, CallArgList(), 0, 0); 3064234353Sdim Val = Result.getScalarVal(); 3065234353Sdim return Val; 3066234353Sdim} 3067234353Sdim 3068234353Sdim 3069193326SedCGObjCRuntime::~CGObjCRuntime() {} 3070