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