xref: /freebsd/contrib/llvm-project/clang/lib/CodeGen/CGBlocks.cpp (revision 770cf0a5f02dc8983a89c6568d741fbc25baa999)
1 //===--- CGBlocks.cpp - Emit LLVM Code for declarations ---------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This contains code to emit blocks.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "CGBlocks.h"
14 #include "CGCXXABI.h"
15 #include "CGDebugInfo.h"
16 #include "CGObjCRuntime.h"
17 #include "CGOpenCLRuntime.h"
18 #include "CodeGenFunction.h"
19 #include "CodeGenModule.h"
20 #include "CodeGenPGO.h"
21 #include "ConstantEmitter.h"
22 #include "TargetInfo.h"
23 #include "clang/AST/Attr.h"
24 #include "clang/AST/DeclObjC.h"
25 #include "clang/CodeGen/ConstantInitBuilder.h"
26 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/Support/ScopedPrinter.h"
29 #include <algorithm>
30 #include <cstdio>
31 
32 using namespace clang;
33 using namespace CodeGen;
34 
35 CGBlockInfo::CGBlockInfo(const BlockDecl *block, StringRef name)
36     : Name(name), CXXThisIndex(0), CanBeGlobal(false), NeedsCopyDispose(false),
37       NoEscape(false), HasCXXObject(false), UsesStret(false),
38       HasCapturedVariableLayout(false), CapturesNonExternalType(false),
39       LocalAddress(RawAddress::invalid()), StructureType(nullptr),
40       Block(block) {
41 
42   // Skip asm prefix, if any.  'name' is usually taken directly from
43   // the mangled name of the enclosing function.
44   name.consume_front("\01");
45 }
46 
47 // Anchor the vtable to this translation unit.
48 BlockByrefHelpers::~BlockByrefHelpers() {}
49 
50 /// Build the given block as a global block.
51 static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM,
52                                         const CGBlockInfo &blockInfo,
53                                         llvm::Constant *blockFn);
54 
55 /// Build the helper function to copy a block.
56 static llvm::Constant *buildCopyHelper(CodeGenModule &CGM,
57                                        const CGBlockInfo &blockInfo) {
58   return CodeGenFunction(CGM).GenerateCopyHelperFunction(blockInfo);
59 }
60 
61 /// Build the helper function to dispose of a block.
62 static llvm::Constant *buildDisposeHelper(CodeGenModule &CGM,
63                                           const CGBlockInfo &blockInfo) {
64   return CodeGenFunction(CGM).GenerateDestroyHelperFunction(blockInfo);
65 }
66 
67 namespace {
68 
69 enum class CaptureStrKind {
70   // String for the copy helper.
71   CopyHelper,
72   // String for the dispose helper.
73   DisposeHelper,
74   // Merge the strings for the copy helper and dispose helper.
75   Merged
76 };
77 
78 } // end anonymous namespace
79 
80 static std::string getBlockCaptureStr(const CGBlockInfo::Capture &Cap,
81                                       CaptureStrKind StrKind,
82                                       CharUnits BlockAlignment,
83                                       CodeGenModule &CGM);
84 
85 static std::string getBlockDescriptorName(const CGBlockInfo &BlockInfo,
86                                           CodeGenModule &CGM) {
87   std::string Name = "__block_descriptor_";
88   Name += llvm::to_string(BlockInfo.BlockSize.getQuantity()) + "_";
89 
90   if (BlockInfo.NeedsCopyDispose) {
91     if (CGM.getLangOpts().Exceptions)
92       Name += "e";
93     if (CGM.getCodeGenOpts().ObjCAutoRefCountExceptions)
94       Name += "a";
95     Name += llvm::to_string(BlockInfo.BlockAlign.getQuantity()) + "_";
96 
97     for (auto &Cap : BlockInfo.SortedCaptures) {
98       if (Cap.isConstantOrTrivial())
99         continue;
100 
101       Name += llvm::to_string(Cap.getOffset().getQuantity());
102 
103       if (Cap.CopyKind == Cap.DisposeKind) {
104         // If CopyKind and DisposeKind are the same, merge the capture
105         // information.
106         assert(Cap.CopyKind != BlockCaptureEntityKind::None &&
107                "shouldn't see BlockCaptureManagedEntity that is None");
108         Name += getBlockCaptureStr(Cap, CaptureStrKind::Merged,
109                                    BlockInfo.BlockAlign, CGM);
110       } else {
111         // If CopyKind and DisposeKind are not the same, which can happen when
112         // either Kind is None or the captured object is a __strong block,
113         // concatenate the copy and dispose strings.
114         Name += getBlockCaptureStr(Cap, CaptureStrKind::CopyHelper,
115                                    BlockInfo.BlockAlign, CGM);
116         Name += getBlockCaptureStr(Cap, CaptureStrKind::DisposeHelper,
117                                    BlockInfo.BlockAlign, CGM);
118       }
119     }
120     Name += "_";
121   }
122 
123   std::string TypeAtEncoding;
124 
125   if (!CGM.getCodeGenOpts().DisableBlockSignatureString) {
126     TypeAtEncoding =
127         CGM.getContext().getObjCEncodingForBlock(BlockInfo.getBlockExpr());
128     /// Replace occurrences of '@' with '\1'. '@' is reserved on ELF platforms
129     /// as a separator between symbol name and symbol version.
130     llvm::replace(TypeAtEncoding, '@', '\1');
131   }
132   Name += "e" + llvm::to_string(TypeAtEncoding.size()) + "_" + TypeAtEncoding;
133   Name += "l" + CGM.getObjCRuntime().getRCBlockLayoutStr(CGM, BlockInfo);
134   return Name;
135 }
136 
137 /// buildBlockDescriptor - Build the block descriptor meta-data for a block.
138 /// buildBlockDescriptor is accessed from 5th field of the Block_literal
139 /// meta-data and contains stationary information about the block literal.
140 /// Its definition will have 4 (or optionally 6) words.
141 /// \code
142 /// struct Block_descriptor {
143 ///   unsigned long reserved;
144 ///   unsigned long size;  // size of Block_literal metadata in bytes.
145 ///   void *copy_func_helper_decl;  // optional copy helper.
146 ///   void *destroy_func_decl; // optional destructor helper.
147 ///   void *block_method_encoding_address; // @encode for block literal signature.
148 ///   void *block_layout_info; // encoding of captured block variables.
149 /// };
150 /// \endcode
151 static llvm::Constant *buildBlockDescriptor(CodeGenModule &CGM,
152                                             const CGBlockInfo &blockInfo) {
153   ASTContext &C = CGM.getContext();
154 
155   llvm::IntegerType *ulong =
156     cast<llvm::IntegerType>(CGM.getTypes().ConvertType(C.UnsignedLongTy));
157   llvm::PointerType *i8p = nullptr;
158   if (CGM.getLangOpts().OpenCL)
159     i8p = llvm::PointerType::get(
160         CGM.getLLVMContext(), C.getTargetAddressSpace(LangAS::opencl_constant));
161   else
162     i8p = CGM.VoidPtrTy;
163 
164   std::string descName;
165 
166   // If an equivalent block descriptor global variable exists, return it.
167   if (C.getLangOpts().ObjC &&
168       CGM.getLangOpts().getGC() == LangOptions::NonGC) {
169     descName = getBlockDescriptorName(blockInfo, CGM);
170     if (llvm::GlobalValue *desc = CGM.getModule().getNamedValue(descName))
171       return desc;
172   }
173 
174   // If there isn't an equivalent block descriptor global variable, create a new
175   // one.
176   ConstantInitBuilder builder(CGM);
177   auto elements = builder.beginStruct();
178 
179   // reserved
180   elements.addInt(ulong, 0);
181 
182   // Size
183   // FIXME: What is the right way to say this doesn't fit?  We should give
184   // a user diagnostic in that case.  Better fix would be to change the
185   // API to size_t.
186   elements.addInt(ulong, blockInfo.BlockSize.getQuantity());
187 
188   // Optional copy/dispose helpers.
189   bool hasInternalHelper = false;
190   if (blockInfo.NeedsCopyDispose) {
191     // copy_func_helper_decl
192     llvm::Constant *copyHelper = buildCopyHelper(CGM, blockInfo);
193     elements.add(copyHelper);
194 
195     // destroy_func_decl
196     llvm::Constant *disposeHelper = buildDisposeHelper(CGM, blockInfo);
197     elements.add(disposeHelper);
198 
199     if (cast<llvm::Function>(copyHelper->stripPointerCasts())
200             ->hasInternalLinkage() ||
201         cast<llvm::Function>(disposeHelper->stripPointerCasts())
202             ->hasInternalLinkage())
203       hasInternalHelper = true;
204   }
205 
206   // Signature.  Mandatory ObjC-style method descriptor @encode sequence.
207   if (CGM.getCodeGenOpts().DisableBlockSignatureString) {
208     elements.addNullPointer(i8p);
209   } else {
210     std::string typeAtEncoding =
211         CGM.getContext().getObjCEncodingForBlock(blockInfo.getBlockExpr());
212     elements.add(CGM.GetAddrOfConstantCString(typeAtEncoding).getPointer());
213   }
214 
215   // GC layout.
216   if (C.getLangOpts().ObjC) {
217     if (CGM.getLangOpts().getGC() != LangOptions::NonGC)
218       elements.add(CGM.getObjCRuntime().BuildGCBlockLayout(CGM, blockInfo));
219     else
220       elements.add(CGM.getObjCRuntime().BuildRCBlockLayout(CGM, blockInfo));
221   }
222   else
223     elements.addNullPointer(i8p);
224 
225   unsigned AddrSpace = 0;
226   if (C.getLangOpts().OpenCL)
227     AddrSpace = C.getTargetAddressSpace(LangAS::opencl_constant);
228 
229   llvm::GlobalValue::LinkageTypes linkage;
230   if (descName.empty()) {
231     linkage = llvm::GlobalValue::InternalLinkage;
232     descName = "__block_descriptor_tmp";
233   } else if (hasInternalHelper) {
234     // If either the copy helper or the dispose helper has internal linkage,
235     // the block descriptor must have internal linkage too.
236     linkage = llvm::GlobalValue::InternalLinkage;
237   } else {
238     linkage = llvm::GlobalValue::LinkOnceODRLinkage;
239   }
240 
241   llvm::GlobalVariable *global =
242       elements.finishAndCreateGlobal(descName, CGM.getPointerAlign(),
243                                      /*constant*/ true, linkage, AddrSpace);
244 
245   if (linkage == llvm::GlobalValue::LinkOnceODRLinkage) {
246     if (CGM.supportsCOMDAT())
247       global->setComdat(CGM.getModule().getOrInsertComdat(descName));
248     global->setVisibility(llvm::GlobalValue::HiddenVisibility);
249     global->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
250   }
251 
252   return global;
253 }
254 
255 /*
256   Purely notional variadic template describing the layout of a block.
257 
258   template <class _ResultType, class... _ParamTypes, class... _CaptureTypes>
259   struct Block_literal {
260     /// Initialized to one of:
261     ///   extern void *_NSConcreteStackBlock[];
262     ///   extern void *_NSConcreteGlobalBlock[];
263     ///
264     /// In theory, we could start one off malloc'ed by setting
265     /// BLOCK_NEEDS_FREE, giving it a refcount of 1, and using
266     /// this isa:
267     ///   extern void *_NSConcreteMallocBlock[];
268     struct objc_class *isa;
269 
270     /// These are the flags (with corresponding bit number) that the
271     /// compiler is actually supposed to know about.
272     ///  23. BLOCK_IS_NOESCAPE - indicates that the block is non-escaping
273     ///  25. BLOCK_HAS_COPY_DISPOSE - indicates that the block
274     ///   descriptor provides copy and dispose helper functions
275     ///  26. BLOCK_HAS_CXX_OBJ - indicates that there's a captured
276     ///   object with a nontrivial destructor or copy constructor
277     ///  28. BLOCK_IS_GLOBAL - indicates that the block is allocated
278     ///   as global memory
279     ///  29. BLOCK_USE_STRET - indicates that the block function
280     ///   uses stret, which objc_msgSend needs to know about
281     ///  30. BLOCK_HAS_SIGNATURE - indicates that the block has an
282     ///   @encoded signature string
283     /// And we're not supposed to manipulate these:
284     ///  24. BLOCK_NEEDS_FREE - indicates that the block has been moved
285     ///   to malloc'ed memory
286     ///  27. BLOCK_IS_GC - indicates that the block has been moved to
287     ///   to GC-allocated memory
288     /// Additionally, the bottom 16 bits are a reference count which
289     /// should be zero on the stack.
290     int flags;
291 
292     /// Reserved;  should be zero-initialized.
293     int reserved;
294 
295     /// Function pointer generated from block literal.
296     _ResultType (*invoke)(Block_literal *, _ParamTypes...);
297 
298     /// Block description metadata generated from block literal.
299     struct Block_descriptor *block_descriptor;
300 
301     /// Captured values follow.
302     _CapturesTypes captures...;
303   };
304  */
305 
306 namespace {
307   /// A chunk of data that we actually have to capture in the block.
308   struct BlockLayoutChunk {
309     CharUnits Alignment;
310     CharUnits Size;
311     const BlockDecl::Capture *Capture; // null for 'this'
312     llvm::Type *Type;
313     QualType FieldType;
314     BlockCaptureEntityKind CopyKind, DisposeKind;
315     BlockFieldFlags CopyFlags, DisposeFlags;
316 
317     BlockLayoutChunk(CharUnits align, CharUnits size,
318                      const BlockDecl::Capture *capture, llvm::Type *type,
319                      QualType fieldType, BlockCaptureEntityKind CopyKind,
320                      BlockFieldFlags CopyFlags,
321                      BlockCaptureEntityKind DisposeKind,
322                      BlockFieldFlags DisposeFlags)
323         : Alignment(align), Size(size), Capture(capture), Type(type),
324           FieldType(fieldType), CopyKind(CopyKind), DisposeKind(DisposeKind),
325           CopyFlags(CopyFlags), DisposeFlags(DisposeFlags) {}
326 
327     /// Tell the block info that this chunk has the given field index.
328     void setIndex(CGBlockInfo &info, unsigned index, CharUnits offset) {
329       if (!Capture) {
330         info.CXXThisIndex = index;
331         info.CXXThisOffset = offset;
332       } else {
333         info.SortedCaptures.push_back(CGBlockInfo::Capture::makeIndex(
334             index, offset, FieldType, CopyKind, CopyFlags, DisposeKind,
335             DisposeFlags, Capture));
336       }
337     }
338 
339     bool isTrivial() const {
340       return CopyKind == BlockCaptureEntityKind::None &&
341              DisposeKind == BlockCaptureEntityKind::None;
342     }
343   };
344 
345   /// Order by 1) all __strong together 2) next, all block together 3) next,
346   /// all byref together 4) next, all __weak together. Preserve descending
347   /// alignment in all situations.
348   bool operator<(const BlockLayoutChunk &left, const BlockLayoutChunk &right) {
349     if (left.Alignment != right.Alignment)
350       return left.Alignment > right.Alignment;
351 
352     auto getPrefOrder = [](const BlockLayoutChunk &chunk) {
353       switch (chunk.CopyKind) {
354       case BlockCaptureEntityKind::ARCStrong:
355         return 0;
356       case BlockCaptureEntityKind::BlockObject:
357         switch (chunk.CopyFlags.getBitMask()) {
358         case BLOCK_FIELD_IS_OBJECT:
359           return 0;
360         case BLOCK_FIELD_IS_BLOCK:
361           return 1;
362         case BLOCK_FIELD_IS_BYREF:
363           return 2;
364         default:
365           break;
366         }
367         break;
368       case BlockCaptureEntityKind::ARCWeak:
369         return 3;
370       default:
371         break;
372       }
373       return 4;
374     };
375 
376     return getPrefOrder(left) < getPrefOrder(right);
377   }
378 } // end anonymous namespace
379 
380 static std::pair<BlockCaptureEntityKind, BlockFieldFlags>
381 computeCopyInfoForBlockCapture(const BlockDecl::Capture &CI, QualType T,
382                                const LangOptions &LangOpts);
383 
384 static std::pair<BlockCaptureEntityKind, BlockFieldFlags>
385 computeDestroyInfoForBlockCapture(const BlockDecl::Capture &CI, QualType T,
386                                   const LangOptions &LangOpts);
387 
388 static void addBlockLayout(CharUnits align, CharUnits size,
389                            const BlockDecl::Capture *capture, llvm::Type *type,
390                            QualType fieldType,
391                            SmallVectorImpl<BlockLayoutChunk> &Layout,
392                            CGBlockInfo &Info, CodeGenModule &CGM) {
393   if (!capture) {
394     // 'this' capture.
395     Layout.push_back(BlockLayoutChunk(
396         align, size, capture, type, fieldType, BlockCaptureEntityKind::None,
397         BlockFieldFlags(), BlockCaptureEntityKind::None, BlockFieldFlags()));
398     return;
399   }
400 
401   const LangOptions &LangOpts = CGM.getLangOpts();
402   BlockCaptureEntityKind CopyKind, DisposeKind;
403   BlockFieldFlags CopyFlags, DisposeFlags;
404 
405   std::tie(CopyKind, CopyFlags) =
406       computeCopyInfoForBlockCapture(*capture, fieldType, LangOpts);
407   std::tie(DisposeKind, DisposeFlags) =
408       computeDestroyInfoForBlockCapture(*capture, fieldType, LangOpts);
409   Layout.push_back(BlockLayoutChunk(align, size, capture, type, fieldType,
410                                     CopyKind, CopyFlags, DisposeKind,
411                                     DisposeFlags));
412 
413   if (Info.NoEscape)
414     return;
415 
416   if (!Layout.back().isTrivial())
417     Info.NeedsCopyDispose = true;
418 }
419 
420 /// Determines if the given type is safe for constant capture in C++.
421 static bool isSafeForCXXConstantCapture(QualType type) {
422   const RecordType *recordType =
423     type->getBaseElementTypeUnsafe()->getAs<RecordType>();
424 
425   // Only records can be unsafe.
426   if (!recordType) return true;
427 
428   const auto *record = cast<CXXRecordDecl>(recordType->getDecl());
429 
430   // Maintain semantics for classes with non-trivial dtors or copy ctors.
431   if (!record->hasTrivialDestructor()) return false;
432   if (record->hasNonTrivialCopyConstructor()) return false;
433 
434   // Otherwise, we just have to make sure there aren't any mutable
435   // fields that might have changed since initialization.
436   return !record->hasMutableFields();
437 }
438 
439 /// It is illegal to modify a const object after initialization.
440 /// Therefore, if a const object has a constant initializer, we don't
441 /// actually need to keep storage for it in the block; we'll just
442 /// rematerialize it at the start of the block function.  This is
443 /// acceptable because we make no promises about address stability of
444 /// captured variables.
445 static llvm::Constant *tryCaptureAsConstant(CodeGenModule &CGM,
446                                             CodeGenFunction *CGF,
447                                             const VarDecl *var) {
448   // Return if this is a function parameter. We shouldn't try to
449   // rematerialize default arguments of function parameters.
450   if (isa<ParmVarDecl>(var))
451     return nullptr;
452 
453   QualType type = var->getType();
454 
455   // We can only do this if the variable is const.
456   if (!type.isConstQualified()) return nullptr;
457 
458   // Furthermore, in C++ we have to worry about mutable fields:
459   // C++ [dcl.type.cv]p4:
460   //   Except that any class member declared mutable can be
461   //   modified, any attempt to modify a const object during its
462   //   lifetime results in undefined behavior.
463   if (CGM.getLangOpts().CPlusPlus && !isSafeForCXXConstantCapture(type))
464     return nullptr;
465 
466   // If the variable doesn't have any initializer (shouldn't this be
467   // invalid?), it's not clear what we should do.  Maybe capture as
468   // zero?
469   const Expr *init = var->getInit();
470   if (!init) return nullptr;
471 
472   return ConstantEmitter(CGM, CGF).tryEmitAbstractForInitializer(*var);
473 }
474 
475 /// Get the low bit of a nonzero character count.  This is the
476 /// alignment of the nth byte if the 0th byte is universally aligned.
477 static CharUnits getLowBit(CharUnits v) {
478   return CharUnits::fromQuantity(v.getQuantity() & (~v.getQuantity() + 1));
479 }
480 
481 static void initializeForBlockHeader(CodeGenModule &CGM, CGBlockInfo &info,
482                              SmallVectorImpl<llvm::Type*> &elementTypes) {
483 
484   assert(elementTypes.empty());
485   if (CGM.getLangOpts().OpenCL) {
486     // The header is basically 'struct { int; int; generic void *;
487     // custom_fields; }'. Assert that struct is packed.
488     auto GenPtrAlign = CharUnits::fromQuantity(
489         CGM.getTarget().getPointerAlign(LangAS::opencl_generic) / 8);
490     auto GenPtrSize = CharUnits::fromQuantity(
491         CGM.getTarget().getPointerWidth(LangAS::opencl_generic) / 8);
492     assert(CGM.getIntSize() <= GenPtrSize);
493     assert(CGM.getIntAlign() <= GenPtrAlign);
494     assert((2 * CGM.getIntSize()).isMultipleOf(GenPtrAlign));
495     elementTypes.push_back(CGM.IntTy); /* total size */
496     elementTypes.push_back(CGM.IntTy); /* align */
497     elementTypes.push_back(
498         CGM.getOpenCLRuntime()
499             .getGenericVoidPointerType()); /* invoke function */
500     unsigned Offset =
501         2 * CGM.getIntSize().getQuantity() + GenPtrSize.getQuantity();
502     unsigned BlockAlign = GenPtrAlign.getQuantity();
503     if (auto *Helper =
504             CGM.getTargetCodeGenInfo().getTargetOpenCLBlockHelper()) {
505       for (auto *I : Helper->getCustomFieldTypes()) /* custom fields */ {
506         // TargetOpenCLBlockHelp needs to make sure the struct is packed.
507         // If necessary, add padding fields to the custom fields.
508         unsigned Align = CGM.getDataLayout().getABITypeAlign(I).value();
509         if (BlockAlign < Align)
510           BlockAlign = Align;
511         assert(Offset % Align == 0);
512         Offset += CGM.getDataLayout().getTypeAllocSize(I);
513         elementTypes.push_back(I);
514       }
515     }
516     info.BlockAlign = CharUnits::fromQuantity(BlockAlign);
517     info.BlockSize = CharUnits::fromQuantity(Offset);
518   } else {
519     // The header is basically 'struct { void *; int; int; void *; void *; }'.
520     // Assert that the struct is packed.
521     assert(CGM.getIntSize() <= CGM.getPointerSize());
522     assert(CGM.getIntAlign() <= CGM.getPointerAlign());
523     assert((2 * CGM.getIntSize()).isMultipleOf(CGM.getPointerAlign()));
524     info.BlockAlign = CGM.getPointerAlign();
525     info.BlockSize = 3 * CGM.getPointerSize() + 2 * CGM.getIntSize();
526     elementTypes.push_back(CGM.VoidPtrTy);
527     elementTypes.push_back(CGM.IntTy);
528     elementTypes.push_back(CGM.IntTy);
529     elementTypes.push_back(CGM.VoidPtrTy);
530     elementTypes.push_back(CGM.getBlockDescriptorType());
531   }
532 }
533 
534 static QualType getCaptureFieldType(const CodeGenFunction &CGF,
535                                     const BlockDecl::Capture &CI) {
536   const VarDecl *VD = CI.getVariable();
537 
538   // If the variable is captured by an enclosing block or lambda expression,
539   // use the type of the capture field.
540   if (CGF.BlockInfo && CI.isNested())
541     return CGF.BlockInfo->getCapture(VD).fieldType();
542   if (auto *FD = CGF.LambdaCaptureFields.lookup(VD))
543     return FD->getType();
544   // If the captured variable is a non-escaping __block variable, the field
545   // type is the reference type. If the variable is a __block variable that
546   // already has a reference type, the field type is the variable's type.
547   return VD->isNonEscapingByref() ?
548          CGF.getContext().getLValueReferenceType(VD->getType()) : VD->getType();
549 }
550 
551 /// Compute the layout of the given block.  Attempts to lay the block
552 /// out with minimal space requirements.
553 static void computeBlockInfo(CodeGenModule &CGM, CodeGenFunction *CGF,
554                              CGBlockInfo &info) {
555   ASTContext &C = CGM.getContext();
556   const BlockDecl *block = info.getBlockDecl();
557 
558   SmallVector<llvm::Type*, 8> elementTypes;
559   initializeForBlockHeader(CGM, info, elementTypes);
560   bool hasNonConstantCustomFields = false;
561   if (auto *OpenCLHelper =
562           CGM.getTargetCodeGenInfo().getTargetOpenCLBlockHelper())
563     hasNonConstantCustomFields =
564         !OpenCLHelper->areAllCustomFieldValuesConstant(info);
565   if (!block->hasCaptures() && !hasNonConstantCustomFields) {
566     info.StructureType =
567       llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true);
568     info.CanBeGlobal = true;
569     return;
570   }
571   else if (C.getLangOpts().ObjC &&
572            CGM.getLangOpts().getGC() == LangOptions::NonGC)
573     info.HasCapturedVariableLayout = true;
574 
575   if (block->doesNotEscape())
576     info.NoEscape = true;
577 
578   // Collect the layout chunks.
579   SmallVector<BlockLayoutChunk, 16> layout;
580   layout.reserve(block->capturesCXXThis() +
581                  (block->capture_end() - block->capture_begin()));
582 
583   CharUnits maxFieldAlign;
584 
585   // First, 'this'.
586   if (block->capturesCXXThis()) {
587     assert(CGF && isa_and_nonnull<CXXMethodDecl>(CGF->CurFuncDecl) &&
588            "Can't capture 'this' outside a method");
589     QualType thisType = cast<CXXMethodDecl>(CGF->CurFuncDecl)->getThisType();
590 
591     // Theoretically, this could be in a different address space, so
592     // don't assume standard pointer size/align.
593     llvm::Type *llvmType = CGM.getTypes().ConvertType(thisType);
594     auto TInfo = CGM.getContext().getTypeInfoInChars(thisType);
595     maxFieldAlign = std::max(maxFieldAlign, TInfo.Align);
596 
597     addBlockLayout(TInfo.Align, TInfo.Width, nullptr, llvmType, thisType,
598                    layout, info, CGM);
599   }
600 
601   // Next, all the block captures.
602   for (const auto &CI : block->captures()) {
603     const VarDecl *variable = CI.getVariable();
604 
605     if (CI.isEscapingByref()) {
606       // Just use void* instead of a pointer to the byref type.
607       CharUnits align = CGM.getPointerAlign();
608       maxFieldAlign = std::max(maxFieldAlign, align);
609 
610       // Since a __block variable cannot be captured by lambdas, its type and
611       // the capture field type should always match.
612       assert(CGF && getCaptureFieldType(*CGF, CI) == variable->getType() &&
613              "capture type differs from the variable type");
614       addBlockLayout(align, CGM.getPointerSize(), &CI, CGM.VoidPtrTy,
615                      variable->getType(), layout, info, CGM);
616       continue;
617     }
618 
619     // Otherwise, build a layout chunk with the size and alignment of
620     // the declaration.
621     if (llvm::Constant *constant = tryCaptureAsConstant(CGM, CGF, variable)) {
622       info.SortedCaptures.push_back(
623           CGBlockInfo::Capture::makeConstant(constant, &CI));
624       continue;
625     }
626 
627     QualType VT = getCaptureFieldType(*CGF, CI);
628 
629     if (CGM.getLangOpts().CPlusPlus)
630       if (const CXXRecordDecl *record = VT->getAsCXXRecordDecl())
631         if (CI.hasCopyExpr() || !record->hasTrivialDestructor()) {
632           info.HasCXXObject = true;
633           if (!record->isExternallyVisible())
634             info.CapturesNonExternalType = true;
635         }
636 
637     CharUnits size = C.getTypeSizeInChars(VT);
638     CharUnits align = C.getDeclAlign(variable);
639 
640     maxFieldAlign = std::max(maxFieldAlign, align);
641 
642     llvm::Type *llvmType =
643       CGM.getTypes().ConvertTypeForMem(VT);
644 
645     addBlockLayout(align, size, &CI, llvmType, VT, layout, info, CGM);
646   }
647 
648   // If that was everything, we're done here.
649   if (layout.empty()) {
650     info.StructureType =
651       llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true);
652     info.CanBeGlobal = true;
653     info.buildCaptureMap();
654     return;
655   }
656 
657   // Sort the layout by alignment.  We have to use a stable sort here
658   // to get reproducible results.  There should probably be an
659   // llvm::array_pod_stable_sort.
660   llvm::stable_sort(layout);
661 
662   // Needed for blocks layout info.
663   info.BlockHeaderForcedGapOffset = info.BlockSize;
664   info.BlockHeaderForcedGapSize = CharUnits::Zero();
665 
666   CharUnits &blockSize = info.BlockSize;
667   info.BlockAlign = std::max(maxFieldAlign, info.BlockAlign);
668 
669   // Assuming that the first byte in the header is maximally aligned,
670   // get the alignment of the first byte following the header.
671   CharUnits endAlign = getLowBit(blockSize);
672 
673   // If the end of the header isn't satisfactorily aligned for the
674   // maximum thing, look for things that are okay with the header-end
675   // alignment, and keep appending them until we get something that's
676   // aligned right.  This algorithm is only guaranteed optimal if
677   // that condition is satisfied at some point; otherwise we can get
678   // things like:
679   //   header                 // next byte has alignment 4
680   //   something_with_size_5; // next byte has alignment 1
681   //   something_with_alignment_8;
682   // which has 7 bytes of padding, as opposed to the naive solution
683   // which might have less (?).
684   if (endAlign < maxFieldAlign) {
685     SmallVectorImpl<BlockLayoutChunk>::iterator
686       li = layout.begin() + 1, le = layout.end();
687 
688     // Look for something that the header end is already
689     // satisfactorily aligned for.
690     for (; li != le && endAlign < li->Alignment; ++li)
691       ;
692 
693     // If we found something that's naturally aligned for the end of
694     // the header, keep adding things...
695     if (li != le) {
696       SmallVectorImpl<BlockLayoutChunk>::iterator first = li;
697       for (; li != le; ++li) {
698         assert(endAlign >= li->Alignment);
699 
700         li->setIndex(info, elementTypes.size(), blockSize);
701         elementTypes.push_back(li->Type);
702         blockSize += li->Size;
703         endAlign = getLowBit(blockSize);
704 
705         // ...until we get to the alignment of the maximum field.
706         if (endAlign >= maxFieldAlign) {
707           ++li;
708           break;
709         }
710       }
711       // Don't re-append everything we just appended.
712       layout.erase(first, li);
713     }
714   }
715 
716   assert(endAlign == getLowBit(blockSize));
717 
718   // At this point, we just have to add padding if the end align still
719   // isn't aligned right.
720   if (endAlign < maxFieldAlign) {
721     CharUnits newBlockSize = blockSize.alignTo(maxFieldAlign);
722     CharUnits padding = newBlockSize - blockSize;
723 
724     // If we haven't yet added any fields, remember that there was an
725     // initial gap; this need to go into the block layout bit map.
726     if (blockSize == info.BlockHeaderForcedGapOffset) {
727       info.BlockHeaderForcedGapSize = padding;
728     }
729 
730     elementTypes.push_back(llvm::ArrayType::get(CGM.Int8Ty,
731                                                 padding.getQuantity()));
732     blockSize = newBlockSize;
733     endAlign = getLowBit(blockSize); // might be > maxFieldAlign
734   }
735 
736   assert(endAlign >= maxFieldAlign);
737   assert(endAlign == getLowBit(blockSize));
738   // Slam everything else on now.  This works because they have
739   // strictly decreasing alignment and we expect that size is always a
740   // multiple of alignment.
741   for (SmallVectorImpl<BlockLayoutChunk>::iterator
742          li = layout.begin(), le = layout.end(); li != le; ++li) {
743     if (endAlign < li->Alignment) {
744       // size may not be multiple of alignment. This can only happen with
745       // an over-aligned variable. We will be adding a padding field to
746       // make the size be multiple of alignment.
747       CharUnits padding = li->Alignment - endAlign;
748       elementTypes.push_back(llvm::ArrayType::get(CGM.Int8Ty,
749                                                   padding.getQuantity()));
750       blockSize += padding;
751       endAlign = getLowBit(blockSize);
752     }
753     assert(endAlign >= li->Alignment);
754     li->setIndex(info, elementTypes.size(), blockSize);
755     elementTypes.push_back(li->Type);
756     blockSize += li->Size;
757     endAlign = getLowBit(blockSize);
758   }
759 
760   info.buildCaptureMap();
761   info.StructureType =
762     llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true);
763 }
764 
765 /// Emit a block literal expression in the current function.
766 llvm::Value *CodeGenFunction::EmitBlockLiteral(const BlockExpr *blockExpr) {
767   // If the block has no captures, we won't have a pre-computed
768   // layout for it.
769   if (!blockExpr->getBlockDecl()->hasCaptures())
770     // The block literal is emitted as a global variable, and the block invoke
771     // function has to be extracted from its initializer.
772     if (llvm::Constant *Block = CGM.getAddrOfGlobalBlockIfEmitted(blockExpr))
773       return Block;
774 
775   CGBlockInfo blockInfo(blockExpr->getBlockDecl(), CurFn->getName());
776   computeBlockInfo(CGM, this, blockInfo);
777   blockInfo.BlockExpression = blockExpr;
778   if (!blockInfo.CanBeGlobal)
779     blockInfo.LocalAddress = CreateTempAlloca(blockInfo.StructureType,
780                                               blockInfo.BlockAlign, "block");
781   return EmitBlockLiteral(blockInfo);
782 }
783 
784 llvm::Value *CodeGenFunction::EmitBlockLiteral(const CGBlockInfo &blockInfo) {
785   bool IsOpenCL = CGM.getContext().getLangOpts().OpenCL;
786   auto GenVoidPtrTy =
787       IsOpenCL ? CGM.getOpenCLRuntime().getGenericVoidPointerType() : VoidPtrTy;
788   LangAS GenVoidPtrAddr = IsOpenCL ? LangAS::opencl_generic : LangAS::Default;
789   auto GenVoidPtrSize = CharUnits::fromQuantity(
790       CGM.getTarget().getPointerWidth(GenVoidPtrAddr) / 8);
791   // Using the computed layout, generate the actual block function.
792   bool isLambdaConv = blockInfo.getBlockDecl()->isConversionFromLambda();
793   CodeGenFunction BlockCGF{CGM, true};
794   BlockCGF.SanOpts = SanOpts;
795   auto *InvokeFn = BlockCGF.GenerateBlockFunction(
796       CurGD, blockInfo, LocalDeclMap, isLambdaConv, blockInfo.CanBeGlobal);
797   auto *blockFn = llvm::ConstantExpr::getPointerCast(InvokeFn, GenVoidPtrTy);
798 
799   // If there is nothing to capture, we can emit this as a global block.
800   if (blockInfo.CanBeGlobal)
801     return CGM.getAddrOfGlobalBlockIfEmitted(blockInfo.BlockExpression);
802 
803   // Otherwise, we have to emit this as a local block.
804 
805   RawAddress blockAddr = blockInfo.LocalAddress;
806   assert(blockAddr.isValid() && "block has no address!");
807 
808   llvm::Constant *isa;
809   llvm::Constant *descriptor;
810   BlockFlags flags;
811   if (!IsOpenCL) {
812     // If the block is non-escaping, set field 'isa 'to NSConcreteGlobalBlock
813     // and set the BLOCK_IS_GLOBAL bit of field 'flags'. Copying a non-escaping
814     // block just returns the original block and releasing it is a no-op.
815     llvm::Constant *blockISA = blockInfo.NoEscape
816                                    ? CGM.getNSConcreteGlobalBlock()
817                                    : CGM.getNSConcreteStackBlock();
818     isa = blockISA;
819 
820     // Build the block descriptor.
821     descriptor = buildBlockDescriptor(CGM, blockInfo);
822 
823     // Compute the initial on-stack block flags.
824     if (!CGM.getCodeGenOpts().DisableBlockSignatureString)
825       flags = BLOCK_HAS_SIGNATURE;
826     if (blockInfo.HasCapturedVariableLayout)
827       flags |= BLOCK_HAS_EXTENDED_LAYOUT;
828     if (blockInfo.NeedsCopyDispose)
829       flags |= BLOCK_HAS_COPY_DISPOSE;
830     if (blockInfo.HasCXXObject)
831       flags |= BLOCK_HAS_CXX_OBJ;
832     if (blockInfo.UsesStret)
833       flags |= BLOCK_USE_STRET;
834     if (blockInfo.NoEscape)
835       flags |= BLOCK_IS_NOESCAPE | BLOCK_IS_GLOBAL;
836   }
837 
838   auto projectField = [&](unsigned index, const Twine &name) -> Address {
839     return Builder.CreateStructGEP(blockAddr, index, name);
840   };
841   auto storeField = [&](llvm::Value *value, unsigned index, const Twine &name) {
842     Builder.CreateStore(value, projectField(index, name));
843   };
844 
845   // Initialize the block header.
846   {
847     // We assume all the header fields are densely packed.
848     unsigned index = 0;
849     CharUnits offset;
850     auto addHeaderField = [&](llvm::Value *value, CharUnits size,
851                               const Twine &name) {
852       storeField(value, index, name);
853       offset += size;
854       index++;
855     };
856     auto addSignedHeaderField =
857         [&](llvm::Value *Value, const PointerAuthSchema &Schema,
858             GlobalDecl Decl, QualType Type, CharUnits Size, const Twine &Name) {
859           auto StorageAddress = projectField(index, Name);
860           if (Schema) {
861             auto AuthInfo = EmitPointerAuthInfo(
862                 Schema, StorageAddress.emitRawPointer(*this), Decl, Type);
863             Value = EmitPointerAuthSign(AuthInfo, Value);
864           }
865           Builder.CreateStore(Value, StorageAddress);
866           offset += Size;
867           index++;
868         };
869 
870     if (!IsOpenCL) {
871       addSignedHeaderField(
872           isa, CGM.getCodeGenOpts().PointerAuth.ObjCIsaPointers, GlobalDecl(),
873           QualType(), getPointerSize(), "block.isa");
874       addHeaderField(llvm::ConstantInt::get(IntTy, flags.getBitMask()),
875                      getIntSize(), "block.flags");
876       addHeaderField(llvm::ConstantInt::get(IntTy, 0), getIntSize(),
877                      "block.reserved");
878     } else {
879       addHeaderField(
880           llvm::ConstantInt::get(IntTy, blockInfo.BlockSize.getQuantity()),
881           getIntSize(), "block.size");
882       addHeaderField(
883           llvm::ConstantInt::get(IntTy, blockInfo.BlockAlign.getQuantity()),
884           getIntSize(), "block.align");
885     }
886     addHeaderField(blockFn, GenVoidPtrSize, "block.invoke");
887     if (!IsOpenCL)
888       addHeaderField(descriptor, getPointerSize(), "block.descriptor");
889     else if (auto *Helper =
890                  CGM.getTargetCodeGenInfo().getTargetOpenCLBlockHelper()) {
891       for (auto I : Helper->getCustomFieldValues(*this, blockInfo)) {
892         addHeaderField(
893             I.first,
894             CharUnits::fromQuantity(
895                 CGM.getDataLayout().getTypeAllocSize(I.first->getType())),
896             I.second);
897       }
898     }
899   }
900 
901   // Finally, capture all the values into the block.
902   const BlockDecl *blockDecl = blockInfo.getBlockDecl();
903 
904   // First, 'this'.
905   if (blockDecl->capturesCXXThis()) {
906     Address addr =
907         projectField(blockInfo.CXXThisIndex, "block.captured-this.addr");
908     Builder.CreateStore(LoadCXXThis(), addr);
909   }
910 
911   // Next, captured variables.
912   for (const auto &CI : blockDecl->captures()) {
913     const VarDecl *variable = CI.getVariable();
914     const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
915 
916     // Ignore constant captures.
917     if (capture.isConstant()) continue;
918 
919     QualType type = capture.fieldType();
920 
921     // This will be a [[type]]*, except that a byref entry will just be
922     // an i8**.
923     Address blockField = projectField(capture.getIndex(), "block.captured");
924 
925     // Compute the address of the thing we're going to move into the
926     // block literal.
927     Address src = Address::invalid();
928 
929     if (blockDecl->isConversionFromLambda()) {
930       // The lambda capture in a lambda's conversion-to-block-pointer is
931       // special; we'll simply emit it directly.
932       src = Address::invalid();
933     } else if (CI.isEscapingByref()) {
934       if (BlockInfo && CI.isNested()) {
935         // We need to use the capture from the enclosing block.
936         const CGBlockInfo::Capture &enclosingCapture =
937             BlockInfo->getCapture(variable);
938 
939         // This is a [[type]]*, except that a byref entry will just be an i8**.
940         src = Builder.CreateStructGEP(LoadBlockStruct(),
941                                       enclosingCapture.getIndex(),
942                                       "block.capture.addr");
943       } else {
944         auto I = LocalDeclMap.find(variable);
945         assert(I != LocalDeclMap.end());
946         src = I->second;
947       }
948     } else {
949       DeclRefExpr declRef(getContext(), const_cast<VarDecl *>(variable),
950                           /*RefersToEnclosingVariableOrCapture*/ CI.isNested(),
951                           type.getNonReferenceType(), VK_LValue,
952                           SourceLocation());
953       src = EmitDeclRefLValue(&declRef).getAddress();
954     };
955 
956     // For byrefs, we just write the pointer to the byref struct into
957     // the block field.  There's no need to chase the forwarding
958     // pointer at this point, since we're building something that will
959     // live a shorter life than the stack byref anyway.
960     if (CI.isEscapingByref()) {
961       // Get a void* that points to the byref struct.
962       llvm::Value *byrefPointer;
963       if (CI.isNested())
964         byrefPointer = Builder.CreateLoad(src, "byref.capture");
965       else
966         byrefPointer = src.emitRawPointer(*this);
967 
968       // Write that void* into the capture field.
969       Builder.CreateStore(byrefPointer, blockField);
970 
971     // If we have a copy constructor, evaluate that into the block field.
972     } else if (const Expr *copyExpr = CI.getCopyExpr()) {
973       if (blockDecl->isConversionFromLambda()) {
974         // If we have a lambda conversion, emit the expression
975         // directly into the block instead.
976         AggValueSlot Slot =
977             AggValueSlot::forAddr(blockField, Qualifiers(),
978                                   AggValueSlot::IsDestructed,
979                                   AggValueSlot::DoesNotNeedGCBarriers,
980                                   AggValueSlot::IsNotAliased,
981                                   AggValueSlot::DoesNotOverlap);
982         EmitAggExpr(copyExpr, Slot);
983       } else {
984         EmitSynthesizedCXXCopyCtor(blockField, src, copyExpr);
985       }
986 
987     // If it's a reference variable, copy the reference into the block field.
988     } else if (type->getAs<ReferenceType>()) {
989       Builder.CreateStore(src.emitRawPointer(*this), blockField);
990 
991       // If type is const-qualified, copy the value into the block field.
992     } else if (type.isConstQualified() &&
993                type.getObjCLifetime() == Qualifiers::OCL_Strong &&
994                CGM.getCodeGenOpts().OptimizationLevel != 0) {
995       llvm::Value *value = Builder.CreateLoad(src, "captured");
996       Builder.CreateStore(value, blockField);
997 
998     // If this is an ARC __strong block-pointer variable, don't do a
999     // block copy.
1000     //
1001     // TODO: this can be generalized into the normal initialization logic:
1002     // we should never need to do a block-copy when initializing a local
1003     // variable, because the local variable's lifetime should be strictly
1004     // contained within the stack block's.
1005     } else if (type.getObjCLifetime() == Qualifiers::OCL_Strong &&
1006                type->isBlockPointerType()) {
1007       // Load the block and do a simple retain.
1008       llvm::Value *value = Builder.CreateLoad(src, "block.captured_block");
1009       value = EmitARCRetainNonBlock(value);
1010 
1011       // Do a primitive store to the block field.
1012       Builder.CreateStore(value, blockField);
1013 
1014     // Otherwise, fake up a POD copy into the block field.
1015     } else {
1016       // Fake up a new variable so that EmitScalarInit doesn't think
1017       // we're referring to the variable in its own initializer.
1018       ImplicitParamDecl BlockFieldPseudoVar(getContext(), type,
1019                                             ImplicitParamKind::Other);
1020 
1021       // We use one of these or the other depending on whether the
1022       // reference is nested.
1023       DeclRefExpr declRef(getContext(), const_cast<VarDecl *>(variable),
1024                           /*RefersToEnclosingVariableOrCapture*/ CI.isNested(),
1025                           type, VK_LValue, SourceLocation());
1026 
1027       ImplicitCastExpr l2r(ImplicitCastExpr::OnStack, type, CK_LValueToRValue,
1028                            &declRef, VK_PRValue, FPOptionsOverride());
1029       // FIXME: Pass a specific location for the expr init so that the store is
1030       // attributed to a reasonable location - otherwise it may be attributed to
1031       // locations of subexpressions in the initialization.
1032       EmitExprAsInit(&l2r, &BlockFieldPseudoVar,
1033                      MakeAddrLValue(blockField, type, AlignmentSource::Decl),
1034                      /*captured by init*/ false);
1035     }
1036 
1037     // Push a cleanup for the capture if necessary.
1038     if (!blockInfo.NoEscape && !blockInfo.NeedsCopyDispose)
1039       continue;
1040 
1041     // Ignore __block captures; there's nothing special in the on-stack block
1042     // that we need to do for them.
1043     if (CI.isByRef())
1044       continue;
1045 
1046     // Ignore objects that aren't destructed.
1047     QualType::DestructionKind dtorKind = type.isDestructedType();
1048     if (dtorKind == QualType::DK_none)
1049       continue;
1050 
1051     CodeGenFunction::Destroyer *destroyer;
1052 
1053     // Block captures count as local values and have imprecise semantics.
1054     // They also can't be arrays, so need to worry about that.
1055     //
1056     // For const-qualified captures, emit clang.arc.use to ensure the captured
1057     // object doesn't get released while we are still depending on its validity
1058     // within the block.
1059     if (type.isConstQualified() &&
1060         type.getObjCLifetime() == Qualifiers::OCL_Strong &&
1061         CGM.getCodeGenOpts().OptimizationLevel != 0) {
1062       assert(CGM.getLangOpts().ObjCAutoRefCount &&
1063              "expected ObjC ARC to be enabled");
1064       destroyer = emitARCIntrinsicUse;
1065     } else if (dtorKind == QualType::DK_objc_strong_lifetime) {
1066       destroyer = destroyARCStrongImprecise;
1067     } else {
1068       destroyer = getDestroyer(dtorKind);
1069     }
1070 
1071     CleanupKind cleanupKind = NormalCleanup;
1072     bool useArrayEHCleanup = needsEHCleanup(dtorKind);
1073     if (useArrayEHCleanup)
1074       cleanupKind = NormalAndEHCleanup;
1075 
1076     // Extend the lifetime of the capture to the end of the scope enclosing the
1077     // block expression except when the block decl is in the list of RetExpr's
1078     // cleanup objects, in which case its lifetime ends after the full
1079     // expression.
1080     auto IsBlockDeclInRetExpr = [&]() {
1081       auto *EWC = llvm::dyn_cast_or_null<ExprWithCleanups>(RetExpr);
1082       if (EWC)
1083         for (auto &C : EWC->getObjects())
1084           if (auto *BD = C.dyn_cast<BlockDecl *>())
1085             if (BD == blockDecl)
1086               return true;
1087       return false;
1088     };
1089 
1090     if (IsBlockDeclInRetExpr())
1091       pushDestroy(cleanupKind, blockField, type, destroyer, useArrayEHCleanup);
1092     else
1093       pushLifetimeExtendedDestroy(cleanupKind, blockField, type, destroyer,
1094                                   useArrayEHCleanup);
1095   }
1096 
1097   // Cast to the converted block-pointer type, which happens (somewhat
1098   // unfortunately) to be a pointer to function type.
1099   llvm::Value *result = Builder.CreatePointerCast(
1100       blockAddr.getPointer(), ConvertType(blockInfo.getBlockExpr()->getType()));
1101 
1102   if (IsOpenCL) {
1103     CGM.getOpenCLRuntime().recordBlockInfo(blockInfo.BlockExpression, InvokeFn,
1104                                            result, blockInfo.StructureType);
1105   }
1106 
1107   return result;
1108 }
1109 
1110 
1111 llvm::Type *CodeGenModule::getBlockDescriptorType() {
1112   if (BlockDescriptorType)
1113     return BlockDescriptorType;
1114 
1115   unsigned AddrSpace = 0;
1116   if (getLangOpts().OpenCL)
1117     AddrSpace = getContext().getTargetAddressSpace(LangAS::opencl_constant);
1118   BlockDescriptorType = llvm::PointerType::get(getLLVMContext(), AddrSpace);
1119   return BlockDescriptorType;
1120 }
1121 
1122 llvm::Type *CodeGenModule::getGenericBlockLiteralType() {
1123   if (GenericBlockLiteralType)
1124     return GenericBlockLiteralType;
1125 
1126   llvm::Type *BlockDescPtrTy = getBlockDescriptorType();
1127 
1128   if (getLangOpts().OpenCL) {
1129     // struct __opencl_block_literal_generic {
1130     //   int __size;
1131     //   int __align;
1132     //   __generic void *__invoke;
1133     //   /* custom fields */
1134     // };
1135     SmallVector<llvm::Type *, 8> StructFields(
1136         {IntTy, IntTy, getOpenCLRuntime().getGenericVoidPointerType()});
1137     if (auto *Helper = getTargetCodeGenInfo().getTargetOpenCLBlockHelper()) {
1138       llvm::append_range(StructFields, Helper->getCustomFieldTypes());
1139     }
1140     GenericBlockLiteralType = llvm::StructType::create(
1141         StructFields, "struct.__opencl_block_literal_generic");
1142   } else {
1143     // struct __block_literal_generic {
1144     //   void *__isa;
1145     //   int __flags;
1146     //   int __reserved;
1147     //   void (*__invoke)(void *);
1148     //   struct __block_descriptor *__descriptor;
1149     // };
1150     GenericBlockLiteralType =
1151         llvm::StructType::create("struct.__block_literal_generic", VoidPtrTy,
1152                                  IntTy, IntTy, VoidPtrTy, BlockDescPtrTy);
1153   }
1154 
1155   return GenericBlockLiteralType;
1156 }
1157 
1158 RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr *E,
1159                                           ReturnValueSlot ReturnValue,
1160                                           llvm::CallBase **CallOrInvoke) {
1161   const auto *BPT = E->getCallee()->getType()->castAs<BlockPointerType>();
1162   llvm::Value *BlockPtr = EmitScalarExpr(E->getCallee());
1163   llvm::Type *GenBlockTy = CGM.getGenericBlockLiteralType();
1164   llvm::Value *Func = nullptr;
1165   QualType FnType = BPT->getPointeeType();
1166   ASTContext &Ctx = getContext();
1167   CallArgList Args;
1168 
1169   if (getLangOpts().OpenCL) {
1170     // For OpenCL, BlockPtr is already casted to generic block literal.
1171 
1172     // First argument of a block call is a generic block literal casted to
1173     // generic void pointer, i.e. i8 addrspace(4)*
1174     llvm::Type *GenericVoidPtrTy =
1175         CGM.getOpenCLRuntime().getGenericVoidPointerType();
1176     llvm::Value *BlockDescriptor = Builder.CreatePointerCast(
1177         BlockPtr, GenericVoidPtrTy);
1178     QualType VoidPtrQualTy = Ctx.getPointerType(
1179         Ctx.getAddrSpaceQualType(Ctx.VoidTy, LangAS::opencl_generic));
1180     Args.add(RValue::get(BlockDescriptor), VoidPtrQualTy);
1181     // And the rest of the arguments.
1182     EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(), E->arguments());
1183 
1184     // We *can* call the block directly unless it is a function argument.
1185     if (!isa<ParmVarDecl>(E->getCalleeDecl()))
1186       Func = CGM.getOpenCLRuntime().getInvokeFunction(E->getCallee());
1187     else {
1188       llvm::Value *FuncPtr = Builder.CreateStructGEP(GenBlockTy, BlockPtr, 2);
1189       Func = Builder.CreateAlignedLoad(GenericVoidPtrTy, FuncPtr,
1190                                        getPointerAlign());
1191     }
1192   } else {
1193     // Bitcast the block literal to a generic block literal.
1194     BlockPtr =
1195         Builder.CreatePointerCast(BlockPtr, UnqualPtrTy, "block.literal");
1196     // Get pointer to the block invoke function
1197     llvm::Value *FuncPtr = Builder.CreateStructGEP(GenBlockTy, BlockPtr, 3);
1198 
1199     // First argument is a block literal casted to a void pointer
1200     BlockPtr = Builder.CreatePointerCast(BlockPtr, VoidPtrTy);
1201     Args.add(RValue::get(BlockPtr), Ctx.VoidPtrTy);
1202     // And the rest of the arguments.
1203     EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(), E->arguments());
1204 
1205     // Load the function.
1206     Func = Builder.CreateAlignedLoad(VoidPtrTy, FuncPtr, getPointerAlign());
1207   }
1208 
1209   const FunctionType *FuncTy = FnType->castAs<FunctionType>();
1210   const CGFunctionInfo &FnInfo =
1211     CGM.getTypes().arrangeBlockFunctionCall(Args, FuncTy);
1212 
1213   // Prepare the callee.
1214   CGCallee Callee(CGCalleeInfo(), Func);
1215 
1216   // And call the block.
1217   return EmitCall(FnInfo, Callee, ReturnValue, Args, CallOrInvoke);
1218 }
1219 
1220 Address CodeGenFunction::GetAddrOfBlockDecl(const VarDecl *variable) {
1221   assert(BlockInfo && "evaluating block ref without block information?");
1222   const CGBlockInfo::Capture &capture = BlockInfo->getCapture(variable);
1223 
1224   // Handle constant captures.
1225   if (capture.isConstant()) return LocalDeclMap.find(variable)->second;
1226 
1227   Address addr = Builder.CreateStructGEP(LoadBlockStruct(), capture.getIndex(),
1228                                          "block.capture.addr");
1229 
1230   if (variable->isEscapingByref()) {
1231     // addr should be a void** right now.  Load, then cast the result
1232     // to byref*.
1233 
1234     auto &byrefInfo = getBlockByrefInfo(variable);
1235     addr = Address(Builder.CreateLoad(addr), byrefInfo.Type,
1236                    byrefInfo.ByrefAlignment);
1237 
1238     addr = emitBlockByrefAddress(addr, byrefInfo, /*follow*/ true,
1239                                  variable->getName());
1240   }
1241 
1242   assert((!variable->isNonEscapingByref() ||
1243           capture.fieldType()->isReferenceType()) &&
1244          "the capture field of a non-escaping variable should have a "
1245          "reference type");
1246   if (capture.fieldType()->isReferenceType())
1247     addr = EmitLoadOfReference(MakeAddrLValue(addr, capture.fieldType()));
1248 
1249   return addr;
1250 }
1251 
1252 void CodeGenModule::setAddrOfGlobalBlock(const BlockExpr *BE,
1253                                          llvm::Constant *Addr) {
1254   bool Ok = EmittedGlobalBlocks.insert(std::make_pair(BE, Addr)).second;
1255   (void)Ok;
1256   assert(Ok && "Trying to replace an already-existing global block!");
1257 }
1258 
1259 llvm::Constant *
1260 CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *BE,
1261                                     StringRef Name) {
1262   if (llvm::Constant *Block = getAddrOfGlobalBlockIfEmitted(BE))
1263     return Block;
1264 
1265   CGBlockInfo blockInfo(BE->getBlockDecl(), Name);
1266   blockInfo.BlockExpression = BE;
1267 
1268   // Compute information about the layout, etc., of this block.
1269   computeBlockInfo(*this, nullptr, blockInfo);
1270 
1271   // Using that metadata, generate the actual block function.
1272   {
1273     CodeGenFunction::DeclMapTy LocalDeclMap;
1274     CodeGenFunction(*this).GenerateBlockFunction(
1275         GlobalDecl(), blockInfo, LocalDeclMap,
1276         /*IsLambdaConversionToBlock*/ false, /*BuildGlobalBlock*/ true);
1277   }
1278 
1279   return getAddrOfGlobalBlockIfEmitted(BE);
1280 }
1281 
1282 static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM,
1283                                         const CGBlockInfo &blockInfo,
1284                                         llvm::Constant *blockFn) {
1285   assert(blockInfo.CanBeGlobal);
1286   // Callers should detect this case on their own: calling this function
1287   // generally requires computing layout information, which is a waste of time
1288   // if we've already emitted this block.
1289   assert(!CGM.getAddrOfGlobalBlockIfEmitted(blockInfo.BlockExpression) &&
1290          "Refusing to re-emit a global block.");
1291 
1292   // Generate the constants for the block literal initializer.
1293   ConstantInitBuilder builder(CGM);
1294   auto fields = builder.beginStruct();
1295 
1296   bool IsOpenCL = CGM.getLangOpts().OpenCL;
1297   bool IsWindows = CGM.getTarget().getTriple().isOSWindows();
1298   if (!IsOpenCL) {
1299     // isa
1300     if (IsWindows)
1301       fields.addNullPointer(CGM.Int8PtrPtrTy);
1302     else
1303       fields.addSignedPointer(CGM.getNSConcreteGlobalBlock(),
1304                               CGM.getCodeGenOpts().PointerAuth.ObjCIsaPointers,
1305                               GlobalDecl(), QualType());
1306 
1307     // __flags
1308     BlockFlags flags = BLOCK_IS_GLOBAL;
1309     if (!CGM.getCodeGenOpts().DisableBlockSignatureString)
1310       flags |= BLOCK_HAS_SIGNATURE;
1311     if (blockInfo.UsesStret)
1312       flags |= BLOCK_USE_STRET;
1313 
1314     fields.addInt(CGM.IntTy, flags.getBitMask());
1315 
1316     // Reserved
1317     fields.addInt(CGM.IntTy, 0);
1318   } else {
1319     fields.addInt(CGM.IntTy, blockInfo.BlockSize.getQuantity());
1320     fields.addInt(CGM.IntTy, blockInfo.BlockAlign.getQuantity());
1321   }
1322 
1323   // Function
1324   fields.add(blockFn);
1325 
1326   if (!IsOpenCL) {
1327     // Descriptor
1328     fields.add(buildBlockDescriptor(CGM, blockInfo));
1329   } else if (auto *Helper =
1330                  CGM.getTargetCodeGenInfo().getTargetOpenCLBlockHelper()) {
1331     for (auto *I : Helper->getCustomFieldValues(CGM, blockInfo)) {
1332       fields.add(I);
1333     }
1334   }
1335 
1336   unsigned AddrSpace = 0;
1337   if (CGM.getContext().getLangOpts().OpenCL)
1338     AddrSpace = CGM.getContext().getTargetAddressSpace(LangAS::opencl_global);
1339 
1340   llvm::GlobalVariable *literal = fields.finishAndCreateGlobal(
1341       "__block_literal_global", blockInfo.BlockAlign,
1342       /*constant*/ !IsWindows, llvm::GlobalVariable::InternalLinkage, AddrSpace);
1343 
1344   literal->addAttribute("objc_arc_inert");
1345 
1346   // Windows does not allow globals to be initialised to point to globals in
1347   // different DLLs.  Any such variables must run code to initialise them.
1348   if (IsWindows) {
1349     auto *Init = llvm::Function::Create(llvm::FunctionType::get(CGM.VoidTy,
1350           {}), llvm::GlobalValue::InternalLinkage, ".block_isa_init",
1351         &CGM.getModule());
1352     llvm::IRBuilder<> b(llvm::BasicBlock::Create(CGM.getLLVMContext(), "entry",
1353           Init));
1354     b.CreateAlignedStore(CGM.getNSConcreteGlobalBlock(),
1355                          b.CreateStructGEP(literal->getValueType(), literal, 0),
1356                          CGM.getPointerAlign().getAsAlign());
1357     b.CreateRetVoid();
1358     // We can't use the normal LLVM global initialisation array, because we
1359     // need to specify that this runs early in library initialisation.
1360     auto *InitVar = new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
1361         /*isConstant*/true, llvm::GlobalValue::InternalLinkage,
1362         Init, ".block_isa_init_ptr");
1363     InitVar->setSection(".CRT$XCLa");
1364     CGM.addUsedGlobal(InitVar);
1365   }
1366 
1367   // Return a constant of the appropriately-casted type.
1368   llvm::Type *RequiredType =
1369     CGM.getTypes().ConvertType(blockInfo.getBlockExpr()->getType());
1370   llvm::Constant *Result =
1371       llvm::ConstantExpr::getPointerCast(literal, RequiredType);
1372   CGM.setAddrOfGlobalBlock(blockInfo.BlockExpression, Result);
1373   if (CGM.getContext().getLangOpts().OpenCL)
1374     CGM.getOpenCLRuntime().recordBlockInfo(
1375         blockInfo.BlockExpression,
1376         cast<llvm::Function>(blockFn->stripPointerCasts()), Result,
1377         literal->getValueType());
1378   return Result;
1379 }
1380 
1381 void CodeGenFunction::setBlockContextParameter(const ImplicitParamDecl *D,
1382                                                unsigned argNum,
1383                                                llvm::Value *arg) {
1384   assert(BlockInfo && "not emitting prologue of block invocation function?!");
1385 
1386   // Allocate a stack slot like for any local variable to guarantee optimal
1387   // debug info at -O0. The mem2reg pass will eliminate it when optimizing.
1388   RawAddress alloc = CreateMemTemp(D->getType(), D->getName() + ".addr");
1389   Builder.CreateStore(arg, alloc);
1390   if (CGDebugInfo *DI = getDebugInfo()) {
1391     if (CGM.getCodeGenOpts().hasReducedDebugInfo()) {
1392       DI->setLocation(D->getLocation());
1393       DI->EmitDeclareOfBlockLiteralArgVariable(
1394           *BlockInfo, D->getName(), argNum,
1395           cast<llvm::AllocaInst>(alloc.getPointer()->stripPointerCasts()),
1396           Builder);
1397     }
1398   }
1399 
1400   SourceLocation StartLoc = BlockInfo->getBlockExpr()->getBody()->getBeginLoc();
1401   ApplyDebugLocation Scope(*this, StartLoc);
1402 
1403   // Instead of messing around with LocalDeclMap, just set the value
1404   // directly as BlockPointer.
1405   BlockPointer = Builder.CreatePointerCast(
1406       arg,
1407       llvm::PointerType::get(
1408           getLLVMContext(),
1409           getContext().getLangOpts().OpenCL
1410               ? getContext().getTargetAddressSpace(LangAS::opencl_generic)
1411               : 0),
1412       "block");
1413 }
1414 
1415 Address CodeGenFunction::LoadBlockStruct() {
1416   assert(BlockInfo && "not in a block invocation function!");
1417   assert(BlockPointer && "no block pointer set!");
1418   return Address(BlockPointer, BlockInfo->StructureType, BlockInfo->BlockAlign);
1419 }
1420 
1421 llvm::Function *CodeGenFunction::GenerateBlockFunction(
1422     GlobalDecl GD, const CGBlockInfo &blockInfo, const DeclMapTy &ldm,
1423     bool IsLambdaConversionToBlock, bool BuildGlobalBlock) {
1424   const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1425 
1426   CurGD = GD;
1427 
1428   CurEHLocation = blockInfo.getBlockExpr()->getEndLoc();
1429 
1430   BlockInfo = &blockInfo;
1431 
1432   // Arrange for local static and local extern declarations to appear
1433   // to be local to this function as well, in case they're directly
1434   // referenced in a block.
1435   for (const auto &KV : ldm) {
1436     const auto *var = dyn_cast<VarDecl>(KV.first);
1437     if (var && !var->hasLocalStorage())
1438       setAddrOfLocalVar(var, KV.second);
1439   }
1440 
1441   // Begin building the function declaration.
1442 
1443   // Build the argument list.
1444   FunctionArgList args;
1445 
1446   // The first argument is the block pointer.  Just take it as a void*
1447   // and cast it later.
1448   QualType selfTy = getContext().VoidPtrTy;
1449 
1450   // For OpenCL passed block pointer can be private AS local variable or
1451   // global AS program scope variable (for the case with and without captures).
1452   // Generic AS is used therefore to be able to accommodate both private and
1453   // generic AS in one implementation.
1454   if (getLangOpts().OpenCL)
1455     selfTy = getContext().getPointerType(getContext().getAddrSpaceQualType(
1456         getContext().VoidTy, LangAS::opencl_generic));
1457 
1458   const IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor");
1459 
1460   ImplicitParamDecl SelfDecl(getContext(), const_cast<BlockDecl *>(blockDecl),
1461                              SourceLocation(), II, selfTy,
1462                              ImplicitParamKind::ObjCSelf);
1463   args.push_back(&SelfDecl);
1464 
1465   // Now add the rest of the parameters.
1466   args.append(blockDecl->param_begin(), blockDecl->param_end());
1467 
1468   // Create the function declaration.
1469   const FunctionProtoType *fnType = blockInfo.getBlockExpr()->getFunctionType();
1470   const CGFunctionInfo &fnInfo =
1471     CGM.getTypes().arrangeBlockFunctionDeclaration(fnType, args);
1472   if (CGM.ReturnSlotInterferesWithArgs(fnInfo))
1473     blockInfo.UsesStret = true;
1474 
1475   llvm::FunctionType *fnLLVMType = CGM.getTypes().GetFunctionType(fnInfo);
1476 
1477   StringRef name = CGM.getBlockMangledName(GD, blockDecl);
1478   llvm::Function *fn = llvm::Function::Create(
1479       fnLLVMType, llvm::GlobalValue::InternalLinkage, name, &CGM.getModule());
1480   CGM.SetInternalFunctionAttributes(blockDecl, fn, fnInfo);
1481 
1482   if (BuildGlobalBlock) {
1483     auto GenVoidPtrTy = getContext().getLangOpts().OpenCL
1484                             ? CGM.getOpenCLRuntime().getGenericVoidPointerType()
1485                             : VoidPtrTy;
1486     buildGlobalBlock(CGM, blockInfo,
1487                      llvm::ConstantExpr::getPointerCast(fn, GenVoidPtrTy));
1488   }
1489 
1490   // Begin generating the function.
1491   StartFunction(blockDecl, fnType->getReturnType(), fn, fnInfo, args,
1492                 blockDecl->getLocation(),
1493                 blockInfo.getBlockExpr()->getBody()->getBeginLoc());
1494 
1495   // Okay.  Undo some of what StartFunction did.
1496 
1497   // At -O0 we generate an explicit alloca for the BlockPointer, so the RA
1498   // won't delete the dbg.declare intrinsics for captured variables.
1499   llvm::Value *BlockPointerDbgLoc = BlockPointer;
1500   if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
1501     // Allocate a stack slot for it, so we can point the debugger to it
1502     Address Alloca = CreateTempAlloca(BlockPointer->getType(),
1503                                       getPointerAlign(),
1504                                       "block.addr");
1505     // Set the DebugLocation to empty, so the store is recognized as a
1506     // frame setup instruction by llvm::DwarfDebug::beginFunction().
1507     auto NL = ApplyDebugLocation::CreateEmpty(*this);
1508     Builder.CreateStore(BlockPointer, Alloca);
1509     BlockPointerDbgLoc = Alloca.emitRawPointer(*this);
1510   }
1511 
1512   // If we have a C++ 'this' reference, go ahead and force it into
1513   // existence now.
1514   if (blockDecl->capturesCXXThis()) {
1515     Address addr = Builder.CreateStructGEP(
1516         LoadBlockStruct(), blockInfo.CXXThisIndex, "block.captured-this");
1517     CXXThisValue = Builder.CreateLoad(addr, "this");
1518   }
1519 
1520   // Also force all the constant captures.
1521   for (const auto &CI : blockDecl->captures()) {
1522     const VarDecl *variable = CI.getVariable();
1523     const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1524     if (!capture.isConstant()) continue;
1525 
1526     CharUnits align = getContext().getDeclAlign(variable);
1527     Address alloca =
1528       CreateMemTemp(variable->getType(), align, "block.captured-const");
1529 
1530     Builder.CreateStore(capture.getConstant(), alloca);
1531 
1532     setAddrOfLocalVar(variable, alloca);
1533   }
1534 
1535   // Save a spot to insert the debug information for all the DeclRefExprs.
1536   llvm::BasicBlock *entry = Builder.GetInsertBlock();
1537   llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint();
1538   --entry_ptr;
1539 
1540   if (IsLambdaConversionToBlock)
1541     EmitLambdaBlockInvokeBody();
1542   else {
1543     PGO->assignRegionCounters(GlobalDecl(blockDecl), fn);
1544     incrementProfileCounter(blockDecl->getBody());
1545     EmitStmt(blockDecl->getBody());
1546   }
1547 
1548   // Remember where we were...
1549   llvm::BasicBlock *resume = Builder.GetInsertBlock();
1550 
1551   // Go back to the entry.
1552   if (entry_ptr->getNextNonDebugInstruction())
1553     entry_ptr = entry_ptr->getNextNonDebugInstruction()->getIterator();
1554   else
1555     entry_ptr = entry->end();
1556   Builder.SetInsertPoint(entry, entry_ptr);
1557 
1558   // Emit debug information for all the DeclRefExprs.
1559   // FIXME: also for 'this'
1560   if (CGDebugInfo *DI = getDebugInfo()) {
1561     for (const auto &CI : blockDecl->captures()) {
1562       const VarDecl *variable = CI.getVariable();
1563       DI->EmitLocation(Builder, variable->getLocation());
1564 
1565       if (CGM.getCodeGenOpts().hasReducedDebugInfo()) {
1566         const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1567         if (capture.isConstant()) {
1568           auto addr = LocalDeclMap.find(variable)->second;
1569           (void)DI->EmitDeclareOfAutoVariable(
1570               variable, addr.emitRawPointer(*this), Builder);
1571           continue;
1572         }
1573 
1574         DI->EmitDeclareOfBlockDeclRefVariable(
1575             variable, BlockPointerDbgLoc, Builder, blockInfo,
1576             entry_ptr == entry->end() ? nullptr : &*entry_ptr);
1577       }
1578     }
1579     // Recover location if it was changed in the above loop.
1580     DI->EmitLocation(Builder,
1581                      cast<CompoundStmt>(blockDecl->getBody())->getRBracLoc());
1582   }
1583 
1584   // And resume where we left off.
1585   if (resume == nullptr)
1586     Builder.ClearInsertionPoint();
1587   else
1588     Builder.SetInsertPoint(resume);
1589 
1590   FinishFunction(cast<CompoundStmt>(blockDecl->getBody())->getRBracLoc());
1591 
1592   return fn;
1593 }
1594 
1595 static std::pair<BlockCaptureEntityKind, BlockFieldFlags>
1596 computeCopyInfoForBlockCapture(const BlockDecl::Capture &CI, QualType T,
1597                                const LangOptions &LangOpts) {
1598   if (CI.getCopyExpr()) {
1599     assert(!CI.isByRef());
1600     // don't bother computing flags
1601     return std::make_pair(BlockCaptureEntityKind::CXXRecord, BlockFieldFlags());
1602   }
1603   BlockFieldFlags Flags;
1604   if (CI.isEscapingByref()) {
1605     Flags = BLOCK_FIELD_IS_BYREF;
1606     if (T.isObjCGCWeak())
1607       Flags |= BLOCK_FIELD_IS_WEAK;
1608     return std::make_pair(BlockCaptureEntityKind::BlockObject, Flags);
1609   }
1610 
1611   if (T.hasAddressDiscriminatedPointerAuth())
1612     return std::make_pair(
1613         BlockCaptureEntityKind::AddressDiscriminatedPointerAuth, Flags);
1614 
1615   Flags = BLOCK_FIELD_IS_OBJECT;
1616   bool isBlockPointer = T->isBlockPointerType();
1617   if (isBlockPointer)
1618     Flags = BLOCK_FIELD_IS_BLOCK;
1619 
1620   switch (T.isNonTrivialToPrimitiveCopy()) {
1621   case QualType::PCK_Struct:
1622     return std::make_pair(BlockCaptureEntityKind::NonTrivialCStruct,
1623                           BlockFieldFlags());
1624   case QualType::PCK_ARCWeak:
1625     // We need to register __weak direct captures with the runtime.
1626     return std::make_pair(BlockCaptureEntityKind::ARCWeak, Flags);
1627   case QualType::PCK_ARCStrong:
1628     // We need to retain the copied value for __strong direct captures.
1629     // If it's a block pointer, we have to copy the block and assign that to
1630     // the destination pointer, so we might as well use _Block_object_assign.
1631     // Otherwise we can avoid that.
1632     return std::make_pair(!isBlockPointer ? BlockCaptureEntityKind::ARCStrong
1633                                           : BlockCaptureEntityKind::BlockObject,
1634                           Flags);
1635   case QualType::PCK_PtrAuth:
1636     return std::make_pair(
1637         BlockCaptureEntityKind::AddressDiscriminatedPointerAuth,
1638         BlockFieldFlags());
1639   case QualType::PCK_Trivial:
1640   case QualType::PCK_VolatileTrivial: {
1641     if (!T->isObjCRetainableType())
1642       // For all other types, the memcpy is fine.
1643       return std::make_pair(BlockCaptureEntityKind::None, BlockFieldFlags());
1644 
1645     // Honor the inert __unsafe_unretained qualifier, which doesn't actually
1646     // make it into the type system.
1647     if (T->isObjCInertUnsafeUnretainedType())
1648       return std::make_pair(BlockCaptureEntityKind::None, BlockFieldFlags());
1649 
1650     // Special rules for ARC captures:
1651     Qualifiers QS = T.getQualifiers();
1652 
1653     // Non-ARC captures of retainable pointers are strong and
1654     // therefore require a call to _Block_object_assign.
1655     if (!QS.getObjCLifetime() && !LangOpts.ObjCAutoRefCount)
1656       return std::make_pair(BlockCaptureEntityKind::BlockObject, Flags);
1657 
1658     // Otherwise the memcpy is fine.
1659     return std::make_pair(BlockCaptureEntityKind::None, BlockFieldFlags());
1660   }
1661   }
1662   llvm_unreachable("after exhaustive PrimitiveCopyKind switch");
1663 }
1664 
1665 namespace {
1666 /// Release a __block variable.
1667 struct CallBlockRelease final : EHScopeStack::Cleanup {
1668   Address Addr;
1669   BlockFieldFlags FieldFlags;
1670   bool LoadBlockVarAddr, CanThrow;
1671 
1672   CallBlockRelease(Address Addr, BlockFieldFlags Flags, bool LoadValue,
1673                    bool CT)
1674       : Addr(Addr), FieldFlags(Flags), LoadBlockVarAddr(LoadValue),
1675         CanThrow(CT) {}
1676 
1677   void Emit(CodeGenFunction &CGF, Flags flags) override {
1678     llvm::Value *BlockVarAddr;
1679     if (LoadBlockVarAddr) {
1680       BlockVarAddr = CGF.Builder.CreateLoad(Addr);
1681     } else {
1682       BlockVarAddr = Addr.emitRawPointer(CGF);
1683     }
1684 
1685     CGF.BuildBlockRelease(BlockVarAddr, FieldFlags, CanThrow);
1686   }
1687 };
1688 } // end anonymous namespace
1689 
1690 /// Check if \p T is a C++ class that has a destructor that can throw.
1691 bool CodeGenFunction::cxxDestructorCanThrow(QualType T) {
1692   if (const auto *RD = T->getAsCXXRecordDecl())
1693     if (const CXXDestructorDecl *DD = RD->getDestructor())
1694       return DD->getType()->castAs<FunctionProtoType>()->canThrow();
1695   return false;
1696 }
1697 
1698 // Return a string that has the information about a capture.
1699 static std::string getBlockCaptureStr(const CGBlockInfo::Capture &Cap,
1700                                       CaptureStrKind StrKind,
1701                                       CharUnits BlockAlignment,
1702                                       CodeGenModule &CGM) {
1703   std::string Str;
1704   ASTContext &Ctx = CGM.getContext();
1705   const BlockDecl::Capture &CI = *Cap.Cap;
1706   QualType CaptureTy = CI.getVariable()->getType();
1707 
1708   BlockCaptureEntityKind Kind;
1709   BlockFieldFlags Flags;
1710 
1711   // CaptureStrKind::Merged should be passed only when the operations and the
1712   // flags are the same for copy and dispose.
1713   assert((StrKind != CaptureStrKind::Merged ||
1714           (Cap.CopyKind == Cap.DisposeKind &&
1715            Cap.CopyFlags == Cap.DisposeFlags)) &&
1716          "different operations and flags");
1717 
1718   if (StrKind == CaptureStrKind::DisposeHelper) {
1719     Kind = Cap.DisposeKind;
1720     Flags = Cap.DisposeFlags;
1721   } else {
1722     Kind = Cap.CopyKind;
1723     Flags = Cap.CopyFlags;
1724   }
1725 
1726   switch (Kind) {
1727   case BlockCaptureEntityKind::CXXRecord: {
1728     Str += "c";
1729     SmallString<256> TyStr;
1730     llvm::raw_svector_ostream Out(TyStr);
1731     CGM.getCXXABI().getMangleContext().mangleCanonicalTypeName(CaptureTy, Out);
1732     Str += llvm::to_string(TyStr.size()) + TyStr.c_str();
1733     break;
1734   }
1735   case BlockCaptureEntityKind::ARCWeak:
1736     Str += "w";
1737     break;
1738   case BlockCaptureEntityKind::ARCStrong:
1739     Str += "s";
1740     break;
1741   case BlockCaptureEntityKind::AddressDiscriminatedPointerAuth: {
1742     auto PtrAuth = CaptureTy.getPointerAuth();
1743     assert(PtrAuth && PtrAuth.isAddressDiscriminated());
1744     Str += "p" + llvm::to_string(PtrAuth.getKey()) + "d" +
1745            llvm::to_string(PtrAuth.getExtraDiscriminator());
1746     break;
1747   }
1748   case BlockCaptureEntityKind::BlockObject: {
1749     const VarDecl *Var = CI.getVariable();
1750     unsigned F = Flags.getBitMask();
1751     if (F & BLOCK_FIELD_IS_BYREF) {
1752       Str += "r";
1753       if (F & BLOCK_FIELD_IS_WEAK)
1754         Str += "w";
1755       else {
1756         // If CaptureStrKind::Merged is passed, check both the copy expression
1757         // and the destructor.
1758         if (StrKind != CaptureStrKind::DisposeHelper) {
1759           if (Ctx.getBlockVarCopyInit(Var).canThrow())
1760             Str += "c";
1761         }
1762         if (StrKind != CaptureStrKind::CopyHelper) {
1763           if (CodeGenFunction::cxxDestructorCanThrow(CaptureTy))
1764             Str += "d";
1765         }
1766       }
1767     } else {
1768       assert((F & BLOCK_FIELD_IS_OBJECT) && "unexpected flag value");
1769       if (F == BLOCK_FIELD_IS_BLOCK)
1770         Str += "b";
1771       else
1772         Str += "o";
1773     }
1774     break;
1775   }
1776   case BlockCaptureEntityKind::NonTrivialCStruct: {
1777     bool IsVolatile = CaptureTy.isVolatileQualified();
1778     CharUnits Alignment = BlockAlignment.alignmentAtOffset(Cap.getOffset());
1779 
1780     Str += "n";
1781     std::string FuncStr;
1782     if (StrKind == CaptureStrKind::DisposeHelper)
1783       FuncStr = CodeGenFunction::getNonTrivialDestructorStr(
1784           CaptureTy, Alignment, IsVolatile, Ctx);
1785     else
1786       // If CaptureStrKind::Merged is passed, use the copy constructor string.
1787       // It has all the information that the destructor string has.
1788       FuncStr = CodeGenFunction::getNonTrivialCopyConstructorStr(
1789           CaptureTy, Alignment, IsVolatile, Ctx);
1790     // The underscore is necessary here because non-trivial copy constructor
1791     // and destructor strings can start with a number.
1792     Str += llvm::to_string(FuncStr.size()) + "_" + FuncStr;
1793     break;
1794   }
1795   case BlockCaptureEntityKind::None:
1796     break;
1797   }
1798 
1799   return Str;
1800 }
1801 
1802 static std::string getCopyDestroyHelperFuncName(
1803     const SmallVectorImpl<CGBlockInfo::Capture> &Captures,
1804     CharUnits BlockAlignment, CaptureStrKind StrKind, CodeGenModule &CGM) {
1805   assert((StrKind == CaptureStrKind::CopyHelper ||
1806           StrKind == CaptureStrKind::DisposeHelper) &&
1807          "unexpected CaptureStrKind");
1808   std::string Name = StrKind == CaptureStrKind::CopyHelper
1809                          ? "__copy_helper_block_"
1810                          : "__destroy_helper_block_";
1811   if (CGM.getLangOpts().Exceptions)
1812     Name += "e";
1813   if (CGM.getCodeGenOpts().ObjCAutoRefCountExceptions)
1814     Name += "a";
1815   Name += llvm::to_string(BlockAlignment.getQuantity()) + "_";
1816 
1817   for (auto &Cap : Captures) {
1818     if (Cap.isConstantOrTrivial())
1819       continue;
1820     Name += llvm::to_string(Cap.getOffset().getQuantity());
1821     Name += getBlockCaptureStr(Cap, StrKind, BlockAlignment, CGM);
1822   }
1823 
1824   return Name;
1825 }
1826 
1827 static void pushCaptureCleanup(BlockCaptureEntityKind CaptureKind,
1828                                Address Field, QualType CaptureType,
1829                                BlockFieldFlags Flags, bool ForCopyHelper,
1830                                VarDecl *Var, CodeGenFunction &CGF) {
1831   bool EHOnly = ForCopyHelper;
1832 
1833   switch (CaptureKind) {
1834   case BlockCaptureEntityKind::CXXRecord:
1835   case BlockCaptureEntityKind::ARCWeak:
1836   case BlockCaptureEntityKind::NonTrivialCStruct:
1837   case BlockCaptureEntityKind::ARCStrong: {
1838     if (CaptureType.isDestructedType() &&
1839         (!EHOnly || CGF.needsEHCleanup(CaptureType.isDestructedType()))) {
1840       CodeGenFunction::Destroyer *Destroyer =
1841           CaptureKind == BlockCaptureEntityKind::ARCStrong
1842               ? CodeGenFunction::destroyARCStrongImprecise
1843               : CGF.getDestroyer(CaptureType.isDestructedType());
1844       CleanupKind Kind =
1845           EHOnly ? EHCleanup
1846                  : CGF.getCleanupKind(CaptureType.isDestructedType());
1847       CGF.pushDestroy(Kind, Field, CaptureType, Destroyer, Kind & EHCleanup);
1848     }
1849     break;
1850   }
1851   case BlockCaptureEntityKind::BlockObject: {
1852     if (!EHOnly || CGF.getLangOpts().Exceptions) {
1853       CleanupKind Kind = EHOnly ? EHCleanup : NormalAndEHCleanup;
1854       // Calls to _Block_object_dispose along the EH path in the copy helper
1855       // function don't throw as newly-copied __block variables always have a
1856       // reference count of 2.
1857       bool CanThrow =
1858           !ForCopyHelper && CGF.cxxDestructorCanThrow(CaptureType);
1859       CGF.enterByrefCleanup(Kind, Field, Flags, /*LoadBlockVarAddr*/ true,
1860                             CanThrow);
1861     }
1862     break;
1863   }
1864   case BlockCaptureEntityKind::AddressDiscriminatedPointerAuth:
1865   case BlockCaptureEntityKind::None:
1866     break;
1867   }
1868 }
1869 
1870 static void setBlockHelperAttributesVisibility(bool CapturesNonExternalType,
1871                                                llvm::Function *Fn,
1872                                                const CGFunctionInfo &FI,
1873                                                CodeGenModule &CGM) {
1874   if (CapturesNonExternalType) {
1875     CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FI);
1876   } else {
1877     Fn->setVisibility(llvm::GlobalValue::HiddenVisibility);
1878     Fn->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
1879     CGM.SetLLVMFunctionAttributes(GlobalDecl(), FI, Fn, /*IsThunk=*/false);
1880     CGM.SetLLVMFunctionAttributesForDefinition(nullptr, Fn);
1881   }
1882 }
1883 /// Generate the copy-helper function for a block closure object:
1884 ///   static void block_copy_helper(block_t *dst, block_t *src);
1885 /// The runtime will have previously initialized 'dst' by doing a
1886 /// bit-copy of 'src'.
1887 ///
1888 /// Note that this copies an entire block closure object to the heap;
1889 /// it should not be confused with a 'byref copy helper', which moves
1890 /// the contents of an individual __block variable to the heap.
1891 llvm::Constant *
1892 CodeGenFunction::GenerateCopyHelperFunction(const CGBlockInfo &blockInfo) {
1893   std::string FuncName = getCopyDestroyHelperFuncName(
1894       blockInfo.SortedCaptures, blockInfo.BlockAlign,
1895       CaptureStrKind::CopyHelper, CGM);
1896 
1897   if (llvm::GlobalValue *Func = CGM.getModule().getNamedValue(FuncName))
1898     return Func;
1899 
1900   ASTContext &C = getContext();
1901 
1902   QualType ReturnTy = C.VoidTy;
1903 
1904   FunctionArgList args;
1905   ImplicitParamDecl DstDecl(C, C.VoidPtrTy, ImplicitParamKind::Other);
1906   args.push_back(&DstDecl);
1907   ImplicitParamDecl SrcDecl(C, C.VoidPtrTy, ImplicitParamKind::Other);
1908   args.push_back(&SrcDecl);
1909 
1910   const CGFunctionInfo &FI =
1911       CGM.getTypes().arrangeBuiltinFunctionDeclaration(ReturnTy, args);
1912 
1913   // FIXME: it would be nice if these were mergeable with things with
1914   // identical semantics.
1915   llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI);
1916 
1917   llvm::Function *Fn =
1918     llvm::Function::Create(LTy, llvm::GlobalValue::LinkOnceODRLinkage,
1919                            FuncName, &CGM.getModule());
1920   if (CGM.supportsCOMDAT())
1921     Fn->setComdat(CGM.getModule().getOrInsertComdat(FuncName));
1922 
1923   SmallVector<QualType, 2> ArgTys;
1924   ArgTys.push_back(C.VoidPtrTy);
1925   ArgTys.push_back(C.VoidPtrTy);
1926 
1927   setBlockHelperAttributesVisibility(blockInfo.CapturesNonExternalType, Fn, FI,
1928                                      CGM);
1929   StartFunction(GlobalDecl(), ReturnTy, Fn, FI, args);
1930   auto AL = ApplyDebugLocation::CreateArtificial(*this);
1931 
1932   Address src = GetAddrOfLocalVar(&SrcDecl);
1933   src = Address(Builder.CreateLoad(src), blockInfo.StructureType,
1934                 blockInfo.BlockAlign);
1935 
1936   Address dst = GetAddrOfLocalVar(&DstDecl);
1937   dst = Address(Builder.CreateLoad(dst), blockInfo.StructureType,
1938                 blockInfo.BlockAlign);
1939 
1940   for (auto &capture : blockInfo.SortedCaptures) {
1941     if (capture.isConstantOrTrivial())
1942       continue;
1943 
1944     const BlockDecl::Capture &CI = *capture.Cap;
1945     QualType captureType = CI.getVariable()->getType();
1946     BlockFieldFlags flags = capture.CopyFlags;
1947 
1948     unsigned index = capture.getIndex();
1949     Address srcField = Builder.CreateStructGEP(src, index);
1950     Address dstField = Builder.CreateStructGEP(dst, index);
1951 
1952     switch (capture.CopyKind) {
1953     case BlockCaptureEntityKind::CXXRecord:
1954       // If there's an explicit copy expression, we do that.
1955       assert(CI.getCopyExpr() && "copy expression for variable is missing");
1956       EmitSynthesizedCXXCopyCtor(dstField, srcField, CI.getCopyExpr());
1957       break;
1958     case BlockCaptureEntityKind::ARCWeak:
1959       EmitARCCopyWeak(dstField, srcField);
1960       break;
1961     case BlockCaptureEntityKind::AddressDiscriminatedPointerAuth: {
1962       QualType Type = CI.getVariable()->getType();
1963       PointerAuthQualifier PointerAuth = Type.getPointerAuth();
1964       assert(PointerAuth && PointerAuth.isAddressDiscriminated());
1965       EmitPointerAuthCopy(PointerAuth, Type, dstField, srcField);
1966       // We don't need to push cleanups for ptrauth types.
1967       continue;
1968     }
1969     case BlockCaptureEntityKind::NonTrivialCStruct: {
1970       // If this is a C struct that requires non-trivial copy construction,
1971       // emit a call to its copy constructor.
1972       QualType varType = CI.getVariable()->getType();
1973       callCStructCopyConstructor(MakeAddrLValue(dstField, varType),
1974                                  MakeAddrLValue(srcField, varType));
1975       break;
1976     }
1977     case BlockCaptureEntityKind::ARCStrong: {
1978       llvm::Value *srcValue = Builder.CreateLoad(srcField, "blockcopy.src");
1979       // At -O0, store null into the destination field (so that the
1980       // storeStrong doesn't over-release) and then call storeStrong.
1981       // This is a workaround to not having an initStrong call.
1982       if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
1983         auto *ty = cast<llvm::PointerType>(srcValue->getType());
1984         llvm::Value *null = llvm::ConstantPointerNull::get(ty);
1985         Builder.CreateStore(null, dstField);
1986         EmitARCStoreStrongCall(dstField, srcValue, true);
1987 
1988       // With optimization enabled, take advantage of the fact that
1989       // the blocks runtime guarantees a memcpy of the block data, and
1990       // just emit a retain of the src field.
1991       } else {
1992         EmitARCRetainNonBlock(srcValue);
1993 
1994         // Unless EH cleanup is required, we don't need this anymore, so kill
1995         // it. It's not quite worth the annoyance to avoid creating it in the
1996         // first place.
1997         if (!needsEHCleanup(captureType.isDestructedType()))
1998           if (auto *I =
1999                   cast_or_null<llvm::Instruction>(dstField.getBasePointer()))
2000             I->eraseFromParent();
2001       }
2002       break;
2003     }
2004     case BlockCaptureEntityKind::BlockObject: {
2005       llvm::Value *srcValue = Builder.CreateLoad(srcField, "blockcopy.src");
2006       llvm::Value *dstAddr = dstField.emitRawPointer(*this);
2007       llvm::Value *args[] = {
2008         dstAddr, srcValue, llvm::ConstantInt::get(Int32Ty, flags.getBitMask())
2009       };
2010 
2011       if (CI.isByRef() && C.getBlockVarCopyInit(CI.getVariable()).canThrow())
2012         EmitRuntimeCallOrInvoke(CGM.getBlockObjectAssign(), args);
2013       else
2014         EmitNounwindRuntimeCall(CGM.getBlockObjectAssign(), args);
2015       break;
2016     }
2017     case BlockCaptureEntityKind::None:
2018       continue;
2019     }
2020 
2021     // Ensure that we destroy the copied object if an exception is thrown later
2022     // in the helper function.
2023     pushCaptureCleanup(capture.CopyKind, dstField, captureType, flags,
2024                        /*ForCopyHelper*/ true, CI.getVariable(), *this);
2025   }
2026 
2027   FinishFunction();
2028 
2029   return Fn;
2030 }
2031 
2032 static BlockFieldFlags
2033 getBlockFieldFlagsForObjCObjectPointer(const BlockDecl::Capture &CI,
2034                                        QualType T) {
2035   BlockFieldFlags Flags = BLOCK_FIELD_IS_OBJECT;
2036   if (T->isBlockPointerType())
2037     Flags = BLOCK_FIELD_IS_BLOCK;
2038   return Flags;
2039 }
2040 
2041 static std::pair<BlockCaptureEntityKind, BlockFieldFlags>
2042 computeDestroyInfoForBlockCapture(const BlockDecl::Capture &CI, QualType T,
2043                                   const LangOptions &LangOpts) {
2044   if (CI.isEscapingByref()) {
2045     BlockFieldFlags Flags = BLOCK_FIELD_IS_BYREF;
2046     if (T.isObjCGCWeak())
2047       Flags |= BLOCK_FIELD_IS_WEAK;
2048     return std::make_pair(BlockCaptureEntityKind::BlockObject, Flags);
2049   }
2050 
2051   switch (T.isDestructedType()) {
2052   case QualType::DK_cxx_destructor:
2053     return std::make_pair(BlockCaptureEntityKind::CXXRecord, BlockFieldFlags());
2054   case QualType::DK_objc_strong_lifetime:
2055     // Use objc_storeStrong for __strong direct captures; the
2056     // dynamic tools really like it when we do this.
2057     return std::make_pair(BlockCaptureEntityKind::ARCStrong,
2058                           getBlockFieldFlagsForObjCObjectPointer(CI, T));
2059   case QualType::DK_objc_weak_lifetime:
2060     // Support __weak direct captures.
2061     return std::make_pair(BlockCaptureEntityKind::ARCWeak,
2062                           getBlockFieldFlagsForObjCObjectPointer(CI, T));
2063   case QualType::DK_nontrivial_c_struct:
2064     return std::make_pair(BlockCaptureEntityKind::NonTrivialCStruct,
2065                           BlockFieldFlags());
2066   case QualType::DK_none: {
2067     // Non-ARC captures are strong, and we need to use _Block_object_dispose.
2068     // But honor the inert __unsafe_unretained qualifier, which doesn't actually
2069     // make it into the type system.
2070     if (T->isObjCRetainableType() && !T.getQualifiers().hasObjCLifetime() &&
2071         !LangOpts.ObjCAutoRefCount && !T->isObjCInertUnsafeUnretainedType())
2072       return std::make_pair(BlockCaptureEntityKind::BlockObject,
2073                             getBlockFieldFlagsForObjCObjectPointer(CI, T));
2074     // Otherwise, we have nothing to do.
2075     return std::make_pair(BlockCaptureEntityKind::None, BlockFieldFlags());
2076   }
2077   }
2078   llvm_unreachable("after exhaustive DestructionKind switch");
2079 }
2080 
2081 /// Generate the destroy-helper function for a block closure object:
2082 ///   static void block_destroy_helper(block_t *theBlock);
2083 ///
2084 /// Note that this destroys a heap-allocated block closure object;
2085 /// it should not be confused with a 'byref destroy helper', which
2086 /// destroys the heap-allocated contents of an individual __block
2087 /// variable.
2088 llvm::Constant *
2089 CodeGenFunction::GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo) {
2090   std::string FuncName = getCopyDestroyHelperFuncName(
2091       blockInfo.SortedCaptures, blockInfo.BlockAlign,
2092       CaptureStrKind::DisposeHelper, CGM);
2093 
2094   if (llvm::GlobalValue *Func = CGM.getModule().getNamedValue(FuncName))
2095     return Func;
2096 
2097   ASTContext &C = getContext();
2098 
2099   QualType ReturnTy = C.VoidTy;
2100 
2101   FunctionArgList args;
2102   ImplicitParamDecl SrcDecl(C, C.VoidPtrTy, ImplicitParamKind::Other);
2103   args.push_back(&SrcDecl);
2104 
2105   const CGFunctionInfo &FI =
2106       CGM.getTypes().arrangeBuiltinFunctionDeclaration(ReturnTy, args);
2107 
2108   // FIXME: We'd like to put these into a mergable by content, with
2109   // internal linkage.
2110   llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI);
2111 
2112   llvm::Function *Fn =
2113     llvm::Function::Create(LTy, llvm::GlobalValue::LinkOnceODRLinkage,
2114                            FuncName, &CGM.getModule());
2115   if (CGM.supportsCOMDAT())
2116     Fn->setComdat(CGM.getModule().getOrInsertComdat(FuncName));
2117 
2118   SmallVector<QualType, 1> ArgTys;
2119   ArgTys.push_back(C.VoidPtrTy);
2120 
2121   setBlockHelperAttributesVisibility(blockInfo.CapturesNonExternalType, Fn, FI,
2122                                      CGM);
2123   StartFunction(GlobalDecl(), ReturnTy, Fn, FI, args);
2124   markAsIgnoreThreadCheckingAtRuntime(Fn);
2125 
2126   auto AL = ApplyDebugLocation::CreateArtificial(*this);
2127 
2128   Address src = GetAddrOfLocalVar(&SrcDecl);
2129   src = Address(Builder.CreateLoad(src), blockInfo.StructureType,
2130                 blockInfo.BlockAlign);
2131 
2132   CodeGenFunction::RunCleanupsScope cleanups(*this);
2133 
2134   for (auto &capture : blockInfo.SortedCaptures) {
2135     if (capture.isConstantOrTrivial())
2136       continue;
2137 
2138     const BlockDecl::Capture &CI = *capture.Cap;
2139     BlockFieldFlags flags = capture.DisposeFlags;
2140 
2141     Address srcField = Builder.CreateStructGEP(src, capture.getIndex());
2142 
2143     pushCaptureCleanup(capture.DisposeKind, srcField,
2144                        CI.getVariable()->getType(), flags,
2145                        /*ForCopyHelper*/ false, CI.getVariable(), *this);
2146   }
2147 
2148   cleanups.ForceCleanup();
2149 
2150   FinishFunction();
2151 
2152   return Fn;
2153 }
2154 
2155 namespace {
2156 
2157 /// Emits the copy/dispose helper functions for a __block object of id type.
2158 class ObjectByrefHelpers final : public BlockByrefHelpers {
2159   BlockFieldFlags Flags;
2160 
2161 public:
2162   ObjectByrefHelpers(CharUnits alignment, BlockFieldFlags flags)
2163     : BlockByrefHelpers(alignment), Flags(flags) {}
2164 
2165   void emitCopy(CodeGenFunction &CGF, Address destField,
2166                 Address srcField) override {
2167     destField = destField.withElementType(CGF.Int8Ty);
2168 
2169     srcField = srcField.withElementType(CGF.Int8PtrTy);
2170     llvm::Value *srcValue = CGF.Builder.CreateLoad(srcField);
2171 
2172     unsigned flags = (Flags | BLOCK_BYREF_CALLER).getBitMask();
2173 
2174     llvm::Value *flagsVal = llvm::ConstantInt::get(CGF.Int32Ty, flags);
2175     llvm::FunctionCallee fn = CGF.CGM.getBlockObjectAssign();
2176 
2177     llvm::Value *args[] = {destField.emitRawPointer(CGF), srcValue, flagsVal};
2178     CGF.EmitNounwindRuntimeCall(fn, args);
2179   }
2180 
2181   void emitDispose(CodeGenFunction &CGF, Address field) override {
2182     field = field.withElementType(CGF.Int8PtrTy);
2183     llvm::Value *value = CGF.Builder.CreateLoad(field);
2184 
2185     CGF.BuildBlockRelease(value, Flags | BLOCK_BYREF_CALLER, false);
2186   }
2187 
2188   void profileImpl(llvm::FoldingSetNodeID &id) const override {
2189     id.AddInteger(Flags.getBitMask());
2190   }
2191 };
2192 
2193 /// Emits the copy/dispose helpers for an ARC __block __weak variable.
2194 class ARCWeakByrefHelpers final : public BlockByrefHelpers {
2195 public:
2196   ARCWeakByrefHelpers(CharUnits alignment) : BlockByrefHelpers(alignment) {}
2197 
2198   void emitCopy(CodeGenFunction &CGF, Address destField,
2199                 Address srcField) override {
2200     CGF.EmitARCMoveWeak(destField, srcField);
2201   }
2202 
2203   void emitDispose(CodeGenFunction &CGF, Address field) override {
2204     CGF.EmitARCDestroyWeak(field);
2205   }
2206 
2207   void profileImpl(llvm::FoldingSetNodeID &id) const override {
2208     // 0 is distinguishable from all pointers and byref flags
2209     id.AddInteger(0);
2210   }
2211 };
2212 
2213 /// Emits the copy/dispose helpers for an ARC __block __strong variable
2214 /// that's not of block-pointer type.
2215 class ARCStrongByrefHelpers final : public BlockByrefHelpers {
2216 public:
2217   ARCStrongByrefHelpers(CharUnits alignment) : BlockByrefHelpers(alignment) {}
2218 
2219   void emitCopy(CodeGenFunction &CGF, Address destField,
2220                 Address srcField) override {
2221     // Do a "move" by copying the value and then zeroing out the old
2222     // variable.
2223 
2224     llvm::Value *value = CGF.Builder.CreateLoad(srcField);
2225 
2226     llvm::Value *null =
2227       llvm::ConstantPointerNull::get(cast<llvm::PointerType>(value->getType()));
2228 
2229     if (CGF.CGM.getCodeGenOpts().OptimizationLevel == 0) {
2230       CGF.Builder.CreateStore(null, destField);
2231       CGF.EmitARCStoreStrongCall(destField, value, /*ignored*/ true);
2232       CGF.EmitARCStoreStrongCall(srcField, null, /*ignored*/ true);
2233       return;
2234     }
2235     CGF.Builder.CreateStore(value, destField);
2236     CGF.Builder.CreateStore(null, srcField);
2237   }
2238 
2239   void emitDispose(CodeGenFunction &CGF, Address field) override {
2240     CGF.EmitARCDestroyStrong(field, ARCImpreciseLifetime);
2241   }
2242 
2243   void profileImpl(llvm::FoldingSetNodeID &id) const override {
2244     // 1 is distinguishable from all pointers and byref flags
2245     id.AddInteger(1);
2246   }
2247 };
2248 
2249 /// Emits the copy/dispose helpers for an ARC __block __strong
2250 /// variable that's of block-pointer type.
2251 class ARCStrongBlockByrefHelpers final : public BlockByrefHelpers {
2252 public:
2253   ARCStrongBlockByrefHelpers(CharUnits alignment)
2254     : BlockByrefHelpers(alignment) {}
2255 
2256   void emitCopy(CodeGenFunction &CGF, Address destField,
2257                 Address srcField) override {
2258     // Do the copy with objc_retainBlock; that's all that
2259     // _Block_object_assign would do anyway, and we'd have to pass the
2260     // right arguments to make sure it doesn't get no-op'ed.
2261     llvm::Value *oldValue = CGF.Builder.CreateLoad(srcField);
2262     llvm::Value *copy = CGF.EmitARCRetainBlock(oldValue, /*mandatory*/ true);
2263     CGF.Builder.CreateStore(copy, destField);
2264   }
2265 
2266   void emitDispose(CodeGenFunction &CGF, Address field) override {
2267     CGF.EmitARCDestroyStrong(field, ARCImpreciseLifetime);
2268   }
2269 
2270   void profileImpl(llvm::FoldingSetNodeID &id) const override {
2271     // 2 is distinguishable from all pointers and byref flags
2272     id.AddInteger(2);
2273   }
2274 };
2275 
2276 /// Emits the copy/dispose helpers for a __block variable with a
2277 /// nontrivial copy constructor or destructor.
2278 class CXXByrefHelpers final : public BlockByrefHelpers {
2279   QualType VarType;
2280   const Expr *CopyExpr;
2281 
2282 public:
2283   CXXByrefHelpers(CharUnits alignment, QualType type,
2284                   const Expr *copyExpr)
2285     : BlockByrefHelpers(alignment), VarType(type), CopyExpr(copyExpr) {}
2286 
2287   bool needsCopy() const override { return CopyExpr != nullptr; }
2288   void emitCopy(CodeGenFunction &CGF, Address destField,
2289                 Address srcField) override {
2290     if (!CopyExpr) return;
2291     CGF.EmitSynthesizedCXXCopyCtor(destField, srcField, CopyExpr);
2292   }
2293 
2294   void emitDispose(CodeGenFunction &CGF, Address field) override {
2295     EHScopeStack::stable_iterator cleanupDepth = CGF.EHStack.stable_begin();
2296     CGF.PushDestructorCleanup(VarType, field);
2297     CGF.PopCleanupBlocks(cleanupDepth);
2298   }
2299 
2300   void profileImpl(llvm::FoldingSetNodeID &id) const override {
2301     id.AddPointer(VarType.getCanonicalType().getAsOpaquePtr());
2302   }
2303 };
2304 
2305 /// Emits the copy/dispose helpers for a __block variable with
2306 /// address-discriminated pointer authentication.
2307 class AddressDiscriminatedByrefHelpers final : public BlockByrefHelpers {
2308   QualType VarType;
2309 
2310 public:
2311   AddressDiscriminatedByrefHelpers(CharUnits Alignment, QualType Type)
2312       : BlockByrefHelpers(Alignment), VarType(Type) {
2313     assert(Type.hasAddressDiscriminatedPointerAuth());
2314   }
2315 
2316   void emitCopy(CodeGenFunction &CGF, Address DestField,
2317                 Address SrcField) override {
2318     CGF.EmitPointerAuthCopy(VarType.getPointerAuth(), VarType, DestField,
2319                             SrcField);
2320   }
2321 
2322   bool needsDispose() const override { return false; }
2323   void emitDispose(CodeGenFunction &CGF, Address Field) override {
2324     llvm_unreachable("should never be called");
2325   }
2326 
2327   void profileImpl(llvm::FoldingSetNodeID &ID) const override {
2328     ID.AddPointer(VarType.getCanonicalType().getAsOpaquePtr());
2329   }
2330 };
2331 
2332 /// Emits the copy/dispose helpers for a __block variable that is a non-trivial
2333 /// C struct.
2334 class NonTrivialCStructByrefHelpers final : public BlockByrefHelpers {
2335   QualType VarType;
2336 
2337 public:
2338   NonTrivialCStructByrefHelpers(CharUnits alignment, QualType type)
2339     : BlockByrefHelpers(alignment), VarType(type) {}
2340 
2341   void emitCopy(CodeGenFunction &CGF, Address destField,
2342                 Address srcField) override {
2343     CGF.callCStructMoveConstructor(CGF.MakeAddrLValue(destField, VarType),
2344                                    CGF.MakeAddrLValue(srcField, VarType));
2345   }
2346 
2347   bool needsDispose() const override {
2348     return VarType.isDestructedType();
2349   }
2350 
2351   void emitDispose(CodeGenFunction &CGF, Address field) override {
2352     EHScopeStack::stable_iterator cleanupDepth = CGF.EHStack.stable_begin();
2353     CGF.pushDestroy(VarType.isDestructedType(), field, VarType);
2354     CGF.PopCleanupBlocks(cleanupDepth);
2355   }
2356 
2357   void profileImpl(llvm::FoldingSetNodeID &id) const override {
2358     id.AddPointer(VarType.getCanonicalType().getAsOpaquePtr());
2359   }
2360 };
2361 } // end anonymous namespace
2362 
2363 static llvm::Constant *
2364 generateByrefCopyHelper(CodeGenFunction &CGF, const BlockByrefInfo &byrefInfo,
2365                         BlockByrefHelpers &generator) {
2366   ASTContext &Context = CGF.getContext();
2367 
2368   QualType ReturnTy = Context.VoidTy;
2369 
2370   FunctionArgList args;
2371   ImplicitParamDecl Dst(Context, Context.VoidPtrTy, ImplicitParamKind::Other);
2372   args.push_back(&Dst);
2373 
2374   ImplicitParamDecl Src(Context, Context.VoidPtrTy, ImplicitParamKind::Other);
2375   args.push_back(&Src);
2376 
2377   const CGFunctionInfo &FI =
2378       CGF.CGM.getTypes().arrangeBuiltinFunctionDeclaration(ReturnTy, args);
2379 
2380   llvm::FunctionType *LTy = CGF.CGM.getTypes().GetFunctionType(FI);
2381 
2382   // FIXME: We'd like to put these into a mergable by content, with
2383   // internal linkage.
2384   llvm::Function *Fn =
2385     llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
2386                            "__Block_byref_object_copy_", &CGF.CGM.getModule());
2387 
2388   SmallVector<QualType, 2> ArgTys;
2389   ArgTys.push_back(Context.VoidPtrTy);
2390   ArgTys.push_back(Context.VoidPtrTy);
2391 
2392   CGF.CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FI);
2393 
2394   CGF.StartFunction(GlobalDecl(), ReturnTy, Fn, FI, args);
2395     // Create a scope with an artificial location for the body of this function.
2396   auto AL = ApplyDebugLocation::CreateArtificial(CGF);
2397 
2398   if (generator.needsCopy()) {
2399     // dst->x
2400     Address destField = CGF.GetAddrOfLocalVar(&Dst);
2401     destField = Address(CGF.Builder.CreateLoad(destField), byrefInfo.Type,
2402                         byrefInfo.ByrefAlignment);
2403     destField =
2404         CGF.emitBlockByrefAddress(destField, byrefInfo, false, "dest-object");
2405 
2406     // src->x
2407     Address srcField = CGF.GetAddrOfLocalVar(&Src);
2408     srcField = Address(CGF.Builder.CreateLoad(srcField), byrefInfo.Type,
2409                        byrefInfo.ByrefAlignment);
2410     srcField =
2411         CGF.emitBlockByrefAddress(srcField, byrefInfo, false, "src-object");
2412 
2413     generator.emitCopy(CGF, destField, srcField);
2414   }
2415 
2416   CGF.FinishFunction();
2417 
2418   return Fn;
2419 }
2420 
2421 /// Build the copy helper for a __block variable.
2422 static llvm::Constant *buildByrefCopyHelper(CodeGenModule &CGM,
2423                                             const BlockByrefInfo &byrefInfo,
2424                                             BlockByrefHelpers &generator) {
2425   CodeGenFunction CGF(CGM);
2426   return generateByrefCopyHelper(CGF, byrefInfo, generator);
2427 }
2428 
2429 /// Generate code for a __block variable's dispose helper.
2430 static llvm::Constant *
2431 generateByrefDisposeHelper(CodeGenFunction &CGF,
2432                            const BlockByrefInfo &byrefInfo,
2433                            BlockByrefHelpers &generator) {
2434   ASTContext &Context = CGF.getContext();
2435   QualType R = Context.VoidTy;
2436 
2437   FunctionArgList args;
2438   ImplicitParamDecl Src(CGF.getContext(), Context.VoidPtrTy,
2439                         ImplicitParamKind::Other);
2440   args.push_back(&Src);
2441 
2442   const CGFunctionInfo &FI =
2443     CGF.CGM.getTypes().arrangeBuiltinFunctionDeclaration(R, args);
2444 
2445   llvm::FunctionType *LTy = CGF.CGM.getTypes().GetFunctionType(FI);
2446 
2447   // FIXME: We'd like to put these into a mergable by content, with
2448   // internal linkage.
2449   llvm::Function *Fn =
2450     llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
2451                            "__Block_byref_object_dispose_",
2452                            &CGF.CGM.getModule());
2453 
2454   SmallVector<QualType, 1> ArgTys;
2455   ArgTys.push_back(Context.VoidPtrTy);
2456 
2457   CGF.CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FI);
2458 
2459   CGF.StartFunction(GlobalDecl(), R, Fn, FI, args);
2460     // Create a scope with an artificial location for the body of this function.
2461   auto AL = ApplyDebugLocation::CreateArtificial(CGF);
2462 
2463   if (generator.needsDispose()) {
2464     Address addr = CGF.GetAddrOfLocalVar(&Src);
2465     addr = Address(CGF.Builder.CreateLoad(addr), byrefInfo.Type,
2466                    byrefInfo.ByrefAlignment);
2467     addr = CGF.emitBlockByrefAddress(addr, byrefInfo, false, "object");
2468 
2469     generator.emitDispose(CGF, addr);
2470   }
2471 
2472   CGF.FinishFunction();
2473 
2474   return Fn;
2475 }
2476 
2477 /// Build the dispose helper for a __block variable.
2478 static llvm::Constant *buildByrefDisposeHelper(CodeGenModule &CGM,
2479                                                const BlockByrefInfo &byrefInfo,
2480                                                BlockByrefHelpers &generator) {
2481   CodeGenFunction CGF(CGM);
2482   return generateByrefDisposeHelper(CGF, byrefInfo, generator);
2483 }
2484 
2485 /// Lazily build the copy and dispose helpers for a __block variable
2486 /// with the given information.
2487 template <class T>
2488 static T *buildByrefHelpers(CodeGenModule &CGM, const BlockByrefInfo &byrefInfo,
2489                             T &&generator) {
2490   llvm::FoldingSetNodeID id;
2491   generator.Profile(id);
2492 
2493   void *insertPos;
2494   BlockByrefHelpers *node
2495     = CGM.ByrefHelpersCache.FindNodeOrInsertPos(id, insertPos);
2496   if (node) return static_cast<T*>(node);
2497 
2498   generator.CopyHelper = buildByrefCopyHelper(CGM, byrefInfo, generator);
2499   generator.DisposeHelper = buildByrefDisposeHelper(CGM, byrefInfo, generator);
2500 
2501   T *copy = new (CGM.getContext()) T(std::forward<T>(generator));
2502   CGM.ByrefHelpersCache.InsertNode(copy, insertPos);
2503   return copy;
2504 }
2505 
2506 /// Build the copy and dispose helpers for the given __block variable
2507 /// emission.  Places the helpers in the global cache.  Returns null
2508 /// if no helpers are required.
2509 BlockByrefHelpers *
2510 CodeGenFunction::buildByrefHelpers(llvm::StructType &byrefType,
2511                                    const AutoVarEmission &emission) {
2512   const VarDecl &var = *emission.Variable;
2513   assert(var.isEscapingByref() &&
2514          "only escaping __block variables need byref helpers");
2515 
2516   QualType type = var.getType();
2517 
2518   auto &byrefInfo = getBlockByrefInfo(&var);
2519 
2520   // The alignment we care about for the purposes of uniquing byref
2521   // helpers is the alignment of the actual byref value field.
2522   CharUnits valueAlignment =
2523     byrefInfo.ByrefAlignment.alignmentAtOffset(byrefInfo.FieldOffset);
2524 
2525   if (const CXXRecordDecl *record = type->getAsCXXRecordDecl()) {
2526     const Expr *copyExpr =
2527         CGM.getContext().getBlockVarCopyInit(&var).getCopyExpr();
2528     if (!copyExpr && record->hasTrivialDestructor()) return nullptr;
2529 
2530     return ::buildByrefHelpers(
2531         CGM, byrefInfo, CXXByrefHelpers(valueAlignment, type, copyExpr));
2532   }
2533   if (type.hasAddressDiscriminatedPointerAuth()) {
2534     return ::buildByrefHelpers(
2535         CGM, byrefInfo, AddressDiscriminatedByrefHelpers(valueAlignment, type));
2536   }
2537   // If type is a non-trivial C struct type that is non-trivial to
2538   // destructly move or destroy, build the copy and dispose helpers.
2539   if (type.isNonTrivialToPrimitiveDestructiveMove() == QualType::PCK_Struct ||
2540       type.isDestructedType() == QualType::DK_nontrivial_c_struct)
2541     return ::buildByrefHelpers(
2542         CGM, byrefInfo, NonTrivialCStructByrefHelpers(valueAlignment, type));
2543 
2544   // Otherwise, if we don't have a retainable type, there's nothing to do.
2545   // that the runtime does extra copies.
2546   if (!type->isObjCRetainableType()) return nullptr;
2547 
2548   Qualifiers qs = type.getQualifiers();
2549 
2550   // If we have lifetime, that dominates.
2551   if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) {
2552     switch (lifetime) {
2553     case Qualifiers::OCL_None: llvm_unreachable("impossible");
2554 
2555     // These are just bits as far as the runtime is concerned.
2556     case Qualifiers::OCL_ExplicitNone:
2557     case Qualifiers::OCL_Autoreleasing:
2558       return nullptr;
2559 
2560     // Tell the runtime that this is ARC __weak, called by the
2561     // byref routines.
2562     case Qualifiers::OCL_Weak:
2563       return ::buildByrefHelpers(CGM, byrefInfo,
2564                                  ARCWeakByrefHelpers(valueAlignment));
2565 
2566     // ARC __strong __block variables need to be retained.
2567     case Qualifiers::OCL_Strong:
2568       // Block pointers need to be copied, and there's no direct
2569       // transfer possible.
2570       if (type->isBlockPointerType()) {
2571         return ::buildByrefHelpers(CGM, byrefInfo,
2572                                    ARCStrongBlockByrefHelpers(valueAlignment));
2573 
2574       // Otherwise, we transfer ownership of the retain from the stack
2575       // to the heap.
2576       } else {
2577         return ::buildByrefHelpers(CGM, byrefInfo,
2578                                    ARCStrongByrefHelpers(valueAlignment));
2579       }
2580     }
2581     llvm_unreachable("fell out of lifetime switch!");
2582   }
2583 
2584   BlockFieldFlags flags;
2585   if (type->isBlockPointerType()) {
2586     flags |= BLOCK_FIELD_IS_BLOCK;
2587   } else if (CGM.getContext().isObjCNSObjectType(type) ||
2588              type->isObjCObjectPointerType()) {
2589     flags |= BLOCK_FIELD_IS_OBJECT;
2590   } else {
2591     return nullptr;
2592   }
2593 
2594   if (type.isObjCGCWeak())
2595     flags |= BLOCK_FIELD_IS_WEAK;
2596 
2597   return ::buildByrefHelpers(CGM, byrefInfo,
2598                              ObjectByrefHelpers(valueAlignment, flags));
2599 }
2600 
2601 Address CodeGenFunction::emitBlockByrefAddress(Address baseAddr,
2602                                                const VarDecl *var,
2603                                                bool followForward) {
2604   auto &info = getBlockByrefInfo(var);
2605   return emitBlockByrefAddress(baseAddr, info, followForward, var->getName());
2606 }
2607 
2608 Address CodeGenFunction::emitBlockByrefAddress(Address baseAddr,
2609                                                const BlockByrefInfo &info,
2610                                                bool followForward,
2611                                                const llvm::Twine &name) {
2612   // Chase the forwarding address if requested.
2613   if (followForward) {
2614     Address forwardingAddr = Builder.CreateStructGEP(baseAddr, 1, "forwarding");
2615     baseAddr = Address(Builder.CreateLoad(forwardingAddr), info.Type,
2616                        info.ByrefAlignment);
2617   }
2618 
2619   return Builder.CreateStructGEP(baseAddr, info.FieldIndex, name);
2620 }
2621 
2622 /// BuildByrefInfo - This routine changes a __block variable declared as T x
2623 ///   into:
2624 ///
2625 ///      struct {
2626 ///        void *__isa;
2627 ///        void *__forwarding;
2628 ///        int32_t __flags;
2629 ///        int32_t __size;
2630 ///        void *__copy_helper;       // only if needed
2631 ///        void *__destroy_helper;    // only if needed
2632 ///        void *__byref_variable_layout;// only if needed
2633 ///        char padding[X];           // only if needed
2634 ///        T x;
2635 ///      } x
2636 ///
2637 const BlockByrefInfo &CodeGenFunction::getBlockByrefInfo(const VarDecl *D) {
2638   auto it = BlockByrefInfos.find(D);
2639   if (it != BlockByrefInfos.end())
2640     return it->second;
2641 
2642   QualType Ty = D->getType();
2643 
2644   CharUnits size;
2645   SmallVector<llvm::Type *, 8> types;
2646 
2647   // void *__isa;
2648   types.push_back(VoidPtrTy);
2649   size += getPointerSize();
2650 
2651   // void *__forwarding;
2652   types.push_back(VoidPtrTy);
2653   size += getPointerSize();
2654 
2655   // int32_t __flags;
2656   types.push_back(Int32Ty);
2657   size += CharUnits::fromQuantity(4);
2658 
2659   // int32_t __size;
2660   types.push_back(Int32Ty);
2661   size += CharUnits::fromQuantity(4);
2662 
2663   // Note that this must match *exactly* the logic in buildByrefHelpers.
2664   bool hasCopyAndDispose = getContext().BlockRequiresCopying(Ty, D);
2665   if (hasCopyAndDispose) {
2666     /// void *__copy_helper;
2667     types.push_back(VoidPtrTy);
2668     size += getPointerSize();
2669 
2670     /// void *__destroy_helper;
2671     types.push_back(VoidPtrTy);
2672     size += getPointerSize();
2673   }
2674 
2675   bool HasByrefExtendedLayout = false;
2676   Qualifiers::ObjCLifetime Lifetime = Qualifiers::OCL_None;
2677   if (getContext().getByrefLifetime(Ty, Lifetime, HasByrefExtendedLayout) &&
2678       HasByrefExtendedLayout) {
2679     /// void *__byref_variable_layout;
2680     types.push_back(VoidPtrTy);
2681     size += CharUnits::fromQuantity(PointerSizeInBytes);
2682   }
2683 
2684   // T x;
2685   llvm::Type *varTy = ConvertTypeForMem(Ty);
2686 
2687   bool packed = false;
2688   CharUnits varAlign = getContext().getDeclAlign(D);
2689   CharUnits varOffset = size.alignTo(varAlign);
2690 
2691   // We may have to insert padding.
2692   if (varOffset != size) {
2693     llvm::Type *paddingTy =
2694       llvm::ArrayType::get(Int8Ty, (varOffset - size).getQuantity());
2695 
2696     types.push_back(paddingTy);
2697     size = varOffset;
2698 
2699   // Conversely, we might have to prevent LLVM from inserting padding.
2700   } else if (CGM.getDataLayout().getABITypeAlign(varTy) >
2701              uint64_t(varAlign.getQuantity())) {
2702     packed = true;
2703   }
2704   types.push_back(varTy);
2705 
2706   llvm::StructType *byrefType = llvm::StructType::create(
2707       getLLVMContext(), types, "struct.__block_byref_" + D->getNameAsString(),
2708       packed);
2709 
2710   BlockByrefInfo info;
2711   info.Type = byrefType;
2712   info.FieldIndex = types.size() - 1;
2713   info.FieldOffset = varOffset;
2714   info.ByrefAlignment = std::max(varAlign, getPointerAlign());
2715 
2716   auto pair = BlockByrefInfos.insert({D, info});
2717   assert(pair.second && "info was inserted recursively?");
2718   return pair.first->second;
2719 }
2720 
2721 /// Initialize the structural components of a __block variable, i.e.
2722 /// everything but the actual object.
2723 void CodeGenFunction::emitByrefStructureInit(const AutoVarEmission &emission) {
2724   // Find the address of the local.
2725   Address addr = emission.Addr;
2726 
2727   // That's an alloca of the byref structure type.
2728   llvm::StructType *byrefType = cast<llvm::StructType>(addr.getElementType());
2729 
2730   unsigned nextHeaderIndex = 0;
2731   CharUnits nextHeaderOffset;
2732   auto storeHeaderField = [&](llvm::Value *value, CharUnits fieldSize,
2733                               const Twine &name) {
2734     auto fieldAddr = Builder.CreateStructGEP(addr, nextHeaderIndex, name);
2735     Builder.CreateStore(value, fieldAddr);
2736 
2737     nextHeaderIndex++;
2738     nextHeaderOffset += fieldSize;
2739   };
2740 
2741   // Build the byref helpers if necessary.  This is null if we don't need any.
2742   BlockByrefHelpers *helpers = buildByrefHelpers(*byrefType, emission);
2743 
2744   const VarDecl &D = *emission.Variable;
2745   QualType type = D.getType();
2746 
2747   bool HasByrefExtendedLayout = false;
2748   Qualifiers::ObjCLifetime ByrefLifetime = Qualifiers::OCL_None;
2749   bool ByRefHasLifetime =
2750     getContext().getByrefLifetime(type, ByrefLifetime, HasByrefExtendedLayout);
2751 
2752   llvm::Value *V;
2753 
2754   // Initialize the 'isa', which is just 0 or 1.
2755   int isa = 0;
2756   if (type.isObjCGCWeak())
2757     isa = 1;
2758   V = Builder.CreateIntToPtr(Builder.getInt32(isa), Int8PtrTy, "isa");
2759   storeHeaderField(V, getPointerSize(), "byref.isa");
2760 
2761   // Store the address of the variable into its own forwarding pointer.
2762   storeHeaderField(addr.emitRawPointer(*this), getPointerSize(),
2763                    "byref.forwarding");
2764 
2765   // Blocks ABI:
2766   //   c) the flags field is set to either 0 if no helper functions are
2767   //      needed or BLOCK_BYREF_HAS_COPY_DISPOSE if they are,
2768   BlockFlags flags;
2769   if (helpers) flags |= BLOCK_BYREF_HAS_COPY_DISPOSE;
2770   if (ByRefHasLifetime) {
2771     if (HasByrefExtendedLayout) flags |= BLOCK_BYREF_LAYOUT_EXTENDED;
2772       else switch (ByrefLifetime) {
2773         case Qualifiers::OCL_Strong:
2774           flags |= BLOCK_BYREF_LAYOUT_STRONG;
2775           break;
2776         case Qualifiers::OCL_Weak:
2777           flags |= BLOCK_BYREF_LAYOUT_WEAK;
2778           break;
2779         case Qualifiers::OCL_ExplicitNone:
2780           flags |= BLOCK_BYREF_LAYOUT_UNRETAINED;
2781           break;
2782         case Qualifiers::OCL_None:
2783           if (!type->isObjCObjectPointerType() && !type->isBlockPointerType())
2784             flags |= BLOCK_BYREF_LAYOUT_NON_OBJECT;
2785           break;
2786         default:
2787           break;
2788       }
2789     if (CGM.getLangOpts().ObjCGCBitmapPrint) {
2790       printf("\n Inline flag for BYREF variable layout (%d):", flags.getBitMask());
2791       if (flags & BLOCK_BYREF_HAS_COPY_DISPOSE)
2792         printf(" BLOCK_BYREF_HAS_COPY_DISPOSE");
2793       if (flags & BLOCK_BYREF_LAYOUT_MASK) {
2794         BlockFlags ThisFlag(flags.getBitMask() & BLOCK_BYREF_LAYOUT_MASK);
2795         if (ThisFlag ==  BLOCK_BYREF_LAYOUT_EXTENDED)
2796           printf(" BLOCK_BYREF_LAYOUT_EXTENDED");
2797         if (ThisFlag ==  BLOCK_BYREF_LAYOUT_STRONG)
2798           printf(" BLOCK_BYREF_LAYOUT_STRONG");
2799         if (ThisFlag == BLOCK_BYREF_LAYOUT_WEAK)
2800           printf(" BLOCK_BYREF_LAYOUT_WEAK");
2801         if (ThisFlag == BLOCK_BYREF_LAYOUT_UNRETAINED)
2802           printf(" BLOCK_BYREF_LAYOUT_UNRETAINED");
2803         if (ThisFlag == BLOCK_BYREF_LAYOUT_NON_OBJECT)
2804           printf(" BLOCK_BYREF_LAYOUT_NON_OBJECT");
2805       }
2806       printf("\n");
2807     }
2808   }
2809   storeHeaderField(llvm::ConstantInt::get(IntTy, flags.getBitMask()),
2810                    getIntSize(), "byref.flags");
2811 
2812   CharUnits byrefSize = CGM.GetTargetTypeStoreSize(byrefType);
2813   V = llvm::ConstantInt::get(IntTy, byrefSize.getQuantity());
2814   storeHeaderField(V, getIntSize(), "byref.size");
2815 
2816   if (helpers) {
2817     storeHeaderField(helpers->CopyHelper, getPointerSize(),
2818                      "byref.copyHelper");
2819     storeHeaderField(helpers->DisposeHelper, getPointerSize(),
2820                      "byref.disposeHelper");
2821   }
2822 
2823   if (ByRefHasLifetime && HasByrefExtendedLayout) {
2824     auto layoutInfo = CGM.getObjCRuntime().BuildByrefLayout(CGM, type);
2825     storeHeaderField(layoutInfo, getPointerSize(), "byref.layout");
2826   }
2827 }
2828 
2829 void CodeGenFunction::BuildBlockRelease(llvm::Value *V, BlockFieldFlags flags,
2830                                         bool CanThrow) {
2831   llvm::FunctionCallee F = CGM.getBlockObjectDispose();
2832   llvm::Value *args[] = {V,
2833                          llvm::ConstantInt::get(Int32Ty, flags.getBitMask())};
2834 
2835   if (CanThrow)
2836     EmitRuntimeCallOrInvoke(F, args);
2837   else
2838     EmitNounwindRuntimeCall(F, args);
2839 }
2840 
2841 void CodeGenFunction::enterByrefCleanup(CleanupKind Kind, Address Addr,
2842                                         BlockFieldFlags Flags,
2843                                         bool LoadBlockVarAddr, bool CanThrow) {
2844   EHStack.pushCleanup<CallBlockRelease>(Kind, Addr, Flags, LoadBlockVarAddr,
2845                                         CanThrow);
2846 }
2847 
2848 /// Adjust the declaration of something from the blocks API.
2849 static void configureBlocksRuntimeObject(CodeGenModule &CGM,
2850                                          llvm::Constant *C) {
2851   auto *GV = cast<llvm::GlobalValue>(C->stripPointerCasts());
2852 
2853   if (!CGM.getCodeGenOpts().StaticClosure &&
2854       CGM.getTarget().getTriple().isOSBinFormatCOFF()) {
2855     const IdentifierInfo &II = CGM.getContext().Idents.get(C->getName());
2856     TranslationUnitDecl *TUDecl = CGM.getContext().getTranslationUnitDecl();
2857     DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl);
2858 
2859     assert((isa<llvm::Function>(C->stripPointerCasts()) ||
2860             isa<llvm::GlobalVariable>(C->stripPointerCasts())) &&
2861            "expected Function or GlobalVariable");
2862 
2863     const NamedDecl *ND = nullptr;
2864     for (const auto *Result : DC->lookup(&II))
2865       if ((ND = dyn_cast<FunctionDecl>(Result)) ||
2866           (ND = dyn_cast<VarDecl>(Result)))
2867         break;
2868 
2869     if (GV->isDeclaration() && (!ND || !ND->hasAttr<DLLExportAttr>())) {
2870       GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
2871       GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
2872     } else {
2873       GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
2874       GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
2875     }
2876   }
2877 
2878   if (CGM.getLangOpts().BlocksRuntimeOptional && GV->isDeclaration() &&
2879       GV->hasExternalLinkage())
2880     GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
2881 
2882   CGM.setDSOLocal(GV);
2883 }
2884 
2885 llvm::FunctionCallee CodeGenModule::getBlockObjectDispose() {
2886   if (BlockObjectDispose)
2887     return BlockObjectDispose;
2888 
2889   QualType args[] = {Context.VoidPtrTy, Context.IntTy};
2890   BlockObjectDispose =
2891       CreateRuntimeFunction(Context.VoidTy, args, "_Block_object_dispose");
2892   configureBlocksRuntimeObject(
2893       *this, cast<llvm::Constant>(BlockObjectDispose.getCallee()));
2894   return BlockObjectDispose;
2895 }
2896 
2897 llvm::FunctionCallee CodeGenModule::getBlockObjectAssign() {
2898   if (BlockObjectAssign)
2899     return BlockObjectAssign;
2900 
2901   QualType args[] = {Context.VoidPtrTy, Context.VoidPtrTy, Context.IntTy};
2902   BlockObjectAssign =
2903       CreateRuntimeFunction(Context.VoidTy, args, "_Block_object_assign");
2904   configureBlocksRuntimeObject(
2905       *this, cast<llvm::Constant>(BlockObjectAssign.getCallee()));
2906   return BlockObjectAssign;
2907 }
2908 
2909 llvm::Constant *CodeGenModule::getNSConcreteGlobalBlock() {
2910   if (NSConcreteGlobalBlock)
2911     return NSConcreteGlobalBlock;
2912 
2913   NSConcreteGlobalBlock = GetOrCreateLLVMGlobal(
2914       "_NSConcreteGlobalBlock", Int8PtrTy, LangAS::Default, nullptr);
2915   configureBlocksRuntimeObject(*this, NSConcreteGlobalBlock);
2916   return NSConcreteGlobalBlock;
2917 }
2918 
2919 llvm::Constant *CodeGenModule::getNSConcreteStackBlock() {
2920   if (NSConcreteStackBlock)
2921     return NSConcreteStackBlock;
2922 
2923   NSConcreteStackBlock = GetOrCreateLLVMGlobal(
2924       "_NSConcreteStackBlock", Int8PtrTy, LangAS::Default, nullptr);
2925   configureBlocksRuntimeObject(*this, NSConcreteStackBlock);
2926   return NSConcreteStackBlock;
2927 }
2928