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