1 //===---- CGObjC.cpp - Emit LLVM Code for Objective-C ---------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This contains code to emit Objective-C code as LLVM code. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "CGDebugInfo.h" 14 #include "CGObjCRuntime.h" 15 #include "CodeGenFunction.h" 16 #include "CodeGenModule.h" 17 #include "ConstantEmitter.h" 18 #include "TargetInfo.h" 19 #include "clang/AST/ASTContext.h" 20 #include "clang/AST/Attr.h" 21 #include "clang/AST/DeclObjC.h" 22 #include "clang/AST/StmtObjC.h" 23 #include "clang/Basic/Diagnostic.h" 24 #include "clang/CodeGen/CGFunctionInfo.h" 25 #include "llvm/ADT/STLExtras.h" 26 #include "llvm/IR/DataLayout.h" 27 #include "llvm/IR/InlineAsm.h" 28 using namespace clang; 29 using namespace CodeGen; 30 31 typedef llvm::PointerIntPair<llvm::Value*,1,bool> TryEmitResult; 32 static TryEmitResult 33 tryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e); 34 static RValue AdjustObjCObjectType(CodeGenFunction &CGF, 35 QualType ET, 36 RValue Result); 37 38 /// Given the address of a variable of pointer type, find the correct 39 /// null to store into it. 40 static llvm::Constant *getNullForVariable(Address addr) { 41 llvm::Type *type = addr.getElementType(); 42 return llvm::ConstantPointerNull::get(cast<llvm::PointerType>(type)); 43 } 44 45 /// Emits an instance of NSConstantString representing the object. 46 llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E) 47 { 48 llvm::Constant *C = 49 CGM.getObjCRuntime().GenerateConstantString(E->getString()).getPointer(); 50 // FIXME: This bitcast should just be made an invariant on the Runtime. 51 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType())); 52 } 53 54 /// EmitObjCBoxedExpr - This routine generates code to call 55 /// the appropriate expression boxing method. This will either be 56 /// one of +[NSNumber numberWith<Type>:], or +[NSString stringWithUTF8String:], 57 /// or [NSValue valueWithBytes:objCType:]. 58 /// 59 llvm::Value * 60 CodeGenFunction::EmitObjCBoxedExpr(const ObjCBoxedExpr *E) { 61 // Generate the correct selector for this literal's concrete type. 62 // Get the method. 63 const ObjCMethodDecl *BoxingMethod = E->getBoxingMethod(); 64 const Expr *SubExpr = E->getSubExpr(); 65 66 if (E->isExpressibleAsConstantInitializer()) { 67 ConstantEmitter ConstEmitter(CGM); 68 return ConstEmitter.tryEmitAbstract(E, E->getType()); 69 } 70 71 assert(BoxingMethod->isClassMethod() && "BoxingMethod must be a class method"); 72 Selector Sel = BoxingMethod->getSelector(); 73 74 // Generate a reference to the class pointer, which will be the receiver. 75 // Assumes that the method was introduced in the class that should be 76 // messaged (avoids pulling it out of the result type). 77 CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 78 const ObjCInterfaceDecl *ClassDecl = BoxingMethod->getClassInterface(); 79 llvm::Value *Receiver = Runtime.GetClass(*this, ClassDecl); 80 81 CallArgList Args; 82 const ParmVarDecl *ArgDecl = *BoxingMethod->param_begin(); 83 QualType ArgQT = ArgDecl->getType().getUnqualifiedType(); 84 85 // ObjCBoxedExpr supports boxing of structs and unions 86 // via [NSValue valueWithBytes:objCType:] 87 const QualType ValueType(SubExpr->getType().getCanonicalType()); 88 if (ValueType->isObjCBoxableRecordType()) { 89 // Emit CodeGen for first parameter 90 // and cast value to correct type 91 Address Temporary = CreateMemTemp(SubExpr->getType()); 92 EmitAnyExprToMem(SubExpr, Temporary, Qualifiers(), /*isInit*/ true); 93 Address BitCast = Builder.CreateBitCast(Temporary, ConvertType(ArgQT)); 94 Args.add(RValue::get(BitCast.getPointer()), ArgQT); 95 96 // Create char array to store type encoding 97 std::string Str; 98 getContext().getObjCEncodingForType(ValueType, Str); 99 llvm::Constant *GV = CGM.GetAddrOfConstantCString(Str).getPointer(); 100 101 // Cast type encoding to correct type 102 const ParmVarDecl *EncodingDecl = BoxingMethod->parameters()[1]; 103 QualType EncodingQT = EncodingDecl->getType().getUnqualifiedType(); 104 llvm::Value *Cast = Builder.CreateBitCast(GV, ConvertType(EncodingQT)); 105 106 Args.add(RValue::get(Cast), EncodingQT); 107 } else { 108 Args.add(EmitAnyExpr(SubExpr), ArgQT); 109 } 110 111 RValue result = Runtime.GenerateMessageSend( 112 *this, ReturnValueSlot(), BoxingMethod->getReturnType(), Sel, Receiver, 113 Args, ClassDecl, BoxingMethod); 114 return Builder.CreateBitCast(result.getScalarVal(), 115 ConvertType(E->getType())); 116 } 117 118 llvm::Value *CodeGenFunction::EmitObjCCollectionLiteral(const Expr *E, 119 const ObjCMethodDecl *MethodWithObjects) { 120 ASTContext &Context = CGM.getContext(); 121 const ObjCDictionaryLiteral *DLE = nullptr; 122 const ObjCArrayLiteral *ALE = dyn_cast<ObjCArrayLiteral>(E); 123 if (!ALE) 124 DLE = cast<ObjCDictionaryLiteral>(E); 125 126 // Optimize empty collections by referencing constants, when available. 127 uint64_t NumElements = 128 ALE ? ALE->getNumElements() : DLE->getNumElements(); 129 if (NumElements == 0 && CGM.getLangOpts().ObjCRuntime.hasEmptyCollections()) { 130 StringRef ConstantName = ALE ? "__NSArray0__" : "__NSDictionary0__"; 131 QualType IdTy(CGM.getContext().getObjCIdType()); 132 llvm::Constant *Constant = 133 CGM.CreateRuntimeVariable(ConvertType(IdTy), ConstantName); 134 LValue LV = MakeNaturalAlignAddrLValue(Constant, IdTy); 135 llvm::Value *Ptr = EmitLoadOfScalar(LV, E->getBeginLoc()); 136 cast<llvm::LoadInst>(Ptr)->setMetadata( 137 CGM.getModule().getMDKindID("invariant.load"), 138 llvm::MDNode::get(getLLVMContext(), None)); 139 return Builder.CreateBitCast(Ptr, ConvertType(E->getType())); 140 } 141 142 // Compute the type of the array we're initializing. 143 llvm::APInt APNumElements(Context.getTypeSize(Context.getSizeType()), 144 NumElements); 145 QualType ElementType = Context.getObjCIdType().withConst(); 146 QualType ElementArrayType 147 = Context.getConstantArrayType(ElementType, APNumElements, nullptr, 148 ArrayType::Normal, /*IndexTypeQuals=*/0); 149 150 // Allocate the temporary array(s). 151 Address Objects = CreateMemTemp(ElementArrayType, "objects"); 152 Address Keys = Address::invalid(); 153 if (DLE) 154 Keys = CreateMemTemp(ElementArrayType, "keys"); 155 156 // In ARC, we may need to do extra work to keep all the keys and 157 // values alive until after the call. 158 SmallVector<llvm::Value *, 16> NeededObjects; 159 bool TrackNeededObjects = 160 (getLangOpts().ObjCAutoRefCount && 161 CGM.getCodeGenOpts().OptimizationLevel != 0); 162 163 // Perform the actual initialialization of the array(s). 164 for (uint64_t i = 0; i < NumElements; i++) { 165 if (ALE) { 166 // Emit the element and store it to the appropriate array slot. 167 const Expr *Rhs = ALE->getElement(i); 168 LValue LV = MakeAddrLValue(Builder.CreateConstArrayGEP(Objects, i), 169 ElementType, AlignmentSource::Decl); 170 171 llvm::Value *value = EmitScalarExpr(Rhs); 172 EmitStoreThroughLValue(RValue::get(value), LV, true); 173 if (TrackNeededObjects) { 174 NeededObjects.push_back(value); 175 } 176 } else { 177 // Emit the key and store it to the appropriate array slot. 178 const Expr *Key = DLE->getKeyValueElement(i).Key; 179 LValue KeyLV = MakeAddrLValue(Builder.CreateConstArrayGEP(Keys, i), 180 ElementType, AlignmentSource::Decl); 181 llvm::Value *keyValue = EmitScalarExpr(Key); 182 EmitStoreThroughLValue(RValue::get(keyValue), KeyLV, /*isInit=*/true); 183 184 // Emit the value and store it to the appropriate array slot. 185 const Expr *Value = DLE->getKeyValueElement(i).Value; 186 LValue ValueLV = MakeAddrLValue(Builder.CreateConstArrayGEP(Objects, i), 187 ElementType, AlignmentSource::Decl); 188 llvm::Value *valueValue = EmitScalarExpr(Value); 189 EmitStoreThroughLValue(RValue::get(valueValue), ValueLV, /*isInit=*/true); 190 if (TrackNeededObjects) { 191 NeededObjects.push_back(keyValue); 192 NeededObjects.push_back(valueValue); 193 } 194 } 195 } 196 197 // Generate the argument list. 198 CallArgList Args; 199 ObjCMethodDecl::param_const_iterator PI = MethodWithObjects->param_begin(); 200 const ParmVarDecl *argDecl = *PI++; 201 QualType ArgQT = argDecl->getType().getUnqualifiedType(); 202 Args.add(RValue::get(Objects.getPointer()), ArgQT); 203 if (DLE) { 204 argDecl = *PI++; 205 ArgQT = argDecl->getType().getUnqualifiedType(); 206 Args.add(RValue::get(Keys.getPointer()), ArgQT); 207 } 208 argDecl = *PI; 209 ArgQT = argDecl->getType().getUnqualifiedType(); 210 llvm::Value *Count = 211 llvm::ConstantInt::get(CGM.getTypes().ConvertType(ArgQT), NumElements); 212 Args.add(RValue::get(Count), ArgQT); 213 214 // Generate a reference to the class pointer, which will be the receiver. 215 Selector Sel = MethodWithObjects->getSelector(); 216 QualType ResultType = E->getType(); 217 const ObjCObjectPointerType *InterfacePointerType 218 = ResultType->getAsObjCInterfacePointerType(); 219 ObjCInterfaceDecl *Class 220 = InterfacePointerType->getObjectType()->getInterface(); 221 CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 222 llvm::Value *Receiver = Runtime.GetClass(*this, Class); 223 224 // Generate the message send. 225 RValue result = Runtime.GenerateMessageSend( 226 *this, ReturnValueSlot(), MethodWithObjects->getReturnType(), Sel, 227 Receiver, Args, Class, MethodWithObjects); 228 229 // The above message send needs these objects, but in ARC they are 230 // passed in a buffer that is essentially __unsafe_unretained. 231 // Therefore we must prevent the optimizer from releasing them until 232 // after the call. 233 if (TrackNeededObjects) { 234 EmitARCIntrinsicUse(NeededObjects); 235 } 236 237 return Builder.CreateBitCast(result.getScalarVal(), 238 ConvertType(E->getType())); 239 } 240 241 llvm::Value *CodeGenFunction::EmitObjCArrayLiteral(const ObjCArrayLiteral *E) { 242 return EmitObjCCollectionLiteral(E, E->getArrayWithObjectsMethod()); 243 } 244 245 llvm::Value *CodeGenFunction::EmitObjCDictionaryLiteral( 246 const ObjCDictionaryLiteral *E) { 247 return EmitObjCCollectionLiteral(E, E->getDictWithObjectsMethod()); 248 } 249 250 /// Emit a selector. 251 llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) { 252 // Untyped selector. 253 // Note that this implementation allows for non-constant strings to be passed 254 // as arguments to @selector(). Currently, the only thing preventing this 255 // behaviour is the type checking in the front end. 256 return CGM.getObjCRuntime().GetSelector(*this, E->getSelector()); 257 } 258 259 llvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) { 260 // FIXME: This should pass the Decl not the name. 261 return CGM.getObjCRuntime().GenerateProtocolRef(*this, E->getProtocol()); 262 } 263 264 /// Adjust the type of an Objective-C object that doesn't match up due 265 /// to type erasure at various points, e.g., related result types or the use 266 /// of parameterized classes. 267 static RValue AdjustObjCObjectType(CodeGenFunction &CGF, QualType ExpT, 268 RValue Result) { 269 if (!ExpT->isObjCRetainableType()) 270 return Result; 271 272 // If the converted types are the same, we're done. 273 llvm::Type *ExpLLVMTy = CGF.ConvertType(ExpT); 274 if (ExpLLVMTy == Result.getScalarVal()->getType()) 275 return Result; 276 277 // We have applied a substitution. Cast the rvalue appropriately. 278 return RValue::get(CGF.Builder.CreateBitCast(Result.getScalarVal(), 279 ExpLLVMTy)); 280 } 281 282 /// Decide whether to extend the lifetime of the receiver of a 283 /// returns-inner-pointer message. 284 static bool 285 shouldExtendReceiverForInnerPointerMessage(const ObjCMessageExpr *message) { 286 switch (message->getReceiverKind()) { 287 288 // For a normal instance message, we should extend unless the 289 // receiver is loaded from a variable with precise lifetime. 290 case ObjCMessageExpr::Instance: { 291 const Expr *receiver = message->getInstanceReceiver(); 292 293 // Look through OVEs. 294 if (auto opaque = dyn_cast<OpaqueValueExpr>(receiver)) { 295 if (opaque->getSourceExpr()) 296 receiver = opaque->getSourceExpr()->IgnoreParens(); 297 } 298 299 const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(receiver); 300 if (!ice || ice->getCastKind() != CK_LValueToRValue) return true; 301 receiver = ice->getSubExpr()->IgnoreParens(); 302 303 // Look through OVEs. 304 if (auto opaque = dyn_cast<OpaqueValueExpr>(receiver)) { 305 if (opaque->getSourceExpr()) 306 receiver = opaque->getSourceExpr()->IgnoreParens(); 307 } 308 309 // Only __strong variables. 310 if (receiver->getType().getObjCLifetime() != Qualifiers::OCL_Strong) 311 return true; 312 313 // All ivars and fields have precise lifetime. 314 if (isa<MemberExpr>(receiver) || isa<ObjCIvarRefExpr>(receiver)) 315 return false; 316 317 // Otherwise, check for variables. 318 const DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(ice->getSubExpr()); 319 if (!declRef) return true; 320 const VarDecl *var = dyn_cast<VarDecl>(declRef->getDecl()); 321 if (!var) return true; 322 323 // All variables have precise lifetime except local variables with 324 // automatic storage duration that aren't specially marked. 325 return (var->hasLocalStorage() && 326 !var->hasAttr<ObjCPreciseLifetimeAttr>()); 327 } 328 329 case ObjCMessageExpr::Class: 330 case ObjCMessageExpr::SuperClass: 331 // It's never necessary for class objects. 332 return false; 333 334 case ObjCMessageExpr::SuperInstance: 335 // We generally assume that 'self' lives throughout a method call. 336 return false; 337 } 338 339 llvm_unreachable("invalid receiver kind"); 340 } 341 342 /// Given an expression of ObjC pointer type, check whether it was 343 /// immediately loaded from an ARC __weak l-value. 344 static const Expr *findWeakLValue(const Expr *E) { 345 assert(E->getType()->isObjCRetainableType()); 346 E = E->IgnoreParens(); 347 if (auto CE = dyn_cast<CastExpr>(E)) { 348 if (CE->getCastKind() == CK_LValueToRValue) { 349 if (CE->getSubExpr()->getType().getObjCLifetime() == Qualifiers::OCL_Weak) 350 return CE->getSubExpr(); 351 } 352 } 353 354 return nullptr; 355 } 356 357 /// The ObjC runtime may provide entrypoints that are likely to be faster 358 /// than an ordinary message send of the appropriate selector. 359 /// 360 /// The entrypoints are guaranteed to be equivalent to just sending the 361 /// corresponding message. If the entrypoint is implemented naively as just a 362 /// message send, using it is a trade-off: it sacrifices a few cycles of 363 /// overhead to save a small amount of code. However, it's possible for 364 /// runtimes to detect and special-case classes that use "standard" 365 /// behavior; if that's dynamically a large proportion of all objects, using 366 /// the entrypoint will also be faster than using a message send. 367 /// 368 /// If the runtime does support a required entrypoint, then this method will 369 /// generate a call and return the resulting value. Otherwise it will return 370 /// None and the caller can generate a msgSend instead. 371 static Optional<llvm::Value *> 372 tryGenerateSpecializedMessageSend(CodeGenFunction &CGF, QualType ResultType, 373 llvm::Value *Receiver, 374 const CallArgList& Args, Selector Sel, 375 const ObjCMethodDecl *method, 376 bool isClassMessage) { 377 auto &CGM = CGF.CGM; 378 if (!CGM.getCodeGenOpts().ObjCConvertMessagesToRuntimeCalls) 379 return None; 380 381 auto &Runtime = CGM.getLangOpts().ObjCRuntime; 382 switch (Sel.getMethodFamily()) { 383 case OMF_alloc: 384 if (isClassMessage && 385 Runtime.shouldUseRuntimeFunctionsForAlloc() && 386 ResultType->isObjCObjectPointerType()) { 387 // [Foo alloc] -> objc_alloc(Foo) or 388 // [self alloc] -> objc_alloc(self) 389 if (Sel.isUnarySelector() && Sel.getNameForSlot(0) == "alloc") 390 return CGF.EmitObjCAlloc(Receiver, CGF.ConvertType(ResultType)); 391 // [Foo allocWithZone:nil] -> objc_allocWithZone(Foo) or 392 // [self allocWithZone:nil] -> objc_allocWithZone(self) 393 if (Sel.isKeywordSelector() && Sel.getNumArgs() == 1 && 394 Args.size() == 1 && Args.front().getType()->isPointerType() && 395 Sel.getNameForSlot(0) == "allocWithZone") { 396 const llvm::Value* arg = Args.front().getKnownRValue().getScalarVal(); 397 if (isa<llvm::ConstantPointerNull>(arg)) 398 return CGF.EmitObjCAllocWithZone(Receiver, 399 CGF.ConvertType(ResultType)); 400 return None; 401 } 402 } 403 break; 404 405 case OMF_autorelease: 406 if (ResultType->isObjCObjectPointerType() && 407 CGM.getLangOpts().getGC() == LangOptions::NonGC && 408 Runtime.shouldUseARCFunctionsForRetainRelease()) 409 return CGF.EmitObjCAutorelease(Receiver, CGF.ConvertType(ResultType)); 410 break; 411 412 case OMF_retain: 413 if (ResultType->isObjCObjectPointerType() && 414 CGM.getLangOpts().getGC() == LangOptions::NonGC && 415 Runtime.shouldUseARCFunctionsForRetainRelease()) 416 return CGF.EmitObjCRetainNonBlock(Receiver, CGF.ConvertType(ResultType)); 417 break; 418 419 case OMF_release: 420 if (ResultType->isVoidType() && 421 CGM.getLangOpts().getGC() == LangOptions::NonGC && 422 Runtime.shouldUseARCFunctionsForRetainRelease()) { 423 CGF.EmitObjCRelease(Receiver, ARCPreciseLifetime); 424 return nullptr; 425 } 426 break; 427 428 default: 429 break; 430 } 431 return None; 432 } 433 434 CodeGen::RValue CGObjCRuntime::GeneratePossiblySpecializedMessageSend( 435 CodeGenFunction &CGF, ReturnValueSlot Return, QualType ResultType, 436 Selector Sel, llvm::Value *Receiver, const CallArgList &Args, 437 const ObjCInterfaceDecl *OID, const ObjCMethodDecl *Method, 438 bool isClassMessage) { 439 if (Optional<llvm::Value *> SpecializedResult = 440 tryGenerateSpecializedMessageSend(CGF, ResultType, Receiver, Args, 441 Sel, Method, isClassMessage)) { 442 return RValue::get(SpecializedResult.getValue()); 443 } 444 return GenerateMessageSend(CGF, Return, ResultType, Sel, Receiver, Args, OID, 445 Method); 446 } 447 448 /// Instead of '[[MyClass alloc] init]', try to generate 449 /// 'objc_alloc_init(MyClass)'. This provides a code size improvement on the 450 /// caller side, as well as the optimized objc_alloc. 451 static Optional<llvm::Value *> 452 tryEmitSpecializedAllocInit(CodeGenFunction &CGF, const ObjCMessageExpr *OME) { 453 auto &Runtime = CGF.getLangOpts().ObjCRuntime; 454 if (!Runtime.shouldUseRuntimeFunctionForCombinedAllocInit()) 455 return None; 456 457 // Match the exact pattern '[[MyClass alloc] init]'. 458 Selector Sel = OME->getSelector(); 459 if (OME->getReceiverKind() != ObjCMessageExpr::Instance || 460 !OME->getType()->isObjCObjectPointerType() || !Sel.isUnarySelector() || 461 Sel.getNameForSlot(0) != "init") 462 return None; 463 464 // Okay, this is '[receiver init]', check if 'receiver' is '[cls alloc]' or 465 // we are in an ObjC class method and 'receiver' is '[self alloc]'. 466 auto *SubOME = 467 dyn_cast<ObjCMessageExpr>(OME->getInstanceReceiver()->IgnoreParenCasts()); 468 if (!SubOME) 469 return None; 470 Selector SubSel = SubOME->getSelector(); 471 472 // Check if we are in an ObjC class method and the receiver expression is 473 // 'self'. 474 const Expr *SelfInClassMethod = nullptr; 475 if (const auto *CurMD = dyn_cast_or_null<ObjCMethodDecl>(CGF.CurFuncDecl)) 476 if (CurMD->isClassMethod()) 477 if ((SelfInClassMethod = SubOME->getInstanceReceiver())) 478 if (!SelfInClassMethod->isObjCSelfExpr()) 479 SelfInClassMethod = nullptr; 480 481 if ((SubOME->getReceiverKind() != ObjCMessageExpr::Class && 482 !SelfInClassMethod) || !SubOME->getType()->isObjCObjectPointerType() || 483 !SubSel.isUnarySelector() || SubSel.getNameForSlot(0) != "alloc") 484 return None; 485 486 llvm::Value *Receiver; 487 if (SelfInClassMethod) { 488 Receiver = CGF.EmitScalarExpr(SelfInClassMethod); 489 } else { 490 QualType ReceiverType = SubOME->getClassReceiver(); 491 const ObjCObjectType *ObjTy = ReceiverType->getAs<ObjCObjectType>(); 492 const ObjCInterfaceDecl *ID = ObjTy->getInterface(); 493 assert(ID && "null interface should be impossible here"); 494 Receiver = CGF.CGM.getObjCRuntime().GetClass(CGF, ID); 495 } 496 return CGF.EmitObjCAllocInit(Receiver, CGF.ConvertType(OME->getType())); 497 } 498 499 RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E, 500 ReturnValueSlot Return) { 501 // Only the lookup mechanism and first two arguments of the method 502 // implementation vary between runtimes. We can get the receiver and 503 // arguments in generic code. 504 505 bool isDelegateInit = E->isDelegateInitCall(); 506 507 const ObjCMethodDecl *method = E->getMethodDecl(); 508 509 // If the method is -retain, and the receiver's being loaded from 510 // a __weak variable, peephole the entire operation to objc_loadWeakRetained. 511 if (method && E->getReceiverKind() == ObjCMessageExpr::Instance && 512 method->getMethodFamily() == OMF_retain) { 513 if (auto lvalueExpr = findWeakLValue(E->getInstanceReceiver())) { 514 LValue lvalue = EmitLValue(lvalueExpr); 515 llvm::Value *result = EmitARCLoadWeakRetained(lvalue.getAddress(*this)); 516 return AdjustObjCObjectType(*this, E->getType(), RValue::get(result)); 517 } 518 } 519 520 if (Optional<llvm::Value *> Val = tryEmitSpecializedAllocInit(*this, E)) 521 return AdjustObjCObjectType(*this, E->getType(), RValue::get(*Val)); 522 523 // We don't retain the receiver in delegate init calls, and this is 524 // safe because the receiver value is always loaded from 'self', 525 // which we zero out. We don't want to Block_copy block receivers, 526 // though. 527 bool retainSelf = 528 (!isDelegateInit && 529 CGM.getLangOpts().ObjCAutoRefCount && 530 method && 531 method->hasAttr<NSConsumesSelfAttr>()); 532 533 CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 534 bool isSuperMessage = false; 535 bool isClassMessage = false; 536 ObjCInterfaceDecl *OID = nullptr; 537 // Find the receiver 538 QualType ReceiverType; 539 llvm::Value *Receiver = nullptr; 540 switch (E->getReceiverKind()) { 541 case ObjCMessageExpr::Instance: 542 ReceiverType = E->getInstanceReceiver()->getType(); 543 if (auto *OMD = dyn_cast_or_null<ObjCMethodDecl>(CurFuncDecl)) 544 if (OMD->isClassMethod()) 545 if (E->getInstanceReceiver()->isObjCSelfExpr()) 546 isClassMessage = true; 547 if (retainSelf) { 548 TryEmitResult ter = tryEmitARCRetainScalarExpr(*this, 549 E->getInstanceReceiver()); 550 Receiver = ter.getPointer(); 551 if (ter.getInt()) retainSelf = false; 552 } else 553 Receiver = EmitScalarExpr(E->getInstanceReceiver()); 554 break; 555 556 case ObjCMessageExpr::Class: { 557 ReceiverType = E->getClassReceiver(); 558 const ObjCObjectType *ObjTy = ReceiverType->getAs<ObjCObjectType>(); 559 assert(ObjTy && "Invalid Objective-C class message send"); 560 OID = ObjTy->getInterface(); 561 assert(OID && "Invalid Objective-C class message send"); 562 Receiver = Runtime.GetClass(*this, OID); 563 isClassMessage = true; 564 break; 565 } 566 567 case ObjCMessageExpr::SuperInstance: 568 ReceiverType = E->getSuperType(); 569 Receiver = LoadObjCSelf(); 570 isSuperMessage = true; 571 break; 572 573 case ObjCMessageExpr::SuperClass: 574 ReceiverType = E->getSuperType(); 575 Receiver = LoadObjCSelf(); 576 isSuperMessage = true; 577 isClassMessage = true; 578 break; 579 } 580 581 if (retainSelf) 582 Receiver = EmitARCRetainNonBlock(Receiver); 583 584 // In ARC, we sometimes want to "extend the lifetime" 585 // (i.e. retain+autorelease) of receivers of returns-inner-pointer 586 // messages. 587 if (getLangOpts().ObjCAutoRefCount && method && 588 method->hasAttr<ObjCReturnsInnerPointerAttr>() && 589 shouldExtendReceiverForInnerPointerMessage(E)) 590 Receiver = EmitARCRetainAutorelease(ReceiverType, Receiver); 591 592 QualType ResultType = method ? method->getReturnType() : E->getType(); 593 594 CallArgList Args; 595 EmitCallArgs(Args, method, E->arguments(), /*AC*/AbstractCallee(method)); 596 597 // For delegate init calls in ARC, do an unsafe store of null into 598 // self. This represents the call taking direct ownership of that 599 // value. We have to do this after emitting the other call 600 // arguments because they might also reference self, but we don't 601 // have to worry about any of them modifying self because that would 602 // be an undefined read and write of an object in unordered 603 // expressions. 604 if (isDelegateInit) { 605 assert(getLangOpts().ObjCAutoRefCount && 606 "delegate init calls should only be marked in ARC"); 607 608 // Do an unsafe store of null into self. 609 Address selfAddr = 610 GetAddrOfLocalVar(cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()); 611 Builder.CreateStore(getNullForVariable(selfAddr), selfAddr); 612 } 613 614 RValue result; 615 if (isSuperMessage) { 616 // super is only valid in an Objective-C method 617 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); 618 bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext()); 619 result = Runtime.GenerateMessageSendSuper(*this, Return, ResultType, 620 E->getSelector(), 621 OMD->getClassInterface(), 622 isCategoryImpl, 623 Receiver, 624 isClassMessage, 625 Args, 626 method); 627 } else { 628 // Call runtime methods directly if we can. 629 result = Runtime.GeneratePossiblySpecializedMessageSend( 630 *this, Return, ResultType, E->getSelector(), Receiver, Args, OID, 631 method, isClassMessage); 632 } 633 634 // For delegate init calls in ARC, implicitly store the result of 635 // the call back into self. This takes ownership of the value. 636 if (isDelegateInit) { 637 Address selfAddr = 638 GetAddrOfLocalVar(cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()); 639 llvm::Value *newSelf = result.getScalarVal(); 640 641 // The delegate return type isn't necessarily a matching type; in 642 // fact, it's quite likely to be 'id'. 643 llvm::Type *selfTy = selfAddr.getElementType(); 644 newSelf = Builder.CreateBitCast(newSelf, selfTy); 645 646 Builder.CreateStore(newSelf, selfAddr); 647 } 648 649 return AdjustObjCObjectType(*this, E->getType(), result); 650 } 651 652 namespace { 653 struct FinishARCDealloc final : EHScopeStack::Cleanup { 654 void Emit(CodeGenFunction &CGF, Flags flags) override { 655 const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CGF.CurCodeDecl); 656 657 const ObjCImplDecl *impl = cast<ObjCImplDecl>(method->getDeclContext()); 658 const ObjCInterfaceDecl *iface = impl->getClassInterface(); 659 if (!iface->getSuperClass()) return; 660 661 bool isCategory = isa<ObjCCategoryImplDecl>(impl); 662 663 // Call [super dealloc] if we have a superclass. 664 llvm::Value *self = CGF.LoadObjCSelf(); 665 666 CallArgList args; 667 CGF.CGM.getObjCRuntime().GenerateMessageSendSuper(CGF, ReturnValueSlot(), 668 CGF.getContext().VoidTy, 669 method->getSelector(), 670 iface, 671 isCategory, 672 self, 673 /*is class msg*/ false, 674 args, 675 method); 676 } 677 }; 678 } 679 680 /// StartObjCMethod - Begin emission of an ObjCMethod. This generates 681 /// the LLVM function and sets the other context used by 682 /// CodeGenFunction. 683 void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD, 684 const ObjCContainerDecl *CD) { 685 SourceLocation StartLoc = OMD->getBeginLoc(); 686 FunctionArgList args; 687 // Check if we should generate debug info for this method. 688 if (OMD->hasAttr<NoDebugAttr>()) 689 DebugInfo = nullptr; // disable debug info indefinitely for this function 690 691 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD); 692 693 const CGFunctionInfo &FI = CGM.getTypes().arrangeObjCMethodDeclaration(OMD); 694 if (OMD->isDirectMethod()) { 695 Fn->setVisibility(llvm::Function::HiddenVisibility); 696 CGM.SetLLVMFunctionAttributes(OMD, FI, Fn); 697 CGM.SetLLVMFunctionAttributesForDefinition(OMD, Fn); 698 } else { 699 CGM.SetInternalFunctionAttributes(OMD, Fn, FI); 700 } 701 702 args.push_back(OMD->getSelfDecl()); 703 args.push_back(OMD->getCmdDecl()); 704 705 args.append(OMD->param_begin(), OMD->param_end()); 706 707 CurGD = OMD; 708 CurEHLocation = OMD->getEndLoc(); 709 710 StartFunction(OMD, OMD->getReturnType(), Fn, FI, args, 711 OMD->getLocation(), StartLoc); 712 713 if (OMD->isDirectMethod()) { 714 // This function is a direct call, it has to implement a nil check 715 // on entry. 716 // 717 // TODO: possibly have several entry points to elide the check 718 CGM.getObjCRuntime().GenerateDirectMethodPrologue(*this, Fn, OMD, CD); 719 } 720 721 // In ARC, certain methods get an extra cleanup. 722 if (CGM.getLangOpts().ObjCAutoRefCount && 723 OMD->isInstanceMethod() && 724 OMD->getSelector().isUnarySelector()) { 725 const IdentifierInfo *ident = 726 OMD->getSelector().getIdentifierInfoForSlot(0); 727 if (ident->isStr("dealloc")) 728 EHStack.pushCleanup<FinishARCDealloc>(getARCCleanupKind()); 729 } 730 } 731 732 static llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF, 733 LValue lvalue, QualType type); 734 735 /// Generate an Objective-C method. An Objective-C method is a C function with 736 /// its pointer, name, and types registered in the class structure. 737 void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) { 738 StartObjCMethod(OMD, OMD->getClassInterface()); 739 PGO.assignRegionCounters(GlobalDecl(OMD), CurFn); 740 assert(isa<CompoundStmt>(OMD->getBody())); 741 incrementProfileCounter(OMD->getBody()); 742 EmitCompoundStmtWithoutScope(*cast<CompoundStmt>(OMD->getBody())); 743 FinishFunction(OMD->getBodyRBrace()); 744 } 745 746 /// emitStructGetterCall - Call the runtime function to load a property 747 /// into the return value slot. 748 static void emitStructGetterCall(CodeGenFunction &CGF, ObjCIvarDecl *ivar, 749 bool isAtomic, bool hasStrong) { 750 ASTContext &Context = CGF.getContext(); 751 752 Address src = 753 CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(), ivar, 0) 754 .getAddress(CGF); 755 756 // objc_copyStruct (ReturnValue, &structIvar, 757 // sizeof (Type of Ivar), isAtomic, false); 758 CallArgList args; 759 760 Address dest = CGF.Builder.CreateBitCast(CGF.ReturnValue, CGF.VoidPtrTy); 761 args.add(RValue::get(dest.getPointer()), Context.VoidPtrTy); 762 763 src = CGF.Builder.CreateBitCast(src, CGF.VoidPtrTy); 764 args.add(RValue::get(src.getPointer()), Context.VoidPtrTy); 765 766 CharUnits size = CGF.getContext().getTypeSizeInChars(ivar->getType()); 767 args.add(RValue::get(CGF.CGM.getSize(size)), Context.getSizeType()); 768 args.add(RValue::get(CGF.Builder.getInt1(isAtomic)), Context.BoolTy); 769 args.add(RValue::get(CGF.Builder.getInt1(hasStrong)), Context.BoolTy); 770 771 llvm::FunctionCallee fn = CGF.CGM.getObjCRuntime().GetGetStructFunction(); 772 CGCallee callee = CGCallee::forDirect(fn); 773 CGF.EmitCall(CGF.getTypes().arrangeBuiltinFunctionCall(Context.VoidTy, args), 774 callee, ReturnValueSlot(), args); 775 } 776 777 /// Determine whether the given architecture supports unaligned atomic 778 /// accesses. They don't have to be fast, just faster than a function 779 /// call and a mutex. 780 static bool hasUnalignedAtomics(llvm::Triple::ArchType arch) { 781 // FIXME: Allow unaligned atomic load/store on x86. (It is not 782 // currently supported by the backend.) 783 return 0; 784 } 785 786 /// Return the maximum size that permits atomic accesses for the given 787 /// architecture. 788 static CharUnits getMaxAtomicAccessSize(CodeGenModule &CGM, 789 llvm::Triple::ArchType arch) { 790 // ARM has 8-byte atomic accesses, but it's not clear whether we 791 // want to rely on them here. 792 793 // In the default case, just assume that any size up to a pointer is 794 // fine given adequate alignment. 795 return CharUnits::fromQuantity(CGM.PointerSizeInBytes); 796 } 797 798 namespace { 799 class PropertyImplStrategy { 800 public: 801 enum StrategyKind { 802 /// The 'native' strategy is to use the architecture's provided 803 /// reads and writes. 804 Native, 805 806 /// Use objc_setProperty and objc_getProperty. 807 GetSetProperty, 808 809 /// Use objc_setProperty for the setter, but use expression 810 /// evaluation for the getter. 811 SetPropertyAndExpressionGet, 812 813 /// Use objc_copyStruct. 814 CopyStruct, 815 816 /// The 'expression' strategy is to emit normal assignment or 817 /// lvalue-to-rvalue expressions. 818 Expression 819 }; 820 821 StrategyKind getKind() const { return StrategyKind(Kind); } 822 823 bool hasStrongMember() const { return HasStrong; } 824 bool isAtomic() const { return IsAtomic; } 825 bool isCopy() const { return IsCopy; } 826 827 CharUnits getIvarSize() const { return IvarSize; } 828 CharUnits getIvarAlignment() const { return IvarAlignment; } 829 830 PropertyImplStrategy(CodeGenModule &CGM, 831 const ObjCPropertyImplDecl *propImpl); 832 833 private: 834 unsigned Kind : 8; 835 unsigned IsAtomic : 1; 836 unsigned IsCopy : 1; 837 unsigned HasStrong : 1; 838 839 CharUnits IvarSize; 840 CharUnits IvarAlignment; 841 }; 842 } 843 844 /// Pick an implementation strategy for the given property synthesis. 845 PropertyImplStrategy::PropertyImplStrategy(CodeGenModule &CGM, 846 const ObjCPropertyImplDecl *propImpl) { 847 const ObjCPropertyDecl *prop = propImpl->getPropertyDecl(); 848 ObjCPropertyDecl::SetterKind setterKind = prop->getSetterKind(); 849 850 IsCopy = (setterKind == ObjCPropertyDecl::Copy); 851 IsAtomic = prop->isAtomic(); 852 HasStrong = false; // doesn't matter here. 853 854 // Evaluate the ivar's size and alignment. 855 ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); 856 QualType ivarType = ivar->getType(); 857 std::tie(IvarSize, IvarAlignment) = 858 CGM.getContext().getTypeInfoInChars(ivarType); 859 860 // If we have a copy property, we always have to use getProperty/setProperty. 861 // TODO: we could actually use setProperty and an expression for non-atomics. 862 if (IsCopy) { 863 Kind = GetSetProperty; 864 return; 865 } 866 867 // Handle retain. 868 if (setterKind == ObjCPropertyDecl::Retain) { 869 // In GC-only, there's nothing special that needs to be done. 870 if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) { 871 // fallthrough 872 873 // In ARC, if the property is non-atomic, use expression emission, 874 // which translates to objc_storeStrong. This isn't required, but 875 // it's slightly nicer. 876 } else if (CGM.getLangOpts().ObjCAutoRefCount && !IsAtomic) { 877 // Using standard expression emission for the setter is only 878 // acceptable if the ivar is __strong, which won't be true if 879 // the property is annotated with __attribute__((NSObject)). 880 // TODO: falling all the way back to objc_setProperty here is 881 // just laziness, though; we could still use objc_storeStrong 882 // if we hacked it right. 883 if (ivarType.getObjCLifetime() == Qualifiers::OCL_Strong) 884 Kind = Expression; 885 else 886 Kind = SetPropertyAndExpressionGet; 887 return; 888 889 // Otherwise, we need to at least use setProperty. However, if 890 // the property isn't atomic, we can use normal expression 891 // emission for the getter. 892 } else if (!IsAtomic) { 893 Kind = SetPropertyAndExpressionGet; 894 return; 895 896 // Otherwise, we have to use both setProperty and getProperty. 897 } else { 898 Kind = GetSetProperty; 899 return; 900 } 901 } 902 903 // If we're not atomic, just use expression accesses. 904 if (!IsAtomic) { 905 Kind = Expression; 906 return; 907 } 908 909 // Properties on bitfield ivars need to be emitted using expression 910 // accesses even if they're nominally atomic. 911 if (ivar->isBitField()) { 912 Kind = Expression; 913 return; 914 } 915 916 // GC-qualified or ARC-qualified ivars need to be emitted as 917 // expressions. This actually works out to being atomic anyway, 918 // except for ARC __strong, but that should trigger the above code. 919 if (ivarType.hasNonTrivialObjCLifetime() || 920 (CGM.getLangOpts().getGC() && 921 CGM.getContext().getObjCGCAttrKind(ivarType))) { 922 Kind = Expression; 923 return; 924 } 925 926 // Compute whether the ivar has strong members. 927 if (CGM.getLangOpts().getGC()) 928 if (const RecordType *recordType = ivarType->getAs<RecordType>()) 929 HasStrong = recordType->getDecl()->hasObjectMember(); 930 931 // We can never access structs with object members with a native 932 // access, because we need to use write barriers. This is what 933 // objc_copyStruct is for. 934 if (HasStrong) { 935 Kind = CopyStruct; 936 return; 937 } 938 939 // Otherwise, this is target-dependent and based on the size and 940 // alignment of the ivar. 941 942 // If the size of the ivar is not a power of two, give up. We don't 943 // want to get into the business of doing compare-and-swaps. 944 if (!IvarSize.isPowerOfTwo()) { 945 Kind = CopyStruct; 946 return; 947 } 948 949 llvm::Triple::ArchType arch = 950 CGM.getTarget().getTriple().getArch(); 951 952 // Most architectures require memory to fit within a single cache 953 // line, so the alignment has to be at least the size of the access. 954 // Otherwise we have to grab a lock. 955 if (IvarAlignment < IvarSize && !hasUnalignedAtomics(arch)) { 956 Kind = CopyStruct; 957 return; 958 } 959 960 // If the ivar's size exceeds the architecture's maximum atomic 961 // access size, we have to use CopyStruct. 962 if (IvarSize > getMaxAtomicAccessSize(CGM, arch)) { 963 Kind = CopyStruct; 964 return; 965 } 966 967 // Otherwise, we can use native loads and stores. 968 Kind = Native; 969 } 970 971 /// Generate an Objective-C property getter function. 972 /// 973 /// The given Decl must be an ObjCImplementationDecl. \@synthesize 974 /// is illegal within a category. 975 void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP, 976 const ObjCPropertyImplDecl *PID) { 977 llvm::Constant *AtomicHelperFn = 978 CodeGenFunction(CGM).GenerateObjCAtomicGetterCopyHelperFunction(PID); 979 ObjCMethodDecl *OMD = PID->getGetterMethodDecl(); 980 assert(OMD && "Invalid call to generate getter (empty method)"); 981 StartObjCMethod(OMD, IMP->getClassInterface()); 982 983 generateObjCGetterBody(IMP, PID, OMD, AtomicHelperFn); 984 985 FinishFunction(OMD->getEndLoc()); 986 } 987 988 static bool hasTrivialGetExpr(const ObjCPropertyImplDecl *propImpl) { 989 const Expr *getter = propImpl->getGetterCXXConstructor(); 990 if (!getter) return true; 991 992 // Sema only makes only of these when the ivar has a C++ class type, 993 // so the form is pretty constrained. 994 995 // If the property has a reference type, we might just be binding a 996 // reference, in which case the result will be a gl-value. We should 997 // treat this as a non-trivial operation. 998 if (getter->isGLValue()) 999 return false; 1000 1001 // If we selected a trivial copy-constructor, we're okay. 1002 if (const CXXConstructExpr *construct = dyn_cast<CXXConstructExpr>(getter)) 1003 return (construct->getConstructor()->isTrivial()); 1004 1005 // The constructor might require cleanups (in which case it's never 1006 // trivial). 1007 assert(isa<ExprWithCleanups>(getter)); 1008 return false; 1009 } 1010 1011 /// emitCPPObjectAtomicGetterCall - Call the runtime function to 1012 /// copy the ivar into the resturn slot. 1013 static void emitCPPObjectAtomicGetterCall(CodeGenFunction &CGF, 1014 llvm::Value *returnAddr, 1015 ObjCIvarDecl *ivar, 1016 llvm::Constant *AtomicHelperFn) { 1017 // objc_copyCppObjectAtomic (&returnSlot, &CppObjectIvar, 1018 // AtomicHelperFn); 1019 CallArgList args; 1020 1021 // The 1st argument is the return Slot. 1022 args.add(RValue::get(returnAddr), CGF.getContext().VoidPtrTy); 1023 1024 // The 2nd argument is the address of the ivar. 1025 llvm::Value *ivarAddr = 1026 CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(), ivar, 0) 1027 .getPointer(CGF); 1028 ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy); 1029 args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy); 1030 1031 // Third argument is the helper function. 1032 args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy); 1033 1034 llvm::FunctionCallee copyCppAtomicObjectFn = 1035 CGF.CGM.getObjCRuntime().GetCppAtomicObjectGetFunction(); 1036 CGCallee callee = CGCallee::forDirect(copyCppAtomicObjectFn); 1037 CGF.EmitCall( 1038 CGF.getTypes().arrangeBuiltinFunctionCall(CGF.getContext().VoidTy, args), 1039 callee, ReturnValueSlot(), args); 1040 } 1041 1042 void 1043 CodeGenFunction::generateObjCGetterBody(const ObjCImplementationDecl *classImpl, 1044 const ObjCPropertyImplDecl *propImpl, 1045 const ObjCMethodDecl *GetterMethodDecl, 1046 llvm::Constant *AtomicHelperFn) { 1047 // If there's a non-trivial 'get' expression, we just have to emit that. 1048 if (!hasTrivialGetExpr(propImpl)) { 1049 if (!AtomicHelperFn) { 1050 auto *ret = ReturnStmt::Create(getContext(), SourceLocation(), 1051 propImpl->getGetterCXXConstructor(), 1052 /* NRVOCandidate=*/nullptr); 1053 EmitReturnStmt(*ret); 1054 } 1055 else { 1056 ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); 1057 emitCPPObjectAtomicGetterCall(*this, ReturnValue.getPointer(), 1058 ivar, AtomicHelperFn); 1059 } 1060 return; 1061 } 1062 1063 const ObjCPropertyDecl *prop = propImpl->getPropertyDecl(); 1064 QualType propType = prop->getType(); 1065 ObjCMethodDecl *getterMethod = propImpl->getGetterMethodDecl(); 1066 1067 ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); 1068 1069 // Pick an implementation strategy. 1070 PropertyImplStrategy strategy(CGM, propImpl); 1071 switch (strategy.getKind()) { 1072 case PropertyImplStrategy::Native: { 1073 // We don't need to do anything for a zero-size struct. 1074 if (strategy.getIvarSize().isZero()) 1075 return; 1076 1077 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0); 1078 1079 // Currently, all atomic accesses have to be through integer 1080 // types, so there's no point in trying to pick a prettier type. 1081 uint64_t ivarSize = getContext().toBits(strategy.getIvarSize()); 1082 llvm::Type *bitcastType = llvm::Type::getIntNTy(getLLVMContext(), ivarSize); 1083 bitcastType = bitcastType->getPointerTo(); // addrspace 0 okay 1084 1085 // Perform an atomic load. This does not impose ordering constraints. 1086 Address ivarAddr = LV.getAddress(*this); 1087 ivarAddr = Builder.CreateBitCast(ivarAddr, bitcastType); 1088 llvm::LoadInst *load = Builder.CreateLoad(ivarAddr, "load"); 1089 load->setAtomic(llvm::AtomicOrdering::Unordered); 1090 1091 // Store that value into the return address. Doing this with a 1092 // bitcast is likely to produce some pretty ugly IR, but it's not 1093 // the *most* terrible thing in the world. 1094 llvm::Type *retTy = ConvertType(getterMethod->getReturnType()); 1095 uint64_t retTySize = CGM.getDataLayout().getTypeSizeInBits(retTy); 1096 llvm::Value *ivarVal = load; 1097 if (ivarSize > retTySize) { 1098 llvm::Type *newTy = llvm::Type::getIntNTy(getLLVMContext(), retTySize); 1099 ivarVal = Builder.CreateTrunc(load, newTy); 1100 bitcastType = newTy->getPointerTo(); 1101 } 1102 Builder.CreateStore(ivarVal, 1103 Builder.CreateBitCast(ReturnValue, bitcastType)); 1104 1105 // Make sure we don't do an autorelease. 1106 AutoreleaseResult = false; 1107 return; 1108 } 1109 1110 case PropertyImplStrategy::GetSetProperty: { 1111 llvm::FunctionCallee getPropertyFn = 1112 CGM.getObjCRuntime().GetPropertyGetFunction(); 1113 if (!getPropertyFn) { 1114 CGM.ErrorUnsupported(propImpl, "Obj-C getter requiring atomic copy"); 1115 return; 1116 } 1117 CGCallee callee = CGCallee::forDirect(getPropertyFn); 1118 1119 // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true). 1120 // FIXME: Can't this be simpler? This might even be worse than the 1121 // corresponding gcc code. 1122 llvm::Value *cmd = 1123 Builder.CreateLoad(GetAddrOfLocalVar(getterMethod->getCmdDecl()), "cmd"); 1124 llvm::Value *self = Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy); 1125 llvm::Value *ivarOffset = 1126 EmitIvarOffset(classImpl->getClassInterface(), ivar); 1127 1128 CallArgList args; 1129 args.add(RValue::get(self), getContext().getObjCIdType()); 1130 args.add(RValue::get(cmd), getContext().getObjCSelType()); 1131 args.add(RValue::get(ivarOffset), getContext().getPointerDiffType()); 1132 args.add(RValue::get(Builder.getInt1(strategy.isAtomic())), 1133 getContext().BoolTy); 1134 1135 // FIXME: We shouldn't need to get the function info here, the 1136 // runtime already should have computed it to build the function. 1137 llvm::CallBase *CallInstruction; 1138 RValue RV = EmitCall(getTypes().arrangeBuiltinFunctionCall( 1139 getContext().getObjCIdType(), args), 1140 callee, ReturnValueSlot(), args, &CallInstruction); 1141 if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(CallInstruction)) 1142 call->setTailCall(); 1143 1144 // We need to fix the type here. Ivars with copy & retain are 1145 // always objects so we don't need to worry about complex or 1146 // aggregates. 1147 RV = RValue::get(Builder.CreateBitCast( 1148 RV.getScalarVal(), 1149 getTypes().ConvertType(getterMethod->getReturnType()))); 1150 1151 EmitReturnOfRValue(RV, propType); 1152 1153 // objc_getProperty does an autorelease, so we should suppress ours. 1154 AutoreleaseResult = false; 1155 1156 return; 1157 } 1158 1159 case PropertyImplStrategy::CopyStruct: 1160 emitStructGetterCall(*this, ivar, strategy.isAtomic(), 1161 strategy.hasStrongMember()); 1162 return; 1163 1164 case PropertyImplStrategy::Expression: 1165 case PropertyImplStrategy::SetPropertyAndExpressionGet: { 1166 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, 0); 1167 1168 QualType ivarType = ivar->getType(); 1169 switch (getEvaluationKind(ivarType)) { 1170 case TEK_Complex: { 1171 ComplexPairTy pair = EmitLoadOfComplex(LV, SourceLocation()); 1172 EmitStoreOfComplex(pair, MakeAddrLValue(ReturnValue, ivarType), 1173 /*init*/ true); 1174 return; 1175 } 1176 case TEK_Aggregate: { 1177 // The return value slot is guaranteed to not be aliased, but 1178 // that's not necessarily the same as "on the stack", so 1179 // we still potentially need objc_memmove_collectable. 1180 EmitAggregateCopy(/* Dest= */ MakeAddrLValue(ReturnValue, ivarType), 1181 /* Src= */ LV, ivarType, getOverlapForReturnValue()); 1182 return; 1183 } 1184 case TEK_Scalar: { 1185 llvm::Value *value; 1186 if (propType->isReferenceType()) { 1187 value = LV.getAddress(*this).getPointer(); 1188 } else { 1189 // We want to load and autoreleaseReturnValue ARC __weak ivars. 1190 if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak) { 1191 if (getLangOpts().ObjCAutoRefCount) { 1192 value = emitARCRetainLoadOfScalar(*this, LV, ivarType); 1193 } else { 1194 value = EmitARCLoadWeak(LV.getAddress(*this)); 1195 } 1196 1197 // Otherwise we want to do a simple load, suppressing the 1198 // final autorelease. 1199 } else { 1200 value = EmitLoadOfLValue(LV, SourceLocation()).getScalarVal(); 1201 AutoreleaseResult = false; 1202 } 1203 1204 value = Builder.CreateBitCast( 1205 value, ConvertType(GetterMethodDecl->getReturnType())); 1206 } 1207 1208 EmitReturnOfRValue(RValue::get(value), propType); 1209 return; 1210 } 1211 } 1212 llvm_unreachable("bad evaluation kind"); 1213 } 1214 1215 } 1216 llvm_unreachable("bad @property implementation strategy!"); 1217 } 1218 1219 /// emitStructSetterCall - Call the runtime function to store the value 1220 /// from the first formal parameter into the given ivar. 1221 static void emitStructSetterCall(CodeGenFunction &CGF, ObjCMethodDecl *OMD, 1222 ObjCIvarDecl *ivar) { 1223 // objc_copyStruct (&structIvar, &Arg, 1224 // sizeof (struct something), true, false); 1225 CallArgList args; 1226 1227 // The first argument is the address of the ivar. 1228 llvm::Value *ivarAddr = 1229 CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(), ivar, 0) 1230 .getPointer(CGF); 1231 ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy); 1232 args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy); 1233 1234 // The second argument is the address of the parameter variable. 1235 ParmVarDecl *argVar = *OMD->param_begin(); 1236 DeclRefExpr argRef(CGF.getContext(), argVar, false, 1237 argVar->getType().getNonReferenceType(), VK_LValue, 1238 SourceLocation()); 1239 llvm::Value *argAddr = CGF.EmitLValue(&argRef).getPointer(CGF); 1240 argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy); 1241 args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy); 1242 1243 // The third argument is the sizeof the type. 1244 llvm::Value *size = 1245 CGF.CGM.getSize(CGF.getContext().getTypeSizeInChars(ivar->getType())); 1246 args.add(RValue::get(size), CGF.getContext().getSizeType()); 1247 1248 // The fourth argument is the 'isAtomic' flag. 1249 args.add(RValue::get(CGF.Builder.getTrue()), CGF.getContext().BoolTy); 1250 1251 // The fifth argument is the 'hasStrong' flag. 1252 // FIXME: should this really always be false? 1253 args.add(RValue::get(CGF.Builder.getFalse()), CGF.getContext().BoolTy); 1254 1255 llvm::FunctionCallee fn = CGF.CGM.getObjCRuntime().GetSetStructFunction(); 1256 CGCallee callee = CGCallee::forDirect(fn); 1257 CGF.EmitCall( 1258 CGF.getTypes().arrangeBuiltinFunctionCall(CGF.getContext().VoidTy, args), 1259 callee, ReturnValueSlot(), args); 1260 } 1261 1262 /// emitCPPObjectAtomicSetterCall - Call the runtime function to store 1263 /// the value from the first formal parameter into the given ivar, using 1264 /// the Cpp API for atomic Cpp objects with non-trivial copy assignment. 1265 static void emitCPPObjectAtomicSetterCall(CodeGenFunction &CGF, 1266 ObjCMethodDecl *OMD, 1267 ObjCIvarDecl *ivar, 1268 llvm::Constant *AtomicHelperFn) { 1269 // objc_copyCppObjectAtomic (&CppObjectIvar, &Arg, 1270 // AtomicHelperFn); 1271 CallArgList args; 1272 1273 // The first argument is the address of the ivar. 1274 llvm::Value *ivarAddr = 1275 CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), CGF.LoadObjCSelf(), ivar, 0) 1276 .getPointer(CGF); 1277 ivarAddr = CGF.Builder.CreateBitCast(ivarAddr, CGF.Int8PtrTy); 1278 args.add(RValue::get(ivarAddr), CGF.getContext().VoidPtrTy); 1279 1280 // The second argument is the address of the parameter variable. 1281 ParmVarDecl *argVar = *OMD->param_begin(); 1282 DeclRefExpr argRef(CGF.getContext(), argVar, false, 1283 argVar->getType().getNonReferenceType(), VK_LValue, 1284 SourceLocation()); 1285 llvm::Value *argAddr = CGF.EmitLValue(&argRef).getPointer(CGF); 1286 argAddr = CGF.Builder.CreateBitCast(argAddr, CGF.Int8PtrTy); 1287 args.add(RValue::get(argAddr), CGF.getContext().VoidPtrTy); 1288 1289 // Third argument is the helper function. 1290 args.add(RValue::get(AtomicHelperFn), CGF.getContext().VoidPtrTy); 1291 1292 llvm::FunctionCallee fn = 1293 CGF.CGM.getObjCRuntime().GetCppAtomicObjectSetFunction(); 1294 CGCallee callee = CGCallee::forDirect(fn); 1295 CGF.EmitCall( 1296 CGF.getTypes().arrangeBuiltinFunctionCall(CGF.getContext().VoidTy, args), 1297 callee, ReturnValueSlot(), args); 1298 } 1299 1300 1301 static bool hasTrivialSetExpr(const ObjCPropertyImplDecl *PID) { 1302 Expr *setter = PID->getSetterCXXAssignment(); 1303 if (!setter) return true; 1304 1305 // Sema only makes only of these when the ivar has a C++ class type, 1306 // so the form is pretty constrained. 1307 1308 // An operator call is trivial if the function it calls is trivial. 1309 // This also implies that there's nothing non-trivial going on with 1310 // the arguments, because operator= can only be trivial if it's a 1311 // synthesized assignment operator and therefore both parameters are 1312 // references. 1313 if (CallExpr *call = dyn_cast<CallExpr>(setter)) { 1314 if (const FunctionDecl *callee 1315 = dyn_cast_or_null<FunctionDecl>(call->getCalleeDecl())) 1316 if (callee->isTrivial()) 1317 return true; 1318 return false; 1319 } 1320 1321 assert(isa<ExprWithCleanups>(setter)); 1322 return false; 1323 } 1324 1325 static bool UseOptimizedSetter(CodeGenModule &CGM) { 1326 if (CGM.getLangOpts().getGC() != LangOptions::NonGC) 1327 return false; 1328 return CGM.getLangOpts().ObjCRuntime.hasOptimizedSetter(); 1329 } 1330 1331 void 1332 CodeGenFunction::generateObjCSetterBody(const ObjCImplementationDecl *classImpl, 1333 const ObjCPropertyImplDecl *propImpl, 1334 llvm::Constant *AtomicHelperFn) { 1335 ObjCIvarDecl *ivar = propImpl->getPropertyIvarDecl(); 1336 ObjCMethodDecl *setterMethod = propImpl->getSetterMethodDecl(); 1337 1338 // Just use the setter expression if Sema gave us one and it's 1339 // non-trivial. 1340 if (!hasTrivialSetExpr(propImpl)) { 1341 if (!AtomicHelperFn) 1342 // If non-atomic, assignment is called directly. 1343 EmitStmt(propImpl->getSetterCXXAssignment()); 1344 else 1345 // If atomic, assignment is called via a locking api. 1346 emitCPPObjectAtomicSetterCall(*this, setterMethod, ivar, 1347 AtomicHelperFn); 1348 return; 1349 } 1350 1351 PropertyImplStrategy strategy(CGM, propImpl); 1352 switch (strategy.getKind()) { 1353 case PropertyImplStrategy::Native: { 1354 // We don't need to do anything for a zero-size struct. 1355 if (strategy.getIvarSize().isZero()) 1356 return; 1357 1358 Address argAddr = GetAddrOfLocalVar(*setterMethod->param_begin()); 1359 1360 LValue ivarLValue = 1361 EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), ivar, /*quals*/ 0); 1362 Address ivarAddr = ivarLValue.getAddress(*this); 1363 1364 // Currently, all atomic accesses have to be through integer 1365 // types, so there's no point in trying to pick a prettier type. 1366 llvm::Type *bitcastType = 1367 llvm::Type::getIntNTy(getLLVMContext(), 1368 getContext().toBits(strategy.getIvarSize())); 1369 1370 // Cast both arguments to the chosen operation type. 1371 argAddr = Builder.CreateElementBitCast(argAddr, bitcastType); 1372 ivarAddr = Builder.CreateElementBitCast(ivarAddr, bitcastType); 1373 1374 // This bitcast load is likely to cause some nasty IR. 1375 llvm::Value *load = Builder.CreateLoad(argAddr); 1376 1377 // Perform an atomic store. There are no memory ordering requirements. 1378 llvm::StoreInst *store = Builder.CreateStore(load, ivarAddr); 1379 store->setAtomic(llvm::AtomicOrdering::Unordered); 1380 return; 1381 } 1382 1383 case PropertyImplStrategy::GetSetProperty: 1384 case PropertyImplStrategy::SetPropertyAndExpressionGet: { 1385 1386 llvm::FunctionCallee setOptimizedPropertyFn = nullptr; 1387 llvm::FunctionCallee setPropertyFn = nullptr; 1388 if (UseOptimizedSetter(CGM)) { 1389 // 10.8 and iOS 6.0 code and GC is off 1390 setOptimizedPropertyFn = 1391 CGM.getObjCRuntime().GetOptimizedPropertySetFunction( 1392 strategy.isAtomic(), strategy.isCopy()); 1393 if (!setOptimizedPropertyFn) { 1394 CGM.ErrorUnsupported(propImpl, "Obj-C optimized setter - NYI"); 1395 return; 1396 } 1397 } 1398 else { 1399 setPropertyFn = CGM.getObjCRuntime().GetPropertySetFunction(); 1400 if (!setPropertyFn) { 1401 CGM.ErrorUnsupported(propImpl, "Obj-C setter requiring atomic copy"); 1402 return; 1403 } 1404 } 1405 1406 // Emit objc_setProperty((id) self, _cmd, offset, arg, 1407 // <is-atomic>, <is-copy>). 1408 llvm::Value *cmd = 1409 Builder.CreateLoad(GetAddrOfLocalVar(setterMethod->getCmdDecl())); 1410 llvm::Value *self = 1411 Builder.CreateBitCast(LoadObjCSelf(), VoidPtrTy); 1412 llvm::Value *ivarOffset = 1413 EmitIvarOffset(classImpl->getClassInterface(), ivar); 1414 Address argAddr = GetAddrOfLocalVar(*setterMethod->param_begin()); 1415 llvm::Value *arg = Builder.CreateLoad(argAddr, "arg"); 1416 arg = Builder.CreateBitCast(arg, VoidPtrTy); 1417 1418 CallArgList args; 1419 args.add(RValue::get(self), getContext().getObjCIdType()); 1420 args.add(RValue::get(cmd), getContext().getObjCSelType()); 1421 if (setOptimizedPropertyFn) { 1422 args.add(RValue::get(arg), getContext().getObjCIdType()); 1423 args.add(RValue::get(ivarOffset), getContext().getPointerDiffType()); 1424 CGCallee callee = CGCallee::forDirect(setOptimizedPropertyFn); 1425 EmitCall(getTypes().arrangeBuiltinFunctionCall(getContext().VoidTy, args), 1426 callee, ReturnValueSlot(), args); 1427 } else { 1428 args.add(RValue::get(ivarOffset), getContext().getPointerDiffType()); 1429 args.add(RValue::get(arg), getContext().getObjCIdType()); 1430 args.add(RValue::get(Builder.getInt1(strategy.isAtomic())), 1431 getContext().BoolTy); 1432 args.add(RValue::get(Builder.getInt1(strategy.isCopy())), 1433 getContext().BoolTy); 1434 // FIXME: We shouldn't need to get the function info here, the runtime 1435 // already should have computed it to build the function. 1436 CGCallee callee = CGCallee::forDirect(setPropertyFn); 1437 EmitCall(getTypes().arrangeBuiltinFunctionCall(getContext().VoidTy, args), 1438 callee, ReturnValueSlot(), args); 1439 } 1440 1441 return; 1442 } 1443 1444 case PropertyImplStrategy::CopyStruct: 1445 emitStructSetterCall(*this, setterMethod, ivar); 1446 return; 1447 1448 case PropertyImplStrategy::Expression: 1449 break; 1450 } 1451 1452 // Otherwise, fake up some ASTs and emit a normal assignment. 1453 ValueDecl *selfDecl = setterMethod->getSelfDecl(); 1454 DeclRefExpr self(getContext(), selfDecl, false, selfDecl->getType(), 1455 VK_LValue, SourceLocation()); 1456 ImplicitCastExpr selfLoad(ImplicitCastExpr::OnStack, 1457 selfDecl->getType(), CK_LValueToRValue, &self, 1458 VK_RValue); 1459 ObjCIvarRefExpr ivarRef(ivar, ivar->getType().getNonReferenceType(), 1460 SourceLocation(), SourceLocation(), 1461 &selfLoad, true, true); 1462 1463 ParmVarDecl *argDecl = *setterMethod->param_begin(); 1464 QualType argType = argDecl->getType().getNonReferenceType(); 1465 DeclRefExpr arg(getContext(), argDecl, false, argType, VK_LValue, 1466 SourceLocation()); 1467 ImplicitCastExpr argLoad(ImplicitCastExpr::OnStack, 1468 argType.getUnqualifiedType(), CK_LValueToRValue, 1469 &arg, VK_RValue); 1470 1471 // The property type can differ from the ivar type in some situations with 1472 // Objective-C pointer types, we can always bit cast the RHS in these cases. 1473 // The following absurdity is just to ensure well-formed IR. 1474 CastKind argCK = CK_NoOp; 1475 if (ivarRef.getType()->isObjCObjectPointerType()) { 1476 if (argLoad.getType()->isObjCObjectPointerType()) 1477 argCK = CK_BitCast; 1478 else if (argLoad.getType()->isBlockPointerType()) 1479 argCK = CK_BlockPointerToObjCPointerCast; 1480 else 1481 argCK = CK_CPointerToObjCPointerCast; 1482 } else if (ivarRef.getType()->isBlockPointerType()) { 1483 if (argLoad.getType()->isBlockPointerType()) 1484 argCK = CK_BitCast; 1485 else 1486 argCK = CK_AnyPointerToBlockPointerCast; 1487 } else if (ivarRef.getType()->isPointerType()) { 1488 argCK = CK_BitCast; 1489 } 1490 ImplicitCastExpr argCast(ImplicitCastExpr::OnStack, 1491 ivarRef.getType(), argCK, &argLoad, 1492 VK_RValue); 1493 Expr *finalArg = &argLoad; 1494 if (!getContext().hasSameUnqualifiedType(ivarRef.getType(), 1495 argLoad.getType())) 1496 finalArg = &argCast; 1497 1498 1499 BinaryOperator assign(&ivarRef, finalArg, BO_Assign, 1500 ivarRef.getType(), VK_RValue, OK_Ordinary, 1501 SourceLocation(), FPOptions()); 1502 EmitStmt(&assign); 1503 } 1504 1505 /// Generate an Objective-C property setter function. 1506 /// 1507 /// The given Decl must be an ObjCImplementationDecl. \@synthesize 1508 /// is illegal within a category. 1509 void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP, 1510 const ObjCPropertyImplDecl *PID) { 1511 llvm::Constant *AtomicHelperFn = 1512 CodeGenFunction(CGM).GenerateObjCAtomicSetterCopyHelperFunction(PID); 1513 ObjCMethodDecl *OMD = PID->getSetterMethodDecl(); 1514 assert(OMD && "Invalid call to generate setter (empty method)"); 1515 StartObjCMethod(OMD, IMP->getClassInterface()); 1516 1517 generateObjCSetterBody(IMP, PID, AtomicHelperFn); 1518 1519 FinishFunction(OMD->getEndLoc()); 1520 } 1521 1522 namespace { 1523 struct DestroyIvar final : EHScopeStack::Cleanup { 1524 private: 1525 llvm::Value *addr; 1526 const ObjCIvarDecl *ivar; 1527 CodeGenFunction::Destroyer *destroyer; 1528 bool useEHCleanupForArray; 1529 public: 1530 DestroyIvar(llvm::Value *addr, const ObjCIvarDecl *ivar, 1531 CodeGenFunction::Destroyer *destroyer, 1532 bool useEHCleanupForArray) 1533 : addr(addr), ivar(ivar), destroyer(destroyer), 1534 useEHCleanupForArray(useEHCleanupForArray) {} 1535 1536 void Emit(CodeGenFunction &CGF, Flags flags) override { 1537 LValue lvalue 1538 = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), addr, ivar, /*CVR*/ 0); 1539 CGF.emitDestroy(lvalue.getAddress(CGF), ivar->getType(), destroyer, 1540 flags.isForNormalCleanup() && useEHCleanupForArray); 1541 } 1542 }; 1543 } 1544 1545 /// Like CodeGenFunction::destroyARCStrong, but do it with a call. 1546 static void destroyARCStrongWithStore(CodeGenFunction &CGF, 1547 Address addr, 1548 QualType type) { 1549 llvm::Value *null = getNullForVariable(addr); 1550 CGF.EmitARCStoreStrongCall(addr, null, /*ignored*/ true); 1551 } 1552 1553 static void emitCXXDestructMethod(CodeGenFunction &CGF, 1554 ObjCImplementationDecl *impl) { 1555 CodeGenFunction::RunCleanupsScope scope(CGF); 1556 1557 llvm::Value *self = CGF.LoadObjCSelf(); 1558 1559 const ObjCInterfaceDecl *iface = impl->getClassInterface(); 1560 for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin(); 1561 ivar; ivar = ivar->getNextIvar()) { 1562 QualType type = ivar->getType(); 1563 1564 // Check whether the ivar is a destructible type. 1565 QualType::DestructionKind dtorKind = type.isDestructedType(); 1566 if (!dtorKind) continue; 1567 1568 CodeGenFunction::Destroyer *destroyer = nullptr; 1569 1570 // Use a call to objc_storeStrong to destroy strong ivars, for the 1571 // general benefit of the tools. 1572 if (dtorKind == QualType::DK_objc_strong_lifetime) { 1573 destroyer = destroyARCStrongWithStore; 1574 1575 // Otherwise use the default for the destruction kind. 1576 } else { 1577 destroyer = CGF.getDestroyer(dtorKind); 1578 } 1579 1580 CleanupKind cleanupKind = CGF.getCleanupKind(dtorKind); 1581 1582 CGF.EHStack.pushCleanup<DestroyIvar>(cleanupKind, self, ivar, destroyer, 1583 cleanupKind & EHCleanup); 1584 } 1585 1586 assert(scope.requiresCleanups() && "nothing to do in .cxx_destruct?"); 1587 } 1588 1589 void CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP, 1590 ObjCMethodDecl *MD, 1591 bool ctor) { 1592 MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface()); 1593 StartObjCMethod(MD, IMP->getClassInterface()); 1594 1595 // Emit .cxx_construct. 1596 if (ctor) { 1597 // Suppress the final autorelease in ARC. 1598 AutoreleaseResult = false; 1599 1600 for (const auto *IvarInit : IMP->inits()) { 1601 FieldDecl *Field = IvarInit->getAnyMember(); 1602 ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field); 1603 LValue LV = EmitLValueForIvar(TypeOfSelfObject(), 1604 LoadObjCSelf(), Ivar, 0); 1605 EmitAggExpr(IvarInit->getInit(), 1606 AggValueSlot::forLValue(LV, *this, AggValueSlot::IsDestructed, 1607 AggValueSlot::DoesNotNeedGCBarriers, 1608 AggValueSlot::IsNotAliased, 1609 AggValueSlot::DoesNotOverlap)); 1610 } 1611 // constructor returns 'self'. 1612 CodeGenTypes &Types = CGM.getTypes(); 1613 QualType IdTy(CGM.getContext().getObjCIdType()); 1614 llvm::Value *SelfAsId = 1615 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy)); 1616 EmitReturnOfRValue(RValue::get(SelfAsId), IdTy); 1617 1618 // Emit .cxx_destruct. 1619 } else { 1620 emitCXXDestructMethod(*this, IMP); 1621 } 1622 FinishFunction(); 1623 } 1624 1625 llvm::Value *CodeGenFunction::LoadObjCSelf() { 1626 VarDecl *Self = cast<ObjCMethodDecl>(CurFuncDecl)->getSelfDecl(); 1627 DeclRefExpr DRE(getContext(), Self, 1628 /*is enclosing local*/ (CurFuncDecl != CurCodeDecl), 1629 Self->getType(), VK_LValue, SourceLocation()); 1630 return EmitLoadOfScalar(EmitDeclRefLValue(&DRE), SourceLocation()); 1631 } 1632 1633 QualType CodeGenFunction::TypeOfSelfObject() { 1634 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); 1635 ImplicitParamDecl *selfDecl = OMD->getSelfDecl(); 1636 const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>( 1637 getContext().getCanonicalType(selfDecl->getType())); 1638 return PTy->getPointeeType(); 1639 } 1640 1641 void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){ 1642 llvm::FunctionCallee EnumerationMutationFnPtr = 1643 CGM.getObjCRuntime().EnumerationMutationFunction(); 1644 if (!EnumerationMutationFnPtr) { 1645 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime"); 1646 return; 1647 } 1648 CGCallee EnumerationMutationFn = 1649 CGCallee::forDirect(EnumerationMutationFnPtr); 1650 1651 CGDebugInfo *DI = getDebugInfo(); 1652 if (DI) 1653 DI->EmitLexicalBlockStart(Builder, S.getSourceRange().getBegin()); 1654 1655 RunCleanupsScope ForScope(*this); 1656 1657 // The local variable comes into scope immediately. 1658 AutoVarEmission variable = AutoVarEmission::invalid(); 1659 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) 1660 variable = EmitAutoVarAlloca(*cast<VarDecl>(SD->getSingleDecl())); 1661 1662 JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end"); 1663 1664 // Fast enumeration state. 1665 QualType StateTy = CGM.getObjCFastEnumerationStateType(); 1666 Address StatePtr = CreateMemTemp(StateTy, "state.ptr"); 1667 EmitNullInitialization(StatePtr, StateTy); 1668 1669 // Number of elements in the items array. 1670 static const unsigned NumItems = 16; 1671 1672 // Fetch the countByEnumeratingWithState:objects:count: selector. 1673 IdentifierInfo *II[] = { 1674 &CGM.getContext().Idents.get("countByEnumeratingWithState"), 1675 &CGM.getContext().Idents.get("objects"), 1676 &CGM.getContext().Idents.get("count") 1677 }; 1678 Selector FastEnumSel = 1679 CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]); 1680 1681 QualType ItemsTy = 1682 getContext().getConstantArrayType(getContext().getObjCIdType(), 1683 llvm::APInt(32, NumItems), nullptr, 1684 ArrayType::Normal, 0); 1685 Address ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr"); 1686 1687 // Emit the collection pointer. In ARC, we do a retain. 1688 llvm::Value *Collection; 1689 if (getLangOpts().ObjCAutoRefCount) { 1690 Collection = EmitARCRetainScalarExpr(S.getCollection()); 1691 1692 // Enter a cleanup to do the release. 1693 EmitObjCConsumeObject(S.getCollection()->getType(), Collection); 1694 } else { 1695 Collection = EmitScalarExpr(S.getCollection()); 1696 } 1697 1698 // The 'continue' label needs to appear within the cleanup for the 1699 // collection object. 1700 JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next"); 1701 1702 // Send it our message: 1703 CallArgList Args; 1704 1705 // The first argument is a temporary of the enumeration-state type. 1706 Args.add(RValue::get(StatePtr.getPointer()), 1707 getContext().getPointerType(StateTy)); 1708 1709 // The second argument is a temporary array with space for NumItems 1710 // pointers. We'll actually be loading elements from the array 1711 // pointer written into the control state; this buffer is so that 1712 // collections that *aren't* backed by arrays can still queue up 1713 // batches of elements. 1714 Args.add(RValue::get(ItemsPtr.getPointer()), 1715 getContext().getPointerType(ItemsTy)); 1716 1717 // The third argument is the capacity of that temporary array. 1718 llvm::Type *NSUIntegerTy = ConvertType(getContext().getNSUIntegerType()); 1719 llvm::Constant *Count = llvm::ConstantInt::get(NSUIntegerTy, NumItems); 1720 Args.add(RValue::get(Count), getContext().getNSUIntegerType()); 1721 1722 // Start the enumeration. 1723 RValue CountRV = 1724 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), 1725 getContext().getNSUIntegerType(), 1726 FastEnumSel, Collection, Args); 1727 1728 // The initial number of objects that were returned in the buffer. 1729 llvm::Value *initialBufferLimit = CountRV.getScalarVal(); 1730 1731 llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty"); 1732 llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit"); 1733 1734 llvm::Value *zero = llvm::Constant::getNullValue(NSUIntegerTy); 1735 1736 // If the limit pointer was zero to begin with, the collection is 1737 // empty; skip all this. Set the branch weight assuming this has the same 1738 // probability of exiting the loop as any other loop exit. 1739 uint64_t EntryCount = getCurrentProfileCount(); 1740 Builder.CreateCondBr( 1741 Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"), EmptyBB, 1742 LoopInitBB, 1743 createProfileWeights(EntryCount, getProfileCount(S.getBody()))); 1744 1745 // Otherwise, initialize the loop. 1746 EmitBlock(LoopInitBB); 1747 1748 // Save the initial mutations value. This is the value at an 1749 // address that was written into the state object by 1750 // countByEnumeratingWithState:objects:count:. 1751 Address StateMutationsPtrPtr = 1752 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr"); 1753 llvm::Value *StateMutationsPtr 1754 = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr"); 1755 1756 llvm::Value *initialMutations = 1757 Builder.CreateAlignedLoad(StateMutationsPtr, getPointerAlign(), 1758 "forcoll.initial-mutations"); 1759 1760 // Start looping. This is the point we return to whenever we have a 1761 // fresh, non-empty batch of objects. 1762 llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody"); 1763 EmitBlock(LoopBodyBB); 1764 1765 // The current index into the buffer. 1766 llvm::PHINode *index = Builder.CreatePHI(NSUIntegerTy, 3, "forcoll.index"); 1767 index->addIncoming(zero, LoopInitBB); 1768 1769 // The current buffer size. 1770 llvm::PHINode *count = Builder.CreatePHI(NSUIntegerTy, 3, "forcoll.count"); 1771 count->addIncoming(initialBufferLimit, LoopInitBB); 1772 1773 incrementProfileCounter(&S); 1774 1775 // Check whether the mutations value has changed from where it was 1776 // at start. StateMutationsPtr should actually be invariant between 1777 // refreshes. 1778 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr"); 1779 llvm::Value *currentMutations 1780 = Builder.CreateAlignedLoad(StateMutationsPtr, getPointerAlign(), 1781 "statemutations"); 1782 1783 llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated"); 1784 llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcoll.notmutated"); 1785 1786 Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations), 1787 WasNotMutatedBB, WasMutatedBB); 1788 1789 // If so, call the enumeration-mutation function. 1790 EmitBlock(WasMutatedBB); 1791 llvm::Value *V = 1792 Builder.CreateBitCast(Collection, 1793 ConvertType(getContext().getObjCIdType())); 1794 CallArgList Args2; 1795 Args2.add(RValue::get(V), getContext().getObjCIdType()); 1796 // FIXME: We shouldn't need to get the function info here, the runtime already 1797 // should have computed it to build the function. 1798 EmitCall( 1799 CGM.getTypes().arrangeBuiltinFunctionCall(getContext().VoidTy, Args2), 1800 EnumerationMutationFn, ReturnValueSlot(), Args2); 1801 1802 // Otherwise, or if the mutation function returns, just continue. 1803 EmitBlock(WasNotMutatedBB); 1804 1805 // Initialize the element variable. 1806 RunCleanupsScope elementVariableScope(*this); 1807 bool elementIsVariable; 1808 LValue elementLValue; 1809 QualType elementType; 1810 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) { 1811 // Initialize the variable, in case it's a __block variable or something. 1812 EmitAutoVarInit(variable); 1813 1814 const VarDecl *D = cast<VarDecl>(SD->getSingleDecl()); 1815 DeclRefExpr tempDRE(getContext(), const_cast<VarDecl *>(D), false, 1816 D->getType(), VK_LValue, SourceLocation()); 1817 elementLValue = EmitLValue(&tempDRE); 1818 elementType = D->getType(); 1819 elementIsVariable = true; 1820 1821 if (D->isARCPseudoStrong()) 1822 elementLValue.getQuals().setObjCLifetime(Qualifiers::OCL_ExplicitNone); 1823 } else { 1824 elementLValue = LValue(); // suppress warning 1825 elementType = cast<Expr>(S.getElement())->getType(); 1826 elementIsVariable = false; 1827 } 1828 llvm::Type *convertedElementType = ConvertType(elementType); 1829 1830 // Fetch the buffer out of the enumeration state. 1831 // TODO: this pointer should actually be invariant between 1832 // refreshes, which would help us do certain loop optimizations. 1833 Address StateItemsPtr = 1834 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr"); 1835 llvm::Value *EnumStateItems = 1836 Builder.CreateLoad(StateItemsPtr, "stateitems"); 1837 1838 // Fetch the value at the current index from the buffer. 1839 llvm::Value *CurrentItemPtr = 1840 Builder.CreateGEP(EnumStateItems, index, "currentitem.ptr"); 1841 llvm::Value *CurrentItem = 1842 Builder.CreateAlignedLoad(CurrentItemPtr, getPointerAlign()); 1843 1844 // Cast that value to the right type. 1845 CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType, 1846 "currentitem"); 1847 1848 // Make sure we have an l-value. Yes, this gets evaluated every 1849 // time through the loop. 1850 if (!elementIsVariable) { 1851 elementLValue = EmitLValue(cast<Expr>(S.getElement())); 1852 EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue); 1853 } else { 1854 EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue, 1855 /*isInit*/ true); 1856 } 1857 1858 // If we do have an element variable, this assignment is the end of 1859 // its initialization. 1860 if (elementIsVariable) 1861 EmitAutoVarCleanups(variable); 1862 1863 // Perform the loop body, setting up break and continue labels. 1864 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody)); 1865 { 1866 RunCleanupsScope Scope(*this); 1867 EmitStmt(S.getBody()); 1868 } 1869 BreakContinueStack.pop_back(); 1870 1871 // Destroy the element variable now. 1872 elementVariableScope.ForceCleanup(); 1873 1874 // Check whether there are more elements. 1875 EmitBlock(AfterBody.getBlock()); 1876 1877 llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch"); 1878 1879 // First we check in the local buffer. 1880 llvm::Value *indexPlusOne = 1881 Builder.CreateAdd(index, llvm::ConstantInt::get(NSUIntegerTy, 1)); 1882 1883 // If we haven't overrun the buffer yet, we can continue. 1884 // Set the branch weights based on the simplifying assumption that this is 1885 // like a while-loop, i.e., ignoring that the false branch fetches more 1886 // elements and then returns to the loop. 1887 Builder.CreateCondBr( 1888 Builder.CreateICmpULT(indexPlusOne, count), LoopBodyBB, FetchMoreBB, 1889 createProfileWeights(getProfileCount(S.getBody()), EntryCount)); 1890 1891 index->addIncoming(indexPlusOne, AfterBody.getBlock()); 1892 count->addIncoming(count, AfterBody.getBlock()); 1893 1894 // Otherwise, we have to fetch more elements. 1895 EmitBlock(FetchMoreBB); 1896 1897 CountRV = 1898 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), 1899 getContext().getNSUIntegerType(), 1900 FastEnumSel, Collection, Args); 1901 1902 // If we got a zero count, we're done. 1903 llvm::Value *refetchCount = CountRV.getScalarVal(); 1904 1905 // (note that the message send might split FetchMoreBB) 1906 index->addIncoming(zero, Builder.GetInsertBlock()); 1907 count->addIncoming(refetchCount, Builder.GetInsertBlock()); 1908 1909 Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero), 1910 EmptyBB, LoopBodyBB); 1911 1912 // No more elements. 1913 EmitBlock(EmptyBB); 1914 1915 if (!elementIsVariable) { 1916 // If the element was not a declaration, set it to be null. 1917 1918 llvm::Value *null = llvm::Constant::getNullValue(convertedElementType); 1919 elementLValue = EmitLValue(cast<Expr>(S.getElement())); 1920 EmitStoreThroughLValue(RValue::get(null), elementLValue); 1921 } 1922 1923 if (DI) 1924 DI->EmitLexicalBlockEnd(Builder, S.getSourceRange().getEnd()); 1925 1926 ForScope.ForceCleanup(); 1927 EmitBlock(LoopEnd.getBlock()); 1928 } 1929 1930 void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) { 1931 CGM.getObjCRuntime().EmitTryStmt(*this, S); 1932 } 1933 1934 void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) { 1935 CGM.getObjCRuntime().EmitThrowStmt(*this, S); 1936 } 1937 1938 void CodeGenFunction::EmitObjCAtSynchronizedStmt( 1939 const ObjCAtSynchronizedStmt &S) { 1940 CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S); 1941 } 1942 1943 namespace { 1944 struct CallObjCRelease final : EHScopeStack::Cleanup { 1945 CallObjCRelease(llvm::Value *object) : object(object) {} 1946 llvm::Value *object; 1947 1948 void Emit(CodeGenFunction &CGF, Flags flags) override { 1949 // Releases at the end of the full-expression are imprecise. 1950 CGF.EmitARCRelease(object, ARCImpreciseLifetime); 1951 } 1952 }; 1953 } 1954 1955 /// Produce the code for a CK_ARCConsumeObject. Does a primitive 1956 /// release at the end of the full-expression. 1957 llvm::Value *CodeGenFunction::EmitObjCConsumeObject(QualType type, 1958 llvm::Value *object) { 1959 // If we're in a conditional branch, we need to make the cleanup 1960 // conditional. 1961 pushFullExprCleanup<CallObjCRelease>(getARCCleanupKind(), object); 1962 return object; 1963 } 1964 1965 llvm::Value *CodeGenFunction::EmitObjCExtendObjectLifetime(QualType type, 1966 llvm::Value *value) { 1967 return EmitARCRetainAutorelease(type, value); 1968 } 1969 1970 /// Given a number of pointers, inform the optimizer that they're 1971 /// being intrinsically used up until this point in the program. 1972 void CodeGenFunction::EmitARCIntrinsicUse(ArrayRef<llvm::Value*> values) { 1973 llvm::Function *&fn = CGM.getObjCEntrypoints().clang_arc_use; 1974 if (!fn) 1975 fn = CGM.getIntrinsic(llvm::Intrinsic::objc_clang_arc_use); 1976 1977 // This isn't really a "runtime" function, but as an intrinsic it 1978 // doesn't really matter as long as we align things up. 1979 EmitNounwindRuntimeCall(fn, values); 1980 } 1981 1982 static void setARCRuntimeFunctionLinkage(CodeGenModule &CGM, llvm::Value *RTF) { 1983 if (auto *F = dyn_cast<llvm::Function>(RTF)) { 1984 // If the target runtime doesn't naturally support ARC, emit weak 1985 // references to the runtime support library. We don't really 1986 // permit this to fail, but we need a particular relocation style. 1987 if (!CGM.getLangOpts().ObjCRuntime.hasNativeARC() && 1988 !CGM.getTriple().isOSBinFormatCOFF()) { 1989 F->setLinkage(llvm::Function::ExternalWeakLinkage); 1990 } 1991 } 1992 } 1993 1994 static void setARCRuntimeFunctionLinkage(CodeGenModule &CGM, 1995 llvm::FunctionCallee RTF) { 1996 setARCRuntimeFunctionLinkage(CGM, RTF.getCallee()); 1997 } 1998 1999 /// Perform an operation having the signature 2000 /// i8* (i8*) 2001 /// where a null input causes a no-op and returns null. 2002 static llvm::Value *emitARCValueOperation( 2003 CodeGenFunction &CGF, llvm::Value *value, llvm::Type *returnType, 2004 llvm::Function *&fn, llvm::Intrinsic::ID IntID, 2005 llvm::CallInst::TailCallKind tailKind = llvm::CallInst::TCK_None) { 2006 if (isa<llvm::ConstantPointerNull>(value)) 2007 return value; 2008 2009 if (!fn) { 2010 fn = CGF.CGM.getIntrinsic(IntID); 2011 setARCRuntimeFunctionLinkage(CGF.CGM, fn); 2012 } 2013 2014 // Cast the argument to 'id'. 2015 llvm::Type *origType = returnType ? returnType : value->getType(); 2016 value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy); 2017 2018 // Call the function. 2019 llvm::CallInst *call = CGF.EmitNounwindRuntimeCall(fn, value); 2020 call->setTailCallKind(tailKind); 2021 2022 // Cast the result back to the original type. 2023 return CGF.Builder.CreateBitCast(call, origType); 2024 } 2025 2026 /// Perform an operation having the following signature: 2027 /// i8* (i8**) 2028 static llvm::Value *emitARCLoadOperation(CodeGenFunction &CGF, Address addr, 2029 llvm::Function *&fn, 2030 llvm::Intrinsic::ID IntID) { 2031 if (!fn) { 2032 fn = CGF.CGM.getIntrinsic(IntID); 2033 setARCRuntimeFunctionLinkage(CGF.CGM, fn); 2034 } 2035 2036 // Cast the argument to 'id*'. 2037 llvm::Type *origType = addr.getElementType(); 2038 addr = CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy); 2039 2040 // Call the function. 2041 llvm::Value *result = CGF.EmitNounwindRuntimeCall(fn, addr.getPointer()); 2042 2043 // Cast the result back to a dereference of the original type. 2044 if (origType != CGF.Int8PtrTy) 2045 result = CGF.Builder.CreateBitCast(result, origType); 2046 2047 return result; 2048 } 2049 2050 /// Perform an operation having the following signature: 2051 /// i8* (i8**, i8*) 2052 static llvm::Value *emitARCStoreOperation(CodeGenFunction &CGF, Address addr, 2053 llvm::Value *value, 2054 llvm::Function *&fn, 2055 llvm::Intrinsic::ID IntID, 2056 bool ignored) { 2057 assert(addr.getElementType() == value->getType()); 2058 2059 if (!fn) { 2060 fn = CGF.CGM.getIntrinsic(IntID); 2061 setARCRuntimeFunctionLinkage(CGF.CGM, fn); 2062 } 2063 2064 llvm::Type *origType = value->getType(); 2065 2066 llvm::Value *args[] = { 2067 CGF.Builder.CreateBitCast(addr.getPointer(), CGF.Int8PtrPtrTy), 2068 CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy) 2069 }; 2070 llvm::CallInst *result = CGF.EmitNounwindRuntimeCall(fn, args); 2071 2072 if (ignored) return nullptr; 2073 2074 return CGF.Builder.CreateBitCast(result, origType); 2075 } 2076 2077 /// Perform an operation having the following signature: 2078 /// void (i8**, i8**) 2079 static void emitARCCopyOperation(CodeGenFunction &CGF, Address dst, Address src, 2080 llvm::Function *&fn, 2081 llvm::Intrinsic::ID IntID) { 2082 assert(dst.getType() == src.getType()); 2083 2084 if (!fn) { 2085 fn = CGF.CGM.getIntrinsic(IntID); 2086 setARCRuntimeFunctionLinkage(CGF.CGM, fn); 2087 } 2088 2089 llvm::Value *args[] = { 2090 CGF.Builder.CreateBitCast(dst.getPointer(), CGF.Int8PtrPtrTy), 2091 CGF.Builder.CreateBitCast(src.getPointer(), CGF.Int8PtrPtrTy) 2092 }; 2093 CGF.EmitNounwindRuntimeCall(fn, args); 2094 } 2095 2096 /// Perform an operation having the signature 2097 /// i8* (i8*) 2098 /// where a null input causes a no-op and returns null. 2099 static llvm::Value *emitObjCValueOperation(CodeGenFunction &CGF, 2100 llvm::Value *value, 2101 llvm::Type *returnType, 2102 llvm::FunctionCallee &fn, 2103 StringRef fnName) { 2104 if (isa<llvm::ConstantPointerNull>(value)) 2105 return value; 2106 2107 if (!fn) { 2108 llvm::FunctionType *fnType = 2109 llvm::FunctionType::get(CGF.Int8PtrTy, CGF.Int8PtrTy, false); 2110 fn = CGF.CGM.CreateRuntimeFunction(fnType, fnName); 2111 2112 // We have Native ARC, so set nonlazybind attribute for performance 2113 if (llvm::Function *f = dyn_cast<llvm::Function>(fn.getCallee())) 2114 if (fnName == "objc_retain") 2115 f->addFnAttr(llvm::Attribute::NonLazyBind); 2116 } 2117 2118 // Cast the argument to 'id'. 2119 llvm::Type *origType = returnType ? returnType : value->getType(); 2120 value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy); 2121 2122 // Call the function. 2123 llvm::CallBase *Inst = CGF.EmitCallOrInvoke(fn, value); 2124 2125 // Cast the result back to the original type. 2126 return CGF.Builder.CreateBitCast(Inst, origType); 2127 } 2128 2129 /// Produce the code to do a retain. Based on the type, calls one of: 2130 /// call i8* \@objc_retain(i8* %value) 2131 /// call i8* \@objc_retainBlock(i8* %value) 2132 llvm::Value *CodeGenFunction::EmitARCRetain(QualType type, llvm::Value *value) { 2133 if (type->isBlockPointerType()) 2134 return EmitARCRetainBlock(value, /*mandatory*/ false); 2135 else 2136 return EmitARCRetainNonBlock(value); 2137 } 2138 2139 /// Retain the given object, with normal retain semantics. 2140 /// call i8* \@objc_retain(i8* %value) 2141 llvm::Value *CodeGenFunction::EmitARCRetainNonBlock(llvm::Value *value) { 2142 return emitARCValueOperation(*this, value, nullptr, 2143 CGM.getObjCEntrypoints().objc_retain, 2144 llvm::Intrinsic::objc_retain); 2145 } 2146 2147 /// Retain the given block, with _Block_copy semantics. 2148 /// call i8* \@objc_retainBlock(i8* %value) 2149 /// 2150 /// \param mandatory - If false, emit the call with metadata 2151 /// indicating that it's okay for the optimizer to eliminate this call 2152 /// if it can prove that the block never escapes except down the stack. 2153 llvm::Value *CodeGenFunction::EmitARCRetainBlock(llvm::Value *value, 2154 bool mandatory) { 2155 llvm::Value *result 2156 = emitARCValueOperation(*this, value, nullptr, 2157 CGM.getObjCEntrypoints().objc_retainBlock, 2158 llvm::Intrinsic::objc_retainBlock); 2159 2160 // If the copy isn't mandatory, add !clang.arc.copy_on_escape to 2161 // tell the optimizer that it doesn't need to do this copy if the 2162 // block doesn't escape, where being passed as an argument doesn't 2163 // count as escaping. 2164 if (!mandatory && isa<llvm::Instruction>(result)) { 2165 llvm::CallInst *call 2166 = cast<llvm::CallInst>(result->stripPointerCasts()); 2167 assert(call->getCalledValue() == CGM.getObjCEntrypoints().objc_retainBlock); 2168 2169 call->setMetadata("clang.arc.copy_on_escape", 2170 llvm::MDNode::get(Builder.getContext(), None)); 2171 } 2172 2173 return result; 2174 } 2175 2176 static void emitAutoreleasedReturnValueMarker(CodeGenFunction &CGF) { 2177 // Fetch the void(void) inline asm which marks that we're going to 2178 // do something with the autoreleased return value. 2179 llvm::InlineAsm *&marker 2180 = CGF.CGM.getObjCEntrypoints().retainAutoreleasedReturnValueMarker; 2181 if (!marker) { 2182 StringRef assembly 2183 = CGF.CGM.getTargetCodeGenInfo() 2184 .getARCRetainAutoreleasedReturnValueMarker(); 2185 2186 // If we have an empty assembly string, there's nothing to do. 2187 if (assembly.empty()) { 2188 2189 // Otherwise, at -O0, build an inline asm that we're going to call 2190 // in a moment. 2191 } else if (CGF.CGM.getCodeGenOpts().OptimizationLevel == 0) { 2192 llvm::FunctionType *type = 2193 llvm::FunctionType::get(CGF.VoidTy, /*variadic*/false); 2194 2195 marker = llvm::InlineAsm::get(type, assembly, "", /*sideeffects*/ true); 2196 2197 // If we're at -O1 and above, we don't want to litter the code 2198 // with this marker yet, so leave a breadcrumb for the ARC 2199 // optimizer to pick up. 2200 } else { 2201 const char *markerKey = "clang.arc.retainAutoreleasedReturnValueMarker"; 2202 if (!CGF.CGM.getModule().getModuleFlag(markerKey)) { 2203 auto *str = llvm::MDString::get(CGF.getLLVMContext(), assembly); 2204 CGF.CGM.getModule().addModuleFlag(llvm::Module::Error, markerKey, str); 2205 } 2206 } 2207 } 2208 2209 // Call the marker asm if we made one, which we do only at -O0. 2210 if (marker) 2211 CGF.Builder.CreateCall(marker, None, CGF.getBundlesForFunclet(marker)); 2212 } 2213 2214 /// Retain the given object which is the result of a function call. 2215 /// call i8* \@objc_retainAutoreleasedReturnValue(i8* %value) 2216 /// 2217 /// Yes, this function name is one character away from a different 2218 /// call with completely different semantics. 2219 llvm::Value * 2220 CodeGenFunction::EmitARCRetainAutoreleasedReturnValue(llvm::Value *value) { 2221 emitAutoreleasedReturnValueMarker(*this); 2222 llvm::CallInst::TailCallKind tailKind = 2223 CGM.getTargetCodeGenInfo() 2224 .shouldSuppressTailCallsOfRetainAutoreleasedReturnValue() 2225 ? llvm::CallInst::TCK_NoTail 2226 : llvm::CallInst::TCK_None; 2227 return emitARCValueOperation( 2228 *this, value, nullptr, 2229 CGM.getObjCEntrypoints().objc_retainAutoreleasedReturnValue, 2230 llvm::Intrinsic::objc_retainAutoreleasedReturnValue, tailKind); 2231 } 2232 2233 /// Claim a possibly-autoreleased return value at +0. This is only 2234 /// valid to do in contexts which do not rely on the retain to keep 2235 /// the object valid for all of its uses; for example, when 2236 /// the value is ignored, or when it is being assigned to an 2237 /// __unsafe_unretained variable. 2238 /// 2239 /// call i8* \@objc_unsafeClaimAutoreleasedReturnValue(i8* %value) 2240 llvm::Value * 2241 CodeGenFunction::EmitARCUnsafeClaimAutoreleasedReturnValue(llvm::Value *value) { 2242 emitAutoreleasedReturnValueMarker(*this); 2243 return emitARCValueOperation(*this, value, nullptr, 2244 CGM.getObjCEntrypoints().objc_unsafeClaimAutoreleasedReturnValue, 2245 llvm::Intrinsic::objc_unsafeClaimAutoreleasedReturnValue); 2246 } 2247 2248 /// Release the given object. 2249 /// call void \@objc_release(i8* %value) 2250 void CodeGenFunction::EmitARCRelease(llvm::Value *value, 2251 ARCPreciseLifetime_t precise) { 2252 if (isa<llvm::ConstantPointerNull>(value)) return; 2253 2254 llvm::Function *&fn = CGM.getObjCEntrypoints().objc_release; 2255 if (!fn) { 2256 fn = CGM.getIntrinsic(llvm::Intrinsic::objc_release); 2257 setARCRuntimeFunctionLinkage(CGM, fn); 2258 } 2259 2260 // Cast the argument to 'id'. 2261 value = Builder.CreateBitCast(value, Int8PtrTy); 2262 2263 // Call objc_release. 2264 llvm::CallInst *call = EmitNounwindRuntimeCall(fn, value); 2265 2266 if (precise == ARCImpreciseLifetime) { 2267 call->setMetadata("clang.imprecise_release", 2268 llvm::MDNode::get(Builder.getContext(), None)); 2269 } 2270 } 2271 2272 /// Destroy a __strong variable. 2273 /// 2274 /// At -O0, emit a call to store 'null' into the address; 2275 /// instrumenting tools prefer this because the address is exposed, 2276 /// but it's relatively cumbersome to optimize. 2277 /// 2278 /// At -O1 and above, just load and call objc_release. 2279 /// 2280 /// call void \@objc_storeStrong(i8** %addr, i8* null) 2281 void CodeGenFunction::EmitARCDestroyStrong(Address addr, 2282 ARCPreciseLifetime_t precise) { 2283 if (CGM.getCodeGenOpts().OptimizationLevel == 0) { 2284 llvm::Value *null = getNullForVariable(addr); 2285 EmitARCStoreStrongCall(addr, null, /*ignored*/ true); 2286 return; 2287 } 2288 2289 llvm::Value *value = Builder.CreateLoad(addr); 2290 EmitARCRelease(value, precise); 2291 } 2292 2293 /// Store into a strong object. Always calls this: 2294 /// call void \@objc_storeStrong(i8** %addr, i8* %value) 2295 llvm::Value *CodeGenFunction::EmitARCStoreStrongCall(Address addr, 2296 llvm::Value *value, 2297 bool ignored) { 2298 assert(addr.getElementType() == value->getType()); 2299 2300 llvm::Function *&fn = CGM.getObjCEntrypoints().objc_storeStrong; 2301 if (!fn) { 2302 fn = CGM.getIntrinsic(llvm::Intrinsic::objc_storeStrong); 2303 setARCRuntimeFunctionLinkage(CGM, fn); 2304 } 2305 2306 llvm::Value *args[] = { 2307 Builder.CreateBitCast(addr.getPointer(), Int8PtrPtrTy), 2308 Builder.CreateBitCast(value, Int8PtrTy) 2309 }; 2310 EmitNounwindRuntimeCall(fn, args); 2311 2312 if (ignored) return nullptr; 2313 return value; 2314 } 2315 2316 /// Store into a strong object. Sometimes calls this: 2317 /// call void \@objc_storeStrong(i8** %addr, i8* %value) 2318 /// Other times, breaks it down into components. 2319 llvm::Value *CodeGenFunction::EmitARCStoreStrong(LValue dst, 2320 llvm::Value *newValue, 2321 bool ignored) { 2322 QualType type = dst.getType(); 2323 bool isBlock = type->isBlockPointerType(); 2324 2325 // Use a store barrier at -O0 unless this is a block type or the 2326 // lvalue is inadequately aligned. 2327 if (shouldUseFusedARCCalls() && 2328 !isBlock && 2329 (dst.getAlignment().isZero() || 2330 dst.getAlignment() >= CharUnits::fromQuantity(PointerAlignInBytes))) { 2331 return EmitARCStoreStrongCall(dst.getAddress(*this), newValue, ignored); 2332 } 2333 2334 // Otherwise, split it out. 2335 2336 // Retain the new value. 2337 newValue = EmitARCRetain(type, newValue); 2338 2339 // Read the old value. 2340 llvm::Value *oldValue = EmitLoadOfScalar(dst, SourceLocation()); 2341 2342 // Store. We do this before the release so that any deallocs won't 2343 // see the old value. 2344 EmitStoreOfScalar(newValue, dst); 2345 2346 // Finally, release the old value. 2347 EmitARCRelease(oldValue, dst.isARCPreciseLifetime()); 2348 2349 return newValue; 2350 } 2351 2352 /// Autorelease the given object. 2353 /// call i8* \@objc_autorelease(i8* %value) 2354 llvm::Value *CodeGenFunction::EmitARCAutorelease(llvm::Value *value) { 2355 return emitARCValueOperation(*this, value, nullptr, 2356 CGM.getObjCEntrypoints().objc_autorelease, 2357 llvm::Intrinsic::objc_autorelease); 2358 } 2359 2360 /// Autorelease the given object. 2361 /// call i8* \@objc_autoreleaseReturnValue(i8* %value) 2362 llvm::Value * 2363 CodeGenFunction::EmitARCAutoreleaseReturnValue(llvm::Value *value) { 2364 return emitARCValueOperation(*this, value, nullptr, 2365 CGM.getObjCEntrypoints().objc_autoreleaseReturnValue, 2366 llvm::Intrinsic::objc_autoreleaseReturnValue, 2367 llvm::CallInst::TCK_Tail); 2368 } 2369 2370 /// Do a fused retain/autorelease of the given object. 2371 /// call i8* \@objc_retainAutoreleaseReturnValue(i8* %value) 2372 llvm::Value * 2373 CodeGenFunction::EmitARCRetainAutoreleaseReturnValue(llvm::Value *value) { 2374 return emitARCValueOperation(*this, value, nullptr, 2375 CGM.getObjCEntrypoints().objc_retainAutoreleaseReturnValue, 2376 llvm::Intrinsic::objc_retainAutoreleaseReturnValue, 2377 llvm::CallInst::TCK_Tail); 2378 } 2379 2380 /// Do a fused retain/autorelease of the given object. 2381 /// call i8* \@objc_retainAutorelease(i8* %value) 2382 /// or 2383 /// %retain = call i8* \@objc_retainBlock(i8* %value) 2384 /// call i8* \@objc_autorelease(i8* %retain) 2385 llvm::Value *CodeGenFunction::EmitARCRetainAutorelease(QualType type, 2386 llvm::Value *value) { 2387 if (!type->isBlockPointerType()) 2388 return EmitARCRetainAutoreleaseNonBlock(value); 2389 2390 if (isa<llvm::ConstantPointerNull>(value)) return value; 2391 2392 llvm::Type *origType = value->getType(); 2393 value = Builder.CreateBitCast(value, Int8PtrTy); 2394 value = EmitARCRetainBlock(value, /*mandatory*/ true); 2395 value = EmitARCAutorelease(value); 2396 return Builder.CreateBitCast(value, origType); 2397 } 2398 2399 /// Do a fused retain/autorelease of the given object. 2400 /// call i8* \@objc_retainAutorelease(i8* %value) 2401 llvm::Value * 2402 CodeGenFunction::EmitARCRetainAutoreleaseNonBlock(llvm::Value *value) { 2403 return emitARCValueOperation(*this, value, nullptr, 2404 CGM.getObjCEntrypoints().objc_retainAutorelease, 2405 llvm::Intrinsic::objc_retainAutorelease); 2406 } 2407 2408 /// i8* \@objc_loadWeak(i8** %addr) 2409 /// Essentially objc_autorelease(objc_loadWeakRetained(addr)). 2410 llvm::Value *CodeGenFunction::EmitARCLoadWeak(Address addr) { 2411 return emitARCLoadOperation(*this, addr, 2412 CGM.getObjCEntrypoints().objc_loadWeak, 2413 llvm::Intrinsic::objc_loadWeak); 2414 } 2415 2416 /// i8* \@objc_loadWeakRetained(i8** %addr) 2417 llvm::Value *CodeGenFunction::EmitARCLoadWeakRetained(Address addr) { 2418 return emitARCLoadOperation(*this, addr, 2419 CGM.getObjCEntrypoints().objc_loadWeakRetained, 2420 llvm::Intrinsic::objc_loadWeakRetained); 2421 } 2422 2423 /// i8* \@objc_storeWeak(i8** %addr, i8* %value) 2424 /// Returns %value. 2425 llvm::Value *CodeGenFunction::EmitARCStoreWeak(Address addr, 2426 llvm::Value *value, 2427 bool ignored) { 2428 return emitARCStoreOperation(*this, addr, value, 2429 CGM.getObjCEntrypoints().objc_storeWeak, 2430 llvm::Intrinsic::objc_storeWeak, ignored); 2431 } 2432 2433 /// i8* \@objc_initWeak(i8** %addr, i8* %value) 2434 /// Returns %value. %addr is known to not have a current weak entry. 2435 /// Essentially equivalent to: 2436 /// *addr = nil; objc_storeWeak(addr, value); 2437 void CodeGenFunction::EmitARCInitWeak(Address addr, llvm::Value *value) { 2438 // If we're initializing to null, just write null to memory; no need 2439 // to get the runtime involved. But don't do this if optimization 2440 // is enabled, because accounting for this would make the optimizer 2441 // much more complicated. 2442 if (isa<llvm::ConstantPointerNull>(value) && 2443 CGM.getCodeGenOpts().OptimizationLevel == 0) { 2444 Builder.CreateStore(value, addr); 2445 return; 2446 } 2447 2448 emitARCStoreOperation(*this, addr, value, 2449 CGM.getObjCEntrypoints().objc_initWeak, 2450 llvm::Intrinsic::objc_initWeak, /*ignored*/ true); 2451 } 2452 2453 /// void \@objc_destroyWeak(i8** %addr) 2454 /// Essentially objc_storeWeak(addr, nil). 2455 void CodeGenFunction::EmitARCDestroyWeak(Address addr) { 2456 llvm::Function *&fn = CGM.getObjCEntrypoints().objc_destroyWeak; 2457 if (!fn) { 2458 fn = CGM.getIntrinsic(llvm::Intrinsic::objc_destroyWeak); 2459 setARCRuntimeFunctionLinkage(CGM, fn); 2460 } 2461 2462 // Cast the argument to 'id*'. 2463 addr = Builder.CreateBitCast(addr, Int8PtrPtrTy); 2464 2465 EmitNounwindRuntimeCall(fn, addr.getPointer()); 2466 } 2467 2468 /// void \@objc_moveWeak(i8** %dest, i8** %src) 2469 /// Disregards the current value in %dest. Leaves %src pointing to nothing. 2470 /// Essentially (objc_copyWeak(dest, src), objc_destroyWeak(src)). 2471 void CodeGenFunction::EmitARCMoveWeak(Address dst, Address src) { 2472 emitARCCopyOperation(*this, dst, src, 2473 CGM.getObjCEntrypoints().objc_moveWeak, 2474 llvm::Intrinsic::objc_moveWeak); 2475 } 2476 2477 /// void \@objc_copyWeak(i8** %dest, i8** %src) 2478 /// Disregards the current value in %dest. Essentially 2479 /// objc_release(objc_initWeak(dest, objc_readWeakRetained(src))) 2480 void CodeGenFunction::EmitARCCopyWeak(Address dst, Address src) { 2481 emitARCCopyOperation(*this, dst, src, 2482 CGM.getObjCEntrypoints().objc_copyWeak, 2483 llvm::Intrinsic::objc_copyWeak); 2484 } 2485 2486 void CodeGenFunction::emitARCCopyAssignWeak(QualType Ty, Address DstAddr, 2487 Address SrcAddr) { 2488 llvm::Value *Object = EmitARCLoadWeakRetained(SrcAddr); 2489 Object = EmitObjCConsumeObject(Ty, Object); 2490 EmitARCStoreWeak(DstAddr, Object, false); 2491 } 2492 2493 void CodeGenFunction::emitARCMoveAssignWeak(QualType Ty, Address DstAddr, 2494 Address SrcAddr) { 2495 llvm::Value *Object = EmitARCLoadWeakRetained(SrcAddr); 2496 Object = EmitObjCConsumeObject(Ty, Object); 2497 EmitARCStoreWeak(DstAddr, Object, false); 2498 EmitARCDestroyWeak(SrcAddr); 2499 } 2500 2501 /// Produce the code to do a objc_autoreleasepool_push. 2502 /// call i8* \@objc_autoreleasePoolPush(void) 2503 llvm::Value *CodeGenFunction::EmitObjCAutoreleasePoolPush() { 2504 llvm::Function *&fn = CGM.getObjCEntrypoints().objc_autoreleasePoolPush; 2505 if (!fn) { 2506 fn = CGM.getIntrinsic(llvm::Intrinsic::objc_autoreleasePoolPush); 2507 setARCRuntimeFunctionLinkage(CGM, fn); 2508 } 2509 2510 return EmitNounwindRuntimeCall(fn); 2511 } 2512 2513 /// Produce the code to do a primitive release. 2514 /// call void \@objc_autoreleasePoolPop(i8* %ptr) 2515 void CodeGenFunction::EmitObjCAutoreleasePoolPop(llvm::Value *value) { 2516 assert(value->getType() == Int8PtrTy); 2517 2518 if (getInvokeDest()) { 2519 // Call the runtime method not the intrinsic if we are handling exceptions 2520 llvm::FunctionCallee &fn = 2521 CGM.getObjCEntrypoints().objc_autoreleasePoolPopInvoke; 2522 if (!fn) { 2523 llvm::FunctionType *fnType = 2524 llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false); 2525 fn = CGM.CreateRuntimeFunction(fnType, "objc_autoreleasePoolPop"); 2526 setARCRuntimeFunctionLinkage(CGM, fn); 2527 } 2528 2529 // objc_autoreleasePoolPop can throw. 2530 EmitRuntimeCallOrInvoke(fn, value); 2531 } else { 2532 llvm::FunctionCallee &fn = CGM.getObjCEntrypoints().objc_autoreleasePoolPop; 2533 if (!fn) { 2534 fn = CGM.getIntrinsic(llvm::Intrinsic::objc_autoreleasePoolPop); 2535 setARCRuntimeFunctionLinkage(CGM, fn); 2536 } 2537 2538 EmitRuntimeCall(fn, value); 2539 } 2540 } 2541 2542 /// Produce the code to do an MRR version objc_autoreleasepool_push. 2543 /// Which is: [[NSAutoreleasePool alloc] init]; 2544 /// Where alloc is declared as: + (id) alloc; in NSAutoreleasePool class. 2545 /// init is declared as: - (id) init; in its NSObject super class. 2546 /// 2547 llvm::Value *CodeGenFunction::EmitObjCMRRAutoreleasePoolPush() { 2548 CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 2549 llvm::Value *Receiver = Runtime.EmitNSAutoreleasePoolClassRef(*this); 2550 // [NSAutoreleasePool alloc] 2551 IdentifierInfo *II = &CGM.getContext().Idents.get("alloc"); 2552 Selector AllocSel = getContext().Selectors.getSelector(0, &II); 2553 CallArgList Args; 2554 RValue AllocRV = 2555 Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 2556 getContext().getObjCIdType(), 2557 AllocSel, Receiver, Args); 2558 2559 // [Receiver init] 2560 Receiver = AllocRV.getScalarVal(); 2561 II = &CGM.getContext().Idents.get("init"); 2562 Selector InitSel = getContext().Selectors.getSelector(0, &II); 2563 RValue InitRV = 2564 Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 2565 getContext().getObjCIdType(), 2566 InitSel, Receiver, Args); 2567 return InitRV.getScalarVal(); 2568 } 2569 2570 /// Allocate the given objc object. 2571 /// call i8* \@objc_alloc(i8* %value) 2572 llvm::Value *CodeGenFunction::EmitObjCAlloc(llvm::Value *value, 2573 llvm::Type *resultType) { 2574 return emitObjCValueOperation(*this, value, resultType, 2575 CGM.getObjCEntrypoints().objc_alloc, 2576 "objc_alloc"); 2577 } 2578 2579 /// Allocate the given objc object. 2580 /// call i8* \@objc_allocWithZone(i8* %value) 2581 llvm::Value *CodeGenFunction::EmitObjCAllocWithZone(llvm::Value *value, 2582 llvm::Type *resultType) { 2583 return emitObjCValueOperation(*this, value, resultType, 2584 CGM.getObjCEntrypoints().objc_allocWithZone, 2585 "objc_allocWithZone"); 2586 } 2587 2588 llvm::Value *CodeGenFunction::EmitObjCAllocInit(llvm::Value *value, 2589 llvm::Type *resultType) { 2590 return emitObjCValueOperation(*this, value, resultType, 2591 CGM.getObjCEntrypoints().objc_alloc_init, 2592 "objc_alloc_init"); 2593 } 2594 2595 /// Produce the code to do a primitive release. 2596 /// [tmp drain]; 2597 void CodeGenFunction::EmitObjCMRRAutoreleasePoolPop(llvm::Value *Arg) { 2598 IdentifierInfo *II = &CGM.getContext().Idents.get("drain"); 2599 Selector DrainSel = getContext().Selectors.getSelector(0, &II); 2600 CallArgList Args; 2601 CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), 2602 getContext().VoidTy, DrainSel, Arg, Args); 2603 } 2604 2605 void CodeGenFunction::destroyARCStrongPrecise(CodeGenFunction &CGF, 2606 Address addr, 2607 QualType type) { 2608 CGF.EmitARCDestroyStrong(addr, ARCPreciseLifetime); 2609 } 2610 2611 void CodeGenFunction::destroyARCStrongImprecise(CodeGenFunction &CGF, 2612 Address addr, 2613 QualType type) { 2614 CGF.EmitARCDestroyStrong(addr, ARCImpreciseLifetime); 2615 } 2616 2617 void CodeGenFunction::destroyARCWeak(CodeGenFunction &CGF, 2618 Address addr, 2619 QualType type) { 2620 CGF.EmitARCDestroyWeak(addr); 2621 } 2622 2623 void CodeGenFunction::emitARCIntrinsicUse(CodeGenFunction &CGF, Address addr, 2624 QualType type) { 2625 llvm::Value *value = CGF.Builder.CreateLoad(addr); 2626 CGF.EmitARCIntrinsicUse(value); 2627 } 2628 2629 /// Autorelease the given object. 2630 /// call i8* \@objc_autorelease(i8* %value) 2631 llvm::Value *CodeGenFunction::EmitObjCAutorelease(llvm::Value *value, 2632 llvm::Type *returnType) { 2633 return emitObjCValueOperation( 2634 *this, value, returnType, 2635 CGM.getObjCEntrypoints().objc_autoreleaseRuntimeFunction, 2636 "objc_autorelease"); 2637 } 2638 2639 /// Retain the given object, with normal retain semantics. 2640 /// call i8* \@objc_retain(i8* %value) 2641 llvm::Value *CodeGenFunction::EmitObjCRetainNonBlock(llvm::Value *value, 2642 llvm::Type *returnType) { 2643 return emitObjCValueOperation( 2644 *this, value, returnType, 2645 CGM.getObjCEntrypoints().objc_retainRuntimeFunction, "objc_retain"); 2646 } 2647 2648 /// Release the given object. 2649 /// call void \@objc_release(i8* %value) 2650 void CodeGenFunction::EmitObjCRelease(llvm::Value *value, 2651 ARCPreciseLifetime_t precise) { 2652 if (isa<llvm::ConstantPointerNull>(value)) return; 2653 2654 llvm::FunctionCallee &fn = 2655 CGM.getObjCEntrypoints().objc_releaseRuntimeFunction; 2656 if (!fn) { 2657 llvm::FunctionType *fnType = 2658 llvm::FunctionType::get(Builder.getVoidTy(), Int8PtrTy, false); 2659 fn = CGM.CreateRuntimeFunction(fnType, "objc_release"); 2660 setARCRuntimeFunctionLinkage(CGM, fn); 2661 // We have Native ARC, so set nonlazybind attribute for performance 2662 if (llvm::Function *f = dyn_cast<llvm::Function>(fn.getCallee())) 2663 f->addFnAttr(llvm::Attribute::NonLazyBind); 2664 } 2665 2666 // Cast the argument to 'id'. 2667 value = Builder.CreateBitCast(value, Int8PtrTy); 2668 2669 // Call objc_release. 2670 llvm::CallBase *call = EmitCallOrInvoke(fn, value); 2671 2672 if (precise == ARCImpreciseLifetime) { 2673 call->setMetadata("clang.imprecise_release", 2674 llvm::MDNode::get(Builder.getContext(), None)); 2675 } 2676 } 2677 2678 namespace { 2679 struct CallObjCAutoreleasePoolObject final : EHScopeStack::Cleanup { 2680 llvm::Value *Token; 2681 2682 CallObjCAutoreleasePoolObject(llvm::Value *token) : Token(token) {} 2683 2684 void Emit(CodeGenFunction &CGF, Flags flags) override { 2685 CGF.EmitObjCAutoreleasePoolPop(Token); 2686 } 2687 }; 2688 struct CallObjCMRRAutoreleasePoolObject final : EHScopeStack::Cleanup { 2689 llvm::Value *Token; 2690 2691 CallObjCMRRAutoreleasePoolObject(llvm::Value *token) : Token(token) {} 2692 2693 void Emit(CodeGenFunction &CGF, Flags flags) override { 2694 CGF.EmitObjCMRRAutoreleasePoolPop(Token); 2695 } 2696 }; 2697 } 2698 2699 void CodeGenFunction::EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr) { 2700 if (CGM.getLangOpts().ObjCAutoRefCount) 2701 EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, Ptr); 2702 else 2703 EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, Ptr); 2704 } 2705 2706 static bool shouldRetainObjCLifetime(Qualifiers::ObjCLifetime lifetime) { 2707 switch (lifetime) { 2708 case Qualifiers::OCL_None: 2709 case Qualifiers::OCL_ExplicitNone: 2710 case Qualifiers::OCL_Strong: 2711 case Qualifiers::OCL_Autoreleasing: 2712 return true; 2713 2714 case Qualifiers::OCL_Weak: 2715 return false; 2716 } 2717 2718 llvm_unreachable("impossible lifetime!"); 2719 } 2720 2721 static TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF, 2722 LValue lvalue, 2723 QualType type) { 2724 llvm::Value *result; 2725 bool shouldRetain = shouldRetainObjCLifetime(type.getObjCLifetime()); 2726 if (shouldRetain) { 2727 result = CGF.EmitLoadOfLValue(lvalue, SourceLocation()).getScalarVal(); 2728 } else { 2729 assert(type.getObjCLifetime() == Qualifiers::OCL_Weak); 2730 result = CGF.EmitARCLoadWeakRetained(lvalue.getAddress(CGF)); 2731 } 2732 return TryEmitResult(result, !shouldRetain); 2733 } 2734 2735 static TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF, 2736 const Expr *e) { 2737 e = e->IgnoreParens(); 2738 QualType type = e->getType(); 2739 2740 // If we're loading retained from a __strong xvalue, we can avoid 2741 // an extra retain/release pair by zeroing out the source of this 2742 // "move" operation. 2743 if (e->isXValue() && 2744 !type.isConstQualified() && 2745 type.getObjCLifetime() == Qualifiers::OCL_Strong) { 2746 // Emit the lvalue. 2747 LValue lv = CGF.EmitLValue(e); 2748 2749 // Load the object pointer. 2750 llvm::Value *result = CGF.EmitLoadOfLValue(lv, 2751 SourceLocation()).getScalarVal(); 2752 2753 // Set the source pointer to NULL. 2754 CGF.EmitStoreOfScalar(getNullForVariable(lv.getAddress(CGF)), lv); 2755 2756 return TryEmitResult(result, true); 2757 } 2758 2759 // As a very special optimization, in ARC++, if the l-value is the 2760 // result of a non-volatile assignment, do a simple retain of the 2761 // result of the call to objc_storeWeak instead of reloading. 2762 if (CGF.getLangOpts().CPlusPlus && 2763 !type.isVolatileQualified() && 2764 type.getObjCLifetime() == Qualifiers::OCL_Weak && 2765 isa<BinaryOperator>(e) && 2766 cast<BinaryOperator>(e)->getOpcode() == BO_Assign) 2767 return TryEmitResult(CGF.EmitScalarExpr(e), false); 2768 2769 // Try to emit code for scalar constant instead of emitting LValue and 2770 // loading it because we are not guaranteed to have an l-value. One of such 2771 // cases is DeclRefExpr referencing non-odr-used constant-evaluated variable. 2772 if (const auto *decl_expr = dyn_cast<DeclRefExpr>(e)) { 2773 auto *DRE = const_cast<DeclRefExpr *>(decl_expr); 2774 if (CodeGenFunction::ConstantEmission constant = CGF.tryEmitAsConstant(DRE)) 2775 return TryEmitResult(CGF.emitScalarConstant(constant, DRE), 2776 !shouldRetainObjCLifetime(type.getObjCLifetime())); 2777 } 2778 2779 return tryEmitARCRetainLoadOfScalar(CGF, CGF.EmitLValue(e), type); 2780 } 2781 2782 typedef llvm::function_ref<llvm::Value *(CodeGenFunction &CGF, 2783 llvm::Value *value)> 2784 ValueTransform; 2785 2786 /// Insert code immediately after a call. 2787 static llvm::Value *emitARCOperationAfterCall(CodeGenFunction &CGF, 2788 llvm::Value *value, 2789 ValueTransform doAfterCall, 2790 ValueTransform doFallback) { 2791 if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(value)) { 2792 CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP(); 2793 2794 // Place the retain immediately following the call. 2795 CGF.Builder.SetInsertPoint(call->getParent(), 2796 ++llvm::BasicBlock::iterator(call)); 2797 value = doAfterCall(CGF, value); 2798 2799 CGF.Builder.restoreIP(ip); 2800 return value; 2801 } else if (llvm::InvokeInst *invoke = dyn_cast<llvm::InvokeInst>(value)) { 2802 CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP(); 2803 2804 // Place the retain at the beginning of the normal destination block. 2805 llvm::BasicBlock *BB = invoke->getNormalDest(); 2806 CGF.Builder.SetInsertPoint(BB, BB->begin()); 2807 value = doAfterCall(CGF, value); 2808 2809 CGF.Builder.restoreIP(ip); 2810 return value; 2811 2812 // Bitcasts can arise because of related-result returns. Rewrite 2813 // the operand. 2814 } else if (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(value)) { 2815 llvm::Value *operand = bitcast->getOperand(0); 2816 operand = emitARCOperationAfterCall(CGF, operand, doAfterCall, doFallback); 2817 bitcast->setOperand(0, operand); 2818 return bitcast; 2819 2820 // Generic fall-back case. 2821 } else { 2822 // Retain using the non-block variant: we never need to do a copy 2823 // of a block that's been returned to us. 2824 return doFallback(CGF, value); 2825 } 2826 } 2827 2828 /// Given that the given expression is some sort of call (which does 2829 /// not return retained), emit a retain following it. 2830 static llvm::Value *emitARCRetainCallResult(CodeGenFunction &CGF, 2831 const Expr *e) { 2832 llvm::Value *value = CGF.EmitScalarExpr(e); 2833 return emitARCOperationAfterCall(CGF, value, 2834 [](CodeGenFunction &CGF, llvm::Value *value) { 2835 return CGF.EmitARCRetainAutoreleasedReturnValue(value); 2836 }, 2837 [](CodeGenFunction &CGF, llvm::Value *value) { 2838 return CGF.EmitARCRetainNonBlock(value); 2839 }); 2840 } 2841 2842 /// Given that the given expression is some sort of call (which does 2843 /// not return retained), perform an unsafeClaim following it. 2844 static llvm::Value *emitARCUnsafeClaimCallResult(CodeGenFunction &CGF, 2845 const Expr *e) { 2846 llvm::Value *value = CGF.EmitScalarExpr(e); 2847 return emitARCOperationAfterCall(CGF, value, 2848 [](CodeGenFunction &CGF, llvm::Value *value) { 2849 return CGF.EmitARCUnsafeClaimAutoreleasedReturnValue(value); 2850 }, 2851 [](CodeGenFunction &CGF, llvm::Value *value) { 2852 return value; 2853 }); 2854 } 2855 2856 llvm::Value *CodeGenFunction::EmitARCReclaimReturnedObject(const Expr *E, 2857 bool allowUnsafeClaim) { 2858 if (allowUnsafeClaim && 2859 CGM.getLangOpts().ObjCRuntime.hasARCUnsafeClaimAutoreleasedReturnValue()) { 2860 return emitARCUnsafeClaimCallResult(*this, E); 2861 } else { 2862 llvm::Value *value = emitARCRetainCallResult(*this, E); 2863 return EmitObjCConsumeObject(E->getType(), value); 2864 } 2865 } 2866 2867 /// Determine whether it might be important to emit a separate 2868 /// objc_retain_block on the result of the given expression, or 2869 /// whether it's okay to just emit it in a +1 context. 2870 static bool shouldEmitSeparateBlockRetain(const Expr *e) { 2871 assert(e->getType()->isBlockPointerType()); 2872 e = e->IgnoreParens(); 2873 2874 // For future goodness, emit block expressions directly in +1 2875 // contexts if we can. 2876 if (isa<BlockExpr>(e)) 2877 return false; 2878 2879 if (const CastExpr *cast = dyn_cast<CastExpr>(e)) { 2880 switch (cast->getCastKind()) { 2881 // Emitting these operations in +1 contexts is goodness. 2882 case CK_LValueToRValue: 2883 case CK_ARCReclaimReturnedObject: 2884 case CK_ARCConsumeObject: 2885 case CK_ARCProduceObject: 2886 return false; 2887 2888 // These operations preserve a block type. 2889 case CK_NoOp: 2890 case CK_BitCast: 2891 return shouldEmitSeparateBlockRetain(cast->getSubExpr()); 2892 2893 // These operations are known to be bad (or haven't been considered). 2894 case CK_AnyPointerToBlockPointerCast: 2895 default: 2896 return true; 2897 } 2898 } 2899 2900 return true; 2901 } 2902 2903 namespace { 2904 /// A CRTP base class for emitting expressions of retainable object 2905 /// pointer type in ARC. 2906 template <typename Impl, typename Result> class ARCExprEmitter { 2907 protected: 2908 CodeGenFunction &CGF; 2909 Impl &asImpl() { return *static_cast<Impl*>(this); } 2910 2911 ARCExprEmitter(CodeGenFunction &CGF) : CGF(CGF) {} 2912 2913 public: 2914 Result visit(const Expr *e); 2915 Result visitCastExpr(const CastExpr *e); 2916 Result visitPseudoObjectExpr(const PseudoObjectExpr *e); 2917 Result visitBlockExpr(const BlockExpr *e); 2918 Result visitBinaryOperator(const BinaryOperator *e); 2919 Result visitBinAssign(const BinaryOperator *e); 2920 Result visitBinAssignUnsafeUnretained(const BinaryOperator *e); 2921 Result visitBinAssignAutoreleasing(const BinaryOperator *e); 2922 Result visitBinAssignWeak(const BinaryOperator *e); 2923 Result visitBinAssignStrong(const BinaryOperator *e); 2924 2925 // Minimal implementation: 2926 // Result visitLValueToRValue(const Expr *e) 2927 // Result visitConsumeObject(const Expr *e) 2928 // Result visitExtendBlockObject(const Expr *e) 2929 // Result visitReclaimReturnedObject(const Expr *e) 2930 // Result visitCall(const Expr *e) 2931 // Result visitExpr(const Expr *e) 2932 // 2933 // Result emitBitCast(Result result, llvm::Type *resultType) 2934 // llvm::Value *getValueOfResult(Result result) 2935 }; 2936 } 2937 2938 /// Try to emit a PseudoObjectExpr under special ARC rules. 2939 /// 2940 /// This massively duplicates emitPseudoObjectRValue. 2941 template <typename Impl, typename Result> 2942 Result 2943 ARCExprEmitter<Impl,Result>::visitPseudoObjectExpr(const PseudoObjectExpr *E) { 2944 SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques; 2945 2946 // Find the result expression. 2947 const Expr *resultExpr = E->getResultExpr(); 2948 assert(resultExpr); 2949 Result result; 2950 2951 for (PseudoObjectExpr::const_semantics_iterator 2952 i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) { 2953 const Expr *semantic = *i; 2954 2955 // If this semantic expression is an opaque value, bind it 2956 // to the result of its source expression. 2957 if (const OpaqueValueExpr *ov = dyn_cast<OpaqueValueExpr>(semantic)) { 2958 typedef CodeGenFunction::OpaqueValueMappingData OVMA; 2959 OVMA opaqueData; 2960 2961 // If this semantic is the result of the pseudo-object 2962 // expression, try to evaluate the source as +1. 2963 if (ov == resultExpr) { 2964 assert(!OVMA::shouldBindAsLValue(ov)); 2965 result = asImpl().visit(ov->getSourceExpr()); 2966 opaqueData = OVMA::bind(CGF, ov, 2967 RValue::get(asImpl().getValueOfResult(result))); 2968 2969 // Otherwise, just bind it. 2970 } else { 2971 opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr()); 2972 } 2973 opaques.push_back(opaqueData); 2974 2975 // Otherwise, if the expression is the result, evaluate it 2976 // and remember the result. 2977 } else if (semantic == resultExpr) { 2978 result = asImpl().visit(semantic); 2979 2980 // Otherwise, evaluate the expression in an ignored context. 2981 } else { 2982 CGF.EmitIgnoredExpr(semantic); 2983 } 2984 } 2985 2986 // Unbind all the opaques now. 2987 for (unsigned i = 0, e = opaques.size(); i != e; ++i) 2988 opaques[i].unbind(CGF); 2989 2990 return result; 2991 } 2992 2993 template <typename Impl, typename Result> 2994 Result ARCExprEmitter<Impl, Result>::visitBlockExpr(const BlockExpr *e) { 2995 // The default implementation just forwards the expression to visitExpr. 2996 return asImpl().visitExpr(e); 2997 } 2998 2999 template <typename Impl, typename Result> 3000 Result ARCExprEmitter<Impl,Result>::visitCastExpr(const CastExpr *e) { 3001 switch (e->getCastKind()) { 3002 3003 // No-op casts don't change the type, so we just ignore them. 3004 case CK_NoOp: 3005 return asImpl().visit(e->getSubExpr()); 3006 3007 // These casts can change the type. 3008 case CK_CPointerToObjCPointerCast: 3009 case CK_BlockPointerToObjCPointerCast: 3010 case CK_AnyPointerToBlockPointerCast: 3011 case CK_BitCast: { 3012 llvm::Type *resultType = CGF.ConvertType(e->getType()); 3013 assert(e->getSubExpr()->getType()->hasPointerRepresentation()); 3014 Result result = asImpl().visit(e->getSubExpr()); 3015 return asImpl().emitBitCast(result, resultType); 3016 } 3017 3018 // Handle some casts specially. 3019 case CK_LValueToRValue: 3020 return asImpl().visitLValueToRValue(e->getSubExpr()); 3021 case CK_ARCConsumeObject: 3022 return asImpl().visitConsumeObject(e->getSubExpr()); 3023 case CK_ARCExtendBlockObject: 3024 return asImpl().visitExtendBlockObject(e->getSubExpr()); 3025 case CK_ARCReclaimReturnedObject: 3026 return asImpl().visitReclaimReturnedObject(e->getSubExpr()); 3027 3028 // Otherwise, use the default logic. 3029 default: 3030 return asImpl().visitExpr(e); 3031 } 3032 } 3033 3034 template <typename Impl, typename Result> 3035 Result 3036 ARCExprEmitter<Impl,Result>::visitBinaryOperator(const BinaryOperator *e) { 3037 switch (e->getOpcode()) { 3038 case BO_Comma: 3039 CGF.EmitIgnoredExpr(e->getLHS()); 3040 CGF.EnsureInsertPoint(); 3041 return asImpl().visit(e->getRHS()); 3042 3043 case BO_Assign: 3044 return asImpl().visitBinAssign(e); 3045 3046 default: 3047 return asImpl().visitExpr(e); 3048 } 3049 } 3050 3051 template <typename Impl, typename Result> 3052 Result ARCExprEmitter<Impl,Result>::visitBinAssign(const BinaryOperator *e) { 3053 switch (e->getLHS()->getType().getObjCLifetime()) { 3054 case Qualifiers::OCL_ExplicitNone: 3055 return asImpl().visitBinAssignUnsafeUnretained(e); 3056 3057 case Qualifiers::OCL_Weak: 3058 return asImpl().visitBinAssignWeak(e); 3059 3060 case Qualifiers::OCL_Autoreleasing: 3061 return asImpl().visitBinAssignAutoreleasing(e); 3062 3063 case Qualifiers::OCL_Strong: 3064 return asImpl().visitBinAssignStrong(e); 3065 3066 case Qualifiers::OCL_None: 3067 return asImpl().visitExpr(e); 3068 } 3069 llvm_unreachable("bad ObjC ownership qualifier"); 3070 } 3071 3072 /// The default rule for __unsafe_unretained emits the RHS recursively, 3073 /// stores into the unsafe variable, and propagates the result outward. 3074 template <typename Impl, typename Result> 3075 Result ARCExprEmitter<Impl,Result>:: 3076 visitBinAssignUnsafeUnretained(const BinaryOperator *e) { 3077 // Recursively emit the RHS. 3078 // For __block safety, do this before emitting the LHS. 3079 Result result = asImpl().visit(e->getRHS()); 3080 3081 // Perform the store. 3082 LValue lvalue = 3083 CGF.EmitCheckedLValue(e->getLHS(), CodeGenFunction::TCK_Store); 3084 CGF.EmitStoreThroughLValue(RValue::get(asImpl().getValueOfResult(result)), 3085 lvalue); 3086 3087 return result; 3088 } 3089 3090 template <typename Impl, typename Result> 3091 Result 3092 ARCExprEmitter<Impl,Result>::visitBinAssignAutoreleasing(const BinaryOperator *e) { 3093 return asImpl().visitExpr(e); 3094 } 3095 3096 template <typename Impl, typename Result> 3097 Result 3098 ARCExprEmitter<Impl,Result>::visitBinAssignWeak(const BinaryOperator *e) { 3099 return asImpl().visitExpr(e); 3100 } 3101 3102 template <typename Impl, typename Result> 3103 Result 3104 ARCExprEmitter<Impl,Result>::visitBinAssignStrong(const BinaryOperator *e) { 3105 return asImpl().visitExpr(e); 3106 } 3107 3108 /// The general expression-emission logic. 3109 template <typename Impl, typename Result> 3110 Result ARCExprEmitter<Impl,Result>::visit(const Expr *e) { 3111 // We should *never* see a nested full-expression here, because if 3112 // we fail to emit at +1, our caller must not retain after we close 3113 // out the full-expression. This isn't as important in the unsafe 3114 // emitter. 3115 assert(!isa<ExprWithCleanups>(e)); 3116 3117 // Look through parens, __extension__, generic selection, etc. 3118 e = e->IgnoreParens(); 3119 3120 // Handle certain kinds of casts. 3121 if (const CastExpr *ce = dyn_cast<CastExpr>(e)) { 3122 return asImpl().visitCastExpr(ce); 3123 3124 // Handle the comma operator. 3125 } else if (auto op = dyn_cast<BinaryOperator>(e)) { 3126 return asImpl().visitBinaryOperator(op); 3127 3128 // TODO: handle conditional operators here 3129 3130 // For calls and message sends, use the retained-call logic. 3131 // Delegate inits are a special case in that they're the only 3132 // returns-retained expression that *isn't* surrounded by 3133 // a consume. 3134 } else if (isa<CallExpr>(e) || 3135 (isa<ObjCMessageExpr>(e) && 3136 !cast<ObjCMessageExpr>(e)->isDelegateInitCall())) { 3137 return asImpl().visitCall(e); 3138 3139 // Look through pseudo-object expressions. 3140 } else if (const PseudoObjectExpr *pseudo = dyn_cast<PseudoObjectExpr>(e)) { 3141 return asImpl().visitPseudoObjectExpr(pseudo); 3142 } else if (auto *be = dyn_cast<BlockExpr>(e)) 3143 return asImpl().visitBlockExpr(be); 3144 3145 return asImpl().visitExpr(e); 3146 } 3147 3148 namespace { 3149 3150 /// An emitter for +1 results. 3151 struct ARCRetainExprEmitter : 3152 public ARCExprEmitter<ARCRetainExprEmitter, TryEmitResult> { 3153 3154 ARCRetainExprEmitter(CodeGenFunction &CGF) : ARCExprEmitter(CGF) {} 3155 3156 llvm::Value *getValueOfResult(TryEmitResult result) { 3157 return result.getPointer(); 3158 } 3159 3160 TryEmitResult emitBitCast(TryEmitResult result, llvm::Type *resultType) { 3161 llvm::Value *value = result.getPointer(); 3162 value = CGF.Builder.CreateBitCast(value, resultType); 3163 result.setPointer(value); 3164 return result; 3165 } 3166 3167 TryEmitResult visitLValueToRValue(const Expr *e) { 3168 return tryEmitARCRetainLoadOfScalar(CGF, e); 3169 } 3170 3171 /// For consumptions, just emit the subexpression and thus elide 3172 /// the retain/release pair. 3173 TryEmitResult visitConsumeObject(const Expr *e) { 3174 llvm::Value *result = CGF.EmitScalarExpr(e); 3175 return TryEmitResult(result, true); 3176 } 3177 3178 TryEmitResult visitBlockExpr(const BlockExpr *e) { 3179 TryEmitResult result = visitExpr(e); 3180 // Avoid the block-retain if this is a block literal that doesn't need to be 3181 // copied to the heap. 3182 if (e->getBlockDecl()->canAvoidCopyToHeap()) 3183 result.setInt(true); 3184 return result; 3185 } 3186 3187 /// Block extends are net +0. Naively, we could just recurse on 3188 /// the subexpression, but actually we need to ensure that the 3189 /// value is copied as a block, so there's a little filter here. 3190 TryEmitResult visitExtendBlockObject(const Expr *e) { 3191 llvm::Value *result; // will be a +0 value 3192 3193 // If we can't safely assume the sub-expression will produce a 3194 // block-copied value, emit the sub-expression at +0. 3195 if (shouldEmitSeparateBlockRetain(e)) { 3196 result = CGF.EmitScalarExpr(e); 3197 3198 // Otherwise, try to emit the sub-expression at +1 recursively. 3199 } else { 3200 TryEmitResult subresult = asImpl().visit(e); 3201 3202 // If that produced a retained value, just use that. 3203 if (subresult.getInt()) { 3204 return subresult; 3205 } 3206 3207 // Otherwise it's +0. 3208 result = subresult.getPointer(); 3209 } 3210 3211 // Retain the object as a block. 3212 result = CGF.EmitARCRetainBlock(result, /*mandatory*/ true); 3213 return TryEmitResult(result, true); 3214 } 3215 3216 /// For reclaims, emit the subexpression as a retained call and 3217 /// skip the consumption. 3218 TryEmitResult visitReclaimReturnedObject(const Expr *e) { 3219 llvm::Value *result = emitARCRetainCallResult(CGF, e); 3220 return TryEmitResult(result, true); 3221 } 3222 3223 /// When we have an undecorated call, retroactively do a claim. 3224 TryEmitResult visitCall(const Expr *e) { 3225 llvm::Value *result = emitARCRetainCallResult(CGF, e); 3226 return TryEmitResult(result, true); 3227 } 3228 3229 // TODO: maybe special-case visitBinAssignWeak? 3230 3231 TryEmitResult visitExpr(const Expr *e) { 3232 // We didn't find an obvious production, so emit what we've got and 3233 // tell the caller that we didn't manage to retain. 3234 llvm::Value *result = CGF.EmitScalarExpr(e); 3235 return TryEmitResult(result, false); 3236 } 3237 }; 3238 } 3239 3240 static TryEmitResult 3241 tryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e) { 3242 return ARCRetainExprEmitter(CGF).visit(e); 3243 } 3244 3245 static llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF, 3246 LValue lvalue, 3247 QualType type) { 3248 TryEmitResult result = tryEmitARCRetainLoadOfScalar(CGF, lvalue, type); 3249 llvm::Value *value = result.getPointer(); 3250 if (!result.getInt()) 3251 value = CGF.EmitARCRetain(type, value); 3252 return value; 3253 } 3254 3255 /// EmitARCRetainScalarExpr - Semantically equivalent to 3256 /// EmitARCRetainObject(e->getType(), EmitScalarExpr(e)), but making a 3257 /// best-effort attempt to peephole expressions that naturally produce 3258 /// retained objects. 3259 llvm::Value *CodeGenFunction::EmitARCRetainScalarExpr(const Expr *e) { 3260 // The retain needs to happen within the full-expression. 3261 if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) { 3262 enterFullExpression(cleanups); 3263 RunCleanupsScope scope(*this); 3264 return EmitARCRetainScalarExpr(cleanups->getSubExpr()); 3265 } 3266 3267 TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e); 3268 llvm::Value *value = result.getPointer(); 3269 if (!result.getInt()) 3270 value = EmitARCRetain(e->getType(), value); 3271 return value; 3272 } 3273 3274 llvm::Value * 3275 CodeGenFunction::EmitARCRetainAutoreleaseScalarExpr(const Expr *e) { 3276 // The retain needs to happen within the full-expression. 3277 if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) { 3278 enterFullExpression(cleanups); 3279 RunCleanupsScope scope(*this); 3280 return EmitARCRetainAutoreleaseScalarExpr(cleanups->getSubExpr()); 3281 } 3282 3283 TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e); 3284 llvm::Value *value = result.getPointer(); 3285 if (result.getInt()) 3286 value = EmitARCAutorelease(value); 3287 else 3288 value = EmitARCRetainAutorelease(e->getType(), value); 3289 return value; 3290 } 3291 3292 llvm::Value *CodeGenFunction::EmitARCExtendBlockObject(const Expr *e) { 3293 llvm::Value *result; 3294 bool doRetain; 3295 3296 if (shouldEmitSeparateBlockRetain(e)) { 3297 result = EmitScalarExpr(e); 3298 doRetain = true; 3299 } else { 3300 TryEmitResult subresult = tryEmitARCRetainScalarExpr(*this, e); 3301 result = subresult.getPointer(); 3302 doRetain = !subresult.getInt(); 3303 } 3304 3305 if (doRetain) 3306 result = EmitARCRetainBlock(result, /*mandatory*/ true); 3307 return EmitObjCConsumeObject(e->getType(), result); 3308 } 3309 3310 llvm::Value *CodeGenFunction::EmitObjCThrowOperand(const Expr *expr) { 3311 // In ARC, retain and autorelease the expression. 3312 if (getLangOpts().ObjCAutoRefCount) { 3313 // Do so before running any cleanups for the full-expression. 3314 // EmitARCRetainAutoreleaseScalarExpr does this for us. 3315 return EmitARCRetainAutoreleaseScalarExpr(expr); 3316 } 3317 3318 // Otherwise, use the normal scalar-expression emission. The 3319 // exception machinery doesn't do anything special with the 3320 // exception like retaining it, so there's no safety associated with 3321 // only running cleanups after the throw has started, and when it 3322 // matters it tends to be substantially inferior code. 3323 return EmitScalarExpr(expr); 3324 } 3325 3326 namespace { 3327 3328 /// An emitter for assigning into an __unsafe_unretained context. 3329 struct ARCUnsafeUnretainedExprEmitter : 3330 public ARCExprEmitter<ARCUnsafeUnretainedExprEmitter, llvm::Value*> { 3331 3332 ARCUnsafeUnretainedExprEmitter(CodeGenFunction &CGF) : ARCExprEmitter(CGF) {} 3333 3334 llvm::Value *getValueOfResult(llvm::Value *value) { 3335 return value; 3336 } 3337 3338 llvm::Value *emitBitCast(llvm::Value *value, llvm::Type *resultType) { 3339 return CGF.Builder.CreateBitCast(value, resultType); 3340 } 3341 3342 llvm::Value *visitLValueToRValue(const Expr *e) { 3343 return CGF.EmitScalarExpr(e); 3344 } 3345 3346 /// For consumptions, just emit the subexpression and perform the 3347 /// consumption like normal. 3348 llvm::Value *visitConsumeObject(const Expr *e) { 3349 llvm::Value *value = CGF.EmitScalarExpr(e); 3350 return CGF.EmitObjCConsumeObject(e->getType(), value); 3351 } 3352 3353 /// No special logic for block extensions. (This probably can't 3354 /// actually happen in this emitter, though.) 3355 llvm::Value *visitExtendBlockObject(const Expr *e) { 3356 return CGF.EmitARCExtendBlockObject(e); 3357 } 3358 3359 /// For reclaims, perform an unsafeClaim if that's enabled. 3360 llvm::Value *visitReclaimReturnedObject(const Expr *e) { 3361 return CGF.EmitARCReclaimReturnedObject(e, /*unsafe*/ true); 3362 } 3363 3364 /// When we have an undecorated call, just emit it without adding 3365 /// the unsafeClaim. 3366 llvm::Value *visitCall(const Expr *e) { 3367 return CGF.EmitScalarExpr(e); 3368 } 3369 3370 /// Just do normal scalar emission in the default case. 3371 llvm::Value *visitExpr(const Expr *e) { 3372 return CGF.EmitScalarExpr(e); 3373 } 3374 }; 3375 } 3376 3377 static llvm::Value *emitARCUnsafeUnretainedScalarExpr(CodeGenFunction &CGF, 3378 const Expr *e) { 3379 return ARCUnsafeUnretainedExprEmitter(CGF).visit(e); 3380 } 3381 3382 /// EmitARCUnsafeUnretainedScalarExpr - Semantically equivalent to 3383 /// immediately releasing the resut of EmitARCRetainScalarExpr, but 3384 /// avoiding any spurious retains, including by performing reclaims 3385 /// with objc_unsafeClaimAutoreleasedReturnValue. 3386 llvm::Value *CodeGenFunction::EmitARCUnsafeUnretainedScalarExpr(const Expr *e) { 3387 // Look through full-expressions. 3388 if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) { 3389 enterFullExpression(cleanups); 3390 RunCleanupsScope scope(*this); 3391 return emitARCUnsafeUnretainedScalarExpr(*this, cleanups->getSubExpr()); 3392 } 3393 3394 return emitARCUnsafeUnretainedScalarExpr(*this, e); 3395 } 3396 3397 std::pair<LValue,llvm::Value*> 3398 CodeGenFunction::EmitARCStoreUnsafeUnretained(const BinaryOperator *e, 3399 bool ignored) { 3400 // Evaluate the RHS first. If we're ignoring the result, assume 3401 // that we can emit at an unsafe +0. 3402 llvm::Value *value; 3403 if (ignored) { 3404 value = EmitARCUnsafeUnretainedScalarExpr(e->getRHS()); 3405 } else { 3406 value = EmitScalarExpr(e->getRHS()); 3407 } 3408 3409 // Emit the LHS and perform the store. 3410 LValue lvalue = EmitLValue(e->getLHS()); 3411 EmitStoreOfScalar(value, lvalue); 3412 3413 return std::pair<LValue,llvm::Value*>(std::move(lvalue), value); 3414 } 3415 3416 std::pair<LValue,llvm::Value*> 3417 CodeGenFunction::EmitARCStoreStrong(const BinaryOperator *e, 3418 bool ignored) { 3419 // Evaluate the RHS first. 3420 TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e->getRHS()); 3421 llvm::Value *value = result.getPointer(); 3422 3423 bool hasImmediateRetain = result.getInt(); 3424 3425 // If we didn't emit a retained object, and the l-value is of block 3426 // type, then we need to emit the block-retain immediately in case 3427 // it invalidates the l-value. 3428 if (!hasImmediateRetain && e->getType()->isBlockPointerType()) { 3429 value = EmitARCRetainBlock(value, /*mandatory*/ false); 3430 hasImmediateRetain = true; 3431 } 3432 3433 LValue lvalue = EmitLValue(e->getLHS()); 3434 3435 // If the RHS was emitted retained, expand this. 3436 if (hasImmediateRetain) { 3437 llvm::Value *oldValue = EmitLoadOfScalar(lvalue, SourceLocation()); 3438 EmitStoreOfScalar(value, lvalue); 3439 EmitARCRelease(oldValue, lvalue.isARCPreciseLifetime()); 3440 } else { 3441 value = EmitARCStoreStrong(lvalue, value, ignored); 3442 } 3443 3444 return std::pair<LValue,llvm::Value*>(lvalue, value); 3445 } 3446 3447 std::pair<LValue,llvm::Value*> 3448 CodeGenFunction::EmitARCStoreAutoreleasing(const BinaryOperator *e) { 3449 llvm::Value *value = EmitARCRetainAutoreleaseScalarExpr(e->getRHS()); 3450 LValue lvalue = EmitLValue(e->getLHS()); 3451 3452 EmitStoreOfScalar(value, lvalue); 3453 3454 return std::pair<LValue,llvm::Value*>(lvalue, value); 3455 } 3456 3457 void CodeGenFunction::EmitObjCAutoreleasePoolStmt( 3458 const ObjCAutoreleasePoolStmt &ARPS) { 3459 const Stmt *subStmt = ARPS.getSubStmt(); 3460 const CompoundStmt &S = cast<CompoundStmt>(*subStmt); 3461 3462 CGDebugInfo *DI = getDebugInfo(); 3463 if (DI) 3464 DI->EmitLexicalBlockStart(Builder, S.getLBracLoc()); 3465 3466 // Keep track of the current cleanup stack depth. 3467 RunCleanupsScope Scope(*this); 3468 if (CGM.getLangOpts().ObjCRuntime.hasNativeARC()) { 3469 llvm::Value *token = EmitObjCAutoreleasePoolPush(); 3470 EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, token); 3471 } else { 3472 llvm::Value *token = EmitObjCMRRAutoreleasePoolPush(); 3473 EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, token); 3474 } 3475 3476 for (const auto *I : S.body()) 3477 EmitStmt(I); 3478 3479 if (DI) 3480 DI->EmitLexicalBlockEnd(Builder, S.getRBracLoc()); 3481 } 3482 3483 /// EmitExtendGCLifetime - Given a pointer to an Objective-C object, 3484 /// make sure it survives garbage collection until this point. 3485 void CodeGenFunction::EmitExtendGCLifetime(llvm::Value *object) { 3486 // We just use an inline assembly. 3487 llvm::FunctionType *extenderType 3488 = llvm::FunctionType::get(VoidTy, VoidPtrTy, RequiredArgs::All); 3489 llvm::InlineAsm *extender = llvm::InlineAsm::get(extenderType, 3490 /* assembly */ "", 3491 /* constraints */ "r", 3492 /* side effects */ true); 3493 3494 object = Builder.CreateBitCast(object, VoidPtrTy); 3495 EmitNounwindRuntimeCall(extender, object); 3496 } 3497 3498 /// GenerateObjCAtomicSetterCopyHelperFunction - Given a c++ object type with 3499 /// non-trivial copy assignment function, produce following helper function. 3500 /// static void copyHelper(Ty *dest, const Ty *source) { *dest = *source; } 3501 /// 3502 llvm::Constant * 3503 CodeGenFunction::GenerateObjCAtomicSetterCopyHelperFunction( 3504 const ObjCPropertyImplDecl *PID) { 3505 if (!getLangOpts().CPlusPlus || 3506 !getLangOpts().ObjCRuntime.hasAtomicCopyHelper()) 3507 return nullptr; 3508 QualType Ty = PID->getPropertyIvarDecl()->getType(); 3509 if (!Ty->isRecordType()) 3510 return nullptr; 3511 const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 3512 if ((!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_atomic))) 3513 return nullptr; 3514 llvm::Constant *HelperFn = nullptr; 3515 if (hasTrivialSetExpr(PID)) 3516 return nullptr; 3517 assert(PID->getSetterCXXAssignment() && "SetterCXXAssignment - null"); 3518 if ((HelperFn = CGM.getAtomicSetterHelperFnMap(Ty))) 3519 return HelperFn; 3520 3521 ASTContext &C = getContext(); 3522 IdentifierInfo *II 3523 = &CGM.getContext().Idents.get("__assign_helper_atomic_property_"); 3524 3525 QualType ReturnTy = C.VoidTy; 3526 QualType DestTy = C.getPointerType(Ty); 3527 QualType SrcTy = Ty; 3528 SrcTy.addConst(); 3529 SrcTy = C.getPointerType(SrcTy); 3530 3531 SmallVector<QualType, 2> ArgTys; 3532 ArgTys.push_back(DestTy); 3533 ArgTys.push_back(SrcTy); 3534 QualType FunctionTy = C.getFunctionType(ReturnTy, ArgTys, {}); 3535 3536 FunctionDecl *FD = FunctionDecl::Create( 3537 C, C.getTranslationUnitDecl(), SourceLocation(), SourceLocation(), II, 3538 FunctionTy, nullptr, SC_Static, false, false); 3539 3540 FunctionArgList args; 3541 ImplicitParamDecl DstDecl(C, FD, SourceLocation(), /*Id=*/nullptr, DestTy, 3542 ImplicitParamDecl::Other); 3543 args.push_back(&DstDecl); 3544 ImplicitParamDecl SrcDecl(C, FD, SourceLocation(), /*Id=*/nullptr, SrcTy, 3545 ImplicitParamDecl::Other); 3546 args.push_back(&SrcDecl); 3547 3548 const CGFunctionInfo &FI = 3549 CGM.getTypes().arrangeBuiltinFunctionDeclaration(ReturnTy, args); 3550 3551 llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI); 3552 3553 llvm::Function *Fn = 3554 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 3555 "__assign_helper_atomic_property_", 3556 &CGM.getModule()); 3557 3558 CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FI); 3559 3560 StartFunction(FD, ReturnTy, Fn, FI, args); 3561 3562 DeclRefExpr DstExpr(getContext(), &DstDecl, false, DestTy, VK_RValue, 3563 SourceLocation()); 3564 UnaryOperator DST(&DstExpr, UO_Deref, DestTy->getPointeeType(), 3565 VK_LValue, OK_Ordinary, SourceLocation(), false); 3566 3567 DeclRefExpr SrcExpr(getContext(), &SrcDecl, false, SrcTy, VK_RValue, 3568 SourceLocation()); 3569 UnaryOperator SRC(&SrcExpr, UO_Deref, SrcTy->getPointeeType(), 3570 VK_LValue, OK_Ordinary, SourceLocation(), false); 3571 3572 Expr *Args[2] = { &DST, &SRC }; 3573 CallExpr *CalleeExp = cast<CallExpr>(PID->getSetterCXXAssignment()); 3574 CXXOperatorCallExpr *TheCall = CXXOperatorCallExpr::Create( 3575 C, OO_Equal, CalleeExp->getCallee(), Args, DestTy->getPointeeType(), 3576 VK_LValue, SourceLocation(), FPOptions()); 3577 3578 EmitStmt(TheCall); 3579 3580 FinishFunction(); 3581 HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); 3582 CGM.setAtomicSetterHelperFnMap(Ty, HelperFn); 3583 return HelperFn; 3584 } 3585 3586 llvm::Constant * 3587 CodeGenFunction::GenerateObjCAtomicGetterCopyHelperFunction( 3588 const ObjCPropertyImplDecl *PID) { 3589 if (!getLangOpts().CPlusPlus || 3590 !getLangOpts().ObjCRuntime.hasAtomicCopyHelper()) 3591 return nullptr; 3592 const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 3593 QualType Ty = PD->getType(); 3594 if (!Ty->isRecordType()) 3595 return nullptr; 3596 if ((!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_atomic))) 3597 return nullptr; 3598 llvm::Constant *HelperFn = nullptr; 3599 if (hasTrivialGetExpr(PID)) 3600 return nullptr; 3601 assert(PID->getGetterCXXConstructor() && "getGetterCXXConstructor - null"); 3602 if ((HelperFn = CGM.getAtomicGetterHelperFnMap(Ty))) 3603 return HelperFn; 3604 3605 ASTContext &C = getContext(); 3606 IdentifierInfo *II = 3607 &CGM.getContext().Idents.get("__copy_helper_atomic_property_"); 3608 3609 QualType ReturnTy = C.VoidTy; 3610 QualType DestTy = C.getPointerType(Ty); 3611 QualType SrcTy = Ty; 3612 SrcTy.addConst(); 3613 SrcTy = C.getPointerType(SrcTy); 3614 3615 SmallVector<QualType, 2> ArgTys; 3616 ArgTys.push_back(DestTy); 3617 ArgTys.push_back(SrcTy); 3618 QualType FunctionTy = C.getFunctionType(ReturnTy, ArgTys, {}); 3619 3620 FunctionDecl *FD = FunctionDecl::Create( 3621 C, C.getTranslationUnitDecl(), SourceLocation(), SourceLocation(), II, 3622 FunctionTy, nullptr, SC_Static, false, false); 3623 3624 FunctionArgList args; 3625 ImplicitParamDecl DstDecl(C, FD, SourceLocation(), /*Id=*/nullptr, DestTy, 3626 ImplicitParamDecl::Other); 3627 args.push_back(&DstDecl); 3628 ImplicitParamDecl SrcDecl(C, FD, SourceLocation(), /*Id=*/nullptr, SrcTy, 3629 ImplicitParamDecl::Other); 3630 args.push_back(&SrcDecl); 3631 3632 const CGFunctionInfo &FI = 3633 CGM.getTypes().arrangeBuiltinFunctionDeclaration(ReturnTy, args); 3634 3635 llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI); 3636 3637 llvm::Function *Fn = llvm::Function::Create( 3638 LTy, llvm::GlobalValue::InternalLinkage, "__copy_helper_atomic_property_", 3639 &CGM.getModule()); 3640 3641 CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FI); 3642 3643 StartFunction(FD, ReturnTy, Fn, FI, args); 3644 3645 DeclRefExpr SrcExpr(getContext(), &SrcDecl, false, SrcTy, VK_RValue, 3646 SourceLocation()); 3647 3648 UnaryOperator SRC(&SrcExpr, UO_Deref, SrcTy->getPointeeType(), 3649 VK_LValue, OK_Ordinary, SourceLocation(), false); 3650 3651 CXXConstructExpr *CXXConstExpr = 3652 cast<CXXConstructExpr>(PID->getGetterCXXConstructor()); 3653 3654 SmallVector<Expr*, 4> ConstructorArgs; 3655 ConstructorArgs.push_back(&SRC); 3656 ConstructorArgs.append(std::next(CXXConstExpr->arg_begin()), 3657 CXXConstExpr->arg_end()); 3658 3659 CXXConstructExpr *TheCXXConstructExpr = 3660 CXXConstructExpr::Create(C, Ty, SourceLocation(), 3661 CXXConstExpr->getConstructor(), 3662 CXXConstExpr->isElidable(), 3663 ConstructorArgs, 3664 CXXConstExpr->hadMultipleCandidates(), 3665 CXXConstExpr->isListInitialization(), 3666 CXXConstExpr->isStdInitListInitialization(), 3667 CXXConstExpr->requiresZeroInitialization(), 3668 CXXConstExpr->getConstructionKind(), 3669 SourceRange()); 3670 3671 DeclRefExpr DstExpr(getContext(), &DstDecl, false, DestTy, VK_RValue, 3672 SourceLocation()); 3673 3674 RValue DV = EmitAnyExpr(&DstExpr); 3675 CharUnits Alignment 3676 = getContext().getTypeAlignInChars(TheCXXConstructExpr->getType()); 3677 EmitAggExpr(TheCXXConstructExpr, 3678 AggValueSlot::forAddr(Address(DV.getScalarVal(), Alignment), 3679 Qualifiers(), 3680 AggValueSlot::IsDestructed, 3681 AggValueSlot::DoesNotNeedGCBarriers, 3682 AggValueSlot::IsNotAliased, 3683 AggValueSlot::DoesNotOverlap)); 3684 3685 FinishFunction(); 3686 HelperFn = llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); 3687 CGM.setAtomicGetterHelperFnMap(Ty, HelperFn); 3688 return HelperFn; 3689 } 3690 3691 llvm::Value * 3692 CodeGenFunction::EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty) { 3693 // Get selectors for retain/autorelease. 3694 IdentifierInfo *CopyID = &getContext().Idents.get("copy"); 3695 Selector CopySelector = 3696 getContext().Selectors.getNullarySelector(CopyID); 3697 IdentifierInfo *AutoreleaseID = &getContext().Idents.get("autorelease"); 3698 Selector AutoreleaseSelector = 3699 getContext().Selectors.getNullarySelector(AutoreleaseID); 3700 3701 // Emit calls to retain/autorelease. 3702 CGObjCRuntime &Runtime = CGM.getObjCRuntime(); 3703 llvm::Value *Val = Block; 3704 RValue Result; 3705 Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 3706 Ty, CopySelector, 3707 Val, CallArgList(), nullptr, nullptr); 3708 Val = Result.getScalarVal(); 3709 Result = Runtime.GenerateMessageSend(*this, ReturnValueSlot(), 3710 Ty, AutoreleaseSelector, 3711 Val, CallArgList(), nullptr, nullptr); 3712 Val = Result.getScalarVal(); 3713 return Val; 3714 } 3715 3716 llvm::Value * 3717 CodeGenFunction::EmitBuiltinAvailable(ArrayRef<llvm::Value *> Args) { 3718 assert(Args.size() == 3 && "Expected 3 argument here!"); 3719 3720 if (!CGM.IsOSVersionAtLeastFn) { 3721 llvm::FunctionType *FTy = 3722 llvm::FunctionType::get(Int32Ty, {Int32Ty, Int32Ty, Int32Ty}, false); 3723 CGM.IsOSVersionAtLeastFn = 3724 CGM.CreateRuntimeFunction(FTy, "__isOSVersionAtLeast"); 3725 } 3726 3727 llvm::Value *CallRes = 3728 EmitNounwindRuntimeCall(CGM.IsOSVersionAtLeastFn, Args); 3729 3730 return Builder.CreateICmpNE(CallRes, llvm::Constant::getNullValue(Int32Ty)); 3731 } 3732 3733 void CodeGenModule::emitAtAvailableLinkGuard() { 3734 if (!IsOSVersionAtLeastFn) 3735 return; 3736 // @available requires CoreFoundation only on Darwin. 3737 if (!Target.getTriple().isOSDarwin()) 3738 return; 3739 // Add -framework CoreFoundation to the linker commands. We still want to 3740 // emit the core foundation reference down below because otherwise if 3741 // CoreFoundation is not used in the code, the linker won't link the 3742 // framework. 3743 auto &Context = getLLVMContext(); 3744 llvm::Metadata *Args[2] = {llvm::MDString::get(Context, "-framework"), 3745 llvm::MDString::get(Context, "CoreFoundation")}; 3746 LinkerOptionsMetadata.push_back(llvm::MDNode::get(Context, Args)); 3747 // Emit a reference to a symbol from CoreFoundation to ensure that 3748 // CoreFoundation is linked into the final binary. 3749 llvm::FunctionType *FTy = 3750 llvm::FunctionType::get(Int32Ty, {VoidPtrTy}, false); 3751 llvm::FunctionCallee CFFunc = 3752 CreateRuntimeFunction(FTy, "CFBundleGetVersionNumber"); 3753 3754 llvm::FunctionType *CheckFTy = llvm::FunctionType::get(VoidTy, {}, false); 3755 llvm::FunctionCallee CFLinkCheckFuncRef = CreateRuntimeFunction( 3756 CheckFTy, "__clang_at_available_requires_core_foundation_framework", 3757 llvm::AttributeList(), /*Local=*/true); 3758 llvm::Function *CFLinkCheckFunc = 3759 cast<llvm::Function>(CFLinkCheckFuncRef.getCallee()->stripPointerCasts()); 3760 if (CFLinkCheckFunc->empty()) { 3761 CFLinkCheckFunc->setLinkage(llvm::GlobalValue::LinkOnceAnyLinkage); 3762 CFLinkCheckFunc->setVisibility(llvm::GlobalValue::HiddenVisibility); 3763 CodeGenFunction CGF(*this); 3764 CGF.Builder.SetInsertPoint(CGF.createBasicBlock("", CFLinkCheckFunc)); 3765 CGF.EmitNounwindRuntimeCall(CFFunc, 3766 llvm::Constant::getNullValue(VoidPtrTy)); 3767 CGF.Builder.CreateUnreachable(); 3768 addCompilerUsedGlobal(CFLinkCheckFunc); 3769 } 3770 } 3771 3772 CGObjCRuntime::~CGObjCRuntime() {} 3773