clang API Documentation

CGBlocks.cpp
Go to the documentation of this file.
00001 //===--- CGBlocks.cpp - Emit LLVM Code for declarations -------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This contains code to emit blocks.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "CGBlocks.h"
00015 #include "CGDebugInfo.h"
00016 #include "CGObjCRuntime.h"
00017 #include "CodeGenFunction.h"
00018 #include "CodeGenModule.h"
00019 #include "clang/AST/DeclObjC.h"
00020 #include "llvm/ADT/SmallSet.h"
00021 #include "llvm/IR/CallSite.h"
00022 #include "llvm/IR/DataLayout.h"
00023 #include "llvm/IR/Module.h"
00024 #include <algorithm>
00025 #include <cstdio>
00026 
00027 using namespace clang;
00028 using namespace CodeGen;
00029 
00030 CGBlockInfo::CGBlockInfo(const BlockDecl *block, StringRef name)
00031   : Name(name), CXXThisIndex(0), CanBeGlobal(false), NeedsCopyDispose(false),
00032     HasCXXObject(false), UsesStret(false), HasCapturedVariableLayout(false),
00033     StructureType(nullptr), Block(block),
00034     DominatingIP(nullptr) {
00035 
00036   // Skip asm prefix, if any.  'name' is usually taken directly from
00037   // the mangled name of the enclosing function.
00038   if (!name.empty() && name[0] == '\01')
00039     name = name.substr(1);
00040 }
00041 
00042 // Anchor the vtable to this translation unit.
00043 CodeGenModule::ByrefHelpers::~ByrefHelpers() {}
00044 
00045 /// Build the given block as a global block.
00046 static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM,
00047                                         const CGBlockInfo &blockInfo,
00048                                         llvm::Constant *blockFn);
00049 
00050 /// Build the helper function to copy a block.
00051 static llvm::Constant *buildCopyHelper(CodeGenModule &CGM,
00052                                        const CGBlockInfo &blockInfo) {
00053   return CodeGenFunction(CGM).GenerateCopyHelperFunction(blockInfo);
00054 }
00055 
00056 /// Build the helper function to dispose of a block.
00057 static llvm::Constant *buildDisposeHelper(CodeGenModule &CGM,
00058                                           const CGBlockInfo &blockInfo) {
00059   return CodeGenFunction(CGM).GenerateDestroyHelperFunction(blockInfo);
00060 }
00061 
00062 /// buildBlockDescriptor - Build the block descriptor meta-data for a block.
00063 /// buildBlockDescriptor is accessed from 5th field of the Block_literal
00064 /// meta-data and contains stationary information about the block literal.
00065 /// Its definition will have 4 (or optinally 6) words.
00066 /// \code
00067 /// struct Block_descriptor {
00068 ///   unsigned long reserved;
00069 ///   unsigned long size;  // size of Block_literal metadata in bytes.
00070 ///   void *copy_func_helper_decl;  // optional copy helper.
00071 ///   void *destroy_func_decl; // optioanl destructor helper.
00072 ///   void *block_method_encoding_address; // @encode for block literal signature.
00073 ///   void *block_layout_info; // encoding of captured block variables.
00074 /// };
00075 /// \endcode
00076 static llvm::Constant *buildBlockDescriptor(CodeGenModule &CGM,
00077                                             const CGBlockInfo &blockInfo) {
00078   ASTContext &C = CGM.getContext();
00079 
00080   llvm::Type *ulong = CGM.getTypes().ConvertType(C.UnsignedLongTy);
00081   llvm::Type *i8p = CGM.getTypes().ConvertType(C.VoidPtrTy);
00082 
00083   SmallVector<llvm::Constant*, 6> elements;
00084 
00085   // reserved
00086   elements.push_back(llvm::ConstantInt::get(ulong, 0));
00087 
00088   // Size
00089   // FIXME: What is the right way to say this doesn't fit?  We should give
00090   // a user diagnostic in that case.  Better fix would be to change the
00091   // API to size_t.
00092   elements.push_back(llvm::ConstantInt::get(ulong,
00093                                             blockInfo.BlockSize.getQuantity()));
00094 
00095   // Optional copy/dispose helpers.
00096   if (blockInfo.NeedsCopyDispose) {
00097     // copy_func_helper_decl
00098     elements.push_back(buildCopyHelper(CGM, blockInfo));
00099 
00100     // destroy_func_decl
00101     elements.push_back(buildDisposeHelper(CGM, blockInfo));
00102   }
00103 
00104   // Signature.  Mandatory ObjC-style method descriptor @encode sequence.
00105   std::string typeAtEncoding =
00106     CGM.getContext().getObjCEncodingForBlock(blockInfo.getBlockExpr());
00107   elements.push_back(llvm::ConstantExpr::getBitCast(
00108                           CGM.GetAddrOfConstantCString(typeAtEncoding), i8p));
00109   
00110   // GC layout.
00111   if (C.getLangOpts().ObjC1) {
00112     if (CGM.getLangOpts().getGC() != LangOptions::NonGC)
00113       elements.push_back(CGM.getObjCRuntime().BuildGCBlockLayout(CGM, blockInfo));
00114     else
00115       elements.push_back(CGM.getObjCRuntime().BuildRCBlockLayout(CGM, blockInfo));
00116   }
00117   else
00118     elements.push_back(llvm::Constant::getNullValue(i8p));
00119 
00120   llvm::Constant *init = llvm::ConstantStruct::getAnon(elements);
00121 
00122   llvm::GlobalVariable *global =
00123     new llvm::GlobalVariable(CGM.getModule(), init->getType(), true,
00124                              llvm::GlobalValue::InternalLinkage,
00125                              init, "__block_descriptor_tmp");
00126 
00127   return llvm::ConstantExpr::getBitCast(global, CGM.getBlockDescriptorType());
00128 }
00129 
00130 /*
00131   Purely notional variadic template describing the layout of a block.
00132 
00133   template <class _ResultType, class... _ParamTypes, class... _CaptureTypes>
00134   struct Block_literal {
00135     /// Initialized to one of:
00136     ///   extern void *_NSConcreteStackBlock[];
00137     ///   extern void *_NSConcreteGlobalBlock[];
00138     ///
00139     /// In theory, we could start one off malloc'ed by setting
00140     /// BLOCK_NEEDS_FREE, giving it a refcount of 1, and using
00141     /// this isa:
00142     ///   extern void *_NSConcreteMallocBlock[];
00143     struct objc_class *isa;
00144 
00145     /// These are the flags (with corresponding bit number) that the
00146     /// compiler is actually supposed to know about.
00147     ///  25. BLOCK_HAS_COPY_DISPOSE - indicates that the block
00148     ///   descriptor provides copy and dispose helper functions
00149     ///  26. BLOCK_HAS_CXX_OBJ - indicates that there's a captured
00150     ///   object with a nontrivial destructor or copy constructor
00151     ///  28. BLOCK_IS_GLOBAL - indicates that the block is allocated
00152     ///   as global memory
00153     ///  29. BLOCK_USE_STRET - indicates that the block function
00154     ///   uses stret, which objc_msgSend needs to know about
00155     ///  30. BLOCK_HAS_SIGNATURE - indicates that the block has an
00156     ///   @encoded signature string
00157     /// And we're not supposed to manipulate these:
00158     ///  24. BLOCK_NEEDS_FREE - indicates that the block has been moved
00159     ///   to malloc'ed memory
00160     ///  27. BLOCK_IS_GC - indicates that the block has been moved to
00161     ///   to GC-allocated memory
00162     /// Additionally, the bottom 16 bits are a reference count which
00163     /// should be zero on the stack.
00164     int flags;
00165 
00166     /// Reserved;  should be zero-initialized.
00167     int reserved;
00168 
00169     /// Function pointer generated from block literal.
00170     _ResultType (*invoke)(Block_literal *, _ParamTypes...);
00171 
00172     /// Block description metadata generated from block literal.
00173     struct Block_descriptor *block_descriptor;
00174 
00175     /// Captured values follow.
00176     _CapturesTypes captures...;
00177   };
00178  */
00179 
00180 /// The number of fields in a block header.
00181 const unsigned BlockHeaderSize = 5;
00182 
00183 namespace {
00184   /// A chunk of data that we actually have to capture in the block.
00185   struct BlockLayoutChunk {
00186     CharUnits Alignment;
00187     CharUnits Size;
00188     Qualifiers::ObjCLifetime Lifetime;
00189     const BlockDecl::Capture *Capture; // null for 'this'
00190     llvm::Type *Type;
00191 
00192     BlockLayoutChunk(CharUnits align, CharUnits size,
00193                      Qualifiers::ObjCLifetime lifetime,
00194                      const BlockDecl::Capture *capture,
00195                      llvm::Type *type)
00196       : Alignment(align), Size(size), Lifetime(lifetime),
00197         Capture(capture), Type(type) {}
00198 
00199     /// Tell the block info that this chunk has the given field index.
00200     void setIndex(CGBlockInfo &info, unsigned index) {
00201       if (!Capture)
00202         info.CXXThisIndex = index;
00203       else
00204         info.Captures[Capture->getVariable()]
00205           = CGBlockInfo::Capture::makeIndex(index);
00206     }
00207   };
00208 
00209   /// Order by 1) all __strong together 2) next, all byfref together 3) next,
00210   /// all __weak together. Preserve descending alignment in all situations.
00211   bool operator<(const BlockLayoutChunk &left, const BlockLayoutChunk &right) {
00212     CharUnits LeftValue, RightValue;
00213     bool LeftByref = left.Capture ? left.Capture->isByRef() : false;
00214     bool RightByref = right.Capture ? right.Capture->isByRef() : false;
00215     
00216     if (left.Lifetime == Qualifiers::OCL_Strong &&
00217         left.Alignment >= right.Alignment)
00218       LeftValue = CharUnits::fromQuantity(64);
00219     else if (LeftByref && left.Alignment >= right.Alignment)
00220       LeftValue = CharUnits::fromQuantity(32);
00221     else if (left.Lifetime == Qualifiers::OCL_Weak &&
00222              left.Alignment >= right.Alignment)
00223       LeftValue = CharUnits::fromQuantity(16);
00224     else
00225       LeftValue = left.Alignment;
00226     if (right.Lifetime == Qualifiers::OCL_Strong &&
00227         right.Alignment >= left.Alignment)
00228       RightValue = CharUnits::fromQuantity(64);
00229     else if (RightByref && right.Alignment >= left.Alignment)
00230       RightValue = CharUnits::fromQuantity(32);
00231     else if (right.Lifetime == Qualifiers::OCL_Weak &&
00232              right.Alignment >= left.Alignment)
00233       RightValue = CharUnits::fromQuantity(16);
00234     else
00235       RightValue = right.Alignment;
00236     
00237       return LeftValue > RightValue;
00238   }
00239 }
00240 
00241 /// Determines if the given type is safe for constant capture in C++.
00242 static bool isSafeForCXXConstantCapture(QualType type) {
00243   const RecordType *recordType =
00244     type->getBaseElementTypeUnsafe()->getAs<RecordType>();
00245 
00246   // Only records can be unsafe.
00247   if (!recordType) return true;
00248 
00249   const auto *record = cast<CXXRecordDecl>(recordType->getDecl());
00250 
00251   // Maintain semantics for classes with non-trivial dtors or copy ctors.
00252   if (!record->hasTrivialDestructor()) return false;
00253   if (record->hasNonTrivialCopyConstructor()) return false;
00254 
00255   // Otherwise, we just have to make sure there aren't any mutable
00256   // fields that might have changed since initialization.
00257   return !record->hasMutableFields();
00258 }
00259 
00260 /// It is illegal to modify a const object after initialization.
00261 /// Therefore, if a const object has a constant initializer, we don't
00262 /// actually need to keep storage for it in the block; we'll just
00263 /// rematerialize it at the start of the block function.  This is
00264 /// acceptable because we make no promises about address stability of
00265 /// captured variables.
00266 static llvm::Constant *tryCaptureAsConstant(CodeGenModule &CGM,
00267                                             CodeGenFunction *CGF,
00268                                             const VarDecl *var) {
00269   QualType type = var->getType();
00270 
00271   // We can only do this if the variable is const.
00272   if (!type.isConstQualified()) return nullptr;
00273 
00274   // Furthermore, in C++ we have to worry about mutable fields:
00275   // C++ [dcl.type.cv]p4:
00276   //   Except that any class member declared mutable can be
00277   //   modified, any attempt to modify a const object during its
00278   //   lifetime results in undefined behavior.
00279   if (CGM.getLangOpts().CPlusPlus && !isSafeForCXXConstantCapture(type))
00280     return nullptr;
00281 
00282   // If the variable doesn't have any initializer (shouldn't this be
00283   // invalid?), it's not clear what we should do.  Maybe capture as
00284   // zero?
00285   const Expr *init = var->getInit();
00286   if (!init) return nullptr;
00287 
00288   return CGM.EmitConstantInit(*var, CGF);
00289 }
00290 
00291 /// Get the low bit of a nonzero character count.  This is the
00292 /// alignment of the nth byte if the 0th byte is universally aligned.
00293 static CharUnits getLowBit(CharUnits v) {
00294   return CharUnits::fromQuantity(v.getQuantity() & (~v.getQuantity() + 1));
00295 }
00296 
00297 static void initializeForBlockHeader(CodeGenModule &CGM, CGBlockInfo &info,
00298                              SmallVectorImpl<llvm::Type*> &elementTypes) {
00299   ASTContext &C = CGM.getContext();
00300 
00301   // The header is basically a 'struct { void *; int; int; void *; void *; }'.
00302   CharUnits ptrSize, ptrAlign, intSize, intAlign;
00303   std::tie(ptrSize, ptrAlign) = C.getTypeInfoInChars(C.VoidPtrTy);
00304   std::tie(intSize, intAlign) = C.getTypeInfoInChars(C.IntTy);
00305 
00306   // Are there crazy embedded platforms where this isn't true?
00307   assert(intSize <= ptrSize && "layout assumptions horribly violated");
00308 
00309   CharUnits headerSize = ptrSize;
00310   if (2 * intSize < ptrAlign) headerSize += ptrSize;
00311   else headerSize += 2 * intSize;
00312   headerSize += 2 * ptrSize;
00313 
00314   info.BlockAlign = ptrAlign;
00315   info.BlockSize = headerSize;
00316 
00317   assert(elementTypes.empty());
00318   llvm::Type *i8p = CGM.getTypes().ConvertType(C.VoidPtrTy);
00319   llvm::Type *intTy = CGM.getTypes().ConvertType(C.IntTy);
00320   elementTypes.push_back(i8p);
00321   elementTypes.push_back(intTy);
00322   elementTypes.push_back(intTy);
00323   elementTypes.push_back(i8p);
00324   elementTypes.push_back(CGM.getBlockDescriptorType());
00325 
00326   assert(elementTypes.size() == BlockHeaderSize);
00327 }
00328 
00329 /// Compute the layout of the given block.  Attempts to lay the block
00330 /// out with minimal space requirements.
00331 static void computeBlockInfo(CodeGenModule &CGM, CodeGenFunction *CGF,
00332                              CGBlockInfo &info) {
00333   ASTContext &C = CGM.getContext();
00334   const BlockDecl *block = info.getBlockDecl();
00335 
00336   SmallVector<llvm::Type*, 8> elementTypes;
00337   initializeForBlockHeader(CGM, info, elementTypes);
00338 
00339   if (!block->hasCaptures()) {
00340     info.StructureType =
00341       llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true);
00342     info.CanBeGlobal = true;
00343     return;
00344   }
00345   else if (C.getLangOpts().ObjC1 &&
00346            CGM.getLangOpts().getGC() == LangOptions::NonGC)
00347     info.HasCapturedVariableLayout = true;
00348   
00349   // Collect the layout chunks.
00350   SmallVector<BlockLayoutChunk, 16> layout;
00351   layout.reserve(block->capturesCXXThis() +
00352                  (block->capture_end() - block->capture_begin()));
00353 
00354   CharUnits maxFieldAlign;
00355 
00356   // First, 'this'.
00357   if (block->capturesCXXThis()) {
00358     assert(CGF && CGF->CurFuncDecl && isa<CXXMethodDecl>(CGF->CurFuncDecl) &&
00359            "Can't capture 'this' outside a method");
00360     QualType thisType = cast<CXXMethodDecl>(CGF->CurFuncDecl)->getThisType(C);
00361 
00362     llvm::Type *llvmType = CGM.getTypes().ConvertType(thisType);
00363     std::pair<CharUnits,CharUnits> tinfo
00364       = CGM.getContext().getTypeInfoInChars(thisType);
00365     maxFieldAlign = std::max(maxFieldAlign, tinfo.second);
00366 
00367     layout.push_back(BlockLayoutChunk(tinfo.second, tinfo.first,
00368                                       Qualifiers::OCL_None,
00369                                       nullptr, llvmType));
00370   }
00371 
00372   // Next, all the block captures.
00373   for (const auto &CI : block->captures()) {
00374     const VarDecl *variable = CI.getVariable();
00375 
00376     if (CI.isByRef()) {
00377       // We have to copy/dispose of the __block reference.
00378       info.NeedsCopyDispose = true;
00379 
00380       // Just use void* instead of a pointer to the byref type.
00381       QualType byRefPtrTy = C.VoidPtrTy;
00382 
00383       llvm::Type *llvmType = CGM.getTypes().ConvertType(byRefPtrTy);
00384       std::pair<CharUnits,CharUnits> tinfo
00385         = CGM.getContext().getTypeInfoInChars(byRefPtrTy);
00386       maxFieldAlign = std::max(maxFieldAlign, tinfo.second);
00387 
00388       layout.push_back(BlockLayoutChunk(tinfo.second, tinfo.first,
00389                                         Qualifiers::OCL_None, &CI, llvmType));
00390       continue;
00391     }
00392 
00393     // Otherwise, build a layout chunk with the size and alignment of
00394     // the declaration.
00395     if (llvm::Constant *constant = tryCaptureAsConstant(CGM, CGF, variable)) {
00396       info.Captures[variable] = CGBlockInfo::Capture::makeConstant(constant);
00397       continue;
00398     }
00399 
00400     // If we have a lifetime qualifier, honor it for capture purposes.
00401     // That includes *not* copying it if it's __unsafe_unretained.
00402     Qualifiers::ObjCLifetime lifetime =
00403       variable->getType().getObjCLifetime();
00404     if (lifetime) {
00405       switch (lifetime) {
00406       case Qualifiers::OCL_None: llvm_unreachable("impossible");
00407       case Qualifiers::OCL_ExplicitNone:
00408       case Qualifiers::OCL_Autoreleasing:
00409         break;
00410 
00411       case Qualifiers::OCL_Strong:
00412       case Qualifiers::OCL_Weak:
00413         info.NeedsCopyDispose = true;
00414       }
00415 
00416     // Block pointers require copy/dispose.  So do Objective-C pointers.
00417     } else if (variable->getType()->isObjCRetainableType()) {
00418       info.NeedsCopyDispose = true;
00419       // used for mrr below.
00420       lifetime = Qualifiers::OCL_Strong;
00421 
00422     // So do types that require non-trivial copy construction.
00423     } else if (CI.hasCopyExpr()) {
00424       info.NeedsCopyDispose = true;
00425       info.HasCXXObject = true;
00426 
00427     // And so do types with destructors.
00428     } else if (CGM.getLangOpts().CPlusPlus) {
00429       if (const CXXRecordDecl *record =
00430             variable->getType()->getAsCXXRecordDecl()) {
00431         if (!record->hasTrivialDestructor()) {
00432           info.HasCXXObject = true;
00433           info.NeedsCopyDispose = true;
00434         }
00435       }
00436     }
00437 
00438     QualType VT = variable->getType();
00439     CharUnits size = C.getTypeSizeInChars(VT);
00440     CharUnits align = C.getDeclAlign(variable);
00441     
00442     maxFieldAlign = std::max(maxFieldAlign, align);
00443 
00444     llvm::Type *llvmType =
00445       CGM.getTypes().ConvertTypeForMem(VT);
00446     
00447     layout.push_back(BlockLayoutChunk(align, size, lifetime, &CI, llvmType));
00448   }
00449 
00450   // If that was everything, we're done here.
00451   if (layout.empty()) {
00452     info.StructureType =
00453       llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true);
00454     info.CanBeGlobal = true;
00455     return;
00456   }
00457 
00458   // Sort the layout by alignment.  We have to use a stable sort here
00459   // to get reproducible results.  There should probably be an
00460   // llvm::array_pod_stable_sort.
00461   std::stable_sort(layout.begin(), layout.end());
00462   
00463   // Needed for blocks layout info.
00464   info.BlockHeaderForcedGapOffset = info.BlockSize;
00465   info.BlockHeaderForcedGapSize = CharUnits::Zero();
00466   
00467   CharUnits &blockSize = info.BlockSize;
00468   info.BlockAlign = std::max(maxFieldAlign, info.BlockAlign);
00469 
00470   // Assuming that the first byte in the header is maximally aligned,
00471   // get the alignment of the first byte following the header.
00472   CharUnits endAlign = getLowBit(blockSize);
00473 
00474   // If the end of the header isn't satisfactorily aligned for the
00475   // maximum thing, look for things that are okay with the header-end
00476   // alignment, and keep appending them until we get something that's
00477   // aligned right.  This algorithm is only guaranteed optimal if
00478   // that condition is satisfied at some point; otherwise we can get
00479   // things like:
00480   //   header                 // next byte has alignment 4
00481   //   something_with_size_5; // next byte has alignment 1
00482   //   something_with_alignment_8;
00483   // which has 7 bytes of padding, as opposed to the naive solution
00484   // which might have less (?).
00485   if (endAlign < maxFieldAlign) {
00486     SmallVectorImpl<BlockLayoutChunk>::iterator
00487       li = layout.begin() + 1, le = layout.end();
00488 
00489     // Look for something that the header end is already
00490     // satisfactorily aligned for.
00491     for (; li != le && endAlign < li->Alignment; ++li)
00492       ;
00493 
00494     // If we found something that's naturally aligned for the end of
00495     // the header, keep adding things...
00496     if (li != le) {
00497       SmallVectorImpl<BlockLayoutChunk>::iterator first = li;
00498       for (; li != le; ++li) {
00499         assert(endAlign >= li->Alignment);
00500 
00501         li->setIndex(info, elementTypes.size());
00502         elementTypes.push_back(li->Type);
00503         blockSize += li->Size;
00504         endAlign = getLowBit(blockSize);
00505 
00506         // ...until we get to the alignment of the maximum field.
00507         if (endAlign >= maxFieldAlign) {
00508           if (li == first) {
00509             // No user field was appended. So, a gap was added.
00510             // Save total gap size for use in block layout bit map.
00511             info.BlockHeaderForcedGapSize = li->Size;
00512           }
00513           break;
00514         }
00515       }
00516       // Don't re-append everything we just appended.
00517       layout.erase(first, li);
00518     }
00519   }
00520 
00521   assert(endAlign == getLowBit(blockSize));
00522   
00523   // At this point, we just have to add padding if the end align still
00524   // isn't aligned right.
00525   if (endAlign < maxFieldAlign) {
00526     CharUnits newBlockSize = blockSize.RoundUpToAlignment(maxFieldAlign);
00527     CharUnits padding = newBlockSize - blockSize;
00528 
00529     elementTypes.push_back(llvm::ArrayType::get(CGM.Int8Ty,
00530                                                 padding.getQuantity()));
00531     blockSize = newBlockSize;
00532     endAlign = getLowBit(blockSize); // might be > maxFieldAlign
00533   }
00534 
00535   assert(endAlign >= maxFieldAlign);
00536   assert(endAlign == getLowBit(blockSize));
00537   // Slam everything else on now.  This works because they have
00538   // strictly decreasing alignment and we expect that size is always a
00539   // multiple of alignment.
00540   for (SmallVectorImpl<BlockLayoutChunk>::iterator
00541          li = layout.begin(), le = layout.end(); li != le; ++li) {
00542     assert(endAlign >= li->Alignment);
00543     li->setIndex(info, elementTypes.size());
00544     elementTypes.push_back(li->Type);
00545     blockSize += li->Size;
00546     endAlign = getLowBit(blockSize);
00547   }
00548 
00549   info.StructureType =
00550     llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true);
00551 }
00552 
00553 /// Enter the scope of a block.  This should be run at the entrance to
00554 /// a full-expression so that the block's cleanups are pushed at the
00555 /// right place in the stack.
00556 static void enterBlockScope(CodeGenFunction &CGF, BlockDecl *block) {
00557   assert(CGF.HaveInsertPoint());
00558 
00559   // Allocate the block info and place it at the head of the list.
00560   CGBlockInfo &blockInfo =
00561     *new CGBlockInfo(block, CGF.CurFn->getName());
00562   blockInfo.NextBlockInfo = CGF.FirstBlockInfo;
00563   CGF.FirstBlockInfo = &blockInfo;
00564 
00565   // Compute information about the layout, etc., of this block,
00566   // pushing cleanups as necessary.
00567   computeBlockInfo(CGF.CGM, &CGF, blockInfo);
00568 
00569   // Nothing else to do if it can be global.
00570   if (blockInfo.CanBeGlobal) return;
00571 
00572   // Make the allocation for the block.
00573   blockInfo.Address =
00574     CGF.CreateTempAlloca(blockInfo.StructureType, "block");
00575   blockInfo.Address->setAlignment(blockInfo.BlockAlign.getQuantity());
00576 
00577   // If there are cleanups to emit, enter them (but inactive).
00578   if (!blockInfo.NeedsCopyDispose) return;
00579 
00580   // Walk through the captures (in order) and find the ones not
00581   // captured by constant.
00582   for (const auto &CI : block->captures()) {
00583     // Ignore __block captures; there's nothing special in the
00584     // on-stack block that we need to do for them.
00585     if (CI.isByRef()) continue;
00586 
00587     // Ignore variables that are constant-captured.
00588     const VarDecl *variable = CI.getVariable();
00589     CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
00590     if (capture.isConstant()) continue;
00591 
00592     // Ignore objects that aren't destructed.
00593     QualType::DestructionKind dtorKind =
00594       variable->getType().isDestructedType();
00595     if (dtorKind == QualType::DK_none) continue;
00596 
00597     CodeGenFunction::Destroyer *destroyer;
00598 
00599     // Block captures count as local values and have imprecise semantics.
00600     // They also can't be arrays, so need to worry about that.
00601     if (dtorKind == QualType::DK_objc_strong_lifetime) {
00602       destroyer = CodeGenFunction::destroyARCStrongImprecise;
00603     } else {
00604       destroyer = CGF.getDestroyer(dtorKind);
00605     }
00606 
00607     // GEP down to the address.
00608     llvm::Value *addr = CGF.Builder.CreateStructGEP(blockInfo.Address,
00609                                                     capture.getIndex());
00610 
00611     // We can use that GEP as the dominating IP.
00612     if (!blockInfo.DominatingIP)
00613       blockInfo.DominatingIP = cast<llvm::Instruction>(addr);
00614 
00615     CleanupKind cleanupKind = InactiveNormalCleanup;
00616     bool useArrayEHCleanup = CGF.needsEHCleanup(dtorKind);
00617     if (useArrayEHCleanup) 
00618       cleanupKind = InactiveNormalAndEHCleanup;
00619 
00620     CGF.pushDestroy(cleanupKind, addr, variable->getType(),
00621                     destroyer, useArrayEHCleanup);
00622 
00623     // Remember where that cleanup was.
00624     capture.setCleanup(CGF.EHStack.stable_begin());
00625   }
00626 }
00627 
00628 /// Enter a full-expression with a non-trivial number of objects to
00629 /// clean up.  This is in this file because, at the moment, the only
00630 /// kind of cleanup object is a BlockDecl*.
00631 void CodeGenFunction::enterNonTrivialFullExpression(const ExprWithCleanups *E) {
00632   assert(E->getNumObjects() != 0);
00633   ArrayRef<ExprWithCleanups::CleanupObject> cleanups = E->getObjects();
00634   for (ArrayRef<ExprWithCleanups::CleanupObject>::iterator
00635          i = cleanups.begin(), e = cleanups.end(); i != e; ++i) {
00636     enterBlockScope(*this, *i);
00637   }
00638 }
00639 
00640 /// Find the layout for the given block in a linked list and remove it.
00641 static CGBlockInfo *findAndRemoveBlockInfo(CGBlockInfo **head,
00642                                            const BlockDecl *block) {
00643   while (true) {
00644     assert(head && *head);
00645     CGBlockInfo *cur = *head;
00646 
00647     // If this is the block we're looking for, splice it out of the list.
00648     if (cur->getBlockDecl() == block) {
00649       *head = cur->NextBlockInfo;
00650       return cur;
00651     }
00652 
00653     head = &cur->NextBlockInfo;
00654   }
00655 }
00656 
00657 /// Destroy a chain of block layouts.
00658 void CodeGenFunction::destroyBlockInfos(CGBlockInfo *head) {
00659   assert(head && "destroying an empty chain");
00660   do {
00661     CGBlockInfo *cur = head;
00662     head = cur->NextBlockInfo;
00663     delete cur;
00664   } while (head != nullptr);
00665 }
00666 
00667 /// Emit a block literal expression in the current function.
00668 llvm::Value *CodeGenFunction::EmitBlockLiteral(const BlockExpr *blockExpr) {
00669   // If the block has no captures, we won't have a pre-computed
00670   // layout for it.
00671   if (!blockExpr->getBlockDecl()->hasCaptures()) {
00672     CGBlockInfo blockInfo(blockExpr->getBlockDecl(), CurFn->getName());
00673     computeBlockInfo(CGM, this, blockInfo);
00674     blockInfo.BlockExpression = blockExpr;
00675     return EmitBlockLiteral(blockInfo);
00676   }
00677 
00678   // Find the block info for this block and take ownership of it.
00679   std::unique_ptr<CGBlockInfo> blockInfo;
00680   blockInfo.reset(findAndRemoveBlockInfo(&FirstBlockInfo,
00681                                          blockExpr->getBlockDecl()));
00682 
00683   blockInfo->BlockExpression = blockExpr;
00684   return EmitBlockLiteral(*blockInfo);
00685 }
00686 
00687 llvm::Value *CodeGenFunction::EmitBlockLiteral(const CGBlockInfo &blockInfo) {
00688   // Using the computed layout, generate the actual block function.
00689   bool isLambdaConv = blockInfo.getBlockDecl()->isConversionFromLambda();
00690   llvm::Constant *blockFn
00691     = CodeGenFunction(CGM, true).GenerateBlockFunction(CurGD, blockInfo,
00692                                                        LocalDeclMap,
00693                                                        isLambdaConv);
00694   blockFn = llvm::ConstantExpr::getBitCast(blockFn, VoidPtrTy);
00695 
00696   // If there is nothing to capture, we can emit this as a global block.
00697   if (blockInfo.CanBeGlobal)
00698     return buildGlobalBlock(CGM, blockInfo, blockFn);
00699 
00700   // Otherwise, we have to emit this as a local block.
00701 
00702   llvm::Constant *isa = CGM.getNSConcreteStackBlock();
00703   isa = llvm::ConstantExpr::getBitCast(isa, VoidPtrTy);
00704 
00705   // Build the block descriptor.
00706   llvm::Constant *descriptor = buildBlockDescriptor(CGM, blockInfo);
00707 
00708   llvm::AllocaInst *blockAddr = blockInfo.Address;
00709   assert(blockAddr && "block has no address!");
00710 
00711   // Compute the initial on-stack block flags.
00712   BlockFlags flags = BLOCK_HAS_SIGNATURE;
00713   if (blockInfo.HasCapturedVariableLayout) flags |= BLOCK_HAS_EXTENDED_LAYOUT;
00714   if (blockInfo.NeedsCopyDispose) flags |= BLOCK_HAS_COPY_DISPOSE;
00715   if (blockInfo.HasCXXObject) flags |= BLOCK_HAS_CXX_OBJ;
00716   if (blockInfo.UsesStret) flags |= BLOCK_USE_STRET;
00717 
00718   // Initialize the block literal.
00719   Builder.CreateStore(isa, Builder.CreateStructGEP(blockAddr, 0, "block.isa"));
00720   Builder.CreateStore(llvm::ConstantInt::get(IntTy, flags.getBitMask()),
00721                       Builder.CreateStructGEP(blockAddr, 1, "block.flags"));
00722   Builder.CreateStore(llvm::ConstantInt::get(IntTy, 0),
00723                       Builder.CreateStructGEP(blockAddr, 2, "block.reserved"));
00724   Builder.CreateStore(blockFn, Builder.CreateStructGEP(blockAddr, 3,
00725                                                        "block.invoke"));
00726   Builder.CreateStore(descriptor, Builder.CreateStructGEP(blockAddr, 4,
00727                                                           "block.descriptor"));
00728 
00729   // Finally, capture all the values into the block.
00730   const BlockDecl *blockDecl = blockInfo.getBlockDecl();
00731 
00732   // First, 'this'.
00733   if (blockDecl->capturesCXXThis()) {
00734     llvm::Value *addr = Builder.CreateStructGEP(blockAddr,
00735                                                 blockInfo.CXXThisIndex,
00736                                                 "block.captured-this.addr");
00737     Builder.CreateStore(LoadCXXThis(), addr);
00738   }
00739 
00740   // Next, captured variables.
00741   for (const auto &CI : blockDecl->captures()) {
00742     const VarDecl *variable = CI.getVariable();
00743     const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
00744 
00745     // Ignore constant captures.
00746     if (capture.isConstant()) continue;
00747 
00748     QualType type = variable->getType();
00749     CharUnits align = getContext().getDeclAlign(variable);
00750 
00751     // This will be a [[type]]*, except that a byref entry will just be
00752     // an i8**.
00753     llvm::Value *blockField =
00754       Builder.CreateStructGEP(blockAddr, capture.getIndex(),
00755                               "block.captured");
00756 
00757     // Compute the address of the thing we're going to move into the
00758     // block literal.
00759     llvm::Value *src;
00760     if (BlockInfo && CI.isNested()) {
00761       // We need to use the capture from the enclosing block.
00762       const CGBlockInfo::Capture &enclosingCapture =
00763         BlockInfo->getCapture(variable);
00764 
00765       // This is a [[type]]*, except that a byref entry wil just be an i8**.
00766       src = Builder.CreateStructGEP(LoadBlockStruct(),
00767                                     enclosingCapture.getIndex(),
00768                                     "block.capture.addr");
00769     } else if (blockDecl->isConversionFromLambda()) {
00770       // The lambda capture in a lambda's conversion-to-block-pointer is
00771       // special; we'll simply emit it directly.
00772       src = nullptr;
00773     } else {
00774       // Just look it up in the locals map, which will give us back a
00775       // [[type]]*.  If that doesn't work, do the more elaborate DRE
00776       // emission.
00777       src = LocalDeclMap.lookup(variable);
00778       if (!src) {
00779         DeclRefExpr declRef(const_cast<VarDecl *>(variable),
00780                             /*refersToEnclosing*/ CI.isNested(), type,
00781                             VK_LValue, SourceLocation());
00782         src = EmitDeclRefLValue(&declRef).getAddress();
00783       }
00784     }
00785 
00786     // For byrefs, we just write the pointer to the byref struct into
00787     // the block field.  There's no need to chase the forwarding
00788     // pointer at this point, since we're building something that will
00789     // live a shorter life than the stack byref anyway.
00790     if (CI.isByRef()) {
00791       // Get a void* that points to the byref struct.
00792       if (CI.isNested())
00793         src = Builder.CreateAlignedLoad(src, align.getQuantity(),
00794                                         "byref.capture");
00795       else
00796         src = Builder.CreateBitCast(src, VoidPtrTy);
00797 
00798       // Write that void* into the capture field.
00799       Builder.CreateAlignedStore(src, blockField, align.getQuantity());
00800 
00801     // If we have a copy constructor, evaluate that into the block field.
00802     } else if (const Expr *copyExpr = CI.getCopyExpr()) {
00803       if (blockDecl->isConversionFromLambda()) {
00804         // If we have a lambda conversion, emit the expression
00805         // directly into the block instead.
00806         AggValueSlot Slot =
00807             AggValueSlot::forAddr(blockField, align, Qualifiers(),
00808                                   AggValueSlot::IsDestructed,
00809                                   AggValueSlot::DoesNotNeedGCBarriers,
00810                                   AggValueSlot::IsNotAliased);
00811         EmitAggExpr(copyExpr, Slot);
00812       } else {
00813         EmitSynthesizedCXXCopyCtor(blockField, src, copyExpr);
00814       }
00815 
00816     // If it's a reference variable, copy the reference into the block field.
00817     } else if (type->isReferenceType()) {
00818       llvm::Value *ref =
00819         Builder.CreateAlignedLoad(src, align.getQuantity(), "ref.val");
00820       Builder.CreateAlignedStore(ref, blockField, align.getQuantity());
00821 
00822     // If this is an ARC __strong block-pointer variable, don't do a
00823     // block copy.
00824     //
00825     // TODO: this can be generalized into the normal initialization logic:
00826     // we should never need to do a block-copy when initializing a local
00827     // variable, because the local variable's lifetime should be strictly
00828     // contained within the stack block's.
00829     } else if (type.getObjCLifetime() == Qualifiers::OCL_Strong &&
00830                type->isBlockPointerType()) {
00831       // Load the block and do a simple retain.
00832       LValue srcLV = MakeAddrLValue(src, type, align);
00833       llvm::Value *value = EmitLoadOfScalar(srcLV, SourceLocation());
00834       value = EmitARCRetainNonBlock(value);
00835 
00836       // Do a primitive store to the block field.
00837       LValue destLV = MakeAddrLValue(blockField, type, align);
00838       EmitStoreOfScalar(value, destLV, /*init*/ true);
00839 
00840     // Otherwise, fake up a POD copy into the block field.
00841     } else {
00842       // Fake up a new variable so that EmitScalarInit doesn't think
00843       // we're referring to the variable in its own initializer.
00844       ImplicitParamDecl blockFieldPseudoVar(getContext(), /*DC*/ nullptr,
00845                                             SourceLocation(), /*name*/ nullptr,
00846                                             type);
00847 
00848       // We use one of these or the other depending on whether the
00849       // reference is nested.
00850       DeclRefExpr declRef(const_cast<VarDecl*>(variable),
00851                           /*refersToEnclosing*/ CI.isNested(), type,
00852                           VK_LValue, SourceLocation());
00853 
00854       ImplicitCastExpr l2r(ImplicitCastExpr::OnStack, type, CK_LValueToRValue,
00855                            &declRef, VK_RValue);
00856       EmitExprAsInit(&l2r, &blockFieldPseudoVar,
00857                      MakeAddrLValue(blockField, type, align),
00858                      /*captured by init*/ false);
00859     }
00860 
00861     // Activate the cleanup if layout pushed one.
00862     if (!CI.isByRef()) {
00863       EHScopeStack::stable_iterator cleanup = capture.getCleanup();
00864       if (cleanup.isValid())
00865         ActivateCleanupBlock(cleanup, blockInfo.DominatingIP);
00866     }
00867   }
00868 
00869   // Cast to the converted block-pointer type, which happens (somewhat
00870   // unfortunately) to be a pointer to function type.
00871   llvm::Value *result =
00872     Builder.CreateBitCast(blockAddr,
00873                           ConvertType(blockInfo.getBlockExpr()->getType()));
00874 
00875   return result;
00876 }
00877 
00878 
00879 llvm::Type *CodeGenModule::getBlockDescriptorType() {
00880   if (BlockDescriptorType)
00881     return BlockDescriptorType;
00882 
00883   llvm::Type *UnsignedLongTy =
00884     getTypes().ConvertType(getContext().UnsignedLongTy);
00885 
00886   // struct __block_descriptor {
00887   //   unsigned long reserved;
00888   //   unsigned long block_size;
00889   //
00890   //   // later, the following will be added
00891   //
00892   //   struct {
00893   //     void (*copyHelper)();
00894   //     void (*copyHelper)();
00895   //   } helpers;                // !!! optional
00896   //
00897   //   const char *signature;   // the block signature
00898   //   const char *layout;      // reserved
00899   // };
00900   BlockDescriptorType =
00901     llvm::StructType::create("struct.__block_descriptor",
00902                              UnsignedLongTy, UnsignedLongTy, NULL);
00903 
00904   // Now form a pointer to that.
00905   BlockDescriptorType = llvm::PointerType::getUnqual(BlockDescriptorType);
00906   return BlockDescriptorType;
00907 }
00908 
00909 llvm::Type *CodeGenModule::getGenericBlockLiteralType() {
00910   if (GenericBlockLiteralType)
00911     return GenericBlockLiteralType;
00912 
00913   llvm::Type *BlockDescPtrTy = getBlockDescriptorType();
00914 
00915   // struct __block_literal_generic {
00916   //   void *__isa;
00917   //   int __flags;
00918   //   int __reserved;
00919   //   void (*__invoke)(void *);
00920   //   struct __block_descriptor *__descriptor;
00921   // };
00922   GenericBlockLiteralType =
00923     llvm::StructType::create("struct.__block_literal_generic",
00924                              VoidPtrTy, IntTy, IntTy, VoidPtrTy,
00925                              BlockDescPtrTy, NULL);
00926 
00927   return GenericBlockLiteralType;
00928 }
00929 
00930 
00931 RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr *E, 
00932                                           ReturnValueSlot ReturnValue) {
00933   const BlockPointerType *BPT =
00934     E->getCallee()->getType()->getAs<BlockPointerType>();
00935 
00936   llvm::Value *Callee = EmitScalarExpr(E->getCallee());
00937 
00938   // Get a pointer to the generic block literal.
00939   llvm::Type *BlockLiteralTy =
00940     llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
00941 
00942   // Bitcast the callee to a block literal.
00943   llvm::Value *BlockLiteral =
00944     Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
00945 
00946   // Get the function pointer from the literal.
00947   llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3);
00948 
00949   BlockLiteral = Builder.CreateBitCast(BlockLiteral, VoidPtrTy);
00950 
00951   // Add the block literal.
00952   CallArgList Args;
00953   Args.add(RValue::get(BlockLiteral), getContext().VoidPtrTy);
00954 
00955   QualType FnType = BPT->getPointeeType();
00956 
00957   // And the rest of the arguments.
00958   EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(),
00959                E->arg_begin(), E->arg_end());
00960 
00961   // Load the function.
00962   llvm::Value *Func = Builder.CreateLoad(FuncPtr);
00963 
00964   const FunctionType *FuncTy = FnType->castAs<FunctionType>();
00965   const CGFunctionInfo &FnInfo =
00966     CGM.getTypes().arrangeBlockFunctionCall(Args, FuncTy);
00967 
00968   // Cast the function pointer to the right type.
00969   llvm::Type *BlockFTy = CGM.getTypes().GetFunctionType(FnInfo);
00970 
00971   llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
00972   Func = Builder.CreateBitCast(Func, BlockFTyPtr);
00973 
00974   // And call the block.
00975   return EmitCall(FnInfo, Func, ReturnValue, Args);
00976 }
00977 
00978 llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const VarDecl *variable,
00979                                                  bool isByRef) {
00980   assert(BlockInfo && "evaluating block ref without block information?");
00981   const CGBlockInfo::Capture &capture = BlockInfo->getCapture(variable);
00982 
00983   // Handle constant captures.
00984   if (capture.isConstant()) return LocalDeclMap[variable];
00985 
00986   llvm::Value *addr =
00987     Builder.CreateStructGEP(LoadBlockStruct(), capture.getIndex(),
00988                             "block.capture.addr");
00989 
00990   if (isByRef) {
00991     // addr should be a void** right now.  Load, then cast the result
00992     // to byref*.
00993 
00994     addr = Builder.CreateLoad(addr);
00995     llvm::PointerType *byrefPointerType
00996       = llvm::PointerType::get(BuildByRefType(variable), 0);
00997     addr = Builder.CreateBitCast(addr, byrefPointerType,
00998                                  "byref.addr");
00999 
01000     // Follow the forwarding pointer.
01001     addr = Builder.CreateStructGEP(addr, 1, "byref.forwarding");
01002     addr = Builder.CreateLoad(addr, "byref.addr.forwarded");
01003 
01004     // Cast back to byref* and GEP over to the actual object.
01005     addr = Builder.CreateBitCast(addr, byrefPointerType);
01006     addr = Builder.CreateStructGEP(addr, getByRefValueLLVMField(variable), 
01007                                    variable->getNameAsString());
01008   }
01009 
01010   if (variable->getType()->isReferenceType())
01011     addr = Builder.CreateLoad(addr, "ref.tmp");
01012 
01013   return addr;
01014 }
01015 
01016 llvm::Constant *
01017 CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *blockExpr,
01018                                     const char *name) {
01019   CGBlockInfo blockInfo(blockExpr->getBlockDecl(), name);
01020   blockInfo.BlockExpression = blockExpr;
01021 
01022   // Compute information about the layout, etc., of this block.
01023   computeBlockInfo(*this, nullptr, blockInfo);
01024 
01025   // Using that metadata, generate the actual block function.
01026   llvm::Constant *blockFn;
01027   {
01028     llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
01029     blockFn = CodeGenFunction(*this).GenerateBlockFunction(GlobalDecl(),
01030                                                            blockInfo,
01031                                                            LocalDeclMap,
01032                                                            false);
01033   }
01034   blockFn = llvm::ConstantExpr::getBitCast(blockFn, VoidPtrTy);
01035 
01036   return buildGlobalBlock(*this, blockInfo, blockFn);
01037 }
01038 
01039 static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM,
01040                                         const CGBlockInfo &blockInfo,
01041                                         llvm::Constant *blockFn) {
01042   assert(blockInfo.CanBeGlobal);
01043 
01044   // Generate the constants for the block literal initializer.
01045   llvm::Constant *fields[BlockHeaderSize];
01046 
01047   // isa
01048   fields[0] = CGM.getNSConcreteGlobalBlock();
01049 
01050   // __flags
01051   BlockFlags flags = BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE;
01052   if (blockInfo.UsesStret) flags |= BLOCK_USE_STRET;
01053                                       
01054   fields[1] = llvm::ConstantInt::get(CGM.IntTy, flags.getBitMask());
01055 
01056   // Reserved
01057   fields[2] = llvm::Constant::getNullValue(CGM.IntTy);
01058 
01059   // Function
01060   fields[3] = blockFn;
01061 
01062   // Descriptor
01063   fields[4] = buildBlockDescriptor(CGM, blockInfo);
01064 
01065   llvm::Constant *init = llvm::ConstantStruct::getAnon(fields);
01066 
01067   llvm::GlobalVariable *literal =
01068     new llvm::GlobalVariable(CGM.getModule(),
01069                              init->getType(),
01070                              /*constant*/ true,
01071                              llvm::GlobalVariable::InternalLinkage,
01072                              init,
01073                              "__block_literal_global");
01074   literal->setAlignment(blockInfo.BlockAlign.getQuantity());
01075 
01076   // Return a constant of the appropriately-casted type.
01077   llvm::Type *requiredType =
01078     CGM.getTypes().ConvertType(blockInfo.getBlockExpr()->getType());
01079   return llvm::ConstantExpr::getBitCast(literal, requiredType);
01080 }
01081 
01082 llvm::Function *
01083 CodeGenFunction::GenerateBlockFunction(GlobalDecl GD,
01084                                        const CGBlockInfo &blockInfo,
01085                                        const DeclMapTy &ldm,
01086                                        bool IsLambdaConversionToBlock) {
01087   const BlockDecl *blockDecl = blockInfo.getBlockDecl();
01088 
01089   CurGD = GD;
01090   
01091   BlockInfo = &blockInfo;
01092 
01093   // Arrange for local static and local extern declarations to appear
01094   // to be local to this function as well, in case they're directly
01095   // referenced in a block.
01096   for (DeclMapTy::const_iterator i = ldm.begin(), e = ldm.end(); i != e; ++i) {
01097     const auto *var = dyn_cast<VarDecl>(i->first);
01098     if (var && !var->hasLocalStorage())
01099       LocalDeclMap[var] = i->second;
01100   }
01101 
01102   // Begin building the function declaration.
01103 
01104   // Build the argument list.
01105   FunctionArgList args;
01106 
01107   // The first argument is the block pointer.  Just take it as a void*
01108   // and cast it later.
01109   QualType selfTy = getContext().VoidPtrTy;
01110   IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor");
01111 
01112   ImplicitParamDecl selfDecl(getContext(), const_cast<BlockDecl*>(blockDecl),
01113                              SourceLocation(), II, selfTy);
01114   args.push_back(&selfDecl);
01115 
01116   // Now add the rest of the parameters.
01117   for (auto i : blockDecl->params())
01118     args.push_back(i);
01119 
01120   // Create the function declaration.
01121   const FunctionProtoType *fnType = blockInfo.getBlockExpr()->getFunctionType();
01122   const CGFunctionInfo &fnInfo = CGM.getTypes().arrangeFreeFunctionDeclaration(
01123       fnType->getReturnType(), args, fnType->getExtInfo(),
01124       fnType->isVariadic());
01125   if (CGM.ReturnSlotInterferesWithArgs(fnInfo))
01126     blockInfo.UsesStret = true;
01127 
01128   llvm::FunctionType *fnLLVMType = CGM.getTypes().GetFunctionType(fnInfo);
01129 
01130   StringRef name = CGM.getBlockMangledName(GD, blockDecl);
01131   llvm::Function *fn = llvm::Function::Create(
01132       fnLLVMType, llvm::GlobalValue::InternalLinkage, name, &CGM.getModule());
01133   CGM.SetInternalFunctionAttributes(blockDecl, fn, fnInfo);
01134 
01135   // Begin generating the function.
01136   StartFunction(blockDecl, fnType->getReturnType(), fn, fnInfo, args,
01137                 blockDecl->getLocation(),
01138                 blockInfo.getBlockExpr()->getBody()->getLocStart());
01139 
01140   // Okay.  Undo some of what StartFunction did.
01141   
01142   // Pull the 'self' reference out of the local decl map.
01143   llvm::Value *blockAddr = LocalDeclMap[&selfDecl];
01144   LocalDeclMap.erase(&selfDecl);
01145   BlockPointer = Builder.CreateBitCast(blockAddr,
01146                                        blockInfo.StructureType->getPointerTo(),
01147                                        "block");
01148   // At -O0 we generate an explicit alloca for the BlockPointer, so the RA
01149   // won't delete the dbg.declare intrinsics for captured variables.
01150   llvm::Value *BlockPointerDbgLoc = BlockPointer;
01151   if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
01152     // Allocate a stack slot for it, so we can point the debugger to it
01153     llvm::AllocaInst *Alloca = CreateTempAlloca(BlockPointer->getType(),
01154                                                 "block.addr");
01155     unsigned Align = getContext().getDeclAlign(&selfDecl).getQuantity();
01156     Alloca->setAlignment(Align);
01157     // Set the DebugLocation to empty, so the store is recognized as a
01158     // frame setup instruction by llvm::DwarfDebug::beginFunction().
01159     NoLocation NL(*this, Builder);
01160     Builder.CreateAlignedStore(BlockPointer, Alloca, Align);
01161     BlockPointerDbgLoc = Alloca;
01162   }
01163 
01164   // If we have a C++ 'this' reference, go ahead and force it into
01165   // existence now.
01166   if (blockDecl->capturesCXXThis()) {
01167     llvm::Value *addr = Builder.CreateStructGEP(BlockPointer,
01168                                                 blockInfo.CXXThisIndex,
01169                                                 "block.captured-this");
01170     CXXThisValue = Builder.CreateLoad(addr, "this");
01171   }
01172 
01173   // Also force all the constant captures.
01174   for (const auto &CI : blockDecl->captures()) {
01175     const VarDecl *variable = CI.getVariable();
01176     const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
01177     if (!capture.isConstant()) continue;
01178 
01179     unsigned align = getContext().getDeclAlign(variable).getQuantity();
01180 
01181     llvm::AllocaInst *alloca =
01182       CreateMemTemp(variable->getType(), "block.captured-const");
01183     alloca->setAlignment(align);
01184 
01185     Builder.CreateAlignedStore(capture.getConstant(), alloca, align);
01186 
01187     LocalDeclMap[variable] = alloca;
01188   }
01189 
01190   // Save a spot to insert the debug information for all the DeclRefExprs.
01191   llvm::BasicBlock *entry = Builder.GetInsertBlock();
01192   llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint();
01193   --entry_ptr;
01194 
01195   if (IsLambdaConversionToBlock)
01196     EmitLambdaBlockInvokeBody();
01197   else {
01198     PGO.assignRegionCounters(blockDecl, fn);
01199     RegionCounter Cnt = getPGORegionCounter(blockDecl->getBody());
01200     Cnt.beginRegion(Builder);
01201     EmitStmt(blockDecl->getBody());
01202     PGO.emitInstrumentationData();
01203     PGO.destroyRegionCounters();
01204   }
01205 
01206   // Remember where we were...
01207   llvm::BasicBlock *resume = Builder.GetInsertBlock();
01208 
01209   // Go back to the entry.
01210   ++entry_ptr;
01211   Builder.SetInsertPoint(entry, entry_ptr);
01212 
01213   // Emit debug information for all the DeclRefExprs.
01214   // FIXME: also for 'this'
01215   if (CGDebugInfo *DI = getDebugInfo()) {
01216     for (const auto &CI : blockDecl->captures()) {
01217       const VarDecl *variable = CI.getVariable();
01218       DI->EmitLocation(Builder, variable->getLocation());
01219 
01220       if (CGM.getCodeGenOpts().getDebugInfo()
01221             >= CodeGenOptions::LimitedDebugInfo) {
01222         const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
01223         if (capture.isConstant()) {
01224           DI->EmitDeclareOfAutoVariable(variable, LocalDeclMap[variable],
01225                                         Builder);
01226           continue;
01227         }
01228 
01229         DI->EmitDeclareOfBlockDeclRefVariable(variable, BlockPointerDbgLoc,
01230                                               Builder, blockInfo);
01231       }
01232     }
01233     // Recover location if it was changed in the above loop.
01234     DI->EmitLocation(Builder,
01235                      cast<CompoundStmt>(blockDecl->getBody())->getRBracLoc());
01236   }
01237 
01238   // And resume where we left off.
01239   if (resume == nullptr)
01240     Builder.ClearInsertionPoint();
01241   else
01242     Builder.SetInsertPoint(resume);
01243 
01244   FinishFunction(cast<CompoundStmt>(blockDecl->getBody())->getRBracLoc());
01245 
01246   return fn;
01247 }
01248 
01249 /*
01250     notes.push_back(HelperInfo());
01251     HelperInfo &note = notes.back();
01252     note.index = capture.getIndex();
01253     note.RequiresCopying = (ci->hasCopyExpr() || BlockRequiresCopying(type));
01254     note.cxxbar_import = ci->getCopyExpr();
01255 
01256     if (ci->isByRef()) {
01257       note.flag = BLOCK_FIELD_IS_BYREF;
01258       if (type.isObjCGCWeak())
01259         note.flag |= BLOCK_FIELD_IS_WEAK;
01260     } else if (type->isBlockPointerType()) {
01261       note.flag = BLOCK_FIELD_IS_BLOCK;
01262     } else {
01263       note.flag = BLOCK_FIELD_IS_OBJECT;
01264     }
01265  */
01266 
01267 
01268 /// Generate the copy-helper function for a block closure object:
01269 ///   static void block_copy_helper(block_t *dst, block_t *src);
01270 /// The runtime will have previously initialized 'dst' by doing a
01271 /// bit-copy of 'src'.
01272 ///
01273 /// Note that this copies an entire block closure object to the heap;
01274 /// it should not be confused with a 'byref copy helper', which moves
01275 /// the contents of an individual __block variable to the heap.
01276 llvm::Constant *
01277 CodeGenFunction::GenerateCopyHelperFunction(const CGBlockInfo &blockInfo) {
01278   ASTContext &C = getContext();
01279 
01280   FunctionArgList args;
01281   ImplicitParamDecl dstDecl(getContext(), nullptr, SourceLocation(), nullptr,
01282                             C.VoidPtrTy);
01283   args.push_back(&dstDecl);
01284   ImplicitParamDecl srcDecl(getContext(), nullptr, SourceLocation(), nullptr,
01285                             C.VoidPtrTy);
01286   args.push_back(&srcDecl);
01287 
01288   const CGFunctionInfo &FI = CGM.getTypes().arrangeFreeFunctionDeclaration(
01289       C.VoidTy, args, FunctionType::ExtInfo(), /*variadic=*/false);
01290 
01291   // FIXME: it would be nice if these were mergeable with things with
01292   // identical semantics.
01293   llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI);
01294 
01295   llvm::Function *Fn =
01296     llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
01297                            "__copy_helper_block_", &CGM.getModule());
01298 
01299   IdentifierInfo *II
01300     = &CGM.getContext().Idents.get("__copy_helper_block_");
01301 
01302   FunctionDecl *FD = FunctionDecl::Create(C,
01303                                           C.getTranslationUnitDecl(),
01304                                           SourceLocation(),
01305                                           SourceLocation(), II, C.VoidTy,
01306                                           nullptr, SC_Static,
01307                                           false,
01308                                           false);
01309   // Create a scope with an artificial location for the body of this function.
01310   ArtificialLocation AL(*this, Builder);
01311   StartFunction(FD, C.VoidTy, Fn, FI, args);
01312   AL.Emit();
01313 
01314   llvm::Type *structPtrTy = blockInfo.StructureType->getPointerTo();
01315 
01316   llvm::Value *src = GetAddrOfLocalVar(&srcDecl);
01317   src = Builder.CreateLoad(src);
01318   src = Builder.CreateBitCast(src, structPtrTy, "block.source");
01319 
01320   llvm::Value *dst = GetAddrOfLocalVar(&dstDecl);
01321   dst = Builder.CreateLoad(dst);
01322   dst = Builder.CreateBitCast(dst, structPtrTy, "block.dest");
01323 
01324   const BlockDecl *blockDecl = blockInfo.getBlockDecl();
01325 
01326   for (const auto &CI : blockDecl->captures()) {
01327     const VarDecl *variable = CI.getVariable();
01328     QualType type = variable->getType();
01329 
01330     const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
01331     if (capture.isConstant()) continue;
01332 
01333     const Expr *copyExpr = CI.getCopyExpr();
01334     BlockFieldFlags flags;
01335 
01336     bool useARCWeakCopy = false;
01337     bool useARCStrongCopy = false;
01338 
01339     if (copyExpr) {
01340       assert(!CI.isByRef());
01341       // don't bother computing flags
01342 
01343     } else if (CI.isByRef()) {
01344       flags = BLOCK_FIELD_IS_BYREF;
01345       if (type.isObjCGCWeak())
01346         flags |= BLOCK_FIELD_IS_WEAK;
01347 
01348     } else if (type->isObjCRetainableType()) {
01349       flags = BLOCK_FIELD_IS_OBJECT;
01350       bool isBlockPointer = type->isBlockPointerType();
01351       if (isBlockPointer)
01352         flags = BLOCK_FIELD_IS_BLOCK;
01353 
01354       // Special rules for ARC captures:
01355       if (getLangOpts().ObjCAutoRefCount) {
01356         Qualifiers qs = type.getQualifiers();
01357 
01358         // We need to register __weak direct captures with the runtime.
01359         if (qs.getObjCLifetime() == Qualifiers::OCL_Weak) {
01360           useARCWeakCopy = true;
01361 
01362         // We need to retain the copied value for __strong direct captures.
01363         } else if (qs.getObjCLifetime() == Qualifiers::OCL_Strong) {
01364           // If it's a block pointer, we have to copy the block and
01365           // assign that to the destination pointer, so we might as
01366           // well use _Block_object_assign.  Otherwise we can avoid that.
01367           if (!isBlockPointer)
01368             useARCStrongCopy = true;
01369 
01370         // Otherwise the memcpy is fine.
01371         } else {
01372           continue;
01373         }
01374 
01375       // Non-ARC captures of retainable pointers are strong and
01376       // therefore require a call to _Block_object_assign.
01377       } else {
01378         // fall through
01379       }
01380     } else {
01381       continue;
01382     }
01383 
01384     unsigned index = capture.getIndex();
01385     llvm::Value *srcField = Builder.CreateStructGEP(src, index);
01386     llvm::Value *dstField = Builder.CreateStructGEP(dst, index);
01387 
01388     // If there's an explicit copy expression, we do that.
01389     if (copyExpr) {
01390       EmitSynthesizedCXXCopyCtor(dstField, srcField, copyExpr);
01391     } else if (useARCWeakCopy) {
01392       EmitARCCopyWeak(dstField, srcField);
01393     } else {
01394       llvm::Value *srcValue = Builder.CreateLoad(srcField, "blockcopy.src");
01395       if (useARCStrongCopy) {
01396         // At -O0, store null into the destination field (so that the
01397         // storeStrong doesn't over-release) and then call storeStrong.
01398         // This is a workaround to not having an initStrong call.
01399         if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
01400           auto *ty = cast<llvm::PointerType>(srcValue->getType());
01401           llvm::Value *null = llvm::ConstantPointerNull::get(ty);
01402           Builder.CreateStore(null, dstField);
01403           EmitARCStoreStrongCall(dstField, srcValue, true);
01404 
01405         // With optimization enabled, take advantage of the fact that
01406         // the blocks runtime guarantees a memcpy of the block data, and
01407         // just emit a retain of the src field.
01408         } else {
01409           EmitARCRetainNonBlock(srcValue);
01410 
01411           // We don't need this anymore, so kill it.  It's not quite
01412           // worth the annoyance to avoid creating it in the first place.
01413           cast<llvm::Instruction>(dstField)->eraseFromParent();
01414         }
01415       } else {
01416         srcValue = Builder.CreateBitCast(srcValue, VoidPtrTy);
01417         llvm::Value *dstAddr = Builder.CreateBitCast(dstField, VoidPtrTy);
01418         llvm::Value *args[] = {
01419           dstAddr, srcValue, llvm::ConstantInt::get(Int32Ty, flags.getBitMask())
01420         };
01421 
01422         bool copyCanThrow = false;
01423         if (CI.isByRef() && variable->getType()->getAsCXXRecordDecl()) {
01424           const Expr *copyExpr =
01425             CGM.getContext().getBlockVarCopyInits(variable);
01426           if (copyExpr) {
01427             copyCanThrow = true; // FIXME: reuse the noexcept logic
01428           }
01429         }
01430 
01431         if (copyCanThrow) {
01432           EmitRuntimeCallOrInvoke(CGM.getBlockObjectAssign(), args);
01433         } else {
01434           EmitNounwindRuntimeCall(CGM.getBlockObjectAssign(), args);
01435         }
01436       }
01437     }
01438   }
01439 
01440   FinishFunction();
01441 
01442   return llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy);
01443 }
01444 
01445 /// Generate the destroy-helper function for a block closure object:
01446 ///   static void block_destroy_helper(block_t *theBlock);
01447 ///
01448 /// Note that this destroys a heap-allocated block closure object;
01449 /// it should not be confused with a 'byref destroy helper', which
01450 /// destroys the heap-allocated contents of an individual __block
01451 /// variable.
01452 llvm::Constant *
01453 CodeGenFunction::GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo) {
01454   ASTContext &C = getContext();
01455 
01456   FunctionArgList args;
01457   ImplicitParamDecl srcDecl(getContext(), nullptr, SourceLocation(), nullptr,
01458                             C.VoidPtrTy);
01459   args.push_back(&srcDecl);
01460 
01461   const CGFunctionInfo &FI = CGM.getTypes().arrangeFreeFunctionDeclaration(
01462       C.VoidTy, args, FunctionType::ExtInfo(), /*variadic=*/false);
01463 
01464   // FIXME: We'd like to put these into a mergable by content, with
01465   // internal linkage.
01466   llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI);
01467 
01468   llvm::Function *Fn =
01469     llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
01470                            "__destroy_helper_block_", &CGM.getModule());
01471 
01472   IdentifierInfo *II
01473     = &CGM.getContext().Idents.get("__destroy_helper_block_");
01474 
01475   FunctionDecl *FD = FunctionDecl::Create(C, C.getTranslationUnitDecl(),
01476                                           SourceLocation(),
01477                                           SourceLocation(), II, C.VoidTy,
01478                                           nullptr, SC_Static,
01479                                           false, false);
01480   // Create a scope with an artificial location for the body of this function.
01481   ArtificialLocation AL(*this, Builder);
01482   StartFunction(FD, C.VoidTy, Fn, FI, args);
01483   AL.Emit();
01484 
01485   llvm::Type *structPtrTy = blockInfo.StructureType->getPointerTo();
01486 
01487   llvm::Value *src = GetAddrOfLocalVar(&srcDecl);
01488   src = Builder.CreateLoad(src);
01489   src = Builder.CreateBitCast(src, structPtrTy, "block");
01490 
01491   const BlockDecl *blockDecl = blockInfo.getBlockDecl();
01492 
01493   CodeGenFunction::RunCleanupsScope cleanups(*this);
01494 
01495   for (const auto &CI : blockDecl->captures()) {
01496     const VarDecl *variable = CI.getVariable();
01497     QualType type = variable->getType();
01498 
01499     const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
01500     if (capture.isConstant()) continue;
01501 
01502     BlockFieldFlags flags;
01503     const CXXDestructorDecl *dtor = nullptr;
01504 
01505     bool useARCWeakDestroy = false;
01506     bool useARCStrongDestroy = false;
01507 
01508     if (CI.isByRef()) {
01509       flags = BLOCK_FIELD_IS_BYREF;
01510       if (type.isObjCGCWeak())
01511         flags |= BLOCK_FIELD_IS_WEAK;
01512     } else if (const CXXRecordDecl *record = type->getAsCXXRecordDecl()) {
01513       if (record->hasTrivialDestructor())
01514         continue;
01515       dtor = record->getDestructor();
01516     } else if (type->isObjCRetainableType()) {
01517       flags = BLOCK_FIELD_IS_OBJECT;
01518       if (type->isBlockPointerType())
01519         flags = BLOCK_FIELD_IS_BLOCK;
01520 
01521       // Special rules for ARC captures.
01522       if (getLangOpts().ObjCAutoRefCount) {
01523         Qualifiers qs = type.getQualifiers();
01524 
01525         // Don't generate special dispose logic for a captured object
01526         // unless it's __strong or __weak.
01527         if (!qs.hasStrongOrWeakObjCLifetime())
01528           continue;
01529 
01530         // Support __weak direct captures.
01531         if (qs.getObjCLifetime() == Qualifiers::OCL_Weak)
01532           useARCWeakDestroy = true;
01533 
01534         // Tools really want us to use objc_storeStrong here.
01535         else
01536           useARCStrongDestroy = true;
01537       }
01538     } else {
01539       continue;
01540     }
01541 
01542     unsigned index = capture.getIndex();
01543     llvm::Value *srcField = Builder.CreateStructGEP(src, index);
01544 
01545     // If there's an explicit copy expression, we do that.
01546     if (dtor) {
01547       PushDestructorCleanup(dtor, srcField);
01548 
01549     // If this is a __weak capture, emit the release directly.
01550     } else if (useARCWeakDestroy) {
01551       EmitARCDestroyWeak(srcField);
01552 
01553     // Destroy strong objects with a call if requested.
01554     } else if (useARCStrongDestroy) {
01555       EmitARCDestroyStrong(srcField, ARCImpreciseLifetime);
01556 
01557     // Otherwise we call _Block_object_dispose.  It wouldn't be too
01558     // hard to just emit this as a cleanup if we wanted to make sure
01559     // that things were done in reverse.
01560     } else {
01561       llvm::Value *value = Builder.CreateLoad(srcField);
01562       value = Builder.CreateBitCast(value, VoidPtrTy);
01563       BuildBlockRelease(value, flags);
01564     }
01565   }
01566 
01567   cleanups.ForceCleanup();
01568 
01569   FinishFunction();
01570 
01571   return llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy);
01572 }
01573 
01574 namespace {
01575 
01576 /// Emits the copy/dispose helper functions for a __block object of id type.
01577 class ObjectByrefHelpers : public CodeGenModule::ByrefHelpers {
01578   BlockFieldFlags Flags;
01579 
01580 public:
01581   ObjectByrefHelpers(CharUnits alignment, BlockFieldFlags flags)
01582     : ByrefHelpers(alignment), Flags(flags) {}
01583 
01584   void emitCopy(CodeGenFunction &CGF, llvm::Value *destField,
01585                 llvm::Value *srcField) override {
01586     destField = CGF.Builder.CreateBitCast(destField, CGF.VoidPtrTy);
01587 
01588     srcField = CGF.Builder.CreateBitCast(srcField, CGF.VoidPtrPtrTy);
01589     llvm::Value *srcValue = CGF.Builder.CreateLoad(srcField);
01590 
01591     unsigned flags = (Flags | BLOCK_BYREF_CALLER).getBitMask();
01592 
01593     llvm::Value *flagsVal = llvm::ConstantInt::get(CGF.Int32Ty, flags);
01594     llvm::Value *fn = CGF.CGM.getBlockObjectAssign();
01595 
01596     llvm::Value *args[] = { destField, srcValue, flagsVal };
01597     CGF.EmitNounwindRuntimeCall(fn, args);
01598   }
01599 
01600   void emitDispose(CodeGenFunction &CGF, llvm::Value *field) override {
01601     field = CGF.Builder.CreateBitCast(field, CGF.Int8PtrTy->getPointerTo(0));
01602     llvm::Value *value = CGF.Builder.CreateLoad(field);
01603 
01604     CGF.BuildBlockRelease(value, Flags | BLOCK_BYREF_CALLER);
01605   }
01606 
01607   void profileImpl(llvm::FoldingSetNodeID &id) const override {
01608     id.AddInteger(Flags.getBitMask());
01609   }
01610 };
01611 
01612 /// Emits the copy/dispose helpers for an ARC __block __weak variable.
01613 class ARCWeakByrefHelpers : public CodeGenModule::ByrefHelpers {
01614 public:
01615   ARCWeakByrefHelpers(CharUnits alignment) : ByrefHelpers(alignment) {}
01616 
01617   void emitCopy(CodeGenFunction &CGF, llvm::Value *destField,
01618                 llvm::Value *srcField) override {
01619     CGF.EmitARCMoveWeak(destField, srcField);
01620   }
01621 
01622   void emitDispose(CodeGenFunction &CGF, llvm::Value *field) override {
01623     CGF.EmitARCDestroyWeak(field);
01624   }
01625 
01626   void profileImpl(llvm::FoldingSetNodeID &id) const override {
01627     // 0 is distinguishable from all pointers and byref flags
01628     id.AddInteger(0);
01629   }
01630 };
01631 
01632 /// Emits the copy/dispose helpers for an ARC __block __strong variable
01633 /// that's not of block-pointer type.
01634 class ARCStrongByrefHelpers : public CodeGenModule::ByrefHelpers {
01635 public:
01636   ARCStrongByrefHelpers(CharUnits alignment) : ByrefHelpers(alignment) {}
01637 
01638   void emitCopy(CodeGenFunction &CGF, llvm::Value *destField,
01639                 llvm::Value *srcField) override {
01640     // Do a "move" by copying the value and then zeroing out the old
01641     // variable.
01642 
01643     llvm::LoadInst *value = CGF.Builder.CreateLoad(srcField);
01644     value->setAlignment(Alignment.getQuantity());
01645     
01646     llvm::Value *null =
01647       llvm::ConstantPointerNull::get(cast<llvm::PointerType>(value->getType()));
01648 
01649     if (CGF.CGM.getCodeGenOpts().OptimizationLevel == 0) {
01650       llvm::StoreInst *store = CGF.Builder.CreateStore(null, destField);
01651       store->setAlignment(Alignment.getQuantity());
01652       CGF.EmitARCStoreStrongCall(destField, value, /*ignored*/ true);
01653       CGF.EmitARCStoreStrongCall(srcField, null, /*ignored*/ true);
01654       return;
01655     }
01656     llvm::StoreInst *store = CGF.Builder.CreateStore(value, destField);
01657     store->setAlignment(Alignment.getQuantity());
01658 
01659     store = CGF.Builder.CreateStore(null, srcField);
01660     store->setAlignment(Alignment.getQuantity());
01661   }
01662 
01663   void emitDispose(CodeGenFunction &CGF, llvm::Value *field) override {
01664     CGF.EmitARCDestroyStrong(field, ARCImpreciseLifetime);
01665   }
01666 
01667   void profileImpl(llvm::FoldingSetNodeID &id) const override {
01668     // 1 is distinguishable from all pointers and byref flags
01669     id.AddInteger(1);
01670   }
01671 };
01672 
01673 /// Emits the copy/dispose helpers for an ARC __block __strong
01674 /// variable that's of block-pointer type.
01675 class ARCStrongBlockByrefHelpers : public CodeGenModule::ByrefHelpers {
01676 public:
01677   ARCStrongBlockByrefHelpers(CharUnits alignment) : ByrefHelpers(alignment) {}
01678 
01679   void emitCopy(CodeGenFunction &CGF, llvm::Value *destField,
01680                 llvm::Value *srcField) override {
01681     // Do the copy with objc_retainBlock; that's all that
01682     // _Block_object_assign would do anyway, and we'd have to pass the
01683     // right arguments to make sure it doesn't get no-op'ed.
01684     llvm::LoadInst *oldValue = CGF.Builder.CreateLoad(srcField);
01685     oldValue->setAlignment(Alignment.getQuantity());
01686 
01687     llvm::Value *copy = CGF.EmitARCRetainBlock(oldValue, /*mandatory*/ true);
01688 
01689     llvm::StoreInst *store = CGF.Builder.CreateStore(copy, destField);
01690     store->setAlignment(Alignment.getQuantity());
01691   }
01692 
01693   void emitDispose(CodeGenFunction &CGF, llvm::Value *field) override {
01694     CGF.EmitARCDestroyStrong(field, ARCImpreciseLifetime);
01695   }
01696 
01697   void profileImpl(llvm::FoldingSetNodeID &id) const override {
01698     // 2 is distinguishable from all pointers and byref flags
01699     id.AddInteger(2);
01700   }
01701 };
01702 
01703 /// Emits the copy/dispose helpers for a __block variable with a
01704 /// nontrivial copy constructor or destructor.
01705 class CXXByrefHelpers : public CodeGenModule::ByrefHelpers {
01706   QualType VarType;
01707   const Expr *CopyExpr;
01708 
01709 public:
01710   CXXByrefHelpers(CharUnits alignment, QualType type,
01711                   const Expr *copyExpr)
01712     : ByrefHelpers(alignment), VarType(type), CopyExpr(copyExpr) {}
01713 
01714   bool needsCopy() const override { return CopyExpr != nullptr; }
01715   void emitCopy(CodeGenFunction &CGF, llvm::Value *destField,
01716                 llvm::Value *srcField) override {
01717     if (!CopyExpr) return;
01718     CGF.EmitSynthesizedCXXCopyCtor(destField, srcField, CopyExpr);
01719   }
01720 
01721   void emitDispose(CodeGenFunction &CGF, llvm::Value *field) override {
01722     EHScopeStack::stable_iterator cleanupDepth = CGF.EHStack.stable_begin();
01723     CGF.PushDestructorCleanup(VarType, field);
01724     CGF.PopCleanupBlocks(cleanupDepth);
01725   }
01726 
01727   void profileImpl(llvm::FoldingSetNodeID &id) const override {
01728     id.AddPointer(VarType.getCanonicalType().getAsOpaquePtr());
01729   }
01730 };
01731 } // end anonymous namespace
01732 
01733 static llvm::Constant *
01734 generateByrefCopyHelper(CodeGenFunction &CGF,
01735                         llvm::StructType &byrefType,
01736                         unsigned valueFieldIndex,
01737                         CodeGenModule::ByrefHelpers &byrefInfo) {
01738   ASTContext &Context = CGF.getContext();
01739 
01740   QualType R = Context.VoidTy;
01741 
01742   FunctionArgList args;
01743   ImplicitParamDecl dst(CGF.getContext(), nullptr, SourceLocation(), nullptr,
01744                         Context.VoidPtrTy);
01745   args.push_back(&dst);
01746 
01747   ImplicitParamDecl src(CGF.getContext(), nullptr, SourceLocation(), nullptr,
01748                         Context.VoidPtrTy);
01749   args.push_back(&src);
01750 
01751   const CGFunctionInfo &FI = CGF.CGM.getTypes().arrangeFreeFunctionDeclaration(
01752       R, args, FunctionType::ExtInfo(), /*variadic=*/false);
01753 
01754   CodeGenTypes &Types = CGF.CGM.getTypes();
01755   llvm::FunctionType *LTy = Types.GetFunctionType(FI);
01756 
01757   // FIXME: We'd like to put these into a mergable by content, with
01758   // internal linkage.
01759   llvm::Function *Fn =
01760     llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
01761                            "__Block_byref_object_copy_", &CGF.CGM.getModule());
01762 
01763   IdentifierInfo *II
01764     = &Context.Idents.get("__Block_byref_object_copy_");
01765 
01766   FunctionDecl *FD = FunctionDecl::Create(Context,
01767                                           Context.getTranslationUnitDecl(),
01768                                           SourceLocation(),
01769                                           SourceLocation(), II, R, nullptr,
01770                                           SC_Static,
01771                                           false, false);
01772 
01773   CGF.StartFunction(FD, R, Fn, FI, args);
01774 
01775   if (byrefInfo.needsCopy()) {
01776     llvm::Type *byrefPtrType = byrefType.getPointerTo(0);
01777 
01778     // dst->x
01779     llvm::Value *destField = CGF.GetAddrOfLocalVar(&dst);
01780     destField = CGF.Builder.CreateLoad(destField);
01781     destField = CGF.Builder.CreateBitCast(destField, byrefPtrType);
01782     destField = CGF.Builder.CreateStructGEP(destField, valueFieldIndex, "x");
01783 
01784     // src->x
01785     llvm::Value *srcField = CGF.GetAddrOfLocalVar(&src);
01786     srcField = CGF.Builder.CreateLoad(srcField);
01787     srcField = CGF.Builder.CreateBitCast(srcField, byrefPtrType);
01788     srcField = CGF.Builder.CreateStructGEP(srcField, valueFieldIndex, "x");
01789 
01790     byrefInfo.emitCopy(CGF, destField, srcField);
01791   }  
01792 
01793   CGF.FinishFunction();
01794 
01795   return llvm::ConstantExpr::getBitCast(Fn, CGF.Int8PtrTy);
01796 }
01797 
01798 /// Build the copy helper for a __block variable.
01799 static llvm::Constant *buildByrefCopyHelper(CodeGenModule &CGM,
01800                                             llvm::StructType &byrefType,
01801                                             unsigned byrefValueIndex,
01802                                             CodeGenModule::ByrefHelpers &info) {
01803   CodeGenFunction CGF(CGM);
01804   return generateByrefCopyHelper(CGF, byrefType, byrefValueIndex, info);
01805 }
01806 
01807 /// Generate code for a __block variable's dispose helper.
01808 static llvm::Constant *
01809 generateByrefDisposeHelper(CodeGenFunction &CGF,
01810                            llvm::StructType &byrefType,
01811                            unsigned byrefValueIndex,
01812                            CodeGenModule::ByrefHelpers &byrefInfo) {
01813   ASTContext &Context = CGF.getContext();
01814   QualType R = Context.VoidTy;
01815 
01816   FunctionArgList args;
01817   ImplicitParamDecl src(CGF.getContext(), nullptr, SourceLocation(), nullptr,
01818                         Context.VoidPtrTy);
01819   args.push_back(&src);
01820 
01821   const CGFunctionInfo &FI = CGF.CGM.getTypes().arrangeFreeFunctionDeclaration(
01822       R, args, FunctionType::ExtInfo(), /*variadic=*/false);
01823 
01824   CodeGenTypes &Types = CGF.CGM.getTypes();
01825   llvm::FunctionType *LTy = Types.GetFunctionType(FI);
01826 
01827   // FIXME: We'd like to put these into a mergable by content, with
01828   // internal linkage.
01829   llvm::Function *Fn =
01830     llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
01831                            "__Block_byref_object_dispose_",
01832                            &CGF.CGM.getModule());
01833 
01834   IdentifierInfo *II
01835     = &Context.Idents.get("__Block_byref_object_dispose_");
01836 
01837   FunctionDecl *FD = FunctionDecl::Create(Context,
01838                                           Context.getTranslationUnitDecl(),
01839                                           SourceLocation(),
01840                                           SourceLocation(), II, R, nullptr,
01841                                           SC_Static,
01842                                           false, false);
01843   CGF.StartFunction(FD, R, Fn, FI, args);
01844 
01845   if (byrefInfo.needsDispose()) {
01846     llvm::Value *V = CGF.GetAddrOfLocalVar(&src);
01847     V = CGF.Builder.CreateLoad(V);
01848     V = CGF.Builder.CreateBitCast(V, byrefType.getPointerTo(0));
01849     V = CGF.Builder.CreateStructGEP(V, byrefValueIndex, "x");
01850 
01851     byrefInfo.emitDispose(CGF, V);
01852   }
01853 
01854   CGF.FinishFunction();
01855 
01856   return llvm::ConstantExpr::getBitCast(Fn, CGF.Int8PtrTy);
01857 }
01858 
01859 /// Build the dispose helper for a __block variable.
01860 static llvm::Constant *buildByrefDisposeHelper(CodeGenModule &CGM,
01861                                               llvm::StructType &byrefType,
01862                                                unsigned byrefValueIndex,
01863                                             CodeGenModule::ByrefHelpers &info) {
01864   CodeGenFunction CGF(CGM);
01865   return generateByrefDisposeHelper(CGF, byrefType, byrefValueIndex, info);
01866 }
01867 
01868 /// Lazily build the copy and dispose helpers for a __block variable
01869 /// with the given information.
01870 template <class T> static T *buildByrefHelpers(CodeGenModule &CGM,
01871                                                llvm::StructType &byrefTy,
01872                                                unsigned byrefValueIndex,
01873                                                T &byrefInfo) {
01874   // Increase the field's alignment to be at least pointer alignment,
01875   // since the layout of the byref struct will guarantee at least that.
01876   byrefInfo.Alignment = std::max(byrefInfo.Alignment,
01877                               CharUnits::fromQuantity(CGM.PointerAlignInBytes));
01878 
01879   llvm::FoldingSetNodeID id;
01880   byrefInfo.Profile(id);
01881 
01882   void *insertPos;
01883   CodeGenModule::ByrefHelpers *node
01884     = CGM.ByrefHelpersCache.FindNodeOrInsertPos(id, insertPos);
01885   if (node) return static_cast<T*>(node);
01886 
01887   byrefInfo.CopyHelper =
01888     buildByrefCopyHelper(CGM, byrefTy, byrefValueIndex, byrefInfo);
01889   byrefInfo.DisposeHelper =
01890     buildByrefDisposeHelper(CGM, byrefTy, byrefValueIndex,byrefInfo);
01891 
01892   T *copy = new (CGM.getContext()) T(byrefInfo);
01893   CGM.ByrefHelpersCache.InsertNode(copy, insertPos);
01894   return copy;
01895 }
01896 
01897 /// Build the copy and dispose helpers for the given __block variable
01898 /// emission.  Places the helpers in the global cache.  Returns null
01899 /// if no helpers are required.
01900 CodeGenModule::ByrefHelpers *
01901 CodeGenFunction::buildByrefHelpers(llvm::StructType &byrefType,
01902                                    const AutoVarEmission &emission) {
01903   const VarDecl &var = *emission.Variable;
01904   QualType type = var.getType();
01905 
01906   unsigned byrefValueIndex = getByRefValueLLVMField(&var);
01907 
01908   if (const CXXRecordDecl *record = type->getAsCXXRecordDecl()) {
01909     const Expr *copyExpr = CGM.getContext().getBlockVarCopyInits(&var);
01910     if (!copyExpr && record->hasTrivialDestructor()) return nullptr;
01911 
01912     CXXByrefHelpers byrefInfo(emission.Alignment, type, copyExpr);
01913     return ::buildByrefHelpers(CGM, byrefType, byrefValueIndex, byrefInfo);
01914   }
01915 
01916   // Otherwise, if we don't have a retainable type, there's nothing to do.
01917   // that the runtime does extra copies.
01918   if (!type->isObjCRetainableType()) return nullptr;
01919 
01920   Qualifiers qs = type.getQualifiers();
01921 
01922   // If we have lifetime, that dominates.
01923   if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) {
01924     assert(getLangOpts().ObjCAutoRefCount);
01925 
01926     switch (lifetime) {
01927     case Qualifiers::OCL_None: llvm_unreachable("impossible");
01928 
01929     // These are just bits as far as the runtime is concerned.
01930     case Qualifiers::OCL_ExplicitNone:
01931     case Qualifiers::OCL_Autoreleasing:
01932       return nullptr;
01933 
01934     // Tell the runtime that this is ARC __weak, called by the
01935     // byref routines.
01936     case Qualifiers::OCL_Weak: {
01937       ARCWeakByrefHelpers byrefInfo(emission.Alignment);
01938       return ::buildByrefHelpers(CGM, byrefType, byrefValueIndex, byrefInfo);
01939     }
01940 
01941     // ARC __strong __block variables need to be retained.
01942     case Qualifiers::OCL_Strong:
01943       // Block pointers need to be copied, and there's no direct
01944       // transfer possible.
01945       if (type->isBlockPointerType()) {
01946         ARCStrongBlockByrefHelpers byrefInfo(emission.Alignment);
01947         return ::buildByrefHelpers(CGM, byrefType, byrefValueIndex, byrefInfo);
01948 
01949       // Otherwise, we transfer ownership of the retain from the stack
01950       // to the heap.
01951       } else {
01952         ARCStrongByrefHelpers byrefInfo(emission.Alignment);
01953         return ::buildByrefHelpers(CGM, byrefType, byrefValueIndex, byrefInfo);
01954       }
01955     }
01956     llvm_unreachable("fell out of lifetime switch!");
01957   }
01958 
01959   BlockFieldFlags flags;
01960   if (type->isBlockPointerType()) {
01961     flags |= BLOCK_FIELD_IS_BLOCK;
01962   } else if (CGM.getContext().isObjCNSObjectType(type) || 
01963              type->isObjCObjectPointerType()) {
01964     flags |= BLOCK_FIELD_IS_OBJECT;
01965   } else {
01966     return nullptr;
01967   }
01968 
01969   if (type.isObjCGCWeak())
01970     flags |= BLOCK_FIELD_IS_WEAK;
01971 
01972   ObjectByrefHelpers byrefInfo(emission.Alignment, flags);
01973   return ::buildByrefHelpers(CGM, byrefType, byrefValueIndex, byrefInfo);
01974 }
01975 
01976 unsigned CodeGenFunction::getByRefValueLLVMField(const ValueDecl *VD) const {
01977   assert(ByRefValueInfo.count(VD) && "Did not find value!");
01978   
01979   return ByRefValueInfo.find(VD)->second.second;
01980 }
01981 
01982 llvm::Value *CodeGenFunction::BuildBlockByrefAddress(llvm::Value *BaseAddr,
01983                                                      const VarDecl *V) {
01984   llvm::Value *Loc = Builder.CreateStructGEP(BaseAddr, 1, "forwarding");
01985   Loc = Builder.CreateLoad(Loc);
01986   Loc = Builder.CreateStructGEP(Loc, getByRefValueLLVMField(V),
01987                                 V->getNameAsString());
01988   return Loc;
01989 }
01990 
01991 /// BuildByRefType - This routine changes a __block variable declared as T x
01992 ///   into:
01993 ///
01994 ///      struct {
01995 ///        void *__isa;
01996 ///        void *__forwarding;
01997 ///        int32_t __flags;
01998 ///        int32_t __size;
01999 ///        void *__copy_helper;       // only if needed
02000 ///        void *__destroy_helper;    // only if needed
02001 ///        void *__byref_variable_layout;// only if needed
02002 ///        char padding[X];           // only if needed
02003 ///        T x;
02004 ///      } x
02005 ///
02006 llvm::Type *CodeGenFunction::BuildByRefType(const VarDecl *D) {
02007   std::pair<llvm::Type *, unsigned> &Info = ByRefValueInfo[D];
02008   if (Info.first)
02009     return Info.first;
02010   
02011   QualType Ty = D->getType();
02012 
02013   SmallVector<llvm::Type *, 8> types;
02014   
02015   llvm::StructType *ByRefType =
02016     llvm::StructType::create(getLLVMContext(),
02017                              "struct.__block_byref_" + D->getNameAsString());
02018   
02019   // void *__isa;
02020   types.push_back(Int8PtrTy);
02021   
02022   // void *__forwarding;
02023   types.push_back(llvm::PointerType::getUnqual(ByRefType));
02024   
02025   // int32_t __flags;
02026   types.push_back(Int32Ty);
02027     
02028   // int32_t __size;
02029   types.push_back(Int32Ty);
02030   // Note that this must match *exactly* the logic in buildByrefHelpers.
02031   bool HasCopyAndDispose = getContext().BlockRequiresCopying(Ty, D);
02032   if (HasCopyAndDispose) {
02033     /// void *__copy_helper;
02034     types.push_back(Int8PtrTy);
02035     
02036     /// void *__destroy_helper;
02037     types.push_back(Int8PtrTy);
02038   }
02039   bool HasByrefExtendedLayout = false;
02040   Qualifiers::ObjCLifetime Lifetime;
02041   if (getContext().getByrefLifetime(Ty, Lifetime, HasByrefExtendedLayout) &&
02042       HasByrefExtendedLayout)
02043     /// void *__byref_variable_layout;
02044     types.push_back(Int8PtrTy);
02045 
02046   bool Packed = false;
02047   CharUnits Align = getContext().getDeclAlign(D);
02048   if (Align >
02049       getContext().toCharUnitsFromBits(getTarget().getPointerAlign(0))) {
02050     // We have to insert padding.
02051     
02052     // The struct above has 2 32-bit integers.
02053     unsigned CurrentOffsetInBytes = 4 * 2;
02054     
02055     // And either 2, 3, 4 or 5 pointers.
02056     unsigned noPointers = 2;
02057     if (HasCopyAndDispose)
02058       noPointers += 2;
02059     if (HasByrefExtendedLayout)
02060       noPointers += 1;
02061     
02062     CurrentOffsetInBytes += noPointers * CGM.getDataLayout().getTypeAllocSize(Int8PtrTy);
02063     
02064     // Align the offset.
02065     unsigned AlignedOffsetInBytes = 
02066       llvm::RoundUpToAlignment(CurrentOffsetInBytes, Align.getQuantity());
02067     
02068     unsigned NumPaddingBytes = AlignedOffsetInBytes - CurrentOffsetInBytes;
02069     if (NumPaddingBytes > 0) {
02070       llvm::Type *Ty = Int8Ty;
02071       // FIXME: We need a sema error for alignment larger than the minimum of
02072       // the maximal stack alignment and the alignment of malloc on the system.
02073       if (NumPaddingBytes > 1)
02074         Ty = llvm::ArrayType::get(Ty, NumPaddingBytes);
02075     
02076       types.push_back(Ty);
02077 
02078       // We want a packed struct.
02079       Packed = true;
02080     }
02081   }
02082 
02083   // T x;
02084   types.push_back(ConvertTypeForMem(Ty));
02085   
02086   ByRefType->setBody(types, Packed);
02087   
02088   Info.first = ByRefType;
02089   
02090   Info.second = types.size() - 1;
02091   
02092   return Info.first;
02093 }
02094 
02095 /// Initialize the structural components of a __block variable, i.e.
02096 /// everything but the actual object.
02097 void CodeGenFunction::emitByrefStructureInit(const AutoVarEmission &emission) {
02098   // Find the address of the local.
02099   llvm::Value *addr = emission.Address;
02100 
02101   // That's an alloca of the byref structure type.
02102   llvm::StructType *byrefType = cast<llvm::StructType>(
02103                  cast<llvm::PointerType>(addr->getType())->getElementType());
02104 
02105   // Build the byref helpers if necessary.  This is null if we don't need any.
02106   CodeGenModule::ByrefHelpers *helpers =
02107     buildByrefHelpers(*byrefType, emission);
02108 
02109   const VarDecl &D = *emission.Variable;
02110   QualType type = D.getType();
02111 
02112   bool HasByrefExtendedLayout;
02113   Qualifiers::ObjCLifetime ByrefLifetime;
02114   bool ByRefHasLifetime =
02115     getContext().getByrefLifetime(type, ByrefLifetime, HasByrefExtendedLayout);
02116   
02117   llvm::Value *V;
02118 
02119   // Initialize the 'isa', which is just 0 or 1.
02120   int isa = 0;
02121   if (type.isObjCGCWeak())
02122     isa = 1;
02123   V = Builder.CreateIntToPtr(Builder.getInt32(isa), Int8PtrTy, "isa");
02124   Builder.CreateStore(V, Builder.CreateStructGEP(addr, 0, "byref.isa"));
02125 
02126   // Store the address of the variable into its own forwarding pointer.
02127   Builder.CreateStore(addr,
02128                       Builder.CreateStructGEP(addr, 1, "byref.forwarding"));
02129 
02130   // Blocks ABI:
02131   //   c) the flags field is set to either 0 if no helper functions are
02132   //      needed or BLOCK_BYREF_HAS_COPY_DISPOSE if they are,
02133   BlockFlags flags;
02134   if (helpers) flags |= BLOCK_BYREF_HAS_COPY_DISPOSE;
02135   if (ByRefHasLifetime) {
02136     if (HasByrefExtendedLayout) flags |= BLOCK_BYREF_LAYOUT_EXTENDED;
02137       else switch (ByrefLifetime) {
02138         case Qualifiers::OCL_Strong:
02139           flags |= BLOCK_BYREF_LAYOUT_STRONG;
02140           break;
02141         case Qualifiers::OCL_Weak:
02142           flags |= BLOCK_BYREF_LAYOUT_WEAK;
02143           break;
02144         case Qualifiers::OCL_ExplicitNone:
02145           flags |= BLOCK_BYREF_LAYOUT_UNRETAINED;
02146           break;
02147         case Qualifiers::OCL_None:
02148           if (!type->isObjCObjectPointerType() && !type->isBlockPointerType())
02149             flags |= BLOCK_BYREF_LAYOUT_NON_OBJECT;
02150           break;
02151         default:
02152           break;
02153       }
02154     if (CGM.getLangOpts().ObjCGCBitmapPrint) {
02155       printf("\n Inline flag for BYREF variable layout (%d):", flags.getBitMask());
02156       if (flags & BLOCK_BYREF_HAS_COPY_DISPOSE)
02157         printf(" BLOCK_BYREF_HAS_COPY_DISPOSE");
02158       if (flags & BLOCK_BYREF_LAYOUT_MASK) {
02159         BlockFlags ThisFlag(flags.getBitMask() & BLOCK_BYREF_LAYOUT_MASK);
02160         if (ThisFlag ==  BLOCK_BYREF_LAYOUT_EXTENDED)
02161           printf(" BLOCK_BYREF_LAYOUT_EXTENDED");
02162         if (ThisFlag ==  BLOCK_BYREF_LAYOUT_STRONG)
02163           printf(" BLOCK_BYREF_LAYOUT_STRONG");
02164         if (ThisFlag == BLOCK_BYREF_LAYOUT_WEAK)
02165           printf(" BLOCK_BYREF_LAYOUT_WEAK");
02166         if (ThisFlag == BLOCK_BYREF_LAYOUT_UNRETAINED)
02167           printf(" BLOCK_BYREF_LAYOUT_UNRETAINED");
02168         if (ThisFlag == BLOCK_BYREF_LAYOUT_NON_OBJECT)
02169           printf(" BLOCK_BYREF_LAYOUT_NON_OBJECT");
02170       }
02171       printf("\n");
02172     }
02173   }
02174   
02175   Builder.CreateStore(llvm::ConstantInt::get(IntTy, flags.getBitMask()),
02176                       Builder.CreateStructGEP(addr, 2, "byref.flags"));
02177 
02178   CharUnits byrefSize = CGM.GetTargetTypeStoreSize(byrefType);
02179   V = llvm::ConstantInt::get(IntTy, byrefSize.getQuantity());
02180   Builder.CreateStore(V, Builder.CreateStructGEP(addr, 3, "byref.size"));
02181 
02182   if (helpers) {
02183     llvm::Value *copy_helper = Builder.CreateStructGEP(addr, 4);
02184     Builder.CreateStore(helpers->CopyHelper, copy_helper);
02185 
02186     llvm::Value *destroy_helper = Builder.CreateStructGEP(addr, 5);
02187     Builder.CreateStore(helpers->DisposeHelper, destroy_helper);
02188   }
02189   if (ByRefHasLifetime && HasByrefExtendedLayout) {
02190     llvm::Constant* ByrefLayoutInfo = CGM.getObjCRuntime().BuildByrefLayout(CGM, type);
02191     llvm::Value *ByrefInfoAddr = Builder.CreateStructGEP(addr, helpers ? 6 : 4,
02192                                                          "byref.layout");
02193     // cast destination to pointer to source type.
02194     llvm::Type *DesTy = ByrefLayoutInfo->getType();
02195     DesTy = DesTy->getPointerTo();
02196     llvm::Value *BC = Builder.CreatePointerCast(ByrefInfoAddr, DesTy);
02197     Builder.CreateStore(ByrefLayoutInfo, BC);
02198   }
02199 }
02200 
02201 void CodeGenFunction::BuildBlockRelease(llvm::Value *V, BlockFieldFlags flags) {
02202   llvm::Value *F = CGM.getBlockObjectDispose();
02203   llvm::Value *args[] = {
02204     Builder.CreateBitCast(V, Int8PtrTy),
02205     llvm::ConstantInt::get(Int32Ty, flags.getBitMask())
02206   };
02207   EmitNounwindRuntimeCall(F, args); // FIXME: throwing destructors?
02208 }
02209 
02210 namespace {
02211   struct CallBlockRelease : EHScopeStack::Cleanup {
02212     llvm::Value *Addr;
02213     CallBlockRelease(llvm::Value *Addr) : Addr(Addr) {}
02214 
02215     void Emit(CodeGenFunction &CGF, Flags flags) override {
02216       // Should we be passing FIELD_IS_WEAK here?
02217       CGF.BuildBlockRelease(Addr, BLOCK_FIELD_IS_BYREF);
02218     }
02219   };
02220 }
02221 
02222 /// Enter a cleanup to destroy a __block variable.  Note that this
02223 /// cleanup should be a no-op if the variable hasn't left the stack
02224 /// yet; if a cleanup is required for the variable itself, that needs
02225 /// to be done externally.
02226 void CodeGenFunction::enterByrefCleanup(const AutoVarEmission &emission) {
02227   // We don't enter this cleanup if we're in pure-GC mode.
02228   if (CGM.getLangOpts().getGC() == LangOptions::GCOnly)
02229     return;
02230 
02231   EHStack.pushCleanup<CallBlockRelease>(NormalAndEHCleanup, emission.Address);
02232 }
02233 
02234 /// Adjust the declaration of something from the blocks API.
02235 static void configureBlocksRuntimeObject(CodeGenModule &CGM,
02236                                          llvm::Constant *C) {
02237   if (!CGM.getLangOpts().BlocksRuntimeOptional) return;
02238 
02239   auto *GV = cast<llvm::GlobalValue>(C->stripPointerCasts());
02240   if (GV->isDeclaration() && GV->hasExternalLinkage())
02241     GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
02242 }
02243 
02244 llvm::Constant *CodeGenModule::getBlockObjectDispose() {
02245   if (BlockObjectDispose)
02246     return BlockObjectDispose;
02247 
02248   llvm::Type *args[] = { Int8PtrTy, Int32Ty };
02249   llvm::FunctionType *fty
02250     = llvm::FunctionType::get(VoidTy, args, false);
02251   BlockObjectDispose = CreateRuntimeFunction(fty, "_Block_object_dispose");
02252   configureBlocksRuntimeObject(*this, BlockObjectDispose);
02253   return BlockObjectDispose;
02254 }
02255 
02256 llvm::Constant *CodeGenModule::getBlockObjectAssign() {
02257   if (BlockObjectAssign)
02258     return BlockObjectAssign;
02259 
02260   llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, Int32Ty };
02261   llvm::FunctionType *fty
02262     = llvm::FunctionType::get(VoidTy, args, false);
02263   BlockObjectAssign = CreateRuntimeFunction(fty, "_Block_object_assign");
02264   configureBlocksRuntimeObject(*this, BlockObjectAssign);
02265   return BlockObjectAssign;
02266 }
02267 
02268 llvm::Constant *CodeGenModule::getNSConcreteGlobalBlock() {
02269   if (NSConcreteGlobalBlock)
02270     return NSConcreteGlobalBlock;
02271 
02272   NSConcreteGlobalBlock = GetOrCreateLLVMGlobal("_NSConcreteGlobalBlock",
02273                                                 Int8PtrTy->getPointerTo(),
02274                                                 nullptr);
02275   configureBlocksRuntimeObject(*this, NSConcreteGlobalBlock);
02276   return NSConcreteGlobalBlock;
02277 }
02278 
02279 llvm::Constant *CodeGenModule::getNSConcreteStackBlock() {
02280   if (NSConcreteStackBlock)
02281     return NSConcreteStackBlock;
02282 
02283   NSConcreteStackBlock = GetOrCreateLLVMGlobal("_NSConcreteStackBlock",
02284                                                Int8PtrTy->getPointerTo(),
02285                                                nullptr);
02286   configureBlocksRuntimeObject(*this, NSConcreteStackBlock);
02287   return NSConcreteStackBlock;  
02288 }