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