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