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