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