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