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