1 //===--- CGBlocks.cpp - Emit LLVM Code for declarations ---------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This contains code to emit blocks. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "CGBlocks.h" 15 #include "CGDebugInfo.h" 16 #include "CGObjCRuntime.h" 17 #include "CodeGenFunction.h" 18 #include "CodeGenModule.h" 19 #include "clang/CodeGen/ConstantInitBuilder.h" 20 #include "clang/AST/DeclObjC.h" 21 #include "llvm/ADT/SmallSet.h" 22 #include "llvm/IR/CallSite.h" 23 #include "llvm/IR/DataLayout.h" 24 #include "llvm/IR/Module.h" 25 #include <algorithm> 26 #include <cstdio> 27 28 using namespace clang; 29 using namespace CodeGen; 30 31 CGBlockInfo::CGBlockInfo(const BlockDecl *block, StringRef name) 32 : Name(name), CXXThisIndex(0), CanBeGlobal(false), NeedsCopyDispose(false), 33 HasCXXObject(false), UsesStret(false), HasCapturedVariableLayout(false), 34 LocalAddress(Address::invalid()), StructureType(nullptr), Block(block), 35 DominatingIP(nullptr) { 36 37 // Skip asm prefix, if any. 'name' is usually taken directly from 38 // the mangled name of the enclosing function. 39 if (!name.empty() && name[0] == '\01') 40 name = name.substr(1); 41 } 42 43 // Anchor the vtable to this translation unit. 44 BlockByrefHelpers::~BlockByrefHelpers() {} 45 46 /// Build the given block as a global block. 47 static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM, 48 const CGBlockInfo &blockInfo, 49 llvm::Constant *blockFn); 50 51 /// Build the helper function to copy a block. 52 static llvm::Constant *buildCopyHelper(CodeGenModule &CGM, 53 const CGBlockInfo &blockInfo) { 54 return CodeGenFunction(CGM).GenerateCopyHelperFunction(blockInfo); 55 } 56 57 /// Build the helper function to dispose of a block. 58 static llvm::Constant *buildDisposeHelper(CodeGenModule &CGM, 59 const CGBlockInfo &blockInfo) { 60 return CodeGenFunction(CGM).GenerateDestroyHelperFunction(blockInfo); 61 } 62 63 /// buildBlockDescriptor - Build the block descriptor meta-data for a block. 64 /// buildBlockDescriptor is accessed from 5th field of the Block_literal 65 /// meta-data and contains stationary information about the block literal. 66 /// Its definition will have 4 (or optinally 6) words. 67 /// \code 68 /// struct Block_descriptor { 69 /// unsigned long reserved; 70 /// unsigned long size; // size of Block_literal metadata in bytes. 71 /// void *copy_func_helper_decl; // optional copy helper. 72 /// void *destroy_func_decl; // optioanl destructor helper. 73 /// void *block_method_encoding_address; // @encode for block literal signature. 74 /// void *block_layout_info; // encoding of captured block variables. 75 /// }; 76 /// \endcode 77 static llvm::Constant *buildBlockDescriptor(CodeGenModule &CGM, 78 const CGBlockInfo &blockInfo) { 79 ASTContext &C = CGM.getContext(); 80 81 llvm::IntegerType *ulong = 82 cast<llvm::IntegerType>(CGM.getTypes().ConvertType(C.UnsignedLongTy)); 83 llvm::PointerType *i8p = nullptr; 84 if (CGM.getLangOpts().OpenCL) 85 i8p = 86 llvm::Type::getInt8PtrTy( 87 CGM.getLLVMContext(), C.getTargetAddressSpace(LangAS::opencl_constant)); 88 else 89 i8p = CGM.VoidPtrTy; 90 91 ConstantInitBuilder builder(CGM); 92 auto elements = builder.beginStruct(); 93 94 // reserved 95 elements.addInt(ulong, 0); 96 97 // Size 98 // FIXME: What is the right way to say this doesn't fit? We should give 99 // a user diagnostic in that case. Better fix would be to change the 100 // API to size_t. 101 elements.addInt(ulong, blockInfo.BlockSize.getQuantity()); 102 103 // Optional copy/dispose helpers. 104 if (blockInfo.NeedsCopyDispose) { 105 // copy_func_helper_decl 106 elements.add(buildCopyHelper(CGM, blockInfo)); 107 108 // destroy_func_decl 109 elements.add(buildDisposeHelper(CGM, blockInfo)); 110 } 111 112 // Signature. Mandatory ObjC-style method descriptor @encode sequence. 113 std::string typeAtEncoding = 114 CGM.getContext().getObjCEncodingForBlock(blockInfo.getBlockExpr()); 115 elements.add(llvm::ConstantExpr::getBitCast( 116 CGM.GetAddrOfConstantCString(typeAtEncoding).getPointer(), i8p)); 117 118 // GC layout. 119 if (C.getLangOpts().ObjC1) { 120 if (CGM.getLangOpts().getGC() != LangOptions::NonGC) 121 elements.add(CGM.getObjCRuntime().BuildGCBlockLayout(CGM, blockInfo)); 122 else 123 elements.add(CGM.getObjCRuntime().BuildRCBlockLayout(CGM, blockInfo)); 124 } 125 else 126 elements.addNullPointer(i8p); 127 128 unsigned AddrSpace = 0; 129 if (C.getLangOpts().OpenCL) 130 AddrSpace = C.getTargetAddressSpace(LangAS::opencl_constant); 131 132 llvm::GlobalVariable *global = 133 elements.finishAndCreateGlobal("__block_descriptor_tmp", 134 CGM.getPointerAlign(), 135 /*constant*/ true, 136 llvm::GlobalValue::InternalLinkage, 137 AddrSpace); 138 139 return llvm::ConstantExpr::getBitCast(global, CGM.getBlockDescriptorType()); 140 } 141 142 /* 143 Purely notional variadic template describing the layout of a block. 144 145 template <class _ResultType, class... _ParamTypes, class... _CaptureTypes> 146 struct Block_literal { 147 /// Initialized to one of: 148 /// extern void *_NSConcreteStackBlock[]; 149 /// extern void *_NSConcreteGlobalBlock[]; 150 /// 151 /// In theory, we could start one off malloc'ed by setting 152 /// BLOCK_NEEDS_FREE, giving it a refcount of 1, and using 153 /// this isa: 154 /// extern void *_NSConcreteMallocBlock[]; 155 struct objc_class *isa; 156 157 /// These are the flags (with corresponding bit number) that the 158 /// compiler is actually supposed to know about. 159 /// 25. BLOCK_HAS_COPY_DISPOSE - indicates that the block 160 /// descriptor provides copy and dispose helper functions 161 /// 26. BLOCK_HAS_CXX_OBJ - indicates that there's a captured 162 /// object with a nontrivial destructor or copy constructor 163 /// 28. BLOCK_IS_GLOBAL - indicates that the block is allocated 164 /// as global memory 165 /// 29. BLOCK_USE_STRET - indicates that the block function 166 /// uses stret, which objc_msgSend needs to know about 167 /// 30. BLOCK_HAS_SIGNATURE - indicates that the block has an 168 /// @encoded signature string 169 /// And we're not supposed to manipulate these: 170 /// 24. BLOCK_NEEDS_FREE - indicates that the block has been moved 171 /// to malloc'ed memory 172 /// 27. BLOCK_IS_GC - indicates that the block has been moved to 173 /// to GC-allocated memory 174 /// Additionally, the bottom 16 bits are a reference count which 175 /// should be zero on the stack. 176 int flags; 177 178 /// Reserved; should be zero-initialized. 179 int reserved; 180 181 /// Function pointer generated from block literal. 182 _ResultType (*invoke)(Block_literal *, _ParamTypes...); 183 184 /// Block description metadata generated from block literal. 185 struct Block_descriptor *block_descriptor; 186 187 /// Captured values follow. 188 _CapturesTypes captures...; 189 }; 190 */ 191 192 namespace { 193 /// A chunk of data that we actually have to capture in the block. 194 struct BlockLayoutChunk { 195 CharUnits Alignment; 196 CharUnits Size; 197 Qualifiers::ObjCLifetime Lifetime; 198 const BlockDecl::Capture *Capture; // null for 'this' 199 llvm::Type *Type; 200 QualType FieldType; 201 202 BlockLayoutChunk(CharUnits align, CharUnits size, 203 Qualifiers::ObjCLifetime lifetime, 204 const BlockDecl::Capture *capture, 205 llvm::Type *type, QualType fieldType) 206 : Alignment(align), Size(size), Lifetime(lifetime), 207 Capture(capture), Type(type), FieldType(fieldType) {} 208 209 /// Tell the block info that this chunk has the given field index. 210 void setIndex(CGBlockInfo &info, unsigned index, CharUnits offset) { 211 if (!Capture) { 212 info.CXXThisIndex = index; 213 info.CXXThisOffset = offset; 214 } else { 215 auto C = CGBlockInfo::Capture::makeIndex(index, offset, FieldType); 216 info.Captures.insert({Capture->getVariable(), C}); 217 } 218 } 219 }; 220 221 /// Order by 1) all __strong together 2) next, all byfref together 3) next, 222 /// all __weak together. Preserve descending alignment in all situations. 223 bool operator<(const BlockLayoutChunk &left, const BlockLayoutChunk &right) { 224 if (left.Alignment != right.Alignment) 225 return left.Alignment > right.Alignment; 226 227 auto getPrefOrder = [](const BlockLayoutChunk &chunk) { 228 if (chunk.Capture && chunk.Capture->isByRef()) 229 return 1; 230 if (chunk.Lifetime == Qualifiers::OCL_Strong) 231 return 0; 232 if (chunk.Lifetime == Qualifiers::OCL_Weak) 233 return 2; 234 return 3; 235 }; 236 237 return getPrefOrder(left) < getPrefOrder(right); 238 } 239 } // end anonymous namespace 240 241 /// Determines if the given type is safe for constant capture in C++. 242 static bool isSafeForCXXConstantCapture(QualType type) { 243 const RecordType *recordType = 244 type->getBaseElementTypeUnsafe()->getAs<RecordType>(); 245 246 // Only records can be unsafe. 247 if (!recordType) return true; 248 249 const auto *record = cast<CXXRecordDecl>(recordType->getDecl()); 250 251 // Maintain semantics for classes with non-trivial dtors or copy ctors. 252 if (!record->hasTrivialDestructor()) return false; 253 if (record->hasNonTrivialCopyConstructor()) return false; 254 255 // Otherwise, we just have to make sure there aren't any mutable 256 // fields that might have changed since initialization. 257 return !record->hasMutableFields(); 258 } 259 260 /// It is illegal to modify a const object after initialization. 261 /// Therefore, if a const object has a constant initializer, we don't 262 /// actually need to keep storage for it in the block; we'll just 263 /// rematerialize it at the start of the block function. This is 264 /// acceptable because we make no promises about address stability of 265 /// captured variables. 266 static llvm::Constant *tryCaptureAsConstant(CodeGenModule &CGM, 267 CodeGenFunction *CGF, 268 const VarDecl *var) { 269 // Return if this is a function paramter. We shouldn't try to 270 // rematerialize default arguments of function parameters. 271 if (isa<ParmVarDecl>(var)) 272 return nullptr; 273 274 QualType type = var->getType(); 275 276 // We can only do this if the variable is const. 277 if (!type.isConstQualified()) return nullptr; 278 279 // Furthermore, in C++ we have to worry about mutable fields: 280 // C++ [dcl.type.cv]p4: 281 // Except that any class member declared mutable can be 282 // modified, any attempt to modify a const object during its 283 // lifetime results in undefined behavior. 284 if (CGM.getLangOpts().CPlusPlus && !isSafeForCXXConstantCapture(type)) 285 return nullptr; 286 287 // If the variable doesn't have any initializer (shouldn't this be 288 // invalid?), it's not clear what we should do. Maybe capture as 289 // zero? 290 const Expr *init = var->getInit(); 291 if (!init) return nullptr; 292 293 return CGM.EmitConstantInit(*var, CGF); 294 } 295 296 /// Get the low bit of a nonzero character count. This is the 297 /// alignment of the nth byte if the 0th byte is universally aligned. 298 static CharUnits getLowBit(CharUnits v) { 299 return CharUnits::fromQuantity(v.getQuantity() & (~v.getQuantity() + 1)); 300 } 301 302 static void initializeForBlockHeader(CodeGenModule &CGM, CGBlockInfo &info, 303 SmallVectorImpl<llvm::Type*> &elementTypes) { 304 // The header is basically 'struct { void *; int; int; void *; void *; }'. 305 // Assert that that struct is packed. 306 assert(CGM.getIntSize() <= CGM.getPointerSize()); 307 assert(CGM.getIntAlign() <= CGM.getPointerAlign()); 308 assert((2 * CGM.getIntSize()).isMultipleOf(CGM.getPointerAlign())); 309 310 info.BlockAlign = CGM.getPointerAlign(); 311 info.BlockSize = 3 * CGM.getPointerSize() + 2 * CGM.getIntSize(); 312 313 assert(elementTypes.empty()); 314 elementTypes.push_back(CGM.VoidPtrTy); 315 elementTypes.push_back(CGM.IntTy); 316 elementTypes.push_back(CGM.IntTy); 317 elementTypes.push_back(CGM.VoidPtrTy); 318 elementTypes.push_back(CGM.getBlockDescriptorType()); 319 } 320 321 static QualType getCaptureFieldType(const CodeGenFunction &CGF, 322 const BlockDecl::Capture &CI) { 323 const VarDecl *VD = CI.getVariable(); 324 325 // If the variable is captured by an enclosing block or lambda expression, 326 // use the type of the capture field. 327 if (CGF.BlockInfo && CI.isNested()) 328 return CGF.BlockInfo->getCapture(VD).fieldType(); 329 if (auto *FD = CGF.LambdaCaptureFields.lookup(VD)) 330 return FD->getType(); 331 return VD->getType(); 332 } 333 334 /// Compute the layout of the given block. Attempts to lay the block 335 /// out with minimal space requirements. 336 static void computeBlockInfo(CodeGenModule &CGM, CodeGenFunction *CGF, 337 CGBlockInfo &info) { 338 ASTContext &C = CGM.getContext(); 339 const BlockDecl *block = info.getBlockDecl(); 340 341 SmallVector<llvm::Type*, 8> elementTypes; 342 initializeForBlockHeader(CGM, info, elementTypes); 343 344 if (!block->hasCaptures()) { 345 info.StructureType = 346 llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true); 347 info.CanBeGlobal = true; 348 return; 349 } 350 else if (C.getLangOpts().ObjC1 && 351 CGM.getLangOpts().getGC() == LangOptions::NonGC) 352 info.HasCapturedVariableLayout = true; 353 354 // Collect the layout chunks. 355 SmallVector<BlockLayoutChunk, 16> layout; 356 layout.reserve(block->capturesCXXThis() + 357 (block->capture_end() - block->capture_begin())); 358 359 CharUnits maxFieldAlign; 360 361 // First, 'this'. 362 if (block->capturesCXXThis()) { 363 assert(CGF && CGF->CurFuncDecl && isa<CXXMethodDecl>(CGF->CurFuncDecl) && 364 "Can't capture 'this' outside a method"); 365 QualType thisType = cast<CXXMethodDecl>(CGF->CurFuncDecl)->getThisType(C); 366 367 // Theoretically, this could be in a different address space, so 368 // don't assume standard pointer size/align. 369 llvm::Type *llvmType = CGM.getTypes().ConvertType(thisType); 370 std::pair<CharUnits,CharUnits> tinfo 371 = CGM.getContext().getTypeInfoInChars(thisType); 372 maxFieldAlign = std::max(maxFieldAlign, tinfo.second); 373 374 layout.push_back(BlockLayoutChunk(tinfo.second, tinfo.first, 375 Qualifiers::OCL_None, 376 nullptr, llvmType, thisType)); 377 } 378 379 // Next, all the block captures. 380 for (const auto &CI : block->captures()) { 381 const VarDecl *variable = CI.getVariable(); 382 383 if (CI.isByRef()) { 384 // We have to copy/dispose of the __block reference. 385 info.NeedsCopyDispose = true; 386 387 // Just use void* instead of a pointer to the byref type. 388 CharUnits align = CGM.getPointerAlign(); 389 maxFieldAlign = std::max(maxFieldAlign, align); 390 391 layout.push_back(BlockLayoutChunk(align, CGM.getPointerSize(), 392 Qualifiers::OCL_None, &CI, 393 CGM.VoidPtrTy, variable->getType())); 394 continue; 395 } 396 397 // Otherwise, build a layout chunk with the size and alignment of 398 // the declaration. 399 if (llvm::Constant *constant = tryCaptureAsConstant(CGM, CGF, variable)) { 400 info.Captures[variable] = CGBlockInfo::Capture::makeConstant(constant); 401 continue; 402 } 403 404 // If we have a lifetime qualifier, honor it for capture purposes. 405 // That includes *not* copying it if it's __unsafe_unretained. 406 Qualifiers::ObjCLifetime lifetime = 407 variable->getType().getObjCLifetime(); 408 if (lifetime) { 409 switch (lifetime) { 410 case Qualifiers::OCL_None: llvm_unreachable("impossible"); 411 case Qualifiers::OCL_ExplicitNone: 412 case Qualifiers::OCL_Autoreleasing: 413 break; 414 415 case Qualifiers::OCL_Strong: 416 case Qualifiers::OCL_Weak: 417 info.NeedsCopyDispose = true; 418 } 419 420 // Block pointers require copy/dispose. So do Objective-C pointers. 421 } else if (variable->getType()->isObjCRetainableType()) { 422 // But honor the inert __unsafe_unretained qualifier, which doesn't 423 // actually make it into the type system. 424 if (variable->getType()->isObjCInertUnsafeUnretainedType()) { 425 lifetime = Qualifiers::OCL_ExplicitNone; 426 } else { 427 info.NeedsCopyDispose = true; 428 // used for mrr below. 429 lifetime = Qualifiers::OCL_Strong; 430 } 431 432 // So do types that require non-trivial copy construction. 433 } else if (CI.hasCopyExpr()) { 434 info.NeedsCopyDispose = true; 435 info.HasCXXObject = true; 436 437 // And so do types with destructors. 438 } else if (CGM.getLangOpts().CPlusPlus) { 439 if (const CXXRecordDecl *record = 440 variable->getType()->getAsCXXRecordDecl()) { 441 if (!record->hasTrivialDestructor()) { 442 info.HasCXXObject = true; 443 info.NeedsCopyDispose = true; 444 } 445 } 446 } 447 448 QualType VT = getCaptureFieldType(*CGF, CI); 449 CharUnits size = C.getTypeSizeInChars(VT); 450 CharUnits align = C.getDeclAlign(variable); 451 452 maxFieldAlign = std::max(maxFieldAlign, align); 453 454 llvm::Type *llvmType = 455 CGM.getTypes().ConvertTypeForMem(VT); 456 457 layout.push_back( 458 BlockLayoutChunk(align, size, lifetime, &CI, llvmType, VT)); 459 } 460 461 // If that was everything, we're done here. 462 if (layout.empty()) { 463 info.StructureType = 464 llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true); 465 info.CanBeGlobal = true; 466 return; 467 } 468 469 // Sort the layout by alignment. We have to use a stable sort here 470 // to get reproducible results. There should probably be an 471 // llvm::array_pod_stable_sort. 472 std::stable_sort(layout.begin(), layout.end()); 473 474 // Needed for blocks layout info. 475 info.BlockHeaderForcedGapOffset = info.BlockSize; 476 info.BlockHeaderForcedGapSize = CharUnits::Zero(); 477 478 CharUnits &blockSize = info.BlockSize; 479 info.BlockAlign = std::max(maxFieldAlign, info.BlockAlign); 480 481 // Assuming that the first byte in the header is maximally aligned, 482 // get the alignment of the first byte following the header. 483 CharUnits endAlign = getLowBit(blockSize); 484 485 // If the end of the header isn't satisfactorily aligned for the 486 // maximum thing, look for things that are okay with the header-end 487 // alignment, and keep appending them until we get something that's 488 // aligned right. This algorithm is only guaranteed optimal if 489 // that condition is satisfied at some point; otherwise we can get 490 // things like: 491 // header // next byte has alignment 4 492 // something_with_size_5; // next byte has alignment 1 493 // something_with_alignment_8; 494 // which has 7 bytes of padding, as opposed to the naive solution 495 // which might have less (?). 496 if (endAlign < maxFieldAlign) { 497 SmallVectorImpl<BlockLayoutChunk>::iterator 498 li = layout.begin() + 1, le = layout.end(); 499 500 // Look for something that the header end is already 501 // satisfactorily aligned for. 502 for (; li != le && endAlign < li->Alignment; ++li) 503 ; 504 505 // If we found something that's naturally aligned for the end of 506 // the header, keep adding things... 507 if (li != le) { 508 SmallVectorImpl<BlockLayoutChunk>::iterator first = li; 509 for (; li != le; ++li) { 510 assert(endAlign >= li->Alignment); 511 512 li->setIndex(info, elementTypes.size(), blockSize); 513 elementTypes.push_back(li->Type); 514 blockSize += li->Size; 515 endAlign = getLowBit(blockSize); 516 517 // ...until we get to the alignment of the maximum field. 518 if (endAlign >= maxFieldAlign) { 519 break; 520 } 521 } 522 // Don't re-append everything we just appended. 523 layout.erase(first, li); 524 } 525 } 526 527 assert(endAlign == getLowBit(blockSize)); 528 529 // At this point, we just have to add padding if the end align still 530 // isn't aligned right. 531 if (endAlign < maxFieldAlign) { 532 CharUnits newBlockSize = blockSize.alignTo(maxFieldAlign); 533 CharUnits padding = newBlockSize - blockSize; 534 535 // If we haven't yet added any fields, remember that there was an 536 // initial gap; this need to go into the block layout bit map. 537 if (blockSize == info.BlockHeaderForcedGapOffset) { 538 info.BlockHeaderForcedGapSize = padding; 539 } 540 541 elementTypes.push_back(llvm::ArrayType::get(CGM.Int8Ty, 542 padding.getQuantity())); 543 blockSize = newBlockSize; 544 endAlign = getLowBit(blockSize); // might be > maxFieldAlign 545 } 546 547 assert(endAlign >= maxFieldAlign); 548 assert(endAlign == getLowBit(blockSize)); 549 // Slam everything else on now. This works because they have 550 // strictly decreasing alignment and we expect that size is always a 551 // multiple of alignment. 552 for (SmallVectorImpl<BlockLayoutChunk>::iterator 553 li = layout.begin(), le = layout.end(); li != le; ++li) { 554 if (endAlign < li->Alignment) { 555 // size may not be multiple of alignment. This can only happen with 556 // an over-aligned variable. We will be adding a padding field to 557 // make the size be multiple of alignment. 558 CharUnits padding = li->Alignment - endAlign; 559 elementTypes.push_back(llvm::ArrayType::get(CGM.Int8Ty, 560 padding.getQuantity())); 561 blockSize += padding; 562 endAlign = getLowBit(blockSize); 563 } 564 assert(endAlign >= li->Alignment); 565 li->setIndex(info, elementTypes.size(), blockSize); 566 elementTypes.push_back(li->Type); 567 blockSize += li->Size; 568 endAlign = getLowBit(blockSize); 569 } 570 571 info.StructureType = 572 llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true); 573 } 574 575 /// Enter the scope of a block. This should be run at the entrance to 576 /// a full-expression so that the block's cleanups are pushed at the 577 /// right place in the stack. 578 static void enterBlockScope(CodeGenFunction &CGF, BlockDecl *block) { 579 assert(CGF.HaveInsertPoint()); 580 581 // Allocate the block info and place it at the head of the list. 582 CGBlockInfo &blockInfo = 583 *new CGBlockInfo(block, CGF.CurFn->getName()); 584 blockInfo.NextBlockInfo = CGF.FirstBlockInfo; 585 CGF.FirstBlockInfo = &blockInfo; 586 587 // Compute information about the layout, etc., of this block, 588 // pushing cleanups as necessary. 589 computeBlockInfo(CGF.CGM, &CGF, blockInfo); 590 591 // Nothing else to do if it can be global. 592 if (blockInfo.CanBeGlobal) return; 593 594 // Make the allocation for the block. 595 blockInfo.LocalAddress = CGF.CreateTempAlloca(blockInfo.StructureType, 596 blockInfo.BlockAlign, "block"); 597 598 // If there are cleanups to emit, enter them (but inactive). 599 if (!blockInfo.NeedsCopyDispose) return; 600 601 // Walk through the captures (in order) and find the ones not 602 // captured by constant. 603 for (const auto &CI : block->captures()) { 604 // Ignore __block captures; there's nothing special in the 605 // on-stack block that we need to do for them. 606 if (CI.isByRef()) continue; 607 608 // Ignore variables that are constant-captured. 609 const VarDecl *variable = CI.getVariable(); 610 CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); 611 if (capture.isConstant()) continue; 612 613 // Ignore objects that aren't destructed. 614 QualType VT = getCaptureFieldType(CGF, CI); 615 QualType::DestructionKind dtorKind = VT.isDestructedType(); 616 if (dtorKind == QualType::DK_none) continue; 617 618 CodeGenFunction::Destroyer *destroyer; 619 620 // Block captures count as local values and have imprecise semantics. 621 // They also can't be arrays, so need to worry about that. 622 if (dtorKind == QualType::DK_objc_strong_lifetime) { 623 destroyer = CodeGenFunction::destroyARCStrongImprecise; 624 } else { 625 destroyer = CGF.getDestroyer(dtorKind); 626 } 627 628 // GEP down to the address. 629 Address addr = CGF.Builder.CreateStructGEP(blockInfo.LocalAddress, 630 capture.getIndex(), 631 capture.getOffset()); 632 633 // We can use that GEP as the dominating IP. 634 if (!blockInfo.DominatingIP) 635 blockInfo.DominatingIP = cast<llvm::Instruction>(addr.getPointer()); 636 637 CleanupKind cleanupKind = InactiveNormalCleanup; 638 bool useArrayEHCleanup = CGF.needsEHCleanup(dtorKind); 639 if (useArrayEHCleanup) 640 cleanupKind = InactiveNormalAndEHCleanup; 641 642 CGF.pushDestroy(cleanupKind, addr, VT, 643 destroyer, useArrayEHCleanup); 644 645 // Remember where that cleanup was. 646 capture.setCleanup(CGF.EHStack.stable_begin()); 647 } 648 } 649 650 /// Enter a full-expression with a non-trivial number of objects to 651 /// clean up. This is in this file because, at the moment, the only 652 /// kind of cleanup object is a BlockDecl*. 653 void CodeGenFunction::enterNonTrivialFullExpression(const ExprWithCleanups *E) { 654 assert(E->getNumObjects() != 0); 655 ArrayRef<ExprWithCleanups::CleanupObject> cleanups = E->getObjects(); 656 for (ArrayRef<ExprWithCleanups::CleanupObject>::iterator 657 i = cleanups.begin(), e = cleanups.end(); i != e; ++i) { 658 enterBlockScope(*this, *i); 659 } 660 } 661 662 /// Find the layout for the given block in a linked list and remove it. 663 static CGBlockInfo *findAndRemoveBlockInfo(CGBlockInfo **head, 664 const BlockDecl *block) { 665 while (true) { 666 assert(head && *head); 667 CGBlockInfo *cur = *head; 668 669 // If this is the block we're looking for, splice it out of the list. 670 if (cur->getBlockDecl() == block) { 671 *head = cur->NextBlockInfo; 672 return cur; 673 } 674 675 head = &cur->NextBlockInfo; 676 } 677 } 678 679 /// Destroy a chain of block layouts. 680 void CodeGenFunction::destroyBlockInfos(CGBlockInfo *head) { 681 assert(head && "destroying an empty chain"); 682 do { 683 CGBlockInfo *cur = head; 684 head = cur->NextBlockInfo; 685 delete cur; 686 } while (head != nullptr); 687 } 688 689 /// Emit a block literal expression in the current function. 690 llvm::Value *CodeGenFunction::EmitBlockLiteral(const BlockExpr *blockExpr) { 691 // If the block has no captures, we won't have a pre-computed 692 // layout for it. 693 if (!blockExpr->getBlockDecl()->hasCaptures()) { 694 if (llvm::Constant *Block = CGM.getAddrOfGlobalBlockIfEmitted(blockExpr)) 695 return Block; 696 CGBlockInfo blockInfo(blockExpr->getBlockDecl(), CurFn->getName()); 697 computeBlockInfo(CGM, this, blockInfo); 698 blockInfo.BlockExpression = blockExpr; 699 return EmitBlockLiteral(blockInfo); 700 } 701 702 // Find the block info for this block and take ownership of it. 703 std::unique_ptr<CGBlockInfo> blockInfo; 704 blockInfo.reset(findAndRemoveBlockInfo(&FirstBlockInfo, 705 blockExpr->getBlockDecl())); 706 707 blockInfo->BlockExpression = blockExpr; 708 return EmitBlockLiteral(*blockInfo); 709 } 710 711 llvm::Value *CodeGenFunction::EmitBlockLiteral(const CGBlockInfo &blockInfo) { 712 // Using the computed layout, generate the actual block function. 713 bool isLambdaConv = blockInfo.getBlockDecl()->isConversionFromLambda(); 714 llvm::Constant *blockFn 715 = CodeGenFunction(CGM, true).GenerateBlockFunction(CurGD, blockInfo, 716 LocalDeclMap, 717 isLambdaConv); 718 blockFn = llvm::ConstantExpr::getBitCast(blockFn, VoidPtrTy); 719 720 // If there is nothing to capture, we can emit this as a global block. 721 if (blockInfo.CanBeGlobal) 722 return buildGlobalBlock(CGM, blockInfo, blockFn); 723 724 // Otherwise, we have to emit this as a local block. 725 726 llvm::Constant *isa = 727 (!CGM.getContext().getLangOpts().OpenCL) 728 ? CGM.getNSConcreteStackBlock() 729 : CGM.getNullPointer(cast<llvm::PointerType>( 730 CGM.getNSConcreteStackBlock()->getType()), 731 QualType(getContext().VoidPtrTy)); 732 isa = llvm::ConstantExpr::getBitCast(isa, VoidPtrTy); 733 734 // Build the block descriptor. 735 llvm::Constant *descriptor = buildBlockDescriptor(CGM, blockInfo); 736 737 Address blockAddr = blockInfo.LocalAddress; 738 assert(blockAddr.isValid() && "block has no address!"); 739 740 // Compute the initial on-stack block flags. 741 BlockFlags flags = BLOCK_HAS_SIGNATURE; 742 if (blockInfo.HasCapturedVariableLayout) flags |= BLOCK_HAS_EXTENDED_LAYOUT; 743 if (blockInfo.NeedsCopyDispose) flags |= BLOCK_HAS_COPY_DISPOSE; 744 if (blockInfo.HasCXXObject) flags |= BLOCK_HAS_CXX_OBJ; 745 if (blockInfo.UsesStret) flags |= BLOCK_USE_STRET; 746 747 auto projectField = 748 [&](unsigned index, CharUnits offset, const Twine &name) -> Address { 749 return Builder.CreateStructGEP(blockAddr, index, offset, name); 750 }; 751 auto storeField = 752 [&](llvm::Value *value, unsigned index, CharUnits offset, 753 const Twine &name) { 754 Builder.CreateStore(value, projectField(index, offset, name)); 755 }; 756 757 // Initialize the block header. 758 { 759 // We assume all the header fields are densely packed. 760 unsigned index = 0; 761 CharUnits offset; 762 auto addHeaderField = 763 [&](llvm::Value *value, CharUnits size, const Twine &name) { 764 storeField(value, index, offset, name); 765 offset += size; 766 index++; 767 }; 768 769 addHeaderField(isa, getPointerSize(), "block.isa"); 770 addHeaderField(llvm::ConstantInt::get(IntTy, flags.getBitMask()), 771 getIntSize(), "block.flags"); 772 addHeaderField(llvm::ConstantInt::get(IntTy, 0), 773 getIntSize(), "block.reserved"); 774 addHeaderField(blockFn, getPointerSize(), "block.invoke"); 775 addHeaderField(descriptor, getPointerSize(), "block.descriptor"); 776 } 777 778 // Finally, capture all the values into the block. 779 const BlockDecl *blockDecl = blockInfo.getBlockDecl(); 780 781 // First, 'this'. 782 if (blockDecl->capturesCXXThis()) { 783 Address addr = projectField(blockInfo.CXXThisIndex, blockInfo.CXXThisOffset, 784 "block.captured-this.addr"); 785 Builder.CreateStore(LoadCXXThis(), addr); 786 } 787 788 // Next, captured variables. 789 for (const auto &CI : blockDecl->captures()) { 790 const VarDecl *variable = CI.getVariable(); 791 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); 792 793 // Ignore constant captures. 794 if (capture.isConstant()) continue; 795 796 QualType type = capture.fieldType(); 797 798 // This will be a [[type]]*, except that a byref entry will just be 799 // an i8**. 800 Address blockField = 801 projectField(capture.getIndex(), capture.getOffset(), "block.captured"); 802 803 // Compute the address of the thing we're going to move into the 804 // block literal. 805 Address src = Address::invalid(); 806 807 if (blockDecl->isConversionFromLambda()) { 808 // The lambda capture in a lambda's conversion-to-block-pointer is 809 // special; we'll simply emit it directly. 810 src = Address::invalid(); 811 } else if (CI.isByRef()) { 812 if (BlockInfo && CI.isNested()) { 813 // We need to use the capture from the enclosing block. 814 const CGBlockInfo::Capture &enclosingCapture = 815 BlockInfo->getCapture(variable); 816 817 // This is a [[type]]*, except that a byref entry wil just be an i8**. 818 src = Builder.CreateStructGEP(LoadBlockStruct(), 819 enclosingCapture.getIndex(), 820 enclosingCapture.getOffset(), 821 "block.capture.addr"); 822 } else { 823 auto I = LocalDeclMap.find(variable); 824 assert(I != LocalDeclMap.end()); 825 src = I->second; 826 } 827 } else { 828 DeclRefExpr declRef(const_cast<VarDecl *>(variable), 829 /*RefersToEnclosingVariableOrCapture*/ CI.isNested(), 830 type.getNonReferenceType(), VK_LValue, 831 SourceLocation()); 832 src = EmitDeclRefLValue(&declRef).getAddress(); 833 }; 834 835 // For byrefs, we just write the pointer to the byref struct into 836 // the block field. There's no need to chase the forwarding 837 // pointer at this point, since we're building something that will 838 // live a shorter life than the stack byref anyway. 839 if (CI.isByRef()) { 840 // Get a void* that points to the byref struct. 841 llvm::Value *byrefPointer; 842 if (CI.isNested()) 843 byrefPointer = Builder.CreateLoad(src, "byref.capture"); 844 else 845 byrefPointer = Builder.CreateBitCast(src.getPointer(), VoidPtrTy); 846 847 // Write that void* into the capture field. 848 Builder.CreateStore(byrefPointer, blockField); 849 850 // If we have a copy constructor, evaluate that into the block field. 851 } else if (const Expr *copyExpr = CI.getCopyExpr()) { 852 if (blockDecl->isConversionFromLambda()) { 853 // If we have a lambda conversion, emit the expression 854 // directly into the block instead. 855 AggValueSlot Slot = 856 AggValueSlot::forAddr(blockField, Qualifiers(), 857 AggValueSlot::IsDestructed, 858 AggValueSlot::DoesNotNeedGCBarriers, 859 AggValueSlot::IsNotAliased); 860 EmitAggExpr(copyExpr, Slot); 861 } else { 862 EmitSynthesizedCXXCopyCtor(blockField, src, copyExpr); 863 } 864 865 // If it's a reference variable, copy the reference into the block field. 866 } else if (type->isReferenceType()) { 867 Builder.CreateStore(src.getPointer(), blockField); 868 869 // If this is an ARC __strong block-pointer variable, don't do a 870 // block copy. 871 // 872 // TODO: this can be generalized into the normal initialization logic: 873 // we should never need to do a block-copy when initializing a local 874 // variable, because the local variable's lifetime should be strictly 875 // contained within the stack block's. 876 } else if (type.getObjCLifetime() == Qualifiers::OCL_Strong && 877 type->isBlockPointerType()) { 878 // Load the block and do a simple retain. 879 llvm::Value *value = Builder.CreateLoad(src, "block.captured_block"); 880 value = EmitARCRetainNonBlock(value); 881 882 // Do a primitive store to the block field. 883 Builder.CreateStore(value, blockField); 884 885 // Otherwise, fake up a POD copy into the block field. 886 } else { 887 // Fake up a new variable so that EmitScalarInit doesn't think 888 // we're referring to the variable in its own initializer. 889 ImplicitParamDecl blockFieldPseudoVar(getContext(), /*DC*/ nullptr, 890 SourceLocation(), /*name*/ nullptr, 891 type); 892 893 // We use one of these or the other depending on whether the 894 // reference is nested. 895 DeclRefExpr declRef(const_cast<VarDecl *>(variable), 896 /*RefersToEnclosingVariableOrCapture*/ CI.isNested(), 897 type, VK_LValue, SourceLocation()); 898 899 ImplicitCastExpr l2r(ImplicitCastExpr::OnStack, type, CK_LValueToRValue, 900 &declRef, VK_RValue); 901 // FIXME: Pass a specific location for the expr init so that the store is 902 // attributed to a reasonable location - otherwise it may be attributed to 903 // locations of subexpressions in the initialization. 904 EmitExprAsInit(&l2r, &blockFieldPseudoVar, 905 MakeAddrLValue(blockField, type, AlignmentSource::Decl), 906 /*captured by init*/ false); 907 } 908 909 // Activate the cleanup if layout pushed one. 910 if (!CI.isByRef()) { 911 EHScopeStack::stable_iterator cleanup = capture.getCleanup(); 912 if (cleanup.isValid()) 913 ActivateCleanupBlock(cleanup, blockInfo.DominatingIP); 914 } 915 } 916 917 // Cast to the converted block-pointer type, which happens (somewhat 918 // unfortunately) to be a pointer to function type. 919 llvm::Value *result = Builder.CreatePointerCast( 920 blockAddr.getPointer(), ConvertType(blockInfo.getBlockExpr()->getType())); 921 922 return result; 923 } 924 925 926 llvm::Type *CodeGenModule::getBlockDescriptorType() { 927 if (BlockDescriptorType) 928 return BlockDescriptorType; 929 930 llvm::Type *UnsignedLongTy = 931 getTypes().ConvertType(getContext().UnsignedLongTy); 932 933 // struct __block_descriptor { 934 // unsigned long reserved; 935 // unsigned long block_size; 936 // 937 // // later, the following will be added 938 // 939 // struct { 940 // void (*copyHelper)(); 941 // void (*copyHelper)(); 942 // } helpers; // !!! optional 943 // 944 // const char *signature; // the block signature 945 // const char *layout; // reserved 946 // }; 947 BlockDescriptorType = 948 llvm::StructType::create("struct.__block_descriptor", 949 UnsignedLongTy, UnsignedLongTy, nullptr); 950 951 // Now form a pointer to that. 952 unsigned AddrSpace = 0; 953 if (getLangOpts().OpenCL) 954 AddrSpace = getContext().getTargetAddressSpace(LangAS::opencl_constant); 955 BlockDescriptorType = llvm::PointerType::get(BlockDescriptorType, AddrSpace); 956 return BlockDescriptorType; 957 } 958 959 llvm::Type *CodeGenModule::getGenericBlockLiteralType() { 960 if (GenericBlockLiteralType) 961 return GenericBlockLiteralType; 962 963 llvm::Type *BlockDescPtrTy = getBlockDescriptorType(); 964 965 // struct __block_literal_generic { 966 // void *__isa; 967 // int __flags; 968 // int __reserved; 969 // void (*__invoke)(void *); 970 // struct __block_descriptor *__descriptor; 971 // }; 972 GenericBlockLiteralType = 973 llvm::StructType::create("struct.__block_literal_generic", 974 VoidPtrTy, IntTy, IntTy, VoidPtrTy, 975 BlockDescPtrTy, nullptr); 976 977 return GenericBlockLiteralType; 978 } 979 980 RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr *E, 981 ReturnValueSlot ReturnValue) { 982 const BlockPointerType *BPT = 983 E->getCallee()->getType()->getAs<BlockPointerType>(); 984 985 llvm::Value *BlockPtr = EmitScalarExpr(E->getCallee()); 986 987 // Get a pointer to the generic block literal. 988 // For OpenCL we generate generic AS void ptr to be able to reuse the same 989 // block definition for blocks with captures generated as private AS local 990 // variables and without captures generated as global AS program scope 991 // variables. 992 unsigned AddrSpace = 0; 993 if (getLangOpts().OpenCL) 994 AddrSpace = getContext().getTargetAddressSpace(LangAS::opencl_generic); 995 996 llvm::Type *BlockLiteralTy = 997 llvm::PointerType::get(CGM.getGenericBlockLiteralType(), AddrSpace); 998 999 // Bitcast the callee to a block literal. 1000 BlockPtr = 1001 Builder.CreatePointerCast(BlockPtr, BlockLiteralTy, "block.literal"); 1002 1003 // Get the function pointer from the literal. 1004 llvm::Value *FuncPtr = 1005 Builder.CreateStructGEP(CGM.getGenericBlockLiteralType(), BlockPtr, 3); 1006 1007 1008 // Add the block literal. 1009 CallArgList Args; 1010 1011 QualType VoidPtrQualTy = getContext().VoidPtrTy; 1012 llvm::Type *GenericVoidPtrTy = VoidPtrTy; 1013 if (getLangOpts().OpenCL) { 1014 GenericVoidPtrTy = Builder.getInt8PtrTy( 1015 getContext().getTargetAddressSpace(LangAS::opencl_generic)); 1016 VoidPtrQualTy = 1017 getContext().getPointerType(getContext().getAddrSpaceQualType( 1018 getContext().VoidTy, LangAS::opencl_generic)); 1019 } 1020 1021 BlockPtr = Builder.CreatePointerCast(BlockPtr, GenericVoidPtrTy); 1022 Args.add(RValue::get(BlockPtr), VoidPtrQualTy); 1023 1024 QualType FnType = BPT->getPointeeType(); 1025 1026 // And the rest of the arguments. 1027 EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(), E->arguments()); 1028 1029 // Load the function. 1030 llvm::Value *Func = Builder.CreateAlignedLoad(FuncPtr, getPointerAlign()); 1031 1032 const FunctionType *FuncTy = FnType->castAs<FunctionType>(); 1033 const CGFunctionInfo &FnInfo = 1034 CGM.getTypes().arrangeBlockFunctionCall(Args, FuncTy); 1035 1036 // Cast the function pointer to the right type. 1037 llvm::Type *BlockFTy = CGM.getTypes().GetFunctionType(FnInfo); 1038 1039 llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy); 1040 Func = Builder.CreateBitCast(Func, BlockFTyPtr); 1041 1042 // Prepare the callee. 1043 CGCallee Callee(CGCalleeInfo(), Func); 1044 1045 // And call the block. 1046 return EmitCall(FnInfo, Callee, ReturnValue, Args); 1047 } 1048 1049 Address CodeGenFunction::GetAddrOfBlockDecl(const VarDecl *variable, 1050 bool isByRef) { 1051 assert(BlockInfo && "evaluating block ref without block information?"); 1052 const CGBlockInfo::Capture &capture = BlockInfo->getCapture(variable); 1053 1054 // Handle constant captures. 1055 if (capture.isConstant()) return LocalDeclMap.find(variable)->second; 1056 1057 Address addr = 1058 Builder.CreateStructGEP(LoadBlockStruct(), capture.getIndex(), 1059 capture.getOffset(), "block.capture.addr"); 1060 1061 if (isByRef) { 1062 // addr should be a void** right now. Load, then cast the result 1063 // to byref*. 1064 1065 auto &byrefInfo = getBlockByrefInfo(variable); 1066 addr = Address(Builder.CreateLoad(addr), byrefInfo.ByrefAlignment); 1067 1068 auto byrefPointerType = llvm::PointerType::get(byrefInfo.Type, 0); 1069 addr = Builder.CreateBitCast(addr, byrefPointerType, "byref.addr"); 1070 1071 addr = emitBlockByrefAddress(addr, byrefInfo, /*follow*/ true, 1072 variable->getName()); 1073 } 1074 1075 if (auto refType = capture.fieldType()->getAs<ReferenceType>()) 1076 addr = EmitLoadOfReference(addr, refType); 1077 1078 return addr; 1079 } 1080 1081 void CodeGenModule::setAddrOfGlobalBlock(const BlockExpr *BE, 1082 llvm::Constant *Addr) { 1083 bool Ok = EmittedGlobalBlocks.insert(std::make_pair(BE, Addr)).second; 1084 (void)Ok; 1085 assert(Ok && "Trying to replace an already-existing global block!"); 1086 } 1087 1088 llvm::Constant * 1089 CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *BE, 1090 StringRef Name) { 1091 if (llvm::Constant *Block = getAddrOfGlobalBlockIfEmitted(BE)) 1092 return Block; 1093 1094 CGBlockInfo blockInfo(BE->getBlockDecl(), Name); 1095 blockInfo.BlockExpression = BE; 1096 1097 // Compute information about the layout, etc., of this block. 1098 computeBlockInfo(*this, nullptr, blockInfo); 1099 1100 // Using that metadata, generate the actual block function. 1101 llvm::Constant *blockFn; 1102 { 1103 CodeGenFunction::DeclMapTy LocalDeclMap; 1104 blockFn = CodeGenFunction(*this).GenerateBlockFunction(GlobalDecl(), 1105 blockInfo, 1106 LocalDeclMap, 1107 false); 1108 } 1109 blockFn = llvm::ConstantExpr::getBitCast(blockFn, VoidPtrTy); 1110 1111 return buildGlobalBlock(*this, blockInfo, blockFn); 1112 } 1113 1114 static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM, 1115 const CGBlockInfo &blockInfo, 1116 llvm::Constant *blockFn) { 1117 assert(blockInfo.CanBeGlobal); 1118 // Callers should detect this case on their own: calling this function 1119 // generally requires computing layout information, which is a waste of time 1120 // if we've already emitted this block. 1121 assert(!CGM.getAddrOfGlobalBlockIfEmitted(blockInfo.BlockExpression) && 1122 "Refusing to re-emit a global block."); 1123 1124 // Generate the constants for the block literal initializer. 1125 ConstantInitBuilder builder(CGM); 1126 auto fields = builder.beginStruct(); 1127 1128 // isa 1129 fields.add( 1130 (!CGM.getContext().getLangOpts().OpenCL) 1131 ? CGM.getNSConcreteGlobalBlock() 1132 : CGM.getNullPointer(cast<llvm::PointerType>( 1133 CGM.getNSConcreteGlobalBlock()->getType()), 1134 QualType(CGM.getContext().VoidPtrTy))); 1135 1136 // __flags 1137 BlockFlags flags = BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE; 1138 if (blockInfo.UsesStret) flags |= BLOCK_USE_STRET; 1139 1140 fields.addInt(CGM.IntTy, flags.getBitMask()); 1141 1142 // Reserved 1143 fields.addInt(CGM.IntTy, 0); 1144 1145 // Function 1146 fields.add(blockFn); 1147 1148 // Descriptor 1149 fields.add(buildBlockDescriptor(CGM, blockInfo)); 1150 1151 unsigned AddrSpace = 0; 1152 if (CGM.getContext().getLangOpts().OpenCL) 1153 AddrSpace = CGM.getContext().getTargetAddressSpace(LangAS::opencl_global); 1154 1155 llvm::Constant *literal = fields.finishAndCreateGlobal( 1156 "__block_literal_global", blockInfo.BlockAlign, 1157 /*constant*/ true, llvm::GlobalVariable::InternalLinkage, AddrSpace); 1158 1159 // Return a constant of the appropriately-casted type. 1160 llvm::Type *RequiredType = 1161 CGM.getTypes().ConvertType(blockInfo.getBlockExpr()->getType()); 1162 llvm::Constant *Result = 1163 llvm::ConstantExpr::getPointerCast(literal, RequiredType); 1164 CGM.setAddrOfGlobalBlock(blockInfo.BlockExpression, Result); 1165 return Result; 1166 } 1167 1168 void CodeGenFunction::setBlockContextParameter(const ImplicitParamDecl *D, 1169 unsigned argNum, 1170 llvm::Value *arg) { 1171 assert(BlockInfo && "not emitting prologue of block invocation function?!"); 1172 1173 llvm::Value *localAddr = nullptr; 1174 if (CGM.getCodeGenOpts().OptimizationLevel == 0) { 1175 // Allocate a stack slot to let the debug info survive the RA. 1176 Address alloc = CreateMemTemp(D->getType(), D->getName() + ".addr"); 1177 Builder.CreateStore(arg, alloc); 1178 localAddr = Builder.CreateLoad(alloc); 1179 } 1180 1181 if (CGDebugInfo *DI = getDebugInfo()) { 1182 if (CGM.getCodeGenOpts().getDebugInfo() >= 1183 codegenoptions::LimitedDebugInfo) { 1184 DI->setLocation(D->getLocation()); 1185 DI->EmitDeclareOfBlockLiteralArgVariable(*BlockInfo, arg, argNum, 1186 localAddr, Builder); 1187 } 1188 } 1189 1190 SourceLocation StartLoc = BlockInfo->getBlockExpr()->getBody()->getLocStart(); 1191 ApplyDebugLocation Scope(*this, StartLoc); 1192 1193 // Instead of messing around with LocalDeclMap, just set the value 1194 // directly as BlockPointer. 1195 BlockPointer = Builder.CreatePointerCast( 1196 arg, 1197 BlockInfo->StructureType->getPointerTo( 1198 getContext().getLangOpts().OpenCL 1199 ? getContext().getTargetAddressSpace(LangAS::opencl_generic) 1200 : 0), 1201 "block"); 1202 } 1203 1204 Address CodeGenFunction::LoadBlockStruct() { 1205 assert(BlockInfo && "not in a block invocation function!"); 1206 assert(BlockPointer && "no block pointer set!"); 1207 return Address(BlockPointer, BlockInfo->BlockAlign); 1208 } 1209 1210 llvm::Function * 1211 CodeGenFunction::GenerateBlockFunction(GlobalDecl GD, 1212 const CGBlockInfo &blockInfo, 1213 const DeclMapTy &ldm, 1214 bool IsLambdaConversionToBlock) { 1215 const BlockDecl *blockDecl = blockInfo.getBlockDecl(); 1216 1217 CurGD = GD; 1218 1219 CurEHLocation = blockInfo.getBlockExpr()->getLocEnd(); 1220 1221 BlockInfo = &blockInfo; 1222 1223 // Arrange for local static and local extern declarations to appear 1224 // to be local to this function as well, in case they're directly 1225 // referenced in a block. 1226 for (DeclMapTy::const_iterator i = ldm.begin(), e = ldm.end(); i != e; ++i) { 1227 const auto *var = dyn_cast<VarDecl>(i->first); 1228 if (var && !var->hasLocalStorage()) 1229 setAddrOfLocalVar(var, i->second); 1230 } 1231 1232 // Begin building the function declaration. 1233 1234 // Build the argument list. 1235 FunctionArgList args; 1236 1237 // The first argument is the block pointer. Just take it as a void* 1238 // and cast it later. 1239 QualType selfTy = getContext().VoidPtrTy; 1240 1241 // For OpenCL passed block pointer can be private AS local variable or 1242 // global AS program scope variable (for the case with and without captures). 1243 // Generic AS is used therefore to be able to accomodate both private and 1244 // generic AS in one implementation. 1245 if (getLangOpts().OpenCL) 1246 selfTy = getContext().getPointerType(getContext().getAddrSpaceQualType( 1247 getContext().VoidTy, LangAS::opencl_generic)); 1248 1249 IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor"); 1250 1251 ImplicitParamDecl selfDecl(getContext(), const_cast<BlockDecl*>(blockDecl), 1252 SourceLocation(), II, selfTy); 1253 args.push_back(&selfDecl); 1254 1255 // Now add the rest of the parameters. 1256 args.append(blockDecl->param_begin(), blockDecl->param_end()); 1257 1258 // Create the function declaration. 1259 const FunctionProtoType *fnType = blockInfo.getBlockExpr()->getFunctionType(); 1260 const CGFunctionInfo &fnInfo = 1261 CGM.getTypes().arrangeBlockFunctionDeclaration(fnType, args); 1262 if (CGM.ReturnSlotInterferesWithArgs(fnInfo)) 1263 blockInfo.UsesStret = true; 1264 1265 llvm::FunctionType *fnLLVMType = CGM.getTypes().GetFunctionType(fnInfo); 1266 1267 StringRef name = CGM.getBlockMangledName(GD, blockDecl); 1268 llvm::Function *fn = llvm::Function::Create( 1269 fnLLVMType, llvm::GlobalValue::InternalLinkage, name, &CGM.getModule()); 1270 CGM.SetInternalFunctionAttributes(blockDecl, fn, fnInfo); 1271 1272 // Begin generating the function. 1273 StartFunction(blockDecl, fnType->getReturnType(), fn, fnInfo, args, 1274 blockDecl->getLocation(), 1275 blockInfo.getBlockExpr()->getBody()->getLocStart()); 1276 1277 // Okay. Undo some of what StartFunction did. 1278 1279 // At -O0 we generate an explicit alloca for the BlockPointer, so the RA 1280 // won't delete the dbg.declare intrinsics for captured variables. 1281 llvm::Value *BlockPointerDbgLoc = BlockPointer; 1282 if (CGM.getCodeGenOpts().OptimizationLevel == 0) { 1283 // Allocate a stack slot for it, so we can point the debugger to it 1284 Address Alloca = CreateTempAlloca(BlockPointer->getType(), 1285 getPointerAlign(), 1286 "block.addr"); 1287 // Set the DebugLocation to empty, so the store is recognized as a 1288 // frame setup instruction by llvm::DwarfDebug::beginFunction(). 1289 auto NL = ApplyDebugLocation::CreateEmpty(*this); 1290 Builder.CreateStore(BlockPointer, Alloca); 1291 BlockPointerDbgLoc = Alloca.getPointer(); 1292 } 1293 1294 // If we have a C++ 'this' reference, go ahead and force it into 1295 // existence now. 1296 if (blockDecl->capturesCXXThis()) { 1297 Address addr = 1298 Builder.CreateStructGEP(LoadBlockStruct(), blockInfo.CXXThisIndex, 1299 blockInfo.CXXThisOffset, "block.captured-this"); 1300 CXXThisValue = Builder.CreateLoad(addr, "this"); 1301 } 1302 1303 // Also force all the constant captures. 1304 for (const auto &CI : blockDecl->captures()) { 1305 const VarDecl *variable = CI.getVariable(); 1306 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); 1307 if (!capture.isConstant()) continue; 1308 1309 CharUnits align = getContext().getDeclAlign(variable); 1310 Address alloca = 1311 CreateMemTemp(variable->getType(), align, "block.captured-const"); 1312 1313 Builder.CreateStore(capture.getConstant(), alloca); 1314 1315 setAddrOfLocalVar(variable, alloca); 1316 } 1317 1318 // Save a spot to insert the debug information for all the DeclRefExprs. 1319 llvm::BasicBlock *entry = Builder.GetInsertBlock(); 1320 llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint(); 1321 --entry_ptr; 1322 1323 if (IsLambdaConversionToBlock) 1324 EmitLambdaBlockInvokeBody(); 1325 else { 1326 PGO.assignRegionCounters(GlobalDecl(blockDecl), fn); 1327 incrementProfileCounter(blockDecl->getBody()); 1328 EmitStmt(blockDecl->getBody()); 1329 } 1330 1331 // Remember where we were... 1332 llvm::BasicBlock *resume = Builder.GetInsertBlock(); 1333 1334 // Go back to the entry. 1335 ++entry_ptr; 1336 Builder.SetInsertPoint(entry, entry_ptr); 1337 1338 // Emit debug information for all the DeclRefExprs. 1339 // FIXME: also for 'this' 1340 if (CGDebugInfo *DI = getDebugInfo()) { 1341 for (const auto &CI : blockDecl->captures()) { 1342 const VarDecl *variable = CI.getVariable(); 1343 DI->EmitLocation(Builder, variable->getLocation()); 1344 1345 if (CGM.getCodeGenOpts().getDebugInfo() >= 1346 codegenoptions::LimitedDebugInfo) { 1347 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); 1348 if (capture.isConstant()) { 1349 auto addr = LocalDeclMap.find(variable)->second; 1350 DI->EmitDeclareOfAutoVariable(variable, addr.getPointer(), 1351 Builder); 1352 continue; 1353 } 1354 1355 DI->EmitDeclareOfBlockDeclRefVariable( 1356 variable, BlockPointerDbgLoc, Builder, blockInfo, 1357 entry_ptr == entry->end() ? nullptr : &*entry_ptr); 1358 } 1359 } 1360 // Recover location if it was changed in the above loop. 1361 DI->EmitLocation(Builder, 1362 cast<CompoundStmt>(blockDecl->getBody())->getRBracLoc()); 1363 } 1364 1365 // And resume where we left off. 1366 if (resume == nullptr) 1367 Builder.ClearInsertionPoint(); 1368 else 1369 Builder.SetInsertPoint(resume); 1370 1371 FinishFunction(cast<CompoundStmt>(blockDecl->getBody())->getRBracLoc()); 1372 1373 return fn; 1374 } 1375 1376 /// Generate the copy-helper function for a block closure object: 1377 /// static void block_copy_helper(block_t *dst, block_t *src); 1378 /// The runtime will have previously initialized 'dst' by doing a 1379 /// bit-copy of 'src'. 1380 /// 1381 /// Note that this copies an entire block closure object to the heap; 1382 /// it should not be confused with a 'byref copy helper', which moves 1383 /// the contents of an individual __block variable to the heap. 1384 llvm::Constant * 1385 CodeGenFunction::GenerateCopyHelperFunction(const CGBlockInfo &blockInfo) { 1386 ASTContext &C = getContext(); 1387 1388 FunctionArgList args; 1389 ImplicitParamDecl dstDecl(getContext(), nullptr, SourceLocation(), nullptr, 1390 C.VoidPtrTy); 1391 args.push_back(&dstDecl); 1392 ImplicitParamDecl srcDecl(getContext(), nullptr, SourceLocation(), nullptr, 1393 C.VoidPtrTy); 1394 args.push_back(&srcDecl); 1395 1396 const CGFunctionInfo &FI = 1397 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, args); 1398 1399 // FIXME: it would be nice if these were mergeable with things with 1400 // identical semantics. 1401 llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI); 1402 1403 llvm::Function *Fn = 1404 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 1405 "__copy_helper_block_", &CGM.getModule()); 1406 1407 IdentifierInfo *II 1408 = &CGM.getContext().Idents.get("__copy_helper_block_"); 1409 1410 FunctionDecl *FD = FunctionDecl::Create(C, 1411 C.getTranslationUnitDecl(), 1412 SourceLocation(), 1413 SourceLocation(), II, C.VoidTy, 1414 nullptr, SC_Static, 1415 false, 1416 false); 1417 1418 CGM.SetInternalFunctionAttributes(nullptr, Fn, FI); 1419 1420 auto NL = ApplyDebugLocation::CreateEmpty(*this); 1421 StartFunction(FD, C.VoidTy, Fn, FI, args); 1422 // Create a scope with an artificial location for the body of this function. 1423 auto AL = ApplyDebugLocation::CreateArtificial(*this); 1424 llvm::Type *structPtrTy = blockInfo.StructureType->getPointerTo(); 1425 1426 Address src = GetAddrOfLocalVar(&srcDecl); 1427 src = Address(Builder.CreateLoad(src), blockInfo.BlockAlign); 1428 src = Builder.CreateBitCast(src, structPtrTy, "block.source"); 1429 1430 Address dst = GetAddrOfLocalVar(&dstDecl); 1431 dst = Address(Builder.CreateLoad(dst), blockInfo.BlockAlign); 1432 dst = Builder.CreateBitCast(dst, structPtrTy, "block.dest"); 1433 1434 const BlockDecl *blockDecl = blockInfo.getBlockDecl(); 1435 1436 for (const auto &CI : blockDecl->captures()) { 1437 const VarDecl *variable = CI.getVariable(); 1438 QualType type = variable->getType(); 1439 1440 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); 1441 if (capture.isConstant()) continue; 1442 1443 const Expr *copyExpr = CI.getCopyExpr(); 1444 BlockFieldFlags flags; 1445 1446 bool useARCWeakCopy = false; 1447 bool useARCStrongCopy = false; 1448 1449 if (copyExpr) { 1450 assert(!CI.isByRef()); 1451 // don't bother computing flags 1452 1453 } else if (CI.isByRef()) { 1454 flags = BLOCK_FIELD_IS_BYREF; 1455 if (type.isObjCGCWeak()) 1456 flags |= BLOCK_FIELD_IS_WEAK; 1457 1458 } else if (type->isObjCRetainableType()) { 1459 flags = BLOCK_FIELD_IS_OBJECT; 1460 bool isBlockPointer = type->isBlockPointerType(); 1461 if (isBlockPointer) 1462 flags = BLOCK_FIELD_IS_BLOCK; 1463 1464 // Special rules for ARC captures: 1465 Qualifiers qs = type.getQualifiers(); 1466 1467 // We need to register __weak direct captures with the runtime. 1468 if (qs.getObjCLifetime() == Qualifiers::OCL_Weak) { 1469 useARCWeakCopy = true; 1470 1471 // We need to retain the copied value for __strong direct captures. 1472 } else if (qs.getObjCLifetime() == Qualifiers::OCL_Strong) { 1473 // If it's a block pointer, we have to copy the block and 1474 // assign that to the destination pointer, so we might as 1475 // well use _Block_object_assign. Otherwise we can avoid that. 1476 if (!isBlockPointer) 1477 useARCStrongCopy = true; 1478 1479 // Non-ARC captures of retainable pointers are strong and 1480 // therefore require a call to _Block_object_assign. 1481 } else if (!qs.getObjCLifetime() && !getLangOpts().ObjCAutoRefCount) { 1482 // fall through 1483 1484 // Otherwise the memcpy is fine. 1485 } else { 1486 continue; 1487 } 1488 1489 // For all other types, the memcpy is fine. 1490 } else { 1491 continue; 1492 } 1493 1494 unsigned index = capture.getIndex(); 1495 Address srcField = Builder.CreateStructGEP(src, index, capture.getOffset()); 1496 Address dstField = Builder.CreateStructGEP(dst, index, capture.getOffset()); 1497 1498 // If there's an explicit copy expression, we do that. 1499 if (copyExpr) { 1500 EmitSynthesizedCXXCopyCtor(dstField, srcField, copyExpr); 1501 } else if (useARCWeakCopy) { 1502 EmitARCCopyWeak(dstField, srcField); 1503 } else { 1504 llvm::Value *srcValue = Builder.CreateLoad(srcField, "blockcopy.src"); 1505 if (useARCStrongCopy) { 1506 // At -O0, store null into the destination field (so that the 1507 // storeStrong doesn't over-release) and then call storeStrong. 1508 // This is a workaround to not having an initStrong call. 1509 if (CGM.getCodeGenOpts().OptimizationLevel == 0) { 1510 auto *ty = cast<llvm::PointerType>(srcValue->getType()); 1511 llvm::Value *null = llvm::ConstantPointerNull::get(ty); 1512 Builder.CreateStore(null, dstField); 1513 EmitARCStoreStrongCall(dstField, srcValue, true); 1514 1515 // With optimization enabled, take advantage of the fact that 1516 // the blocks runtime guarantees a memcpy of the block data, and 1517 // just emit a retain of the src field. 1518 } else { 1519 EmitARCRetainNonBlock(srcValue); 1520 1521 // We don't need this anymore, so kill it. It's not quite 1522 // worth the annoyance to avoid creating it in the first place. 1523 cast<llvm::Instruction>(dstField.getPointer())->eraseFromParent(); 1524 } 1525 } else { 1526 srcValue = Builder.CreateBitCast(srcValue, VoidPtrTy); 1527 llvm::Value *dstAddr = 1528 Builder.CreateBitCast(dstField.getPointer(), VoidPtrTy); 1529 llvm::Value *args[] = { 1530 dstAddr, srcValue, llvm::ConstantInt::get(Int32Ty, flags.getBitMask()) 1531 }; 1532 1533 bool copyCanThrow = false; 1534 if (CI.isByRef() && variable->getType()->getAsCXXRecordDecl()) { 1535 const Expr *copyExpr = 1536 CGM.getContext().getBlockVarCopyInits(variable); 1537 if (copyExpr) { 1538 copyCanThrow = true; // FIXME: reuse the noexcept logic 1539 } 1540 } 1541 1542 if (copyCanThrow) { 1543 EmitRuntimeCallOrInvoke(CGM.getBlockObjectAssign(), args); 1544 } else { 1545 EmitNounwindRuntimeCall(CGM.getBlockObjectAssign(), args); 1546 } 1547 } 1548 } 1549 } 1550 1551 FinishFunction(); 1552 1553 return llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); 1554 } 1555 1556 /// Generate the destroy-helper function for a block closure object: 1557 /// static void block_destroy_helper(block_t *theBlock); 1558 /// 1559 /// Note that this destroys a heap-allocated block closure object; 1560 /// it should not be confused with a 'byref destroy helper', which 1561 /// destroys the heap-allocated contents of an individual __block 1562 /// variable. 1563 llvm::Constant * 1564 CodeGenFunction::GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo) { 1565 ASTContext &C = getContext(); 1566 1567 FunctionArgList args; 1568 ImplicitParamDecl srcDecl(getContext(), nullptr, SourceLocation(), nullptr, 1569 C.VoidPtrTy); 1570 args.push_back(&srcDecl); 1571 1572 const CGFunctionInfo &FI = 1573 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, args); 1574 1575 // FIXME: We'd like to put these into a mergable by content, with 1576 // internal linkage. 1577 llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI); 1578 1579 llvm::Function *Fn = 1580 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 1581 "__destroy_helper_block_", &CGM.getModule()); 1582 1583 IdentifierInfo *II 1584 = &CGM.getContext().Idents.get("__destroy_helper_block_"); 1585 1586 FunctionDecl *FD = FunctionDecl::Create(C, C.getTranslationUnitDecl(), 1587 SourceLocation(), 1588 SourceLocation(), II, C.VoidTy, 1589 nullptr, SC_Static, 1590 false, false); 1591 1592 CGM.SetInternalFunctionAttributes(nullptr, Fn, FI); 1593 1594 // Create a scope with an artificial location for the body of this function. 1595 auto NL = ApplyDebugLocation::CreateEmpty(*this); 1596 StartFunction(FD, C.VoidTy, Fn, FI, args); 1597 auto AL = ApplyDebugLocation::CreateArtificial(*this); 1598 1599 llvm::Type *structPtrTy = blockInfo.StructureType->getPointerTo(); 1600 1601 Address src = GetAddrOfLocalVar(&srcDecl); 1602 src = Address(Builder.CreateLoad(src), blockInfo.BlockAlign); 1603 src = Builder.CreateBitCast(src, structPtrTy, "block"); 1604 1605 const BlockDecl *blockDecl = blockInfo.getBlockDecl(); 1606 1607 CodeGenFunction::RunCleanupsScope cleanups(*this); 1608 1609 for (const auto &CI : blockDecl->captures()) { 1610 const VarDecl *variable = CI.getVariable(); 1611 QualType type = variable->getType(); 1612 1613 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); 1614 if (capture.isConstant()) continue; 1615 1616 BlockFieldFlags flags; 1617 const CXXDestructorDecl *dtor = nullptr; 1618 1619 bool useARCWeakDestroy = false; 1620 bool useARCStrongDestroy = false; 1621 1622 if (CI.isByRef()) { 1623 flags = BLOCK_FIELD_IS_BYREF; 1624 if (type.isObjCGCWeak()) 1625 flags |= BLOCK_FIELD_IS_WEAK; 1626 } else if (const CXXRecordDecl *record = type->getAsCXXRecordDecl()) { 1627 if (record->hasTrivialDestructor()) 1628 continue; 1629 dtor = record->getDestructor(); 1630 } else if (type->isObjCRetainableType()) { 1631 flags = BLOCK_FIELD_IS_OBJECT; 1632 if (type->isBlockPointerType()) 1633 flags = BLOCK_FIELD_IS_BLOCK; 1634 1635 // Special rules for ARC captures. 1636 Qualifiers qs = type.getQualifiers(); 1637 1638 // Use objc_storeStrong for __strong direct captures; the 1639 // dynamic tools really like it when we do this. 1640 if (qs.getObjCLifetime() == Qualifiers::OCL_Strong) { 1641 useARCStrongDestroy = true; 1642 1643 // Support __weak direct captures. 1644 } else if (qs.getObjCLifetime() == Qualifiers::OCL_Weak) { 1645 useARCWeakDestroy = true; 1646 1647 // Non-ARC captures are strong, and we need to use _Block_object_dispose. 1648 } else if (!qs.hasObjCLifetime() && !getLangOpts().ObjCAutoRefCount) { 1649 // fall through 1650 1651 // Otherwise, we have nothing to do. 1652 } else { 1653 continue; 1654 } 1655 } else { 1656 continue; 1657 } 1658 1659 Address srcField = 1660 Builder.CreateStructGEP(src, capture.getIndex(), capture.getOffset()); 1661 1662 // If there's an explicit copy expression, we do that. 1663 if (dtor) { 1664 PushDestructorCleanup(dtor, srcField); 1665 1666 // If this is a __weak capture, emit the release directly. 1667 } else if (useARCWeakDestroy) { 1668 EmitARCDestroyWeak(srcField); 1669 1670 // Destroy strong objects with a call if requested. 1671 } else if (useARCStrongDestroy) { 1672 EmitARCDestroyStrong(srcField, ARCImpreciseLifetime); 1673 1674 // Otherwise we call _Block_object_dispose. It wouldn't be too 1675 // hard to just emit this as a cleanup if we wanted to make sure 1676 // that things were done in reverse. 1677 } else { 1678 llvm::Value *value = Builder.CreateLoad(srcField); 1679 value = Builder.CreateBitCast(value, VoidPtrTy); 1680 BuildBlockRelease(value, flags); 1681 } 1682 } 1683 1684 cleanups.ForceCleanup(); 1685 1686 FinishFunction(); 1687 1688 return llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy); 1689 } 1690 1691 namespace { 1692 1693 /// Emits the copy/dispose helper functions for a __block object of id type. 1694 class ObjectByrefHelpers final : public BlockByrefHelpers { 1695 BlockFieldFlags Flags; 1696 1697 public: 1698 ObjectByrefHelpers(CharUnits alignment, BlockFieldFlags flags) 1699 : BlockByrefHelpers(alignment), Flags(flags) {} 1700 1701 void emitCopy(CodeGenFunction &CGF, Address destField, 1702 Address srcField) override { 1703 destField = CGF.Builder.CreateBitCast(destField, CGF.VoidPtrTy); 1704 1705 srcField = CGF.Builder.CreateBitCast(srcField, CGF.VoidPtrPtrTy); 1706 llvm::Value *srcValue = CGF.Builder.CreateLoad(srcField); 1707 1708 unsigned flags = (Flags | BLOCK_BYREF_CALLER).getBitMask(); 1709 1710 llvm::Value *flagsVal = llvm::ConstantInt::get(CGF.Int32Ty, flags); 1711 llvm::Value *fn = CGF.CGM.getBlockObjectAssign(); 1712 1713 llvm::Value *args[] = { destField.getPointer(), srcValue, flagsVal }; 1714 CGF.EmitNounwindRuntimeCall(fn, args); 1715 } 1716 1717 void emitDispose(CodeGenFunction &CGF, Address field) override { 1718 field = CGF.Builder.CreateBitCast(field, CGF.Int8PtrTy->getPointerTo(0)); 1719 llvm::Value *value = CGF.Builder.CreateLoad(field); 1720 1721 CGF.BuildBlockRelease(value, Flags | BLOCK_BYREF_CALLER); 1722 } 1723 1724 void profileImpl(llvm::FoldingSetNodeID &id) const override { 1725 id.AddInteger(Flags.getBitMask()); 1726 } 1727 }; 1728 1729 /// Emits the copy/dispose helpers for an ARC __block __weak variable. 1730 class ARCWeakByrefHelpers final : public BlockByrefHelpers { 1731 public: 1732 ARCWeakByrefHelpers(CharUnits alignment) : BlockByrefHelpers(alignment) {} 1733 1734 void emitCopy(CodeGenFunction &CGF, Address destField, 1735 Address srcField) override { 1736 CGF.EmitARCMoveWeak(destField, srcField); 1737 } 1738 1739 void emitDispose(CodeGenFunction &CGF, Address field) override { 1740 CGF.EmitARCDestroyWeak(field); 1741 } 1742 1743 void profileImpl(llvm::FoldingSetNodeID &id) const override { 1744 // 0 is distinguishable from all pointers and byref flags 1745 id.AddInteger(0); 1746 } 1747 }; 1748 1749 /// Emits the copy/dispose helpers for an ARC __block __strong variable 1750 /// that's not of block-pointer type. 1751 class ARCStrongByrefHelpers final : public BlockByrefHelpers { 1752 public: 1753 ARCStrongByrefHelpers(CharUnits alignment) : BlockByrefHelpers(alignment) {} 1754 1755 void emitCopy(CodeGenFunction &CGF, Address destField, 1756 Address srcField) override { 1757 // Do a "move" by copying the value and then zeroing out the old 1758 // variable. 1759 1760 llvm::Value *value = CGF.Builder.CreateLoad(srcField); 1761 1762 llvm::Value *null = 1763 llvm::ConstantPointerNull::get(cast<llvm::PointerType>(value->getType())); 1764 1765 if (CGF.CGM.getCodeGenOpts().OptimizationLevel == 0) { 1766 CGF.Builder.CreateStore(null, destField); 1767 CGF.EmitARCStoreStrongCall(destField, value, /*ignored*/ true); 1768 CGF.EmitARCStoreStrongCall(srcField, null, /*ignored*/ true); 1769 return; 1770 } 1771 CGF.Builder.CreateStore(value, destField); 1772 CGF.Builder.CreateStore(null, srcField); 1773 } 1774 1775 void emitDispose(CodeGenFunction &CGF, Address field) override { 1776 CGF.EmitARCDestroyStrong(field, ARCImpreciseLifetime); 1777 } 1778 1779 void profileImpl(llvm::FoldingSetNodeID &id) const override { 1780 // 1 is distinguishable from all pointers and byref flags 1781 id.AddInteger(1); 1782 } 1783 }; 1784 1785 /// Emits the copy/dispose helpers for an ARC __block __strong 1786 /// variable that's of block-pointer type. 1787 class ARCStrongBlockByrefHelpers final : public BlockByrefHelpers { 1788 public: 1789 ARCStrongBlockByrefHelpers(CharUnits alignment) 1790 : BlockByrefHelpers(alignment) {} 1791 1792 void emitCopy(CodeGenFunction &CGF, Address destField, 1793 Address srcField) override { 1794 // Do the copy with objc_retainBlock; that's all that 1795 // _Block_object_assign would do anyway, and we'd have to pass the 1796 // right arguments to make sure it doesn't get no-op'ed. 1797 llvm::Value *oldValue = CGF.Builder.CreateLoad(srcField); 1798 llvm::Value *copy = CGF.EmitARCRetainBlock(oldValue, /*mandatory*/ true); 1799 CGF.Builder.CreateStore(copy, destField); 1800 } 1801 1802 void emitDispose(CodeGenFunction &CGF, Address field) override { 1803 CGF.EmitARCDestroyStrong(field, ARCImpreciseLifetime); 1804 } 1805 1806 void profileImpl(llvm::FoldingSetNodeID &id) const override { 1807 // 2 is distinguishable from all pointers and byref flags 1808 id.AddInteger(2); 1809 } 1810 }; 1811 1812 /// Emits the copy/dispose helpers for a __block variable with a 1813 /// nontrivial copy constructor or destructor. 1814 class CXXByrefHelpers final : public BlockByrefHelpers { 1815 QualType VarType; 1816 const Expr *CopyExpr; 1817 1818 public: 1819 CXXByrefHelpers(CharUnits alignment, QualType type, 1820 const Expr *copyExpr) 1821 : BlockByrefHelpers(alignment), VarType(type), CopyExpr(copyExpr) {} 1822 1823 bool needsCopy() const override { return CopyExpr != nullptr; } 1824 void emitCopy(CodeGenFunction &CGF, Address destField, 1825 Address srcField) override { 1826 if (!CopyExpr) return; 1827 CGF.EmitSynthesizedCXXCopyCtor(destField, srcField, CopyExpr); 1828 } 1829 1830 void emitDispose(CodeGenFunction &CGF, Address field) override { 1831 EHScopeStack::stable_iterator cleanupDepth = CGF.EHStack.stable_begin(); 1832 CGF.PushDestructorCleanup(VarType, field); 1833 CGF.PopCleanupBlocks(cleanupDepth); 1834 } 1835 1836 void profileImpl(llvm::FoldingSetNodeID &id) const override { 1837 id.AddPointer(VarType.getCanonicalType().getAsOpaquePtr()); 1838 } 1839 }; 1840 } // end anonymous namespace 1841 1842 static llvm::Constant * 1843 generateByrefCopyHelper(CodeGenFunction &CGF, const BlockByrefInfo &byrefInfo, 1844 BlockByrefHelpers &generator) { 1845 ASTContext &Context = CGF.getContext(); 1846 1847 QualType R = Context.VoidTy; 1848 1849 FunctionArgList args; 1850 ImplicitParamDecl dst(CGF.getContext(), nullptr, SourceLocation(), nullptr, 1851 Context.VoidPtrTy); 1852 args.push_back(&dst); 1853 1854 ImplicitParamDecl src(CGF.getContext(), nullptr, SourceLocation(), nullptr, 1855 Context.VoidPtrTy); 1856 args.push_back(&src); 1857 1858 const CGFunctionInfo &FI = 1859 CGF.CGM.getTypes().arrangeBuiltinFunctionDeclaration(R, args); 1860 1861 llvm::FunctionType *LTy = CGF.CGM.getTypes().GetFunctionType(FI); 1862 1863 // FIXME: We'd like to put these into a mergable by content, with 1864 // internal linkage. 1865 llvm::Function *Fn = 1866 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 1867 "__Block_byref_object_copy_", &CGF.CGM.getModule()); 1868 1869 IdentifierInfo *II 1870 = &Context.Idents.get("__Block_byref_object_copy_"); 1871 1872 FunctionDecl *FD = FunctionDecl::Create(Context, 1873 Context.getTranslationUnitDecl(), 1874 SourceLocation(), 1875 SourceLocation(), II, R, nullptr, 1876 SC_Static, 1877 false, false); 1878 1879 CGF.CGM.SetInternalFunctionAttributes(nullptr, Fn, FI); 1880 1881 CGF.StartFunction(FD, R, Fn, FI, args); 1882 1883 if (generator.needsCopy()) { 1884 llvm::Type *byrefPtrType = byrefInfo.Type->getPointerTo(0); 1885 1886 // dst->x 1887 Address destField = CGF.GetAddrOfLocalVar(&dst); 1888 destField = Address(CGF.Builder.CreateLoad(destField), 1889 byrefInfo.ByrefAlignment); 1890 destField = CGF.Builder.CreateBitCast(destField, byrefPtrType); 1891 destField = CGF.emitBlockByrefAddress(destField, byrefInfo, false, 1892 "dest-object"); 1893 1894 // src->x 1895 Address srcField = CGF.GetAddrOfLocalVar(&src); 1896 srcField = Address(CGF.Builder.CreateLoad(srcField), 1897 byrefInfo.ByrefAlignment); 1898 srcField = CGF.Builder.CreateBitCast(srcField, byrefPtrType); 1899 srcField = CGF.emitBlockByrefAddress(srcField, byrefInfo, false, 1900 "src-object"); 1901 1902 generator.emitCopy(CGF, destField, srcField); 1903 } 1904 1905 CGF.FinishFunction(); 1906 1907 return llvm::ConstantExpr::getBitCast(Fn, CGF.Int8PtrTy); 1908 } 1909 1910 /// Build the copy helper for a __block variable. 1911 static llvm::Constant *buildByrefCopyHelper(CodeGenModule &CGM, 1912 const BlockByrefInfo &byrefInfo, 1913 BlockByrefHelpers &generator) { 1914 CodeGenFunction CGF(CGM); 1915 return generateByrefCopyHelper(CGF, byrefInfo, generator); 1916 } 1917 1918 /// Generate code for a __block variable's dispose helper. 1919 static llvm::Constant * 1920 generateByrefDisposeHelper(CodeGenFunction &CGF, 1921 const BlockByrefInfo &byrefInfo, 1922 BlockByrefHelpers &generator) { 1923 ASTContext &Context = CGF.getContext(); 1924 QualType R = Context.VoidTy; 1925 1926 FunctionArgList args; 1927 ImplicitParamDecl src(CGF.getContext(), nullptr, SourceLocation(), nullptr, 1928 Context.VoidPtrTy); 1929 args.push_back(&src); 1930 1931 const CGFunctionInfo &FI = 1932 CGF.CGM.getTypes().arrangeBuiltinFunctionDeclaration(R, args); 1933 1934 llvm::FunctionType *LTy = CGF.CGM.getTypes().GetFunctionType(FI); 1935 1936 // FIXME: We'd like to put these into a mergable by content, with 1937 // internal linkage. 1938 llvm::Function *Fn = 1939 llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, 1940 "__Block_byref_object_dispose_", 1941 &CGF.CGM.getModule()); 1942 1943 IdentifierInfo *II 1944 = &Context.Idents.get("__Block_byref_object_dispose_"); 1945 1946 FunctionDecl *FD = FunctionDecl::Create(Context, 1947 Context.getTranslationUnitDecl(), 1948 SourceLocation(), 1949 SourceLocation(), II, R, nullptr, 1950 SC_Static, 1951 false, false); 1952 1953 CGF.CGM.SetInternalFunctionAttributes(nullptr, Fn, FI); 1954 1955 CGF.StartFunction(FD, R, Fn, FI, args); 1956 1957 if (generator.needsDispose()) { 1958 Address addr = CGF.GetAddrOfLocalVar(&src); 1959 addr = Address(CGF.Builder.CreateLoad(addr), byrefInfo.ByrefAlignment); 1960 auto byrefPtrType = byrefInfo.Type->getPointerTo(0); 1961 addr = CGF.Builder.CreateBitCast(addr, byrefPtrType); 1962 addr = CGF.emitBlockByrefAddress(addr, byrefInfo, false, "object"); 1963 1964 generator.emitDispose(CGF, addr); 1965 } 1966 1967 CGF.FinishFunction(); 1968 1969 return llvm::ConstantExpr::getBitCast(Fn, CGF.Int8PtrTy); 1970 } 1971 1972 /// Build the dispose helper for a __block variable. 1973 static llvm::Constant *buildByrefDisposeHelper(CodeGenModule &CGM, 1974 const BlockByrefInfo &byrefInfo, 1975 BlockByrefHelpers &generator) { 1976 CodeGenFunction CGF(CGM); 1977 return generateByrefDisposeHelper(CGF, byrefInfo, generator); 1978 } 1979 1980 /// Lazily build the copy and dispose helpers for a __block variable 1981 /// with the given information. 1982 template <class T> 1983 static T *buildByrefHelpers(CodeGenModule &CGM, const BlockByrefInfo &byrefInfo, 1984 T &&generator) { 1985 llvm::FoldingSetNodeID id; 1986 generator.Profile(id); 1987 1988 void *insertPos; 1989 BlockByrefHelpers *node 1990 = CGM.ByrefHelpersCache.FindNodeOrInsertPos(id, insertPos); 1991 if (node) return static_cast<T*>(node); 1992 1993 generator.CopyHelper = buildByrefCopyHelper(CGM, byrefInfo, generator); 1994 generator.DisposeHelper = buildByrefDisposeHelper(CGM, byrefInfo, generator); 1995 1996 T *copy = new (CGM.getContext()) T(std::forward<T>(generator)); 1997 CGM.ByrefHelpersCache.InsertNode(copy, insertPos); 1998 return copy; 1999 } 2000 2001 /// Build the copy and dispose helpers for the given __block variable 2002 /// emission. Places the helpers in the global cache. Returns null 2003 /// if no helpers are required. 2004 BlockByrefHelpers * 2005 CodeGenFunction::buildByrefHelpers(llvm::StructType &byrefType, 2006 const AutoVarEmission &emission) { 2007 const VarDecl &var = *emission.Variable; 2008 QualType type = var.getType(); 2009 2010 auto &byrefInfo = getBlockByrefInfo(&var); 2011 2012 // The alignment we care about for the purposes of uniquing byref 2013 // helpers is the alignment of the actual byref value field. 2014 CharUnits valueAlignment = 2015 byrefInfo.ByrefAlignment.alignmentAtOffset(byrefInfo.FieldOffset); 2016 2017 if (const CXXRecordDecl *record = type->getAsCXXRecordDecl()) { 2018 const Expr *copyExpr = CGM.getContext().getBlockVarCopyInits(&var); 2019 if (!copyExpr && record->hasTrivialDestructor()) return nullptr; 2020 2021 return ::buildByrefHelpers( 2022 CGM, byrefInfo, CXXByrefHelpers(valueAlignment, type, copyExpr)); 2023 } 2024 2025 // Otherwise, if we don't have a retainable type, there's nothing to do. 2026 // that the runtime does extra copies. 2027 if (!type->isObjCRetainableType()) return nullptr; 2028 2029 Qualifiers qs = type.getQualifiers(); 2030 2031 // If we have lifetime, that dominates. 2032 if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) { 2033 switch (lifetime) { 2034 case Qualifiers::OCL_None: llvm_unreachable("impossible"); 2035 2036 // These are just bits as far as the runtime is concerned. 2037 case Qualifiers::OCL_ExplicitNone: 2038 case Qualifiers::OCL_Autoreleasing: 2039 return nullptr; 2040 2041 // Tell the runtime that this is ARC __weak, called by the 2042 // byref routines. 2043 case Qualifiers::OCL_Weak: 2044 return ::buildByrefHelpers(CGM, byrefInfo, 2045 ARCWeakByrefHelpers(valueAlignment)); 2046 2047 // ARC __strong __block variables need to be retained. 2048 case Qualifiers::OCL_Strong: 2049 // Block pointers need to be copied, and there's no direct 2050 // transfer possible. 2051 if (type->isBlockPointerType()) { 2052 return ::buildByrefHelpers(CGM, byrefInfo, 2053 ARCStrongBlockByrefHelpers(valueAlignment)); 2054 2055 // Otherwise, we transfer ownership of the retain from the stack 2056 // to the heap. 2057 } else { 2058 return ::buildByrefHelpers(CGM, byrefInfo, 2059 ARCStrongByrefHelpers(valueAlignment)); 2060 } 2061 } 2062 llvm_unreachable("fell out of lifetime switch!"); 2063 } 2064 2065 BlockFieldFlags flags; 2066 if (type->isBlockPointerType()) { 2067 flags |= BLOCK_FIELD_IS_BLOCK; 2068 } else if (CGM.getContext().isObjCNSObjectType(type) || 2069 type->isObjCObjectPointerType()) { 2070 flags |= BLOCK_FIELD_IS_OBJECT; 2071 } else { 2072 return nullptr; 2073 } 2074 2075 if (type.isObjCGCWeak()) 2076 flags |= BLOCK_FIELD_IS_WEAK; 2077 2078 return ::buildByrefHelpers(CGM, byrefInfo, 2079 ObjectByrefHelpers(valueAlignment, flags)); 2080 } 2081 2082 Address CodeGenFunction::emitBlockByrefAddress(Address baseAddr, 2083 const VarDecl *var, 2084 bool followForward) { 2085 auto &info = getBlockByrefInfo(var); 2086 return emitBlockByrefAddress(baseAddr, info, followForward, var->getName()); 2087 } 2088 2089 Address CodeGenFunction::emitBlockByrefAddress(Address baseAddr, 2090 const BlockByrefInfo &info, 2091 bool followForward, 2092 const llvm::Twine &name) { 2093 // Chase the forwarding address if requested. 2094 if (followForward) { 2095 Address forwardingAddr = 2096 Builder.CreateStructGEP(baseAddr, 1, getPointerSize(), "forwarding"); 2097 baseAddr = Address(Builder.CreateLoad(forwardingAddr), info.ByrefAlignment); 2098 } 2099 2100 return Builder.CreateStructGEP(baseAddr, info.FieldIndex, 2101 info.FieldOffset, name); 2102 } 2103 2104 /// BuildByrefInfo - This routine changes a __block variable declared as T x 2105 /// into: 2106 /// 2107 /// struct { 2108 /// void *__isa; 2109 /// void *__forwarding; 2110 /// int32_t __flags; 2111 /// int32_t __size; 2112 /// void *__copy_helper; // only if needed 2113 /// void *__destroy_helper; // only if needed 2114 /// void *__byref_variable_layout;// only if needed 2115 /// char padding[X]; // only if needed 2116 /// T x; 2117 /// } x 2118 /// 2119 const BlockByrefInfo &CodeGenFunction::getBlockByrefInfo(const VarDecl *D) { 2120 auto it = BlockByrefInfos.find(D); 2121 if (it != BlockByrefInfos.end()) 2122 return it->second; 2123 2124 llvm::StructType *byrefType = 2125 llvm::StructType::create(getLLVMContext(), 2126 "struct.__block_byref_" + D->getNameAsString()); 2127 2128 QualType Ty = D->getType(); 2129 2130 CharUnits size; 2131 SmallVector<llvm::Type *, 8> types; 2132 2133 // void *__isa; 2134 types.push_back(Int8PtrTy); 2135 size += getPointerSize(); 2136 2137 // void *__forwarding; 2138 types.push_back(llvm::PointerType::getUnqual(byrefType)); 2139 size += getPointerSize(); 2140 2141 // int32_t __flags; 2142 types.push_back(Int32Ty); 2143 size += CharUnits::fromQuantity(4); 2144 2145 // int32_t __size; 2146 types.push_back(Int32Ty); 2147 size += CharUnits::fromQuantity(4); 2148 2149 // Note that this must match *exactly* the logic in buildByrefHelpers. 2150 bool hasCopyAndDispose = getContext().BlockRequiresCopying(Ty, D); 2151 if (hasCopyAndDispose) { 2152 /// void *__copy_helper; 2153 types.push_back(Int8PtrTy); 2154 size += getPointerSize(); 2155 2156 /// void *__destroy_helper; 2157 types.push_back(Int8PtrTy); 2158 size += getPointerSize(); 2159 } 2160 2161 bool HasByrefExtendedLayout = false; 2162 Qualifiers::ObjCLifetime Lifetime; 2163 if (getContext().getByrefLifetime(Ty, Lifetime, HasByrefExtendedLayout) && 2164 HasByrefExtendedLayout) { 2165 /// void *__byref_variable_layout; 2166 types.push_back(Int8PtrTy); 2167 size += CharUnits::fromQuantity(PointerSizeInBytes); 2168 } 2169 2170 // T x; 2171 llvm::Type *varTy = ConvertTypeForMem(Ty); 2172 2173 bool packed = false; 2174 CharUnits varAlign = getContext().getDeclAlign(D); 2175 CharUnits varOffset = size.alignTo(varAlign); 2176 2177 // We may have to insert padding. 2178 if (varOffset != size) { 2179 llvm::Type *paddingTy = 2180 llvm::ArrayType::get(Int8Ty, (varOffset - size).getQuantity()); 2181 2182 types.push_back(paddingTy); 2183 size = varOffset; 2184 2185 // Conversely, we might have to prevent LLVM from inserting padding. 2186 } else if (CGM.getDataLayout().getABITypeAlignment(varTy) 2187 > varAlign.getQuantity()) { 2188 packed = true; 2189 } 2190 types.push_back(varTy); 2191 2192 byrefType->setBody(types, packed); 2193 2194 BlockByrefInfo info; 2195 info.Type = byrefType; 2196 info.FieldIndex = types.size() - 1; 2197 info.FieldOffset = varOffset; 2198 info.ByrefAlignment = std::max(varAlign, getPointerAlign()); 2199 2200 auto pair = BlockByrefInfos.insert({D, info}); 2201 assert(pair.second && "info was inserted recursively?"); 2202 return pair.first->second; 2203 } 2204 2205 /// Initialize the structural components of a __block variable, i.e. 2206 /// everything but the actual object. 2207 void CodeGenFunction::emitByrefStructureInit(const AutoVarEmission &emission) { 2208 // Find the address of the local. 2209 Address addr = emission.Addr; 2210 2211 // That's an alloca of the byref structure type. 2212 llvm::StructType *byrefType = cast<llvm::StructType>( 2213 cast<llvm::PointerType>(addr.getPointer()->getType())->getElementType()); 2214 2215 unsigned nextHeaderIndex = 0; 2216 CharUnits nextHeaderOffset; 2217 auto storeHeaderField = [&](llvm::Value *value, CharUnits fieldSize, 2218 const Twine &name) { 2219 auto fieldAddr = Builder.CreateStructGEP(addr, nextHeaderIndex, 2220 nextHeaderOffset, name); 2221 Builder.CreateStore(value, fieldAddr); 2222 2223 nextHeaderIndex++; 2224 nextHeaderOffset += fieldSize; 2225 }; 2226 2227 // Build the byref helpers if necessary. This is null if we don't need any. 2228 BlockByrefHelpers *helpers = buildByrefHelpers(*byrefType, emission); 2229 2230 const VarDecl &D = *emission.Variable; 2231 QualType type = D.getType(); 2232 2233 bool HasByrefExtendedLayout; 2234 Qualifiers::ObjCLifetime ByrefLifetime; 2235 bool ByRefHasLifetime = 2236 getContext().getByrefLifetime(type, ByrefLifetime, HasByrefExtendedLayout); 2237 2238 llvm::Value *V; 2239 2240 // Initialize the 'isa', which is just 0 or 1. 2241 int isa = 0; 2242 if (type.isObjCGCWeak()) 2243 isa = 1; 2244 V = Builder.CreateIntToPtr(Builder.getInt32(isa), Int8PtrTy, "isa"); 2245 storeHeaderField(V, getPointerSize(), "byref.isa"); 2246 2247 // Store the address of the variable into its own forwarding pointer. 2248 storeHeaderField(addr.getPointer(), getPointerSize(), "byref.forwarding"); 2249 2250 // Blocks ABI: 2251 // c) the flags field is set to either 0 if no helper functions are 2252 // needed or BLOCK_BYREF_HAS_COPY_DISPOSE if they are, 2253 BlockFlags flags; 2254 if (helpers) flags |= BLOCK_BYREF_HAS_COPY_DISPOSE; 2255 if (ByRefHasLifetime) { 2256 if (HasByrefExtendedLayout) flags |= BLOCK_BYREF_LAYOUT_EXTENDED; 2257 else switch (ByrefLifetime) { 2258 case Qualifiers::OCL_Strong: 2259 flags |= BLOCK_BYREF_LAYOUT_STRONG; 2260 break; 2261 case Qualifiers::OCL_Weak: 2262 flags |= BLOCK_BYREF_LAYOUT_WEAK; 2263 break; 2264 case Qualifiers::OCL_ExplicitNone: 2265 flags |= BLOCK_BYREF_LAYOUT_UNRETAINED; 2266 break; 2267 case Qualifiers::OCL_None: 2268 if (!type->isObjCObjectPointerType() && !type->isBlockPointerType()) 2269 flags |= BLOCK_BYREF_LAYOUT_NON_OBJECT; 2270 break; 2271 default: 2272 break; 2273 } 2274 if (CGM.getLangOpts().ObjCGCBitmapPrint) { 2275 printf("\n Inline flag for BYREF variable layout (%d):", flags.getBitMask()); 2276 if (flags & BLOCK_BYREF_HAS_COPY_DISPOSE) 2277 printf(" BLOCK_BYREF_HAS_COPY_DISPOSE"); 2278 if (flags & BLOCK_BYREF_LAYOUT_MASK) { 2279 BlockFlags ThisFlag(flags.getBitMask() & BLOCK_BYREF_LAYOUT_MASK); 2280 if (ThisFlag == BLOCK_BYREF_LAYOUT_EXTENDED) 2281 printf(" BLOCK_BYREF_LAYOUT_EXTENDED"); 2282 if (ThisFlag == BLOCK_BYREF_LAYOUT_STRONG) 2283 printf(" BLOCK_BYREF_LAYOUT_STRONG"); 2284 if (ThisFlag == BLOCK_BYREF_LAYOUT_WEAK) 2285 printf(" BLOCK_BYREF_LAYOUT_WEAK"); 2286 if (ThisFlag == BLOCK_BYREF_LAYOUT_UNRETAINED) 2287 printf(" BLOCK_BYREF_LAYOUT_UNRETAINED"); 2288 if (ThisFlag == BLOCK_BYREF_LAYOUT_NON_OBJECT) 2289 printf(" BLOCK_BYREF_LAYOUT_NON_OBJECT"); 2290 } 2291 printf("\n"); 2292 } 2293 } 2294 storeHeaderField(llvm::ConstantInt::get(IntTy, flags.getBitMask()), 2295 getIntSize(), "byref.flags"); 2296 2297 CharUnits byrefSize = CGM.GetTargetTypeStoreSize(byrefType); 2298 V = llvm::ConstantInt::get(IntTy, byrefSize.getQuantity()); 2299 storeHeaderField(V, getIntSize(), "byref.size"); 2300 2301 if (helpers) { 2302 storeHeaderField(helpers->CopyHelper, getPointerSize(), 2303 "byref.copyHelper"); 2304 storeHeaderField(helpers->DisposeHelper, getPointerSize(), 2305 "byref.disposeHelper"); 2306 } 2307 2308 if (ByRefHasLifetime && HasByrefExtendedLayout) { 2309 auto layoutInfo = CGM.getObjCRuntime().BuildByrefLayout(CGM, type); 2310 storeHeaderField(layoutInfo, getPointerSize(), "byref.layout"); 2311 } 2312 } 2313 2314 void CodeGenFunction::BuildBlockRelease(llvm::Value *V, BlockFieldFlags flags) { 2315 llvm::Value *F = CGM.getBlockObjectDispose(); 2316 llvm::Value *args[] = { 2317 Builder.CreateBitCast(V, Int8PtrTy), 2318 llvm::ConstantInt::get(Int32Ty, flags.getBitMask()) 2319 }; 2320 EmitNounwindRuntimeCall(F, args); // FIXME: throwing destructors? 2321 } 2322 2323 namespace { 2324 /// Release a __block variable. 2325 struct CallBlockRelease final : EHScopeStack::Cleanup { 2326 llvm::Value *Addr; 2327 CallBlockRelease(llvm::Value *Addr) : Addr(Addr) {} 2328 2329 void Emit(CodeGenFunction &CGF, Flags flags) override { 2330 // Should we be passing FIELD_IS_WEAK here? 2331 CGF.BuildBlockRelease(Addr, BLOCK_FIELD_IS_BYREF); 2332 } 2333 }; 2334 } // end anonymous namespace 2335 2336 /// Enter a cleanup to destroy a __block variable. Note that this 2337 /// cleanup should be a no-op if the variable hasn't left the stack 2338 /// yet; if a cleanup is required for the variable itself, that needs 2339 /// to be done externally. 2340 void CodeGenFunction::enterByrefCleanup(const AutoVarEmission &emission) { 2341 // We don't enter this cleanup if we're in pure-GC mode. 2342 if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) 2343 return; 2344 2345 EHStack.pushCleanup<CallBlockRelease>(NormalAndEHCleanup, 2346 emission.Addr.getPointer()); 2347 } 2348 2349 /// Adjust the declaration of something from the blocks API. 2350 static void configureBlocksRuntimeObject(CodeGenModule &CGM, 2351 llvm::Constant *C) { 2352 auto *GV = cast<llvm::GlobalValue>(C->stripPointerCasts()); 2353 2354 if (CGM.getTarget().getTriple().isOSBinFormatCOFF()) { 2355 IdentifierInfo &II = CGM.getContext().Idents.get(C->getName()); 2356 TranslationUnitDecl *TUDecl = CGM.getContext().getTranslationUnitDecl(); 2357 DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl); 2358 2359 assert((isa<llvm::Function>(C->stripPointerCasts()) || 2360 isa<llvm::GlobalVariable>(C->stripPointerCasts())) && 2361 "expected Function or GlobalVariable"); 2362 2363 const NamedDecl *ND = nullptr; 2364 for (const auto &Result : DC->lookup(&II)) 2365 if ((ND = dyn_cast<FunctionDecl>(Result)) || 2366 (ND = dyn_cast<VarDecl>(Result))) 2367 break; 2368 2369 // TODO: support static blocks runtime 2370 if (GV->isDeclaration() && (!ND || !ND->hasAttr<DLLExportAttr>())) { 2371 GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); 2372 GV->setLinkage(llvm::GlobalValue::ExternalLinkage); 2373 } else { 2374 GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); 2375 GV->setLinkage(llvm::GlobalValue::ExternalLinkage); 2376 } 2377 } 2378 2379 if (!CGM.getLangOpts().BlocksRuntimeOptional) 2380 return; 2381 2382 if (GV->isDeclaration() && GV->hasExternalLinkage()) 2383 GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); 2384 } 2385 2386 llvm::Constant *CodeGenModule::getBlockObjectDispose() { 2387 if (BlockObjectDispose) 2388 return BlockObjectDispose; 2389 2390 llvm::Type *args[] = { Int8PtrTy, Int32Ty }; 2391 llvm::FunctionType *fty 2392 = llvm::FunctionType::get(VoidTy, args, false); 2393 BlockObjectDispose = CreateRuntimeFunction(fty, "_Block_object_dispose"); 2394 configureBlocksRuntimeObject(*this, BlockObjectDispose); 2395 return BlockObjectDispose; 2396 } 2397 2398 llvm::Constant *CodeGenModule::getBlockObjectAssign() { 2399 if (BlockObjectAssign) 2400 return BlockObjectAssign; 2401 2402 llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, Int32Ty }; 2403 llvm::FunctionType *fty 2404 = llvm::FunctionType::get(VoidTy, args, false); 2405 BlockObjectAssign = CreateRuntimeFunction(fty, "_Block_object_assign"); 2406 configureBlocksRuntimeObject(*this, BlockObjectAssign); 2407 return BlockObjectAssign; 2408 } 2409 2410 llvm::Constant *CodeGenModule::getNSConcreteGlobalBlock() { 2411 if (NSConcreteGlobalBlock) 2412 return NSConcreteGlobalBlock; 2413 2414 NSConcreteGlobalBlock = GetOrCreateLLVMGlobal("_NSConcreteGlobalBlock", 2415 Int8PtrTy->getPointerTo(), 2416 nullptr); 2417 configureBlocksRuntimeObject(*this, NSConcreteGlobalBlock); 2418 return NSConcreteGlobalBlock; 2419 } 2420 2421 llvm::Constant *CodeGenModule::getNSConcreteStackBlock() { 2422 if (NSConcreteStackBlock) 2423 return NSConcreteStackBlock; 2424 2425 NSConcreteStackBlock = GetOrCreateLLVMGlobal("_NSConcreteStackBlock", 2426 Int8PtrTy->getPointerTo(), 2427 nullptr); 2428 configureBlocksRuntimeObject(*this, NSConcreteStackBlock); 2429 return NSConcreteStackBlock; 2430 } 2431