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