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