12437cbfaSAnders Carlsson //===--- CGBlocks.cpp - Emit LLVM Code for declarations -------------------===//
22437cbfaSAnders Carlsson //
32437cbfaSAnders Carlsson //                     The LLVM Compiler Infrastructure
42437cbfaSAnders Carlsson //
52437cbfaSAnders Carlsson // This file is distributed under the University of Illinois Open Source
62437cbfaSAnders Carlsson // License. See LICENSE.TXT for details.
72437cbfaSAnders Carlsson //
82437cbfaSAnders Carlsson //===----------------------------------------------------------------------===//
92437cbfaSAnders Carlsson //
102437cbfaSAnders Carlsson // This contains code to emit blocks.
112437cbfaSAnders Carlsson //
122437cbfaSAnders Carlsson //===----------------------------------------------------------------------===//
132437cbfaSAnders Carlsson 
142e722b91SMike Stump #include "CGDebugInfo.h"
152437cbfaSAnders Carlsson #include "CodeGenFunction.h"
16989908fbSFariborz Jahanian #include "CGObjCRuntime.h"
172437cbfaSAnders Carlsson #include "CodeGenModule.h"
18ad7c5c16SJohn McCall #include "CGBlocks.h"
19692c6e37SMike Stump #include "clang/AST/DeclObjC.h"
202437cbfaSAnders Carlsson #include "llvm/Module.h"
219e2e1c90SBenjamin Kramer #include "llvm/ADT/SmallSet.h"
226a60fa24SAnders Carlsson #include "llvm/Target/TargetData.h"
232437cbfaSAnders Carlsson #include <algorithm>
24db714924STorok Edwin 
252437cbfaSAnders Carlsson using namespace clang;
262437cbfaSAnders Carlsson using namespace CodeGen;
272437cbfaSAnders Carlsson 
28351762cdSJohn McCall CGBlockInfo::CGBlockInfo(const BlockExpr *blockExpr, const char *N)
29351762cdSJohn McCall   : Name(N), CXXThisIndex(0), CanBeGlobal(false), NeedsCopyDispose(false),
3085915250SJohn McCall     HasCXXObject(false), UsesStret(false), StructureType(0), Block(blockExpr) {
319d42f0f1SJohn McCall 
329d42f0f1SJohn McCall   // Skip asm prefix, if any.
339d42f0f1SJohn McCall   if (Name && Name[0] == '\01')
349d42f0f1SJohn McCall     ++Name;
359d42f0f1SJohn McCall }
369d42f0f1SJohn McCall 
37f9b056b0SJohn McCall // Anchor the vtable to this translation unit.
38f9b056b0SJohn McCall CodeGenModule::ByrefHelpers::~ByrefHelpers() {}
39f9b056b0SJohn McCall 
40351762cdSJohn McCall /// Build the given block as a global block.
41351762cdSJohn McCall static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM,
42351762cdSJohn McCall                                         const CGBlockInfo &blockInfo,
43351762cdSJohn McCall                                         llvm::Constant *blockFn);
449d42f0f1SJohn McCall 
45351762cdSJohn McCall /// Build the helper function to copy a block.
46351762cdSJohn McCall static llvm::Constant *buildCopyHelper(CodeGenModule &CGM,
47351762cdSJohn McCall                                        const CGBlockInfo &blockInfo) {
48351762cdSJohn McCall   return CodeGenFunction(CGM).GenerateCopyHelperFunction(blockInfo);
49351762cdSJohn McCall }
50351762cdSJohn McCall 
51351762cdSJohn McCall /// Build the helper function to dipose of a block.
52351762cdSJohn McCall static llvm::Constant *buildDisposeHelper(CodeGenModule &CGM,
53351762cdSJohn McCall                                           const CGBlockInfo &blockInfo) {
54351762cdSJohn McCall   return CodeGenFunction(CGM).GenerateDestroyHelperFunction(blockInfo);
55351762cdSJohn McCall }
56351762cdSJohn McCall 
57351762cdSJohn McCall /// Build the block descriptor constant for a block.
58351762cdSJohn McCall static llvm::Constant *buildBlockDescriptor(CodeGenModule &CGM,
59351762cdSJohn McCall                                             const CGBlockInfo &blockInfo) {
60351762cdSJohn McCall   ASTContext &C = CGM.getContext();
61351762cdSJohn McCall 
62351762cdSJohn McCall   const llvm::Type *ulong = CGM.getTypes().ConvertType(C.UnsignedLongTy);
63351762cdSJohn McCall   const llvm::Type *i8p = CGM.getTypes().ConvertType(C.VoidPtrTy);
64351762cdSJohn McCall 
65351762cdSJohn McCall   llvm::SmallVector<llvm::Constant*, 6> elements;
6685284bacSMike Stump 
6785284bacSMike Stump   // reserved
68351762cdSJohn McCall   elements.push_back(llvm::ConstantInt::get(ulong, 0));
6985284bacSMike Stump 
7085284bacSMike Stump   // Size
712ac40a9fSMike Stump   // FIXME: What is the right way to say this doesn't fit?  We should give
722ac40a9fSMike Stump   // a user diagnostic in that case.  Better fix would be to change the
732ac40a9fSMike Stump   // API to size_t.
74351762cdSJohn McCall   elements.push_back(llvm::ConstantInt::get(ulong,
75351762cdSJohn McCall                                             blockInfo.BlockSize.getQuantity()));
7685284bacSMike Stump 
77351762cdSJohn McCall   // Optional copy/dispose helpers.
78351762cdSJohn McCall   if (blockInfo.NeedsCopyDispose) {
7985284bacSMike Stump     // copy_func_helper_decl
80351762cdSJohn McCall     elements.push_back(buildCopyHelper(CGM, blockInfo));
8185284bacSMike Stump 
8285284bacSMike Stump     // destroy_func_decl
83351762cdSJohn McCall     elements.push_back(buildDisposeHelper(CGM, blockInfo));
8485284bacSMike Stump   }
8585284bacSMike Stump 
86351762cdSJohn McCall   // Signature.  Mandatory ObjC-style method descriptor @encode sequence.
87351762cdSJohn McCall   std::string typeAtEncoding =
88351762cdSJohn McCall     CGM.getContext().getObjCEncodingForBlock(blockInfo.getBlockExpr());
89351762cdSJohn McCall   elements.push_back(llvm::ConstantExpr::getBitCast(
90351762cdSJohn McCall                           CGM.GetAddrOfConstantCString(typeAtEncoding), i8p));
91fc83aa04SBlaine Garst 
92351762cdSJohn McCall   // GC layout.
93351762cdSJohn McCall   if (C.getLangOptions().ObjC1)
94351762cdSJohn McCall     elements.push_back(CGM.getObjCRuntime().BuildGCBlockLayout(CGM, blockInfo));
95351762cdSJohn McCall   else
96351762cdSJohn McCall     elements.push_back(llvm::Constant::getNullValue(i8p));
97fc83aa04SBlaine Garst 
98e64d7ba1SChris Lattner   llvm::Constant *init = llvm::ConstantStruct::getAnon(elements);
99c05349e5SFariborz Jahanian 
100351762cdSJohn McCall   llvm::GlobalVariable *global =
101351762cdSJohn McCall     new llvm::GlobalVariable(CGM.getModule(), init->getType(), true,
10285284bacSMike Stump                              llvm::GlobalValue::InternalLinkage,
103351762cdSJohn McCall                              init, "__block_descriptor_tmp");
10485284bacSMike Stump 
105351762cdSJohn McCall   return llvm::ConstantExpr::getBitCast(global, CGM.getBlockDescriptorType());
106ed5e69feSAnders Carlsson }
107ed5e69feSAnders Carlsson 
108351762cdSJohn McCall /*
109351762cdSJohn McCall   Purely notional variadic template describing the layout of a block.
110ed5e69feSAnders Carlsson 
111351762cdSJohn McCall   template <class _ResultType, class... _ParamTypes, class... _CaptureTypes>
112351762cdSJohn McCall   struct Block_literal {
113351762cdSJohn McCall     /// Initialized to one of:
114351762cdSJohn McCall     ///   extern void *_NSConcreteStackBlock[];
115351762cdSJohn McCall     ///   extern void *_NSConcreteGlobalBlock[];
116351762cdSJohn McCall     ///
117351762cdSJohn McCall     /// In theory, we could start one off malloc'ed by setting
118351762cdSJohn McCall     /// BLOCK_NEEDS_FREE, giving it a refcount of 1, and using
119351762cdSJohn McCall     /// this isa:
120351762cdSJohn McCall     ///   extern void *_NSConcreteMallocBlock[];
121351762cdSJohn McCall     struct objc_class *isa;
12285284bacSMike Stump 
123351762cdSJohn McCall     /// These are the flags (with corresponding bit number) that the
124351762cdSJohn McCall     /// compiler is actually supposed to know about.
125351762cdSJohn McCall     ///  25. BLOCK_HAS_COPY_DISPOSE - indicates that the block
126351762cdSJohn McCall     ///   descriptor provides copy and dispose helper functions
127351762cdSJohn McCall     ///  26. BLOCK_HAS_CXX_OBJ - indicates that there's a captured
128351762cdSJohn McCall     ///   object with a nontrivial destructor or copy constructor
129351762cdSJohn McCall     ///  28. BLOCK_IS_GLOBAL - indicates that the block is allocated
130351762cdSJohn McCall     ///   as global memory
131351762cdSJohn McCall     ///  29. BLOCK_USE_STRET - indicates that the block function
132351762cdSJohn McCall     ///   uses stret, which objc_msgSend needs to know about
133351762cdSJohn McCall     ///  30. BLOCK_HAS_SIGNATURE - indicates that the block has an
134351762cdSJohn McCall     ///   @encoded signature string
135351762cdSJohn McCall     /// And we're not supposed to manipulate these:
136351762cdSJohn McCall     ///  24. BLOCK_NEEDS_FREE - indicates that the block has been moved
137351762cdSJohn McCall     ///   to malloc'ed memory
138351762cdSJohn McCall     ///  27. BLOCK_IS_GC - indicates that the block has been moved to
139351762cdSJohn McCall     ///   to GC-allocated memory
140351762cdSJohn McCall     /// Additionally, the bottom 16 bits are a reference count which
141351762cdSJohn McCall     /// should be zero on the stack.
142351762cdSJohn McCall     int flags;
143950a9518SDavid Chisnall 
144351762cdSJohn McCall     /// Reserved;  should be zero-initialized.
145351762cdSJohn McCall     int reserved;
146950a9518SDavid Chisnall 
147351762cdSJohn McCall     /// Function pointer generated from block literal.
148351762cdSJohn McCall     _ResultType (*invoke)(Block_literal *, _ParamTypes...);
14985284bacSMike Stump 
150351762cdSJohn McCall     /// Block description metadata generated from block literal.
151351762cdSJohn McCall     struct Block_descriptor *block_descriptor;
1523882ace2SJohn McCall 
153351762cdSJohn McCall     /// Captured values follow.
154351762cdSJohn McCall     _CapturesTypes captures...;
155351762cdSJohn McCall   };
156351762cdSJohn McCall  */
157950a9518SDavid Chisnall 
158351762cdSJohn McCall /// The number of fields in a block header.
159351762cdSJohn McCall const unsigned BlockHeaderSize = 5;
1604446dcf0SMike Stump 
161351762cdSJohn McCall namespace {
162351762cdSJohn McCall   /// A chunk of data that we actually have to capture in the block.
163351762cdSJohn McCall   struct BlockLayoutChunk {
164351762cdSJohn McCall     CharUnits Alignment;
165351762cdSJohn McCall     CharUnits Size;
166351762cdSJohn McCall     const BlockDecl::Capture *Capture; // null for 'this'
1677c57be3eSJay Foad     llvm::Type *Type;
16885284bacSMike Stump 
169351762cdSJohn McCall     BlockLayoutChunk(CharUnits align, CharUnits size,
170351762cdSJohn McCall                      const BlockDecl::Capture *capture,
1717c57be3eSJay Foad                      llvm::Type *type)
172351762cdSJohn McCall       : Alignment(align), Size(size), Capture(capture), Type(type) {}
17385284bacSMike Stump 
174351762cdSJohn McCall     /// Tell the block info that this chunk has the given field index.
175351762cdSJohn McCall     void setIndex(CGBlockInfo &info, unsigned index) {
176351762cdSJohn McCall       if (!Capture)
177351762cdSJohn McCall         info.CXXThisIndex = index;
178b750d928SMike Stump       else
179351762cdSJohn McCall         info.Captures[Capture->getVariable()]
180351762cdSJohn McCall           = CGBlockInfo::Capture::makeIndex(index);
181b750d928SMike Stump     }
182351762cdSJohn McCall   };
183d6ef62f0SMike Stump 
184351762cdSJohn McCall   /// Order by descending alignment.
185351762cdSJohn McCall   bool operator<(const BlockLayoutChunk &left, const BlockLayoutChunk &right) {
186351762cdSJohn McCall     return left.Alignment > right.Alignment;
187351762cdSJohn McCall   }
188b750d928SMike Stump }
189b750d928SMike Stump 
190b0a3ecb4SJohn McCall /// Determines if the given type is safe for constant capture in C++.
191b0a3ecb4SJohn McCall static bool isSafeForCXXConstantCapture(QualType type) {
192b0a3ecb4SJohn McCall   const RecordType *recordType =
193b0a3ecb4SJohn McCall     type->getBaseElementTypeUnsafe()->getAs<RecordType>();
194b0a3ecb4SJohn McCall 
195b0a3ecb4SJohn McCall   // Only records can be unsafe.
196b0a3ecb4SJohn McCall   if (!recordType) return true;
197b0a3ecb4SJohn McCall 
198b0a3ecb4SJohn McCall   const CXXRecordDecl *record = cast<CXXRecordDecl>(recordType->getDecl());
199b0a3ecb4SJohn McCall 
200b0a3ecb4SJohn McCall   // Maintain semantics for classes with non-trivial dtors or copy ctors.
201b0a3ecb4SJohn McCall   if (!record->hasTrivialDestructor()) return false;
202b0a3ecb4SJohn McCall   if (!record->hasTrivialCopyConstructor()) return false;
203b0a3ecb4SJohn McCall 
204b0a3ecb4SJohn McCall   // Otherwise, we just have to make sure there aren't any mutable
205b0a3ecb4SJohn McCall   // fields that might have changed since initialization.
20661226d3fSDouglas Gregor   return !record->hasMutableFields();
207b0a3ecb4SJohn McCall }
208b0a3ecb4SJohn McCall 
209351762cdSJohn McCall /// It is illegal to modify a const object after initialization.
210351762cdSJohn McCall /// Therefore, if a const object has a constant initializer, we don't
211351762cdSJohn McCall /// actually need to keep storage for it in the block; we'll just
212351762cdSJohn McCall /// rematerialize it at the start of the block function.  This is
213351762cdSJohn McCall /// acceptable because we make no promises about address stability of
214351762cdSJohn McCall /// captured variables.
215351762cdSJohn McCall static llvm::Constant *tryCaptureAsConstant(CodeGenModule &CGM,
216351762cdSJohn McCall                                             const VarDecl *var) {
217351762cdSJohn McCall   QualType type = var->getType();
218351762cdSJohn McCall 
219351762cdSJohn McCall   // We can only do this if the variable is const.
220351762cdSJohn McCall   if (!type.isConstQualified()) return 0;
221351762cdSJohn McCall 
222b0a3ecb4SJohn McCall   // Furthermore, in C++ we have to worry about mutable fields:
223b0a3ecb4SJohn McCall   // C++ [dcl.type.cv]p4:
224b0a3ecb4SJohn McCall   //   Except that any class member declared mutable can be
225b0a3ecb4SJohn McCall   //   modified, any attempt to modify a const object during its
226b0a3ecb4SJohn McCall   //   lifetime results in undefined behavior.
227b0a3ecb4SJohn McCall   if (CGM.getLangOptions().CPlusPlus && !isSafeForCXXConstantCapture(type))
228351762cdSJohn McCall     return 0;
229351762cdSJohn McCall 
230351762cdSJohn McCall   // If the variable doesn't have any initializer (shouldn't this be
231351762cdSJohn McCall   // invalid?), it's not clear what we should do.  Maybe capture as
232351762cdSJohn McCall   // zero?
233351762cdSJohn McCall   const Expr *init = var->getInit();
234351762cdSJohn McCall   if (!init) return 0;
235351762cdSJohn McCall 
236351762cdSJohn McCall   return CGM.EmitConstantExpr(init, var->getType());
237351762cdSJohn McCall }
238351762cdSJohn McCall 
239351762cdSJohn McCall /// Get the low bit of a nonzero character count.  This is the
240351762cdSJohn McCall /// alignment of the nth byte if the 0th byte is universally aligned.
241351762cdSJohn McCall static CharUnits getLowBit(CharUnits v) {
242351762cdSJohn McCall   return CharUnits::fromQuantity(v.getQuantity() & (~v.getQuantity() + 1));
243351762cdSJohn McCall }
244351762cdSJohn McCall 
245351762cdSJohn McCall static void initializeForBlockHeader(CodeGenModule &CGM, CGBlockInfo &info,
2467c57be3eSJay Foad                              llvm::SmallVectorImpl<llvm::Type*> &elementTypes) {
247351762cdSJohn McCall   ASTContext &C = CGM.getContext();
248351762cdSJohn McCall 
249351762cdSJohn McCall   // The header is basically a 'struct { void *; int; int; void *; void *; }'.
250351762cdSJohn McCall   CharUnits ptrSize, ptrAlign, intSize, intAlign;
251351762cdSJohn McCall   llvm::tie(ptrSize, ptrAlign) = C.getTypeInfoInChars(C.VoidPtrTy);
252351762cdSJohn McCall   llvm::tie(intSize, intAlign) = C.getTypeInfoInChars(C.IntTy);
253351762cdSJohn McCall 
254351762cdSJohn McCall   // Are there crazy embedded platforms where this isn't true?
255351762cdSJohn McCall   assert(intSize <= ptrSize && "layout assumptions horribly violated");
256351762cdSJohn McCall 
257351762cdSJohn McCall   CharUnits headerSize = ptrSize;
258351762cdSJohn McCall   if (2 * intSize < ptrAlign) headerSize += ptrSize;
259351762cdSJohn McCall   else headerSize += 2 * intSize;
260351762cdSJohn McCall   headerSize += 2 * ptrSize;
261351762cdSJohn McCall 
262351762cdSJohn McCall   info.BlockAlign = ptrAlign;
263351762cdSJohn McCall   info.BlockSize = headerSize;
264351762cdSJohn McCall 
265351762cdSJohn McCall   assert(elementTypes.empty());
2667c57be3eSJay Foad   llvm::Type *i8p = CGM.getTypes().ConvertType(C.VoidPtrTy);
2677c57be3eSJay Foad   llvm::Type *intTy = CGM.getTypes().ConvertType(C.IntTy);
268351762cdSJohn McCall   elementTypes.push_back(i8p);
269351762cdSJohn McCall   elementTypes.push_back(intTy);
270351762cdSJohn McCall   elementTypes.push_back(intTy);
271351762cdSJohn McCall   elementTypes.push_back(i8p);
272351762cdSJohn McCall   elementTypes.push_back(CGM.getBlockDescriptorType());
273351762cdSJohn McCall 
274351762cdSJohn McCall   assert(elementTypes.size() == BlockHeaderSize);
275351762cdSJohn McCall }
276351762cdSJohn McCall 
277351762cdSJohn McCall /// Compute the layout of the given block.  Attempts to lay the block
278351762cdSJohn McCall /// out with minimal space requirements.
279351762cdSJohn McCall static void computeBlockInfo(CodeGenModule &CGM, CGBlockInfo &info) {
280351762cdSJohn McCall   ASTContext &C = CGM.getContext();
281351762cdSJohn McCall   const BlockDecl *block = info.getBlockDecl();
282351762cdSJohn McCall 
2837c57be3eSJay Foad   llvm::SmallVector<llvm::Type*, 8> elementTypes;
284351762cdSJohn McCall   initializeForBlockHeader(CGM, info, elementTypes);
285351762cdSJohn McCall 
286351762cdSJohn McCall   if (!block->hasCaptures()) {
287351762cdSJohn McCall     info.StructureType =
288351762cdSJohn McCall       llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true);
289351762cdSJohn McCall     info.CanBeGlobal = true;
290351762cdSJohn McCall     return;
291351762cdSJohn McCall   }
292351762cdSJohn McCall 
293351762cdSJohn McCall   // Collect the layout chunks.
294351762cdSJohn McCall   llvm::SmallVector<BlockLayoutChunk, 16> layout;
295351762cdSJohn McCall   layout.reserve(block->capturesCXXThis() +
296351762cdSJohn McCall                  (block->capture_end() - block->capture_begin()));
297351762cdSJohn McCall 
298351762cdSJohn McCall   CharUnits maxFieldAlign;
299351762cdSJohn McCall 
300351762cdSJohn McCall   // First, 'this'.
301351762cdSJohn McCall   if (block->capturesCXXThis()) {
302351762cdSJohn McCall     const DeclContext *DC = block->getDeclContext();
303351762cdSJohn McCall     for (; isa<BlockDecl>(DC); DC = cast<BlockDecl>(DC)->getDeclContext())
304351762cdSJohn McCall       ;
305938f40b5SRichard Smith     QualType thisType;
306938f40b5SRichard Smith     if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC))
307938f40b5SRichard Smith       thisType = C.getPointerType(C.getRecordType(RD));
308938f40b5SRichard Smith     else
309938f40b5SRichard Smith       thisType = cast<CXXMethodDecl>(DC)->getThisType(C);
310351762cdSJohn McCall 
3117c57be3eSJay Foad     llvm::Type *llvmType = CGM.getTypes().ConvertType(thisType);
312351762cdSJohn McCall     std::pair<CharUnits,CharUnits> tinfo
313351762cdSJohn McCall       = CGM.getContext().getTypeInfoInChars(thisType);
314351762cdSJohn McCall     maxFieldAlign = std::max(maxFieldAlign, tinfo.second);
315351762cdSJohn McCall 
316351762cdSJohn McCall     layout.push_back(BlockLayoutChunk(tinfo.second, tinfo.first, 0, llvmType));
317351762cdSJohn McCall   }
318351762cdSJohn McCall 
319351762cdSJohn McCall   // Next, all the block captures.
320351762cdSJohn McCall   for (BlockDecl::capture_const_iterator ci = block->capture_begin(),
321351762cdSJohn McCall          ce = block->capture_end(); ci != ce; ++ci) {
322351762cdSJohn McCall     const VarDecl *variable = ci->getVariable();
323351762cdSJohn McCall 
324351762cdSJohn McCall     if (ci->isByRef()) {
325351762cdSJohn McCall       // We have to copy/dispose of the __block reference.
326351762cdSJohn McCall       info.NeedsCopyDispose = true;
327351762cdSJohn McCall 
328351762cdSJohn McCall       // Just use void* instead of a pointer to the byref type.
329351762cdSJohn McCall       QualType byRefPtrTy = C.VoidPtrTy;
330351762cdSJohn McCall 
3317c57be3eSJay Foad       llvm::Type *llvmType = CGM.getTypes().ConvertType(byRefPtrTy);
332351762cdSJohn McCall       std::pair<CharUnits,CharUnits> tinfo
333351762cdSJohn McCall         = CGM.getContext().getTypeInfoInChars(byRefPtrTy);
334351762cdSJohn McCall       maxFieldAlign = std::max(maxFieldAlign, tinfo.second);
335351762cdSJohn McCall 
336351762cdSJohn McCall       layout.push_back(BlockLayoutChunk(tinfo.second, tinfo.first,
337351762cdSJohn McCall                                         &*ci, llvmType));
338351762cdSJohn McCall       continue;
339351762cdSJohn McCall     }
340351762cdSJohn McCall 
341351762cdSJohn McCall     // Otherwise, build a layout chunk with the size and alignment of
342351762cdSJohn McCall     // the declaration.
343351762cdSJohn McCall     if (llvm::Constant *constant = tryCaptureAsConstant(CGM, variable)) {
344351762cdSJohn McCall       info.Captures[variable] = CGBlockInfo::Capture::makeConstant(constant);
345351762cdSJohn McCall       continue;
346351762cdSJohn McCall     }
347351762cdSJohn McCall 
34831168b07SJohn McCall     // If we have a lifetime qualifier, honor it for capture purposes.
34931168b07SJohn McCall     // That includes *not* copying it if it's __unsafe_unretained.
35031168b07SJohn McCall     if (Qualifiers::ObjCLifetime lifetime
35131168b07SJohn McCall           = variable->getType().getObjCLifetime()) {
35231168b07SJohn McCall       switch (lifetime) {
35331168b07SJohn McCall       case Qualifiers::OCL_None: llvm_unreachable("impossible");
35431168b07SJohn McCall       case Qualifiers::OCL_ExplicitNone:
35531168b07SJohn McCall       case Qualifiers::OCL_Autoreleasing:
35631168b07SJohn McCall         break;
357351762cdSJohn McCall 
35831168b07SJohn McCall       case Qualifiers::OCL_Strong:
35931168b07SJohn McCall       case Qualifiers::OCL_Weak:
36031168b07SJohn McCall         info.NeedsCopyDispose = true;
36131168b07SJohn McCall       }
36231168b07SJohn McCall 
36331168b07SJohn McCall     // Block pointers require copy/dispose.  So do Objective-C pointers.
36431168b07SJohn McCall     } else if (variable->getType()->isObjCRetainableType()) {
365351762cdSJohn McCall       info.NeedsCopyDispose = true;
366351762cdSJohn McCall 
367351762cdSJohn McCall     // So do types that require non-trivial copy construction.
368351762cdSJohn McCall     } else if (ci->hasCopyExpr()) {
369351762cdSJohn McCall       info.NeedsCopyDispose = true;
370351762cdSJohn McCall       info.HasCXXObject = true;
371351762cdSJohn McCall 
372351762cdSJohn McCall     // And so do types with destructors.
373351762cdSJohn McCall     } else if (CGM.getLangOptions().CPlusPlus) {
374351762cdSJohn McCall       if (const CXXRecordDecl *record =
375351762cdSJohn McCall             variable->getType()->getAsCXXRecordDecl()) {
376351762cdSJohn McCall         if (!record->hasTrivialDestructor()) {
377351762cdSJohn McCall           info.HasCXXObject = true;
378351762cdSJohn McCall           info.NeedsCopyDispose = true;
379351762cdSJohn McCall         }
380351762cdSJohn McCall       }
381351762cdSJohn McCall     }
382351762cdSJohn McCall 
383351762cdSJohn McCall     CharUnits size = C.getTypeSizeInChars(variable->getType());
384351762cdSJohn McCall     CharUnits align = C.getDeclAlign(variable);
385351762cdSJohn McCall     maxFieldAlign = std::max(maxFieldAlign, align);
386351762cdSJohn McCall 
3877c57be3eSJay Foad     llvm::Type *llvmType =
388351762cdSJohn McCall       CGM.getTypes().ConvertTypeForMem(variable->getType());
389351762cdSJohn McCall 
390351762cdSJohn McCall     layout.push_back(BlockLayoutChunk(align, size, &*ci, llvmType));
391351762cdSJohn McCall   }
392351762cdSJohn McCall 
393351762cdSJohn McCall   // If that was everything, we're done here.
394351762cdSJohn McCall   if (layout.empty()) {
395351762cdSJohn McCall     info.StructureType =
396351762cdSJohn McCall       llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true);
397351762cdSJohn McCall     info.CanBeGlobal = true;
398351762cdSJohn McCall     return;
399351762cdSJohn McCall   }
400351762cdSJohn McCall 
401351762cdSJohn McCall   // Sort the layout by alignment.  We have to use a stable sort here
402351762cdSJohn McCall   // to get reproducible results.  There should probably be an
403351762cdSJohn McCall   // llvm::array_pod_stable_sort.
404351762cdSJohn McCall   std::stable_sort(layout.begin(), layout.end());
405351762cdSJohn McCall 
406351762cdSJohn McCall   CharUnits &blockSize = info.BlockSize;
407351762cdSJohn McCall   info.BlockAlign = std::max(maxFieldAlign, info.BlockAlign);
408351762cdSJohn McCall 
409351762cdSJohn McCall   // Assuming that the first byte in the header is maximally aligned,
410351762cdSJohn McCall   // get the alignment of the first byte following the header.
411351762cdSJohn McCall   CharUnits endAlign = getLowBit(blockSize);
412351762cdSJohn McCall 
413351762cdSJohn McCall   // If the end of the header isn't satisfactorily aligned for the
414351762cdSJohn McCall   // maximum thing, look for things that are okay with the header-end
415351762cdSJohn McCall   // alignment, and keep appending them until we get something that's
416351762cdSJohn McCall   // aligned right.  This algorithm is only guaranteed optimal if
417351762cdSJohn McCall   // that condition is satisfied at some point; otherwise we can get
418351762cdSJohn McCall   // things like:
419351762cdSJohn McCall   //   header                 // next byte has alignment 4
420351762cdSJohn McCall   //   something_with_size_5; // next byte has alignment 1
421351762cdSJohn McCall   //   something_with_alignment_8;
422351762cdSJohn McCall   // which has 7 bytes of padding, as opposed to the naive solution
423351762cdSJohn McCall   // which might have less (?).
424351762cdSJohn McCall   if (endAlign < maxFieldAlign) {
425351762cdSJohn McCall     llvm::SmallVectorImpl<BlockLayoutChunk>::iterator
426351762cdSJohn McCall       li = layout.begin() + 1, le = layout.end();
427351762cdSJohn McCall 
428351762cdSJohn McCall     // Look for something that the header end is already
429351762cdSJohn McCall     // satisfactorily aligned for.
430351762cdSJohn McCall     for (; li != le && endAlign < li->Alignment; ++li)
431351762cdSJohn McCall       ;
432351762cdSJohn McCall 
433351762cdSJohn McCall     // If we found something that's naturally aligned for the end of
434351762cdSJohn McCall     // the header, keep adding things...
435351762cdSJohn McCall     if (li != le) {
436351762cdSJohn McCall       llvm::SmallVectorImpl<BlockLayoutChunk>::iterator first = li;
437351762cdSJohn McCall       for (; li != le; ++li) {
438351762cdSJohn McCall         assert(endAlign >= li->Alignment);
439351762cdSJohn McCall 
440351762cdSJohn McCall         li->setIndex(info, elementTypes.size());
441351762cdSJohn McCall         elementTypes.push_back(li->Type);
442351762cdSJohn McCall         blockSize += li->Size;
443351762cdSJohn McCall         endAlign = getLowBit(blockSize);
444351762cdSJohn McCall 
445351762cdSJohn McCall         // ...until we get to the alignment of the maximum field.
446351762cdSJohn McCall         if (endAlign >= maxFieldAlign)
447351762cdSJohn McCall           break;
448351762cdSJohn McCall       }
449351762cdSJohn McCall 
450351762cdSJohn McCall       // Don't re-append everything we just appended.
451351762cdSJohn McCall       layout.erase(first, li);
452351762cdSJohn McCall     }
453351762cdSJohn McCall   }
454351762cdSJohn McCall 
455351762cdSJohn McCall   // At this point, we just have to add padding if the end align still
456351762cdSJohn McCall   // isn't aligned right.
457351762cdSJohn McCall   if (endAlign < maxFieldAlign) {
458351762cdSJohn McCall     CharUnits padding = maxFieldAlign - endAlign;
459351762cdSJohn McCall 
460e3dc1707SJohn McCall     elementTypes.push_back(llvm::ArrayType::get(CGM.Int8Ty,
461e3dc1707SJohn McCall                                                 padding.getQuantity()));
462351762cdSJohn McCall     blockSize += padding;
463351762cdSJohn McCall 
464351762cdSJohn McCall     endAlign = getLowBit(blockSize);
465351762cdSJohn McCall     assert(endAlign >= maxFieldAlign);
466351762cdSJohn McCall   }
467351762cdSJohn McCall 
468351762cdSJohn McCall   // Slam everything else on now.  This works because they have
469351762cdSJohn McCall   // strictly decreasing alignment and we expect that size is always a
470351762cdSJohn McCall   // multiple of alignment.
471351762cdSJohn McCall   for (llvm::SmallVectorImpl<BlockLayoutChunk>::iterator
472351762cdSJohn McCall          li = layout.begin(), le = layout.end(); li != le; ++li) {
473351762cdSJohn McCall     assert(endAlign >= li->Alignment);
474351762cdSJohn McCall     li->setIndex(info, elementTypes.size());
475351762cdSJohn McCall     elementTypes.push_back(li->Type);
476351762cdSJohn McCall     blockSize += li->Size;
477351762cdSJohn McCall     endAlign = getLowBit(blockSize);
478351762cdSJohn McCall   }
479351762cdSJohn McCall 
480351762cdSJohn McCall   info.StructureType =
481351762cdSJohn McCall     llvm::StructType::get(CGM.getLLVMContext(), elementTypes, true);
482351762cdSJohn McCall }
483351762cdSJohn McCall 
484351762cdSJohn McCall /// Emit a block literal expression in the current function.
485351762cdSJohn McCall llvm::Value *CodeGenFunction::EmitBlockLiteral(const BlockExpr *blockExpr) {
486351762cdSJohn McCall   std::string Name = CurFn->getName();
487351762cdSJohn McCall   CGBlockInfo blockInfo(blockExpr, Name.c_str());
488351762cdSJohn McCall 
489351762cdSJohn McCall   // Compute information about the layout, etc., of this block.
490351762cdSJohn McCall   computeBlockInfo(CGM, blockInfo);
491351762cdSJohn McCall 
492351762cdSJohn McCall   // Using that metadata, generate the actual block function.
493351762cdSJohn McCall   llvm::Constant *blockFn
494351762cdSJohn McCall     = CodeGenFunction(CGM).GenerateBlockFunction(CurGD, blockInfo,
495351762cdSJohn McCall                                                  CurFuncDecl, LocalDeclMap);
496e3dc1707SJohn McCall   blockFn = llvm::ConstantExpr::getBitCast(blockFn, VoidPtrTy);
497351762cdSJohn McCall 
498351762cdSJohn McCall   // If there is nothing to capture, we can emit this as a global block.
499351762cdSJohn McCall   if (blockInfo.CanBeGlobal)
500351762cdSJohn McCall     return buildGlobalBlock(CGM, blockInfo, blockFn);
501351762cdSJohn McCall 
502351762cdSJohn McCall   // Otherwise, we have to emit this as a local block.
503351762cdSJohn McCall 
504351762cdSJohn McCall   llvm::Constant *isa = CGM.getNSConcreteStackBlock();
505e3dc1707SJohn McCall   isa = llvm::ConstantExpr::getBitCast(isa, VoidPtrTy);
506351762cdSJohn McCall 
507351762cdSJohn McCall   // Build the block descriptor.
508351762cdSJohn McCall   llvm::Constant *descriptor = buildBlockDescriptor(CGM, blockInfo);
509351762cdSJohn McCall 
510351762cdSJohn McCall   const llvm::Type *intTy = ConvertType(getContext().IntTy);
511351762cdSJohn McCall 
512351762cdSJohn McCall   llvm::AllocaInst *blockAddr =
513351762cdSJohn McCall     CreateTempAlloca(blockInfo.StructureType, "block");
514351762cdSJohn McCall   blockAddr->setAlignment(blockInfo.BlockAlign.getQuantity());
515351762cdSJohn McCall 
516351762cdSJohn McCall   // Compute the initial on-stack block flags.
517ad7c5c16SJohn McCall   BlockFlags flags = BLOCK_HAS_SIGNATURE;
518351762cdSJohn McCall   if (blockInfo.NeedsCopyDispose) flags |= BLOCK_HAS_COPY_DISPOSE;
519351762cdSJohn McCall   if (blockInfo.HasCXXObject) flags |= BLOCK_HAS_CXX_OBJ;
52085915250SJohn McCall   if (blockInfo.UsesStret) flags |= BLOCK_USE_STRET;
521351762cdSJohn McCall 
522351762cdSJohn McCall   // Initialize the block literal.
523351762cdSJohn McCall   Builder.CreateStore(isa, Builder.CreateStructGEP(blockAddr, 0, "block.isa"));
524ad7c5c16SJohn McCall   Builder.CreateStore(llvm::ConstantInt::get(intTy, flags.getBitMask()),
525351762cdSJohn McCall                       Builder.CreateStructGEP(blockAddr, 1, "block.flags"));
526351762cdSJohn McCall   Builder.CreateStore(llvm::ConstantInt::get(intTy, 0),
527351762cdSJohn McCall                       Builder.CreateStructGEP(blockAddr, 2, "block.reserved"));
528351762cdSJohn McCall   Builder.CreateStore(blockFn, Builder.CreateStructGEP(blockAddr, 3,
529351762cdSJohn McCall                                                        "block.invoke"));
530351762cdSJohn McCall   Builder.CreateStore(descriptor, Builder.CreateStructGEP(blockAddr, 4,
531351762cdSJohn McCall                                                           "block.descriptor"));
532351762cdSJohn McCall 
533351762cdSJohn McCall   // Finally, capture all the values into the block.
534351762cdSJohn McCall   const BlockDecl *blockDecl = blockInfo.getBlockDecl();
535351762cdSJohn McCall 
536351762cdSJohn McCall   // First, 'this'.
537351762cdSJohn McCall   if (blockDecl->capturesCXXThis()) {
538351762cdSJohn McCall     llvm::Value *addr = Builder.CreateStructGEP(blockAddr,
539351762cdSJohn McCall                                                 blockInfo.CXXThisIndex,
540351762cdSJohn McCall                                                 "block.captured-this.addr");
541351762cdSJohn McCall     Builder.CreateStore(LoadCXXThis(), addr);
542351762cdSJohn McCall   }
543351762cdSJohn McCall 
544351762cdSJohn McCall   // Next, captured variables.
545351762cdSJohn McCall   for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
546351762cdSJohn McCall          ce = blockDecl->capture_end(); ci != ce; ++ci) {
547351762cdSJohn McCall     const VarDecl *variable = ci->getVariable();
548351762cdSJohn McCall     const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
549351762cdSJohn McCall 
550351762cdSJohn McCall     // Ignore constant captures.
551351762cdSJohn McCall     if (capture.isConstant()) continue;
552351762cdSJohn McCall 
553351762cdSJohn McCall     QualType type = variable->getType();
554351762cdSJohn McCall 
555351762cdSJohn McCall     // This will be a [[type]]*, except that a byref entry will just be
556351762cdSJohn McCall     // an i8**.
557351762cdSJohn McCall     llvm::Value *blockField =
558351762cdSJohn McCall       Builder.CreateStructGEP(blockAddr, capture.getIndex(),
559351762cdSJohn McCall                               "block.captured");
560351762cdSJohn McCall 
561351762cdSJohn McCall     // Compute the address of the thing we're going to move into the
562351762cdSJohn McCall     // block literal.
563351762cdSJohn McCall     llvm::Value *src;
564351762cdSJohn McCall     if (ci->isNested()) {
565351762cdSJohn McCall       // We need to use the capture from the enclosing block.
566351762cdSJohn McCall       const CGBlockInfo::Capture &enclosingCapture =
567351762cdSJohn McCall         BlockInfo->getCapture(variable);
568351762cdSJohn McCall 
569351762cdSJohn McCall       // This is a [[type]]*, except that a byref entry wil just be an i8**.
570351762cdSJohn McCall       src = Builder.CreateStructGEP(LoadBlockStruct(),
571351762cdSJohn McCall                                     enclosingCapture.getIndex(),
572351762cdSJohn McCall                                     "block.capture.addr");
573351762cdSJohn McCall     } else {
574351762cdSJohn McCall       // This is a [[type]]*.
575351762cdSJohn McCall       src = LocalDeclMap[variable];
576351762cdSJohn McCall     }
577351762cdSJohn McCall 
578351762cdSJohn McCall     // For byrefs, we just write the pointer to the byref struct into
579351762cdSJohn McCall     // the block field.  There's no need to chase the forwarding
580351762cdSJohn McCall     // pointer at this point, since we're building something that will
581351762cdSJohn McCall     // live a shorter life than the stack byref anyway.
582351762cdSJohn McCall     if (ci->isByRef()) {
583e3dc1707SJohn McCall       // Get a void* that points to the byref struct.
584351762cdSJohn McCall       if (ci->isNested())
585351762cdSJohn McCall         src = Builder.CreateLoad(src, "byref.capture");
586351762cdSJohn McCall       else
587e3dc1707SJohn McCall         src = Builder.CreateBitCast(src, VoidPtrTy);
588351762cdSJohn McCall 
589e3dc1707SJohn McCall       // Write that void* into the capture field.
590351762cdSJohn McCall       Builder.CreateStore(src, blockField);
591351762cdSJohn McCall 
592351762cdSJohn McCall     // If we have a copy constructor, evaluate that into the block field.
593351762cdSJohn McCall     } else if (const Expr *copyExpr = ci->getCopyExpr()) {
594351762cdSJohn McCall       EmitSynthesizedCXXCopyCtor(blockField, src, copyExpr);
595351762cdSJohn McCall 
596351762cdSJohn McCall     // If it's a reference variable, copy the reference into the block field.
597351762cdSJohn McCall     } else if (type->isReferenceType()) {
598351762cdSJohn McCall       Builder.CreateStore(Builder.CreateLoad(src, "ref.val"), blockField);
599351762cdSJohn McCall 
600351762cdSJohn McCall     // Otherwise, fake up a POD copy into the block field.
601351762cdSJohn McCall     } else {
60231168b07SJohn McCall       // Fake up a new variable so that EmitScalarInit doesn't think
60331168b07SJohn McCall       // we're referring to the variable in its own initializer.
60431168b07SJohn McCall       ImplicitParamDecl blockFieldPseudoVar(/*DC*/ 0, SourceLocation(),
60531168b07SJohn McCall                                             /*name*/ 0, type);
60631168b07SJohn McCall 
60793be3f75SJohn McCall       // We use one of these or the other depending on whether the
60893be3f75SJohn McCall       // reference is nested.
60993be3f75SJohn McCall       DeclRefExpr notNested(const_cast<VarDecl*>(variable), type, VK_LValue,
610351762cdSJohn McCall                             SourceLocation());
61193be3f75SJohn McCall       BlockDeclRefExpr nested(const_cast<VarDecl*>(variable), type,
61293be3f75SJohn McCall                               VK_LValue, SourceLocation(), /*byref*/ false);
61393be3f75SJohn McCall 
61493be3f75SJohn McCall       Expr *declRef =
61593be3f75SJohn McCall         (ci->isNested() ? static_cast<Expr*>(&nested) : &notNested);
61693be3f75SJohn McCall 
617351762cdSJohn McCall       ImplicitCastExpr l2r(ImplicitCastExpr::OnStack, type, CK_LValueToRValue,
61893be3f75SJohn McCall                            declRef, VK_RValue);
6191553b190SJohn McCall       EmitExprAsInit(&l2r, &blockFieldPseudoVar,
6201553b190SJohn McCall                      LValue::MakeAddr(blockField, type,
6211553b190SJohn McCall                                       getContext().getDeclAlign(variable)
6221553b190SJohn McCall                                                   .getQuantity(),
6231553b190SJohn McCall                                       getContext()),
6245c8f6c47SJohn McCall                      /*captured by init*/ false);
625351762cdSJohn McCall     }
626351762cdSJohn McCall 
627351762cdSJohn McCall     // Push a destructor if necessary.  The semantics for when this
628351762cdSJohn McCall     // actually gets run are really obscure.
62931168b07SJohn McCall     if (!ci->isByRef()) {
630*4bd0fb1fSJohn McCall       switch (QualType::DestructionKind dtorKind = type.isDestructedType()) {
63131168b07SJohn McCall       case QualType::DK_none:
63231168b07SJohn McCall         break;
633*4bd0fb1fSJohn McCall 
634*4bd0fb1fSJohn McCall       // Block captures count as local values and have imprecise semantics.
635*4bd0fb1fSJohn McCall       // They also can't be arrays, so need to worry about that.
63631168b07SJohn McCall       case QualType::DK_objc_strong_lifetime:
637*4bd0fb1fSJohn McCall         pushDestroy(getCleanupKind(dtorKind), blockField, type,
638*4bd0fb1fSJohn McCall                     destroyARCStrongImprecise,
639*4bd0fb1fSJohn McCall                     /*useEHCleanupForArray*/ false);
64031168b07SJohn McCall         break;
641*4bd0fb1fSJohn McCall 
64231168b07SJohn McCall       case QualType::DK_objc_weak_lifetime:
643*4bd0fb1fSJohn McCall       case QualType::DK_cxx_destructor:
644*4bd0fb1fSJohn McCall         pushDestroy(dtorKind, blockField, type);
64531168b07SJohn McCall         break;
64631168b07SJohn McCall       }
64731168b07SJohn McCall     }
648351762cdSJohn McCall   }
649351762cdSJohn McCall 
650351762cdSJohn McCall   // Cast to the converted block-pointer type, which happens (somewhat
651351762cdSJohn McCall   // unfortunately) to be a pointer to function type.
652351762cdSJohn McCall   llvm::Value *result =
653351762cdSJohn McCall     Builder.CreateBitCast(blockAddr,
654351762cdSJohn McCall                           ConvertType(blockInfo.getBlockExpr()->getType()));
6553882ace2SJohn McCall 
656351762cdSJohn McCall   return result;
657b750d928SMike Stump }
65885284bacSMike Stump 
65985284bacSMike Stump 
660a5f58b05SChris Lattner llvm::Type *CodeGenModule::getBlockDescriptorType() {
661650c932dSMike Stump   if (BlockDescriptorType)
662650c932dSMike Stump     return BlockDescriptorType;
6632437cbfaSAnders Carlsson 
664a5f58b05SChris Lattner   llvm::Type *UnsignedLongTy =
665650c932dSMike Stump     getTypes().ConvertType(getContext().UnsignedLongTy);
6662437cbfaSAnders Carlsson 
6672437cbfaSAnders Carlsson   // struct __block_descriptor {
6682437cbfaSAnders Carlsson   //   unsigned long reserved;
6692437cbfaSAnders Carlsson   //   unsigned long block_size;
670fc83aa04SBlaine Garst   //
671fc83aa04SBlaine Garst   //   // later, the following will be added
672fc83aa04SBlaine Garst   //
673fc83aa04SBlaine Garst   //   struct {
674fc83aa04SBlaine Garst   //     void (*copyHelper)();
675fc83aa04SBlaine Garst   //     void (*copyHelper)();
676fc83aa04SBlaine Garst   //   } helpers;                // !!! optional
677fc83aa04SBlaine Garst   //
678fc83aa04SBlaine Garst   //   const char *signature;   // the block signature
679fc83aa04SBlaine Garst   //   const char *layout;      // reserved
6802437cbfaSAnders Carlsson   // };
681845511feSChris Lattner   BlockDescriptorType =
682a5f58b05SChris Lattner     llvm::StructType::createNamed("struct.__block_descriptor",
683a5f58b05SChris Lattner                                   UnsignedLongTy, UnsignedLongTy, NULL);
6842437cbfaSAnders Carlsson 
685351762cdSJohn McCall   // Now form a pointer to that.
686351762cdSJohn McCall   BlockDescriptorType = llvm::PointerType::getUnqual(BlockDescriptorType);
687650c932dSMike Stump   return BlockDescriptorType;
6882437cbfaSAnders Carlsson }
6892437cbfaSAnders Carlsson 
690a5f58b05SChris Lattner llvm::Type *CodeGenModule::getGenericBlockLiteralType() {
691005c9a62SMike Stump   if (GenericBlockLiteralType)
692005c9a62SMike Stump     return GenericBlockLiteralType;
6932437cbfaSAnders Carlsson 
694a5f58b05SChris Lattner   llvm::Type *BlockDescPtrTy = getBlockDescriptorType();
6952437cbfaSAnders Carlsson 
6962437cbfaSAnders Carlsson   // struct __block_literal_generic {
6975d2534adSMike Stump   //   void *__isa;
6985d2534adSMike Stump   //   int __flags;
6995d2534adSMike Stump   //   int __reserved;
7005d2534adSMike Stump   //   void (*__invoke)(void *);
7015d2534adSMike Stump   //   struct __block_descriptor *__descriptor;
7022437cbfaSAnders Carlsson   // };
703a5f58b05SChris Lattner   GenericBlockLiteralType =
704a5f58b05SChris Lattner     llvm::StructType::createNamed("struct.__block_literal_generic",
705a5f58b05SChris Lattner                                   VoidPtrTy,
70692bbd6d4SMike Stump                                   IntTy,
70792bbd6d4SMike Stump                                   IntTy,
708e3dc1707SJohn McCall                                   VoidPtrTy,
7092437cbfaSAnders Carlsson                                   BlockDescPtrTy,
7102437cbfaSAnders Carlsson                                   NULL);
7112437cbfaSAnders Carlsson 
712005c9a62SMike Stump   return GenericBlockLiteralType;
7132437cbfaSAnders Carlsson }
7142437cbfaSAnders Carlsson 
7155d2534adSMike Stump 
716bfb3671bSAnders Carlsson RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E,
717bfb3671bSAnders Carlsson                                           ReturnValueSlot ReturnValue) {
7182437cbfaSAnders Carlsson   const BlockPointerType *BPT =
719c23c7e6aSTed Kremenek     E->getCallee()->getType()->getAs<BlockPointerType>();
7202437cbfaSAnders Carlsson 
7212437cbfaSAnders Carlsson   llvm::Value *Callee = EmitScalarExpr(E->getCallee());
7222437cbfaSAnders Carlsson 
7232437cbfaSAnders Carlsson   // Get a pointer to the generic block literal.
7242437cbfaSAnders Carlsson   const llvm::Type *BlockLiteralTy =
7259793f0e4SOwen Anderson     llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
7262437cbfaSAnders Carlsson 
7272437cbfaSAnders Carlsson   // Bitcast the callee to a block literal.
7282437cbfaSAnders Carlsson   llvm::Value *BlockLiteral =
7292437cbfaSAnders Carlsson     Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
7302437cbfaSAnders Carlsson 
7312437cbfaSAnders Carlsson   // Get the function pointer from the literal.
7322437cbfaSAnders Carlsson   llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
7332437cbfaSAnders Carlsson 
734e3dc1707SJohn McCall   BlockLiteral = Builder.CreateBitCast(BlockLiteral, VoidPtrTy, "tmp");
7352437cbfaSAnders Carlsson 
7362437cbfaSAnders Carlsson   // Add the block literal.
7372437cbfaSAnders Carlsson   CallArgList Args;
7389dc0db21SJohn McCall   Args.add(RValue::get(BlockLiteral), getContext().VoidPtrTy);
7392437cbfaSAnders Carlsson 
740479e6fc7SAnders Carlsson   QualType FnType = BPT->getPointeeType();
741479e6fc7SAnders Carlsson 
7422437cbfaSAnders Carlsson   // And the rest of the arguments.
7439dd450bbSJohn McCall   EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(),
744479e6fc7SAnders Carlsson                E->arg_begin(), E->arg_end());
7452437cbfaSAnders Carlsson 
7465f50c651SAnders Carlsson   // Load the function.
747c76493a6SDaniel Dunbar   llvm::Value *Func = Builder.CreateLoad(FuncPtr, "tmp");
7485f50c651SAnders Carlsson 
74985915250SJohn McCall   const FunctionType *FuncTy = FnType->castAs<FunctionType>();
750ab26cfa5SJohn McCall   QualType ResultType = FuncTy->getResultType();
751a60cbcdfSAnders Carlsson 
752a60cbcdfSAnders Carlsson   const CGFunctionInfo &FnInfo =
753c50c27ccSRafael Espindola     CGM.getTypes().getFunctionInfo(ResultType, Args,
754c50c27ccSRafael Espindola                                    FuncTy->getExtInfo());
7555f50c651SAnders Carlsson 
7565f50c651SAnders Carlsson   // Cast the function pointer to the right type.
7575f50c651SAnders Carlsson   const llvm::Type *BlockFTy =
758a60cbcdfSAnders Carlsson     CGM.getTypes().GetFunctionType(FnInfo, false);
7595f50c651SAnders Carlsson 
7609793f0e4SOwen Anderson   const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
7615f50c651SAnders Carlsson   Func = Builder.CreateBitCast(Func, BlockFTyPtr);
7625f50c651SAnders Carlsson 
7632437cbfaSAnders Carlsson   // And call the block.
764bfb3671bSAnders Carlsson   return EmitCall(FnInfo, Func, ReturnValue, Args);
7652437cbfaSAnders Carlsson }
7666a60fa24SAnders Carlsson 
767351762cdSJohn McCall llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const VarDecl *variable,
768351762cdSJohn McCall                                                  bool isByRef) {
769351762cdSJohn McCall   assert(BlockInfo && "evaluating block ref without block information?");
770351762cdSJohn McCall   const CGBlockInfo::Capture &capture = BlockInfo->getCapture(variable);
77187fe5d56SJohn McCall 
772351762cdSJohn McCall   // Handle constant captures.
773351762cdSJohn McCall   if (capture.isConstant()) return LocalDeclMap[variable];
77487fe5d56SJohn McCall 
775351762cdSJohn McCall   llvm::Value *addr =
776351762cdSJohn McCall     Builder.CreateStructGEP(LoadBlockStruct(), capture.getIndex(),
777351762cdSJohn McCall                             "block.capture.addr");
778351762cdSJohn McCall 
779351762cdSJohn McCall   if (isByRef) {
780351762cdSJohn McCall     // addr should be a void** right now.  Load, then cast the result
781351762cdSJohn McCall     // to byref*.
782351762cdSJohn McCall 
783351762cdSJohn McCall     addr = Builder.CreateLoad(addr);
784351762cdSJohn McCall     const llvm::PointerType *byrefPointerType
785351762cdSJohn McCall       = llvm::PointerType::get(BuildByRefType(variable), 0);
786351762cdSJohn McCall     addr = Builder.CreateBitCast(addr, byrefPointerType,
787351762cdSJohn McCall                                  "byref.addr");
788351762cdSJohn McCall 
789351762cdSJohn McCall     // Follow the forwarding pointer.
790351762cdSJohn McCall     addr = Builder.CreateStructGEP(addr, 1, "byref.forwarding");
791351762cdSJohn McCall     addr = Builder.CreateLoad(addr, "byref.addr.forwarded");
792351762cdSJohn McCall 
793351762cdSJohn McCall     // Cast back to byref* and GEP over to the actual object.
794351762cdSJohn McCall     addr = Builder.CreateBitCast(addr, byrefPointerType);
795351762cdSJohn McCall     addr = Builder.CreateStructGEP(addr, getByRefValueLLVMField(variable),
796351762cdSJohn McCall                                    variable->getNameAsString());
79787fe5d56SJohn McCall   }
79887fe5d56SJohn McCall 
799351762cdSJohn McCall   if (variable->getType()->isReferenceType())
800351762cdSJohn McCall     addr = Builder.CreateLoad(addr, "ref.tmp");
8010168f4b2SAnders Carlsson 
802351762cdSJohn McCall   return addr;
80397d01d50SMike Stump }
80497d01d50SMike Stump 
8052d5a2878SMike Stump llvm::Constant *
806ad7c5c16SJohn McCall CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *blockExpr,
807351762cdSJohn McCall                                     const char *name) {
808351762cdSJohn McCall   CGBlockInfo blockInfo(blockExpr, name);
8096a60fa24SAnders Carlsson 
810351762cdSJohn McCall   // Compute information about the layout, etc., of this block.
811ad7c5c16SJohn McCall   computeBlockInfo(*this, blockInfo);
8126a60fa24SAnders Carlsson 
813351762cdSJohn McCall   // Using that metadata, generate the actual block function.
814351762cdSJohn McCall   llvm::Constant *blockFn;
815351762cdSJohn McCall   {
8165469f29aSMike Stump     llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
817ad7c5c16SJohn McCall     blockFn = CodeGenFunction(*this).GenerateBlockFunction(GlobalDecl(),
818351762cdSJohn McCall                                                            blockInfo,
819351762cdSJohn McCall                                                            0, LocalDeclMap);
820351762cdSJohn McCall   }
821e3dc1707SJohn McCall   blockFn = llvm::ConstantExpr::getBitCast(blockFn, VoidPtrTy);
8226a60fa24SAnders Carlsson 
823ad7c5c16SJohn McCall   return buildGlobalBlock(*this, blockInfo, blockFn);
8246a60fa24SAnders Carlsson }
8256a60fa24SAnders Carlsson 
826351762cdSJohn McCall static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM,
827351762cdSJohn McCall                                         const CGBlockInfo &blockInfo,
828351762cdSJohn McCall                                         llvm::Constant *blockFn) {
829351762cdSJohn McCall   assert(blockInfo.CanBeGlobal);
830351762cdSJohn McCall 
831351762cdSJohn McCall   // Generate the constants for the block literal initializer.
832351762cdSJohn McCall   llvm::Constant *fields[BlockHeaderSize];
833351762cdSJohn McCall 
834351762cdSJohn McCall   // isa
835351762cdSJohn McCall   fields[0] = CGM.getNSConcreteGlobalBlock();
836351762cdSJohn McCall 
837351762cdSJohn McCall   // __flags
83885915250SJohn McCall   BlockFlags flags = BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE;
83985915250SJohn McCall   if (blockInfo.UsesStret) flags |= BLOCK_USE_STRET;
84085915250SJohn McCall 
841e3dc1707SJohn McCall   fields[1] = llvm::ConstantInt::get(CGM.IntTy, flags.getBitMask());
842351762cdSJohn McCall 
843351762cdSJohn McCall   // Reserved
844e3dc1707SJohn McCall   fields[2] = llvm::Constant::getNullValue(CGM.IntTy);
845351762cdSJohn McCall 
846351762cdSJohn McCall   // Function
847351762cdSJohn McCall   fields[3] = blockFn;
848351762cdSJohn McCall 
849351762cdSJohn McCall   // Descriptor
850351762cdSJohn McCall   fields[4] = buildBlockDescriptor(CGM, blockInfo);
851351762cdSJohn McCall 
852e64d7ba1SChris Lattner   llvm::Constant *init = llvm::ConstantStruct::getAnon(fields);
853351762cdSJohn McCall 
854351762cdSJohn McCall   llvm::GlobalVariable *literal =
855351762cdSJohn McCall     new llvm::GlobalVariable(CGM.getModule(),
856351762cdSJohn McCall                              init->getType(),
857351762cdSJohn McCall                              /*constant*/ true,
858351762cdSJohn McCall                              llvm::GlobalVariable::InternalLinkage,
859351762cdSJohn McCall                              init,
860351762cdSJohn McCall                              "__block_literal_global");
861351762cdSJohn McCall   literal->setAlignment(blockInfo.BlockAlign.getQuantity());
862351762cdSJohn McCall 
863351762cdSJohn McCall   // Return a constant of the appropriately-casted type.
864351762cdSJohn McCall   const llvm::Type *requiredType =
865351762cdSJohn McCall     CGM.getTypes().ConvertType(blockInfo.getBlockExpr()->getType());
866351762cdSJohn McCall   return llvm::ConstantExpr::getBitCast(literal, requiredType);
867cb2fbcb0SMike Stump }
868cb2fbcb0SMike Stump 
8694446dcf0SMike Stump llvm::Function *
870351762cdSJohn McCall CodeGenFunction::GenerateBlockFunction(GlobalDecl GD,
871351762cdSJohn McCall                                        const CGBlockInfo &blockInfo,
872351762cdSJohn McCall                                        const Decl *outerFnDecl,
873351762cdSJohn McCall                                        const DeclMapTy &ldm) {
874351762cdSJohn McCall   const BlockDecl *blockDecl = blockInfo.getBlockDecl();
8759074ed8cSDevang Patel 
8766e40c615SDevang Patel   // Check if we should generate debug info for this block function.
8776e40c615SDevang Patel   if (CGM.getModuleDebugInfo())
8786e40c615SDevang Patel     DebugInfo = CGM.getModuleDebugInfo();
8796e40c615SDevang Patel 
880351762cdSJohn McCall   BlockInfo = &blockInfo;
8819074ed8cSDevang Patel 
8825469f29aSMike Stump   // Arrange for local static and local extern declarations to appear
883351762cdSJohn McCall   // to be local to this function as well, in case they're directly
884351762cdSJohn McCall   // referenced in a block.
885351762cdSJohn McCall   for (DeclMapTy::const_iterator i = ldm.begin(), e = ldm.end(); i != e; ++i) {
886351762cdSJohn McCall     const VarDecl *var = dyn_cast<VarDecl>(i->first);
887351762cdSJohn McCall     if (var && !var->hasLocalStorage())
888351762cdSJohn McCall       LocalDeclMap[var] = i->second;
8895469f29aSMike Stump   }
8905469f29aSMike Stump 
891351762cdSJohn McCall   // Begin building the function declaration.
89209a9b6e3SEli Friedman 
893351762cdSJohn McCall   // Build the argument list.
894351762cdSJohn McCall   FunctionArgList args;
8956a60fa24SAnders Carlsson 
896351762cdSJohn McCall   // The first argument is the block pointer.  Just take it as a void*
897351762cdSJohn McCall   // and cast it later.
898351762cdSJohn McCall   QualType selfTy = getContext().VoidPtrTy;
8997fe9cc1dSMike Stump   IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor");
900d015328fSMike Stump 
901147d021bSJohn McCall   ImplicitParamDecl selfDecl(const_cast<BlockDecl*>(blockDecl),
902351762cdSJohn McCall                              SourceLocation(), II, selfTy);
903a738c25fSJohn McCall   args.push_back(&selfDecl);
9047fe9cc1dSMike Stump 
905351762cdSJohn McCall   // Now add the rest of the parameters.
906351762cdSJohn McCall   for (BlockDecl::param_const_iterator i = blockDecl->param_begin(),
907351762cdSJohn McCall        e = blockDecl->param_end(); i != e; ++i)
908a738c25fSJohn McCall     args.push_back(*i);
909afa3c0a8SFariborz Jahanian 
910351762cdSJohn McCall   // Create the function declaration.
911351762cdSJohn McCall   const FunctionProtoType *fnType =
912351762cdSJohn McCall     cast<FunctionProtoType>(blockInfo.getBlockExpr()->getFunctionType());
913351762cdSJohn McCall   const CGFunctionInfo &fnInfo =
914351762cdSJohn McCall     CGM.getTypes().getFunctionInfo(fnType->getResultType(), args,
915351762cdSJohn McCall                                    fnType->getExtInfo());
91685915250SJohn McCall   if (CGM.ReturnTypeUsesSRet(fnInfo))
91785915250SJohn McCall     blockInfo.UsesStret = true;
91885915250SJohn McCall 
919351762cdSJohn McCall   const llvm::FunctionType *fnLLVMType =
920351762cdSJohn McCall     CGM.getTypes().GetFunctionType(fnInfo, fnType->isVariadic());
92187fe5d56SJohn McCall 
922351762cdSJohn McCall   MangleBuffer name;
923351762cdSJohn McCall   CGM.getBlockMangledName(GD, name, blockDecl);
924351762cdSJohn McCall   llvm::Function *fn =
925351762cdSJohn McCall     llvm::Function::Create(fnLLVMType, llvm::GlobalValue::InternalLinkage,
926351762cdSJohn McCall                            name.getString(), &CGM.getModule());
927351762cdSJohn McCall   CGM.SetInternalFunctionAttributes(blockDecl, fn, fnInfo);
9286a60fa24SAnders Carlsson 
929351762cdSJohn McCall   // Begin generating the function.
930a738c25fSJohn McCall   StartFunction(blockDecl, fnType->getResultType(), fn, fnInfo, args,
9315f070a51SDevang Patel                 blockInfo.getBlockExpr()->getBody()->getLocStart());
932351762cdSJohn McCall   CurFuncDecl = outerFnDecl; // StartFunction sets this to blockDecl
9336a60fa24SAnders Carlsson 
934147d021bSJohn McCall   // Okay.  Undo some of what StartFunction did.
935147d021bSJohn McCall 
936147d021bSJohn McCall   // Pull the 'self' reference out of the local decl map.
937147d021bSJohn McCall   llvm::Value *blockAddr = LocalDeclMap[&selfDecl];
938147d021bSJohn McCall   LocalDeclMap.erase(&selfDecl);
939351762cdSJohn McCall   BlockPointer = Builder.CreateBitCast(blockAddr,
940351762cdSJohn McCall                                        blockInfo.StructureType->getPointerTo(),
941351762cdSJohn McCall                                        "block");
942017460abSMike Stump 
94387fe5d56SJohn McCall   // If we have a C++ 'this' reference, go ahead and force it into
94487fe5d56SJohn McCall   // existence now.
945351762cdSJohn McCall   if (blockDecl->capturesCXXThis()) {
946351762cdSJohn McCall     llvm::Value *addr = Builder.CreateStructGEP(BlockPointer,
947351762cdSJohn McCall                                                 blockInfo.CXXThisIndex,
948351762cdSJohn McCall                                                 "block.captured-this");
949351762cdSJohn McCall     CXXThisValue = Builder.CreateLoad(addr, "this");
95087fe5d56SJohn McCall   }
95187fe5d56SJohn McCall 
952351762cdSJohn McCall   // LoadObjCSelf() expects there to be an entry for 'self' in LocalDeclMap;
953351762cdSJohn McCall   // appease it.
954351762cdSJohn McCall   if (const ObjCMethodDecl *method
955351762cdSJohn McCall         = dyn_cast_or_null<ObjCMethodDecl>(CurFuncDecl)) {
956351762cdSJohn McCall     const VarDecl *self = method->getSelfDecl();
957351762cdSJohn McCall 
958351762cdSJohn McCall     // There might not be a capture for 'self', but if there is...
959351762cdSJohn McCall     if (blockInfo.Captures.count(self)) {
960351762cdSJohn McCall       const CGBlockInfo::Capture &capture = blockInfo.getCapture(self);
961351762cdSJohn McCall       llvm::Value *selfAddr = Builder.CreateStructGEP(BlockPointer,
962351762cdSJohn McCall                                                       capture.getIndex(),
963351762cdSJohn McCall                                                       "block.captured-self");
964351762cdSJohn McCall       LocalDeclMap[self] = selfAddr;
965351762cdSJohn McCall     }
966351762cdSJohn McCall   }
967351762cdSJohn McCall 
968351762cdSJohn McCall   // Also force all the constant captures.
969351762cdSJohn McCall   for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
970351762cdSJohn McCall          ce = blockDecl->capture_end(); ci != ce; ++ci) {
971351762cdSJohn McCall     const VarDecl *variable = ci->getVariable();
972351762cdSJohn McCall     const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
973351762cdSJohn McCall     if (!capture.isConstant()) continue;
974351762cdSJohn McCall 
975351762cdSJohn McCall     unsigned align = getContext().getDeclAlign(variable).getQuantity();
976351762cdSJohn McCall 
977351762cdSJohn McCall     llvm::AllocaInst *alloca =
978351762cdSJohn McCall       CreateMemTemp(variable->getType(), "block.captured-const");
979351762cdSJohn McCall     alloca->setAlignment(align);
980351762cdSJohn McCall 
981351762cdSJohn McCall     Builder.CreateStore(capture.getConstant(), alloca, align);
982351762cdSJohn McCall 
983351762cdSJohn McCall     LocalDeclMap[variable] = alloca;
9849d42f0f1SJohn McCall   }
9859d42f0f1SJohn McCall 
986017460abSMike Stump   // Save a spot to insert the debug information for all the BlockDeclRefDecls.
987017460abSMike Stump   llvm::BasicBlock *entry = Builder.GetInsertBlock();
988017460abSMike Stump   llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint();
989017460abSMike Stump   --entry_ptr;
990017460abSMike Stump 
991351762cdSJohn McCall   EmitStmt(blockDecl->getBody());
992017460abSMike Stump 
9937d69911aSMike Stump   // Remember where we were...
9947d69911aSMike Stump   llvm::BasicBlock *resume = Builder.GetInsertBlock();
995017460abSMike Stump 
9967d69911aSMike Stump   // Go back to the entry.
997017460abSMike Stump   ++entry_ptr;
998017460abSMike Stump   Builder.SetInsertPoint(entry, entry_ptr);
999017460abSMike Stump 
10002e722b91SMike Stump   // Emit debug information for all the BlockDeclRefDecls.
100187fe5d56SJohn McCall   // FIXME: also for 'this'
1002351762cdSJohn McCall   if (CGDebugInfo *DI = getDebugInfo()) {
1003351762cdSJohn McCall     for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1004351762cdSJohn McCall            ce = blockDecl->capture_end(); ci != ce; ++ci) {
1005351762cdSJohn McCall       const VarDecl *variable = ci->getVariable();
1006351762cdSJohn McCall       DI->setLocation(variable->getLocation());
1007351762cdSJohn McCall 
1008351762cdSJohn McCall       const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1009351762cdSJohn McCall       if (capture.isConstant()) {
1010351762cdSJohn McCall         DI->EmitDeclareOfAutoVariable(variable, LocalDeclMap[variable],
1011351762cdSJohn McCall                                       Builder);
1012351762cdSJohn McCall         continue;
1013351762cdSJohn McCall       }
1014351762cdSJohn McCall 
1015147d021bSJohn McCall       DI->EmitDeclareOfBlockDeclRefVariable(variable, BlockPointer,
1016351762cdSJohn McCall                                             Builder, blockInfo);
10172e722b91SMike Stump     }
10182e722b91SMike Stump   }
1019351762cdSJohn McCall 
10207d69911aSMike Stump   // And resume where we left off.
10217d69911aSMike Stump   if (resume == 0)
10227d69911aSMike Stump     Builder.ClearInsertionPoint();
10237d69911aSMike Stump   else
10247d69911aSMike Stump     Builder.SetInsertPoint(resume);
10252e722b91SMike Stump 
1026351762cdSJohn McCall   FinishFunction(cast<CompoundStmt>(blockDecl->getBody())->getRBracLoc());
10276a60fa24SAnders Carlsson 
1028351762cdSJohn McCall   return fn;
10296a60fa24SAnders Carlsson }
10301db7d04bSMike Stump 
1031351762cdSJohn McCall /*
1032351762cdSJohn McCall     notes.push_back(HelperInfo());
1033351762cdSJohn McCall     HelperInfo &note = notes.back();
1034351762cdSJohn McCall     note.index = capture.getIndex();
1035351762cdSJohn McCall     note.RequiresCopying = (ci->hasCopyExpr() || BlockRequiresCopying(type));
1036351762cdSJohn McCall     note.cxxbar_import = ci->getCopyExpr();
10371db7d04bSMike Stump 
1038351762cdSJohn McCall     if (ci->isByRef()) {
1039351762cdSJohn McCall       note.flag = BLOCK_FIELD_IS_BYREF;
1040351762cdSJohn McCall       if (type.isObjCGCWeak())
1041351762cdSJohn McCall         note.flag |= BLOCK_FIELD_IS_WEAK;
1042351762cdSJohn McCall     } else if (type->isBlockPointerType()) {
1043351762cdSJohn McCall       note.flag = BLOCK_FIELD_IS_BLOCK;
1044351762cdSJohn McCall     } else {
1045351762cdSJohn McCall       note.flag = BLOCK_FIELD_IS_OBJECT;
10461db7d04bSMike Stump     }
1047351762cdSJohn McCall  */
10481db7d04bSMike Stump 
104997d01d50SMike Stump 
10500c743277SMike Stump 
1051351762cdSJohn McCall llvm::Constant *
1052ad7c5c16SJohn McCall CodeGenFunction::GenerateCopyHelperFunction(const CGBlockInfo &blockInfo) {
1053351762cdSJohn McCall   ASTContext &C = getContext();
1054351762cdSJohn McCall 
1055351762cdSJohn McCall   FunctionArgList args;
1056a738c25fSJohn McCall   ImplicitParamDecl dstDecl(0, SourceLocation(), 0, C.VoidPtrTy);
1057a738c25fSJohn McCall   args.push_back(&dstDecl);
1058a738c25fSJohn McCall   ImplicitParamDecl srcDecl(0, SourceLocation(), 0, C.VoidPtrTy);
1059a738c25fSJohn McCall   args.push_back(&srcDecl);
10600c743277SMike Stump 
10610c743277SMike Stump   const CGFunctionInfo &FI =
1062351762cdSJohn McCall       CGM.getTypes().getFunctionInfo(C.VoidTy, args, FunctionType::ExtInfo());
10630c743277SMike Stump 
1064351762cdSJohn McCall   // FIXME: it would be nice if these were mergeable with things with
1065351762cdSJohn McCall   // identical semantics.
1066351762cdSJohn McCall   const llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI, false);
10670c743277SMike Stump 
10680c743277SMike Stump   llvm::Function *Fn =
10690c743277SMike Stump     llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
1070d6b28fc6SBenjamin Kramer                            "__copy_helper_block_", &CGM.getModule());
10710c743277SMike Stump 
10720c743277SMike Stump   IdentifierInfo *II
10730c743277SMike Stump     = &CGM.getContext().Idents.get("__copy_helper_block_");
10740c743277SMike Stump 
10752a7c2194SDevang Patel   // Check if we should generate debug info for this block helper function.
10762a7c2194SDevang Patel   if (CGM.getModuleDebugInfo())
10772a7c2194SDevang Patel     DebugInfo = CGM.getModuleDebugInfo();
10782a7c2194SDevang Patel 
1079351762cdSJohn McCall   FunctionDecl *FD = FunctionDecl::Create(C,
1080351762cdSJohn McCall                                           C.getTranslationUnitDecl(),
1081dff1930bSAbramo Bagnara                                           SourceLocation(),
1082351762cdSJohn McCall                                           SourceLocation(), II, C.VoidTy, 0,
10838e7d656aSJohn McCall                                           SC_Static,
10848e7d656aSJohn McCall                                           SC_None,
1085c4df4076SDouglas Gregor                                           false,
10860c743277SMike Stump                                           true);
1087a738c25fSJohn McCall   StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation());
1088aeb0ffd5SMike Stump 
1089351762cdSJohn McCall   const llvm::Type *structPtrTy = blockInfo.StructureType->getPointerTo();
109067645930SMike Stump 
1091a738c25fSJohn McCall   llvm::Value *src = GetAddrOfLocalVar(&srcDecl);
1092ad7c5c16SJohn McCall   src = Builder.CreateLoad(src);
1093ad7c5c16SJohn McCall   src = Builder.CreateBitCast(src, structPtrTy, "block.source");
109467645930SMike Stump 
1095a738c25fSJohn McCall   llvm::Value *dst = GetAddrOfLocalVar(&dstDecl);
1096ad7c5c16SJohn McCall   dst = Builder.CreateLoad(dst);
1097ad7c5c16SJohn McCall   dst = Builder.CreateBitCast(dst, structPtrTy, "block.dest");
1098aeb0ffd5SMike Stump 
1099351762cdSJohn McCall   const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1100aeb0ffd5SMike Stump 
1101351762cdSJohn McCall   for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1102351762cdSJohn McCall          ce = blockDecl->capture_end(); ci != ce; ++ci) {
1103351762cdSJohn McCall     const VarDecl *variable = ci->getVariable();
1104351762cdSJohn McCall     QualType type = variable->getType();
1105aeb0ffd5SMike Stump 
1106351762cdSJohn McCall     const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1107351762cdSJohn McCall     if (capture.isConstant()) continue;
1108351762cdSJohn McCall 
1109351762cdSJohn McCall     const Expr *copyExpr = ci->getCopyExpr();
111031168b07SJohn McCall     BlockFieldFlags flags;
111131168b07SJohn McCall 
111231168b07SJohn McCall     bool isARCWeakCapture = false;
1113351762cdSJohn McCall 
1114351762cdSJohn McCall     if (copyExpr) {
1115351762cdSJohn McCall       assert(!ci->isByRef());
1116351762cdSJohn McCall       // don't bother computing flags
111731168b07SJohn McCall 
1118351762cdSJohn McCall     } else if (ci->isByRef()) {
1119351762cdSJohn McCall       flags = BLOCK_FIELD_IS_BYREF;
112031168b07SJohn McCall       if (type.isObjCGCWeak())
112131168b07SJohn McCall         flags |= BLOCK_FIELD_IS_WEAK;
1122351762cdSJohn McCall 
112331168b07SJohn McCall     } else if (type->isObjCRetainableType()) {
112431168b07SJohn McCall       flags = BLOCK_FIELD_IS_OBJECT;
112531168b07SJohn McCall       if (type->isBlockPointerType())
112631168b07SJohn McCall         flags = BLOCK_FIELD_IS_BLOCK;
112731168b07SJohn McCall 
112831168b07SJohn McCall       // Special rules for ARC captures:
112931168b07SJohn McCall       if (getLangOptions().ObjCAutoRefCount) {
113031168b07SJohn McCall         Qualifiers qs = type.getQualifiers();
113131168b07SJohn McCall 
113231168b07SJohn McCall         // Don't generate special copy logic for a captured object
113331168b07SJohn McCall         // unless it's __strong or __weak.
113431168b07SJohn McCall         if (!qs.hasStrongOrWeakObjCLifetime())
113531168b07SJohn McCall           continue;
113631168b07SJohn McCall 
113731168b07SJohn McCall         // Support __weak direct captures.
113831168b07SJohn McCall         if (qs.getObjCLifetime() == Qualifiers::OCL_Weak)
113931168b07SJohn McCall           isARCWeakCapture = true;
114031168b07SJohn McCall       }
114131168b07SJohn McCall     } else {
114231168b07SJohn McCall       continue;
114331168b07SJohn McCall     }
1144351762cdSJohn McCall 
1145351762cdSJohn McCall     unsigned index = capture.getIndex();
1146ad7c5c16SJohn McCall     llvm::Value *srcField = Builder.CreateStructGEP(src, index);
1147ad7c5c16SJohn McCall     llvm::Value *dstField = Builder.CreateStructGEP(dst, index);
1148351762cdSJohn McCall 
1149351762cdSJohn McCall     // If there's an explicit copy expression, we do that.
1150351762cdSJohn McCall     if (copyExpr) {
1151ad7c5c16SJohn McCall       EmitSynthesizedCXXCopyCtor(dstField, srcField, copyExpr);
115231168b07SJohn McCall     } else if (isARCWeakCapture) {
115331168b07SJohn McCall       EmitARCCopyWeak(dstField, srcField);
1154351762cdSJohn McCall     } else {
1155351762cdSJohn McCall       llvm::Value *srcValue = Builder.CreateLoad(srcField, "blockcopy.src");
1156e3dc1707SJohn McCall       srcValue = Builder.CreateBitCast(srcValue, VoidPtrTy);
1157e3dc1707SJohn McCall       llvm::Value *dstAddr = Builder.CreateBitCast(dstField, VoidPtrTy);
1158351762cdSJohn McCall       Builder.CreateCall3(CGM.getBlockObjectAssign(), dstAddr, srcValue,
115931168b07SJohn McCall                           llvm::ConstantInt::get(Int32Ty, flags.getBitMask()));
116067645930SMike Stump     }
1161e988bdacSFariborz Jahanian   }
1162aeb0ffd5SMike Stump 
1163ad7c5c16SJohn McCall   FinishFunction();
11640c743277SMike Stump 
1165e3dc1707SJohn McCall   return llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy);
116697d01d50SMike Stump }
116797d01d50SMike Stump 
1168351762cdSJohn McCall llvm::Constant *
1169ad7c5c16SJohn McCall CodeGenFunction::GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo) {
1170351762cdSJohn McCall   ASTContext &C = getContext();
11710c743277SMike Stump 
1172351762cdSJohn McCall   FunctionArgList args;
1173a738c25fSJohn McCall   ImplicitParamDecl srcDecl(0, SourceLocation(), 0, C.VoidPtrTy);
1174a738c25fSJohn McCall   args.push_back(&srcDecl);
11750c743277SMike Stump 
11760c743277SMike Stump   const CGFunctionInfo &FI =
1177351762cdSJohn McCall       CGM.getTypes().getFunctionInfo(C.VoidTy, args, FunctionType::ExtInfo());
11780c743277SMike Stump 
1179cbc2bcafSMike Stump   // FIXME: We'd like to put these into a mergable by content, with
1180cbc2bcafSMike Stump   // internal linkage.
1181351762cdSJohn McCall   const llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI, false);
11820c743277SMike Stump 
11830c743277SMike Stump   llvm::Function *Fn =
11840c743277SMike Stump     llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
1185d6b28fc6SBenjamin Kramer                            "__destroy_helper_block_", &CGM.getModule());
11860c743277SMike Stump 
11872a7c2194SDevang Patel   // Check if we should generate debug info for this block destroy function.
11882a7c2194SDevang Patel   if (CGM.getModuleDebugInfo())
11892a7c2194SDevang Patel     DebugInfo = CGM.getModuleDebugInfo();
11902a7c2194SDevang Patel 
11910c743277SMike Stump   IdentifierInfo *II
11920c743277SMike Stump     = &CGM.getContext().Idents.get("__destroy_helper_block_");
11930c743277SMike Stump 
1194351762cdSJohn McCall   FunctionDecl *FD = FunctionDecl::Create(C, C.getTranslationUnitDecl(),
1195dff1930bSAbramo Bagnara                                           SourceLocation(),
1196351762cdSJohn McCall                                           SourceLocation(), II, C.VoidTy, 0,
11978e7d656aSJohn McCall                                           SC_Static,
11988e7d656aSJohn McCall                                           SC_None,
1199c4df4076SDouglas Gregor                                           false, true);
1200a738c25fSJohn McCall   StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation());
12016f7d9f87SMike Stump 
1202351762cdSJohn McCall   const llvm::Type *structPtrTy = blockInfo.StructureType->getPointerTo();
120367645930SMike Stump 
1204a738c25fSJohn McCall   llvm::Value *src = GetAddrOfLocalVar(&srcDecl);
1205ad7c5c16SJohn McCall   src = Builder.CreateLoad(src);
1206ad7c5c16SJohn McCall   src = Builder.CreateBitCast(src, structPtrTy, "block");
12076f7d9f87SMike Stump 
1208351762cdSJohn McCall   const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1209351762cdSJohn McCall 
1210ad7c5c16SJohn McCall   CodeGenFunction::RunCleanupsScope cleanups(*this);
1211351762cdSJohn McCall 
1212351762cdSJohn McCall   for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1213351762cdSJohn McCall          ce = blockDecl->capture_end(); ci != ce; ++ci) {
1214351762cdSJohn McCall     const VarDecl *variable = ci->getVariable();
1215351762cdSJohn McCall     QualType type = variable->getType();
1216351762cdSJohn McCall 
1217351762cdSJohn McCall     const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1218351762cdSJohn McCall     if (capture.isConstant()) continue;
1219351762cdSJohn McCall 
1220ad7c5c16SJohn McCall     BlockFieldFlags flags;
1221351762cdSJohn McCall     const CXXDestructorDecl *dtor = 0;
1222351762cdSJohn McCall 
122331168b07SJohn McCall     bool isARCWeakCapture = false;
122431168b07SJohn McCall 
1225351762cdSJohn McCall     if (ci->isByRef()) {
1226351762cdSJohn McCall       flags = BLOCK_FIELD_IS_BYREF;
122731168b07SJohn McCall       if (type.isObjCGCWeak())
122831168b07SJohn McCall         flags |= BLOCK_FIELD_IS_WEAK;
122931168b07SJohn McCall     } else if (const CXXRecordDecl *record = type->getAsCXXRecordDecl()) {
123031168b07SJohn McCall       if (record->hasTrivialDestructor())
123131168b07SJohn McCall         continue;
1232351762cdSJohn McCall       dtor = record->getDestructor();
123331168b07SJohn McCall     } else if (type->isObjCRetainableType()) {
123431168b07SJohn McCall       flags = BLOCK_FIELD_IS_OBJECT;
123531168b07SJohn McCall       if (type->isBlockPointerType())
123631168b07SJohn McCall         flags = BLOCK_FIELD_IS_BLOCK;
1237351762cdSJohn McCall 
123831168b07SJohn McCall       // Special rules for ARC captures.
123931168b07SJohn McCall       if (getLangOptions().ObjCAutoRefCount) {
124031168b07SJohn McCall         Qualifiers qs = type.getQualifiers();
124131168b07SJohn McCall 
124231168b07SJohn McCall         // Don't generate special dispose logic for a captured object
124331168b07SJohn McCall         // unless it's __strong or __weak.
124431168b07SJohn McCall         if (!qs.hasStrongOrWeakObjCLifetime())
124531168b07SJohn McCall           continue;
124631168b07SJohn McCall 
124731168b07SJohn McCall         // Support __weak direct captures.
124831168b07SJohn McCall         if (qs.getObjCLifetime() == Qualifiers::OCL_Weak)
124931168b07SJohn McCall           isARCWeakCapture = true;
125031168b07SJohn McCall       }
125131168b07SJohn McCall     } else {
125231168b07SJohn McCall       continue;
125331168b07SJohn McCall     }
1254351762cdSJohn McCall 
1255351762cdSJohn McCall     unsigned index = capture.getIndex();
1256ad7c5c16SJohn McCall     llvm::Value *srcField = Builder.CreateStructGEP(src, index);
1257351762cdSJohn McCall 
1258351762cdSJohn McCall     // If there's an explicit copy expression, we do that.
1259351762cdSJohn McCall     if (dtor) {
1260ad7c5c16SJohn McCall       PushDestructorCleanup(dtor, srcField);
1261351762cdSJohn McCall 
126231168b07SJohn McCall     // If this is a __weak capture, emit the release directly.
126331168b07SJohn McCall     } else if (isARCWeakCapture) {
126431168b07SJohn McCall       EmitARCDestroyWeak(srcField);
126531168b07SJohn McCall 
1266351762cdSJohn McCall     // Otherwise we call _Block_object_dispose.  It wouldn't be too
1267351762cdSJohn McCall     // hard to just emit this as a cleanup if we wanted to make sure
1268351762cdSJohn McCall     // that things were done in reverse.
1269351762cdSJohn McCall     } else {
1270351762cdSJohn McCall       llvm::Value *value = Builder.CreateLoad(srcField);
1271e3dc1707SJohn McCall       value = Builder.CreateBitCast(value, VoidPtrTy);
1272351762cdSJohn McCall       BuildBlockRelease(value, flags);
12736f7d9f87SMike Stump     }
12746f7d9f87SMike Stump   }
1275351762cdSJohn McCall 
1276351762cdSJohn McCall   cleanups.ForceCleanup();
12776f7d9f87SMike Stump 
1278ad7c5c16SJohn McCall   FinishFunction();
12790c743277SMike Stump 
1280e3dc1707SJohn McCall   return llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy);
12810c743277SMike Stump }
12820c743277SMike Stump 
1283f9b056b0SJohn McCall namespace {
1284f9b056b0SJohn McCall 
1285f9b056b0SJohn McCall /// Emits the copy/dispose helper functions for a __block object of id type.
1286f9b056b0SJohn McCall class ObjectByrefHelpers : public CodeGenModule::ByrefHelpers {
1287f9b056b0SJohn McCall   BlockFieldFlags Flags;
1288f9b056b0SJohn McCall 
1289f9b056b0SJohn McCall public:
1290f9b056b0SJohn McCall   ObjectByrefHelpers(CharUnits alignment, BlockFieldFlags flags)
1291f9b056b0SJohn McCall     : ByrefHelpers(alignment), Flags(flags) {}
1292f9b056b0SJohn McCall 
12937c623641SJohn McCall   void emitCopy(CodeGenFunction &CGF, llvm::Value *destField,
12947c623641SJohn McCall                 llvm::Value *srcField) {
1295f9b056b0SJohn McCall     destField = CGF.Builder.CreateBitCast(destField, CGF.VoidPtrTy);
1296f9b056b0SJohn McCall 
1297f9b056b0SJohn McCall     srcField = CGF.Builder.CreateBitCast(srcField, CGF.VoidPtrPtrTy);
1298f9b056b0SJohn McCall     llvm::Value *srcValue = CGF.Builder.CreateLoad(srcField);
1299f9b056b0SJohn McCall 
1300f9b056b0SJohn McCall     unsigned flags = (Flags | BLOCK_BYREF_CALLER).getBitMask();
1301f9b056b0SJohn McCall 
1302f9b056b0SJohn McCall     llvm::Value *flagsVal = llvm::ConstantInt::get(CGF.Int32Ty, flags);
1303f9b056b0SJohn McCall     llvm::Value *fn = CGF.CGM.getBlockObjectAssign();
1304f9b056b0SJohn McCall     CGF.Builder.CreateCall3(fn, destField, srcValue, flagsVal);
1305f9b056b0SJohn McCall   }
1306f9b056b0SJohn McCall 
1307f9b056b0SJohn McCall   void emitDispose(CodeGenFunction &CGF, llvm::Value *field) {
1308f9b056b0SJohn McCall     field = CGF.Builder.CreateBitCast(field, CGF.Int8PtrTy->getPointerTo(0));
1309f9b056b0SJohn McCall     llvm::Value *value = CGF.Builder.CreateLoad(field);
1310f9b056b0SJohn McCall 
1311f9b056b0SJohn McCall     CGF.BuildBlockRelease(value, Flags | BLOCK_BYREF_CALLER);
1312f9b056b0SJohn McCall   }
1313f9b056b0SJohn McCall 
1314f9b056b0SJohn McCall   void profileImpl(llvm::FoldingSetNodeID &id) const {
1315f9b056b0SJohn McCall     id.AddInteger(Flags.getBitMask());
1316f9b056b0SJohn McCall   }
1317f9b056b0SJohn McCall };
1318f9b056b0SJohn McCall 
131931168b07SJohn McCall /// Emits the copy/dispose helpers for an ARC __block __weak variable.
132031168b07SJohn McCall class ARCWeakByrefHelpers : public CodeGenModule::ByrefHelpers {
132131168b07SJohn McCall public:
132231168b07SJohn McCall   ARCWeakByrefHelpers(CharUnits alignment) : ByrefHelpers(alignment) {}
132331168b07SJohn McCall 
132431168b07SJohn McCall   void emitCopy(CodeGenFunction &CGF, llvm::Value *destField,
132531168b07SJohn McCall                 llvm::Value *srcField) {
132631168b07SJohn McCall     CGF.EmitARCMoveWeak(destField, srcField);
132731168b07SJohn McCall   }
132831168b07SJohn McCall 
132931168b07SJohn McCall   void emitDispose(CodeGenFunction &CGF, llvm::Value *field) {
133031168b07SJohn McCall     CGF.EmitARCDestroyWeak(field);
133131168b07SJohn McCall   }
133231168b07SJohn McCall 
133331168b07SJohn McCall   void profileImpl(llvm::FoldingSetNodeID &id) const {
133431168b07SJohn McCall     // 0 is distinguishable from all pointers and byref flags
133531168b07SJohn McCall     id.AddInteger(0);
133631168b07SJohn McCall   }
133731168b07SJohn McCall };
133831168b07SJohn McCall 
133931168b07SJohn McCall /// Emits the copy/dispose helpers for an ARC __block __strong variable
134031168b07SJohn McCall /// that's not of block-pointer type.
134131168b07SJohn McCall class ARCStrongByrefHelpers : public CodeGenModule::ByrefHelpers {
134231168b07SJohn McCall public:
134331168b07SJohn McCall   ARCStrongByrefHelpers(CharUnits alignment) : ByrefHelpers(alignment) {}
134431168b07SJohn McCall 
134531168b07SJohn McCall   void emitCopy(CodeGenFunction &CGF, llvm::Value *destField,
134631168b07SJohn McCall                 llvm::Value *srcField) {
134731168b07SJohn McCall     // Do a "move" by copying the value and then zeroing out the old
134831168b07SJohn McCall     // variable.
134931168b07SJohn McCall 
135031168b07SJohn McCall     llvm::Value *value = CGF.Builder.CreateLoad(srcField);
135131168b07SJohn McCall     llvm::Value *null =
135231168b07SJohn McCall       llvm::ConstantPointerNull::get(cast<llvm::PointerType>(value->getType()));
135331168b07SJohn McCall     CGF.Builder.CreateStore(value, destField);
135431168b07SJohn McCall     CGF.Builder.CreateStore(null, srcField);
135531168b07SJohn McCall   }
135631168b07SJohn McCall 
135731168b07SJohn McCall   void emitDispose(CodeGenFunction &CGF, llvm::Value *field) {
135831168b07SJohn McCall     llvm::Value *value = CGF.Builder.CreateLoad(field);
135931168b07SJohn McCall     CGF.EmitARCRelease(value, /*precise*/ false);
136031168b07SJohn McCall   }
136131168b07SJohn McCall 
136231168b07SJohn McCall   void profileImpl(llvm::FoldingSetNodeID &id) const {
136331168b07SJohn McCall     // 1 is distinguishable from all pointers and byref flags
136431168b07SJohn McCall     id.AddInteger(1);
136531168b07SJohn McCall   }
136631168b07SJohn McCall };
136731168b07SJohn McCall 
1368f9b056b0SJohn McCall /// Emits the copy/dispose helpers for a __block variable with a
1369f9b056b0SJohn McCall /// nontrivial copy constructor or destructor.
1370f9b056b0SJohn McCall class CXXByrefHelpers : public CodeGenModule::ByrefHelpers {
1371f9b056b0SJohn McCall   QualType VarType;
1372f9b056b0SJohn McCall   const Expr *CopyExpr;
1373f9b056b0SJohn McCall 
1374f9b056b0SJohn McCall public:
1375f9b056b0SJohn McCall   CXXByrefHelpers(CharUnits alignment, QualType type,
1376f9b056b0SJohn McCall                   const Expr *copyExpr)
1377f9b056b0SJohn McCall     : ByrefHelpers(alignment), VarType(type), CopyExpr(copyExpr) {}
1378f9b056b0SJohn McCall 
1379f9b056b0SJohn McCall   bool needsCopy() const { return CopyExpr != 0; }
1380f9b056b0SJohn McCall   void emitCopy(CodeGenFunction &CGF, llvm::Value *destField,
1381f9b056b0SJohn McCall                 llvm::Value *srcField) {
1382f9b056b0SJohn McCall     if (!CopyExpr) return;
1383f9b056b0SJohn McCall     CGF.EmitSynthesizedCXXCopyCtor(destField, srcField, CopyExpr);
1384f9b056b0SJohn McCall   }
1385f9b056b0SJohn McCall 
1386f9b056b0SJohn McCall   void emitDispose(CodeGenFunction &CGF, llvm::Value *field) {
1387f9b056b0SJohn McCall     EHScopeStack::stable_iterator cleanupDepth = CGF.EHStack.stable_begin();
1388f9b056b0SJohn McCall     CGF.PushDestructorCleanup(VarType, field);
1389f9b056b0SJohn McCall     CGF.PopCleanupBlocks(cleanupDepth);
1390f9b056b0SJohn McCall   }
1391f9b056b0SJohn McCall 
1392f9b056b0SJohn McCall   void profileImpl(llvm::FoldingSetNodeID &id) const {
1393f9b056b0SJohn McCall     id.AddPointer(VarType.getCanonicalType().getAsOpaquePtr());
1394f9b056b0SJohn McCall   }
1395f9b056b0SJohn McCall };
1396f9b056b0SJohn McCall } // end anonymous namespace
1397f9b056b0SJohn McCall 
1398f9b056b0SJohn McCall static llvm::Constant *
1399f9b056b0SJohn McCall generateByrefCopyHelper(CodeGenFunction &CGF,
1400f9b056b0SJohn McCall                         const llvm::StructType &byrefType,
1401f9b056b0SJohn McCall                         CodeGenModule::ByrefHelpers &byrefInfo) {
1402f9b056b0SJohn McCall   ASTContext &Context = CGF.getContext();
1403f9b056b0SJohn McCall 
1404f9b056b0SJohn McCall   QualType R = Context.VoidTy;
1405ee2a5ee5SMike Stump 
1406a738c25fSJohn McCall   FunctionArgList args;
1407f9b056b0SJohn McCall   ImplicitParamDecl dst(0, SourceLocation(), 0, Context.VoidPtrTy);
1408a738c25fSJohn McCall   args.push_back(&dst);
1409f89230d9SMike Stump 
1410f9b056b0SJohn McCall   ImplicitParamDecl src(0, SourceLocation(), 0, Context.VoidPtrTy);
1411a738c25fSJohn McCall   args.push_back(&src);
1412ee2a5ee5SMike Stump 
1413ee2a5ee5SMike Stump   const CGFunctionInfo &FI =
1414f9b056b0SJohn McCall     CGF.CGM.getTypes().getFunctionInfo(R, args, FunctionType::ExtInfo());
1415ee2a5ee5SMike Stump 
1416f9b056b0SJohn McCall   CodeGenTypes &Types = CGF.CGM.getTypes();
1417ee2a5ee5SMike Stump   const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1418ee2a5ee5SMike Stump 
1419cbc2bcafSMike Stump   // FIXME: We'd like to put these into a mergable by content, with
1420cbc2bcafSMike Stump   // internal linkage.
1421ee2a5ee5SMike Stump   llvm::Function *Fn =
1422ee2a5ee5SMike Stump     llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
1423f9b056b0SJohn McCall                            "__Block_byref_object_copy_", &CGF.CGM.getModule());
1424ee2a5ee5SMike Stump 
1425ee2a5ee5SMike Stump   IdentifierInfo *II
1426f9b056b0SJohn McCall     = &Context.Idents.get("__Block_byref_object_copy_");
1427ee2a5ee5SMike Stump 
1428f9b056b0SJohn McCall   FunctionDecl *FD = FunctionDecl::Create(Context,
1429f9b056b0SJohn McCall                                           Context.getTranslationUnitDecl(),
1430dff1930bSAbramo Bagnara                                           SourceLocation(),
143160ed5604SArgyrios Kyrtzidis                                           SourceLocation(), II, R, 0,
14328e7d656aSJohn McCall                                           SC_Static,
14338e7d656aSJohn McCall                                           SC_None,
1434c4df4076SDouglas Gregor                                           false, true);
143531168b07SJohn McCall 
1436f9b056b0SJohn McCall   CGF.StartFunction(FD, R, Fn, FI, args, SourceLocation());
1437f9b056b0SJohn McCall 
1438f9b056b0SJohn McCall   if (byrefInfo.needsCopy()) {
1439f9b056b0SJohn McCall     const llvm::Type *byrefPtrType = byrefType.getPointerTo(0);
1440f89230d9SMike Stump 
1441f89230d9SMike Stump     // dst->x
1442f9b056b0SJohn McCall     llvm::Value *destField = CGF.GetAddrOfLocalVar(&dst);
1443f9b056b0SJohn McCall     destField = CGF.Builder.CreateLoad(destField);
1444f9b056b0SJohn McCall     destField = CGF.Builder.CreateBitCast(destField, byrefPtrType);
1445f9b056b0SJohn McCall     destField = CGF.Builder.CreateStructGEP(destField, 6, "x");
1446f89230d9SMike Stump 
1447f89230d9SMike Stump     // src->x
1448f9b056b0SJohn McCall     llvm::Value *srcField = CGF.GetAddrOfLocalVar(&src);
1449f9b056b0SJohn McCall     srcField = CGF.Builder.CreateLoad(srcField);
1450f9b056b0SJohn McCall     srcField = CGF.Builder.CreateBitCast(srcField, byrefPtrType);
1451f9b056b0SJohn McCall     srcField = CGF.Builder.CreateStructGEP(srcField, 6, "x");
145250198098SFariborz Jahanian 
1453f9b056b0SJohn McCall     byrefInfo.emitCopy(CGF, destField, srcField);
145450198098SFariborz Jahanian   }
1455f89230d9SMike Stump 
1456f9b056b0SJohn McCall   CGF.FinishFunction();
1457ee2a5ee5SMike Stump 
1458f9b056b0SJohn McCall   return llvm::ConstantExpr::getBitCast(Fn, CGF.Int8PtrTy);
1459ee2a5ee5SMike Stump }
1460ee2a5ee5SMike Stump 
1461f9b056b0SJohn McCall /// Build the copy helper for a __block variable.
1462f9b056b0SJohn McCall static llvm::Constant *buildByrefCopyHelper(CodeGenModule &CGM,
1463f9b056b0SJohn McCall                                             const llvm::StructType &byrefType,
1464f9b056b0SJohn McCall                                             CodeGenModule::ByrefHelpers &info) {
1465f9b056b0SJohn McCall   CodeGenFunction CGF(CGM);
1466f9b056b0SJohn McCall   return generateByrefCopyHelper(CGF, byrefType, info);
1467f9b056b0SJohn McCall }
1468f9b056b0SJohn McCall 
1469f9b056b0SJohn McCall /// Generate code for a __block variable's dispose helper.
1470f9b056b0SJohn McCall static llvm::Constant *
1471f9b056b0SJohn McCall generateByrefDisposeHelper(CodeGenFunction &CGF,
1472f9b056b0SJohn McCall                            const llvm::StructType &byrefType,
1473f9b056b0SJohn McCall                            CodeGenModule::ByrefHelpers &byrefInfo) {
1474f9b056b0SJohn McCall   ASTContext &Context = CGF.getContext();
1475f9b056b0SJohn McCall   QualType R = Context.VoidTy;
1476ee2a5ee5SMike Stump 
1477a738c25fSJohn McCall   FunctionArgList args;
1478f9b056b0SJohn McCall   ImplicitParamDecl src(0, SourceLocation(), 0, Context.VoidPtrTy);
1479a738c25fSJohn McCall   args.push_back(&src);
1480ee2a5ee5SMike Stump 
1481ee2a5ee5SMike Stump   const CGFunctionInfo &FI =
1482f9b056b0SJohn McCall     CGF.CGM.getTypes().getFunctionInfo(R, args, FunctionType::ExtInfo());
1483ee2a5ee5SMike Stump 
1484f9b056b0SJohn McCall   CodeGenTypes &Types = CGF.CGM.getTypes();
1485ee2a5ee5SMike Stump   const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1486ee2a5ee5SMike Stump 
1487cbc2bcafSMike Stump   // FIXME: We'd like to put these into a mergable by content, with
1488cbc2bcafSMike Stump   // internal linkage.
1489ee2a5ee5SMike Stump   llvm::Function *Fn =
1490ee2a5ee5SMike Stump     llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
149150198098SFariborz Jahanian                            "__Block_byref_object_dispose_",
1492f9b056b0SJohn McCall                            &CGF.CGM.getModule());
1493ee2a5ee5SMike Stump 
1494ee2a5ee5SMike Stump   IdentifierInfo *II
1495f9b056b0SJohn McCall     = &Context.Idents.get("__Block_byref_object_dispose_");
1496ee2a5ee5SMike Stump 
1497f9b056b0SJohn McCall   FunctionDecl *FD = FunctionDecl::Create(Context,
1498f9b056b0SJohn McCall                                           Context.getTranslationUnitDecl(),
1499dff1930bSAbramo Bagnara                                           SourceLocation(),
150060ed5604SArgyrios Kyrtzidis                                           SourceLocation(), II, R, 0,
15018e7d656aSJohn McCall                                           SC_Static,
15028e7d656aSJohn McCall                                           SC_None,
1503c4df4076SDouglas Gregor                                           false, true);
1504f9b056b0SJohn McCall   CGF.StartFunction(FD, R, Fn, FI, args, SourceLocation());
1505fbe25dd1SMike Stump 
1506f9b056b0SJohn McCall   if (byrefInfo.needsDispose()) {
1507f9b056b0SJohn McCall     llvm::Value *V = CGF.GetAddrOfLocalVar(&src);
1508f9b056b0SJohn McCall     V = CGF.Builder.CreateLoad(V);
1509f9b056b0SJohn McCall     V = CGF.Builder.CreateBitCast(V, byrefType.getPointerTo(0));
1510f9b056b0SJohn McCall     V = CGF.Builder.CreateStructGEP(V, 6, "x");
1511ad7c5c16SJohn McCall 
1512f9b056b0SJohn McCall     byrefInfo.emitDispose(CGF, V);
1513f9b056b0SJohn McCall   }
1514ad7c5c16SJohn McCall 
1515f9b056b0SJohn McCall   CGF.FinishFunction();
1516f9b056b0SJohn McCall 
1517f9b056b0SJohn McCall   return llvm::ConstantExpr::getBitCast(Fn, CGF.Int8PtrTy);
1518f9b056b0SJohn McCall }
1519f9b056b0SJohn McCall 
1520f9b056b0SJohn McCall /// Build the dispose helper for a __block variable.
1521f9b056b0SJohn McCall static llvm::Constant *buildByrefDisposeHelper(CodeGenModule &CGM,
1522f9b056b0SJohn McCall                                               const llvm::StructType &byrefType,
1523f9b056b0SJohn McCall                                             CodeGenModule::ByrefHelpers &info) {
1524f9b056b0SJohn McCall   CodeGenFunction CGF(CGM);
1525f9b056b0SJohn McCall   return generateByrefDisposeHelper(CGF, byrefType, info);
1526f9b056b0SJohn McCall }
1527f9b056b0SJohn McCall 
1528f9b056b0SJohn McCall ///
1529f9b056b0SJohn McCall template <class T> static T *buildByrefHelpers(CodeGenModule &CGM,
1530f9b056b0SJohn McCall                                                const llvm::StructType &byrefTy,
1531f9b056b0SJohn McCall                                                T &byrefInfo) {
1532f9b056b0SJohn McCall   // Increase the field's alignment to be at least pointer alignment,
1533f9b056b0SJohn McCall   // since the layout of the byref struct will guarantee at least that.
1534f9b056b0SJohn McCall   byrefInfo.Alignment = std::max(byrefInfo.Alignment,
1535f9b056b0SJohn McCall                               CharUnits::fromQuantity(CGM.PointerAlignInBytes));
1536f9b056b0SJohn McCall 
1537f9b056b0SJohn McCall   llvm::FoldingSetNodeID id;
1538f9b056b0SJohn McCall   byrefInfo.Profile(id);
1539f9b056b0SJohn McCall 
1540f9b056b0SJohn McCall   void *insertPos;
1541f9b056b0SJohn McCall   CodeGenModule::ByrefHelpers *node
1542f9b056b0SJohn McCall     = CGM.ByrefHelpersCache.FindNodeOrInsertPos(id, insertPos);
1543f9b056b0SJohn McCall   if (node) return static_cast<T*>(node);
1544f9b056b0SJohn McCall 
1545f9b056b0SJohn McCall   byrefInfo.CopyHelper = buildByrefCopyHelper(CGM, byrefTy, byrefInfo);
1546f9b056b0SJohn McCall   byrefInfo.DisposeHelper = buildByrefDisposeHelper(CGM, byrefTy, byrefInfo);
1547f9b056b0SJohn McCall 
1548f9b056b0SJohn McCall   T *copy = new (CGM.getContext()) T(byrefInfo);
1549f9b056b0SJohn McCall   CGM.ByrefHelpersCache.InsertNode(copy, insertPos);
1550f9b056b0SJohn McCall   return copy;
1551f9b056b0SJohn McCall }
1552f9b056b0SJohn McCall 
1553f9b056b0SJohn McCall CodeGenModule::ByrefHelpers *
1554f9b056b0SJohn McCall CodeGenFunction::buildByrefHelpers(const llvm::StructType &byrefType,
1555f9b056b0SJohn McCall                                    const AutoVarEmission &emission) {
1556f9b056b0SJohn McCall   const VarDecl &var = *emission.Variable;
1557f9b056b0SJohn McCall   QualType type = var.getType();
1558f9b056b0SJohn McCall 
1559f9b056b0SJohn McCall   if (const CXXRecordDecl *record = type->getAsCXXRecordDecl()) {
1560f9b056b0SJohn McCall     const Expr *copyExpr = CGM.getContext().getBlockVarCopyInits(&var);
1561f9b056b0SJohn McCall     if (!copyExpr && record->hasTrivialDestructor()) return 0;
1562f9b056b0SJohn McCall 
1563f9b056b0SJohn McCall     CXXByrefHelpers byrefInfo(emission.Alignment, type, copyExpr);
1564f9b056b0SJohn McCall     return ::buildByrefHelpers(CGM, byrefType, byrefInfo);
1565f9b056b0SJohn McCall   }
1566f9b056b0SJohn McCall 
156731168b07SJohn McCall   // Otherwise, if we don't have a retainable type, there's nothing to do.
156831168b07SJohn McCall   // that the runtime does extra copies.
156931168b07SJohn McCall   if (!type->isObjCRetainableType()) return 0;
157031168b07SJohn McCall 
157131168b07SJohn McCall   Qualifiers qs = type.getQualifiers();
157231168b07SJohn McCall 
157331168b07SJohn McCall   // If we have lifetime, that dominates.
157431168b07SJohn McCall   if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) {
157531168b07SJohn McCall     assert(getLangOptions().ObjCAutoRefCount);
157631168b07SJohn McCall 
157731168b07SJohn McCall     switch (lifetime) {
157831168b07SJohn McCall     case Qualifiers::OCL_None: llvm_unreachable("impossible");
157931168b07SJohn McCall 
158031168b07SJohn McCall     // These are just bits as far as the runtime is concerned.
158131168b07SJohn McCall     case Qualifiers::OCL_ExplicitNone:
158231168b07SJohn McCall     case Qualifiers::OCL_Autoreleasing:
158331168b07SJohn McCall       return 0;
158431168b07SJohn McCall 
158531168b07SJohn McCall     // Tell the runtime that this is ARC __weak, called by the
158631168b07SJohn McCall     // byref routines.
158731168b07SJohn McCall     case Qualifiers::OCL_Weak: {
158831168b07SJohn McCall       ARCWeakByrefHelpers byrefInfo(emission.Alignment);
158931168b07SJohn McCall       return ::buildByrefHelpers(CGM, byrefType, byrefInfo);
159031168b07SJohn McCall     }
159131168b07SJohn McCall 
159231168b07SJohn McCall     // ARC __strong __block variables need to be retained.
159331168b07SJohn McCall     case Qualifiers::OCL_Strong:
159431168b07SJohn McCall       // Block-pointers need to be _Block_copy'ed, so we let the
159531168b07SJohn McCall       // runtime be in charge.  But we can't use the code below
159631168b07SJohn McCall       // because we don't want to set BYREF_CALLER, which will
159731168b07SJohn McCall       // just make the runtime ignore us.
159831168b07SJohn McCall       if (type->isBlockPointerType()) {
159931168b07SJohn McCall         BlockFieldFlags flags = BLOCK_FIELD_IS_BLOCK;
160031168b07SJohn McCall         ObjectByrefHelpers byrefInfo(emission.Alignment, flags);
160131168b07SJohn McCall         return ::buildByrefHelpers(CGM, byrefType, byrefInfo);
160231168b07SJohn McCall 
160331168b07SJohn McCall       // Otherwise, we transfer ownership of the retain from the stack
160431168b07SJohn McCall       // to the heap.
160531168b07SJohn McCall       } else {
160631168b07SJohn McCall         ARCStrongByrefHelpers byrefInfo(emission.Alignment);
160731168b07SJohn McCall         return ::buildByrefHelpers(CGM, byrefType, byrefInfo);
160831168b07SJohn McCall       }
160931168b07SJohn McCall     }
161031168b07SJohn McCall     llvm_unreachable("fell out of lifetime switch!");
161131168b07SJohn McCall   }
161231168b07SJohn McCall 
1613f9b056b0SJohn McCall   BlockFieldFlags flags;
1614f9b056b0SJohn McCall   if (type->isBlockPointerType()) {
1615f9b056b0SJohn McCall     flags |= BLOCK_FIELD_IS_BLOCK;
1616f9b056b0SJohn McCall   } else if (CGM.getContext().isObjCNSObjectType(type) ||
1617f9b056b0SJohn McCall              type->isObjCObjectPointerType()) {
1618f9b056b0SJohn McCall     flags |= BLOCK_FIELD_IS_OBJECT;
1619ad7c5c16SJohn McCall   } else {
1620f9b056b0SJohn McCall     return 0;
1621ee2a5ee5SMike Stump   }
1622ee2a5ee5SMike Stump 
1623f9b056b0SJohn McCall   if (type.isObjCGCWeak())
1624f9b056b0SJohn McCall     flags |= BLOCK_FIELD_IS_WEAK;
1625ad7c5c16SJohn McCall 
1626f9b056b0SJohn McCall   ObjectByrefHelpers byrefInfo(emission.Alignment, flags);
1627f9b056b0SJohn McCall   return ::buildByrefHelpers(CGM, byrefType, byrefInfo);
1628ee2a5ee5SMike Stump }
1629ee2a5ee5SMike Stump 
163073064870SJohn McCall unsigned CodeGenFunction::getByRefValueLLVMField(const ValueDecl *VD) const {
163173064870SJohn McCall   assert(ByRefValueInfo.count(VD) && "Did not find value!");
163273064870SJohn McCall 
163373064870SJohn McCall   return ByRefValueInfo.find(VD)->second.second;
163473064870SJohn McCall }
163573064870SJohn McCall 
163673064870SJohn McCall llvm::Value *CodeGenFunction::BuildBlockByrefAddress(llvm::Value *BaseAddr,
163773064870SJohn McCall                                                      const VarDecl *V) {
163873064870SJohn McCall   llvm::Value *Loc = Builder.CreateStructGEP(BaseAddr, 1, "forwarding");
163973064870SJohn McCall   Loc = Builder.CreateLoad(Loc);
164073064870SJohn McCall   Loc = Builder.CreateStructGEP(Loc, getByRefValueLLVMField(V),
164173064870SJohn McCall                                 V->getNameAsString());
164273064870SJohn McCall   return Loc;
164373064870SJohn McCall }
164473064870SJohn McCall 
164573064870SJohn McCall /// BuildByRefType - This routine changes a __block variable declared as T x
164673064870SJohn McCall ///   into:
164773064870SJohn McCall ///
164873064870SJohn McCall ///      struct {
164973064870SJohn McCall ///        void *__isa;
165073064870SJohn McCall ///        void *__forwarding;
165173064870SJohn McCall ///        int32_t __flags;
165273064870SJohn McCall ///        int32_t __size;
165373064870SJohn McCall ///        void *__copy_helper;       // only if needed
165473064870SJohn McCall ///        void *__destroy_helper;    // only if needed
165573064870SJohn McCall ///        char padding[X];           // only if needed
165673064870SJohn McCall ///        T x;
165773064870SJohn McCall ///      } x
165873064870SJohn McCall ///
165973064870SJohn McCall const llvm::Type *CodeGenFunction::BuildByRefType(const VarDecl *D) {
166073064870SJohn McCall   std::pair<const llvm::Type *, unsigned> &Info = ByRefValueInfo[D];
166173064870SJohn McCall   if (Info.first)
166273064870SJohn McCall     return Info.first;
166373064870SJohn McCall 
166473064870SJohn McCall   QualType Ty = D->getType();
166573064870SJohn McCall 
1666a5f58b05SChris Lattner   llvm::SmallVector<llvm::Type *, 8> types;
166773064870SJohn McCall 
1668a5f58b05SChris Lattner   llvm::StructType *ByRefType =
1669a5f58b05SChris Lattner     llvm::StructType::createNamed(getLLVMContext(),
1670a5f58b05SChris Lattner                                 "struct.__block_byref_" + D->getNameAsString());
167173064870SJohn McCall 
167273064870SJohn McCall   // void *__isa;
16739dc0db21SJohn McCall   types.push_back(Int8PtrTy);
167473064870SJohn McCall 
167573064870SJohn McCall   // void *__forwarding;
1676a5f58b05SChris Lattner   types.push_back(llvm::PointerType::getUnqual(ByRefType));
167773064870SJohn McCall 
167873064870SJohn McCall   // int32_t __flags;
16799dc0db21SJohn McCall   types.push_back(Int32Ty);
168073064870SJohn McCall 
168173064870SJohn McCall   // int32_t __size;
16829dc0db21SJohn McCall   types.push_back(Int32Ty);
168373064870SJohn McCall 
168473064870SJohn McCall   bool HasCopyAndDispose = getContext().BlockRequiresCopying(Ty);
168573064870SJohn McCall   if (HasCopyAndDispose) {
168673064870SJohn McCall     /// void *__copy_helper;
16879dc0db21SJohn McCall     types.push_back(Int8PtrTy);
168873064870SJohn McCall 
168973064870SJohn McCall     /// void *__destroy_helper;
16909dc0db21SJohn McCall     types.push_back(Int8PtrTy);
169173064870SJohn McCall   }
169273064870SJohn McCall 
169373064870SJohn McCall   bool Packed = false;
169473064870SJohn McCall   CharUnits Align = getContext().getDeclAlign(D);
169573064870SJohn McCall   if (Align > getContext().toCharUnitsFromBits(Target.getPointerAlign(0))) {
169673064870SJohn McCall     // We have to insert padding.
169773064870SJohn McCall 
169873064870SJohn McCall     // The struct above has 2 32-bit integers.
169973064870SJohn McCall     unsigned CurrentOffsetInBytes = 4 * 2;
170073064870SJohn McCall 
170173064870SJohn McCall     // And either 2 or 4 pointers.
170273064870SJohn McCall     CurrentOffsetInBytes += (HasCopyAndDispose ? 4 : 2) *
170373064870SJohn McCall       CGM.getTargetData().getTypeAllocSize(Int8PtrTy);
170473064870SJohn McCall 
170573064870SJohn McCall     // Align the offset.
170673064870SJohn McCall     unsigned AlignedOffsetInBytes =
170773064870SJohn McCall       llvm::RoundUpToAlignment(CurrentOffsetInBytes, Align.getQuantity());
170873064870SJohn McCall 
170973064870SJohn McCall     unsigned NumPaddingBytes = AlignedOffsetInBytes - CurrentOffsetInBytes;
171073064870SJohn McCall     if (NumPaddingBytes > 0) {
1711a5f58b05SChris Lattner       llvm::Type *Ty = llvm::Type::getInt8Ty(getLLVMContext());
171273064870SJohn McCall       // FIXME: We need a sema error for alignment larger than the minimum of
17139dc0db21SJohn McCall       // the maximal stack alignment and the alignment of malloc on the system.
171473064870SJohn McCall       if (NumPaddingBytes > 1)
171573064870SJohn McCall         Ty = llvm::ArrayType::get(Ty, NumPaddingBytes);
171673064870SJohn McCall 
17179dc0db21SJohn McCall       types.push_back(Ty);
171873064870SJohn McCall 
171973064870SJohn McCall       // We want a packed struct.
172073064870SJohn McCall       Packed = true;
172173064870SJohn McCall     }
172273064870SJohn McCall   }
172373064870SJohn McCall 
172473064870SJohn McCall   // T x;
17259dc0db21SJohn McCall   types.push_back(ConvertTypeForMem(Ty));
172673064870SJohn McCall 
1727a5f58b05SChris Lattner   ByRefType->setBody(types, Packed);
172873064870SJohn McCall 
1729a5f58b05SChris Lattner   Info.first = ByRefType;
173073064870SJohn McCall 
17319dc0db21SJohn McCall   Info.second = types.size() - 1;
173273064870SJohn McCall 
173373064870SJohn McCall   return Info.first;
173473064870SJohn McCall }
173573064870SJohn McCall 
173673064870SJohn McCall /// Initialize the structural components of a __block variable, i.e.
173773064870SJohn McCall /// everything but the actual object.
173873064870SJohn McCall void CodeGenFunction::emitByrefStructureInit(const AutoVarEmission &emission) {
1739f9b056b0SJohn McCall   // Find the address of the local.
1740f9b056b0SJohn McCall   llvm::Value *addr = emission.Address;
174173064870SJohn McCall 
1742f9b056b0SJohn McCall   // That's an alloca of the byref structure type.
1743f9b056b0SJohn McCall   const llvm::StructType *byrefType = cast<llvm::StructType>(
1744f9b056b0SJohn McCall                  cast<llvm::PointerType>(addr->getType())->getElementType());
1745f9b056b0SJohn McCall 
1746f9b056b0SJohn McCall   // Build the byref helpers if necessary.  This is null if we don't need any.
1747f9b056b0SJohn McCall   CodeGenModule::ByrefHelpers *helpers =
1748f9b056b0SJohn McCall     buildByrefHelpers(*byrefType, emission);
174973064870SJohn McCall 
175073064870SJohn McCall   const VarDecl &D = *emission.Variable;
175173064870SJohn McCall   QualType type = D.getType();
175273064870SJohn McCall 
1753f9b056b0SJohn McCall   llvm::Value *V;
175473064870SJohn McCall 
175573064870SJohn McCall   // Initialize the 'isa', which is just 0 or 1.
175673064870SJohn McCall   int isa = 0;
1757f9b056b0SJohn McCall   if (type.isObjCGCWeak())
175873064870SJohn McCall     isa = 1;
175973064870SJohn McCall   V = Builder.CreateIntToPtr(Builder.getInt32(isa), Int8PtrTy, "isa");
176073064870SJohn McCall   Builder.CreateStore(V, Builder.CreateStructGEP(addr, 0, "byref.isa"));
176173064870SJohn McCall 
176273064870SJohn McCall   // Store the address of the variable into its own forwarding pointer.
176373064870SJohn McCall   Builder.CreateStore(addr,
176473064870SJohn McCall                       Builder.CreateStructGEP(addr, 1, "byref.forwarding"));
176573064870SJohn McCall 
176673064870SJohn McCall   // Blocks ABI:
176773064870SJohn McCall   //   c) the flags field is set to either 0 if no helper functions are
176873064870SJohn McCall   //      needed or BLOCK_HAS_COPY_DISPOSE if they are,
176973064870SJohn McCall   BlockFlags flags;
1770f9b056b0SJohn McCall   if (helpers) flags |= BLOCK_HAS_COPY_DISPOSE;
177173064870SJohn McCall   Builder.CreateStore(llvm::ConstantInt::get(IntTy, flags.getBitMask()),
177273064870SJohn McCall                       Builder.CreateStructGEP(addr, 2, "byref.flags"));
177373064870SJohn McCall 
1774f9b056b0SJohn McCall   CharUnits byrefSize = CGM.GetTargetTypeStoreSize(byrefType);
1775f9b056b0SJohn McCall   V = llvm::ConstantInt::get(IntTy, byrefSize.getQuantity());
177673064870SJohn McCall   Builder.CreateStore(V, Builder.CreateStructGEP(addr, 3, "byref.size"));
177773064870SJohn McCall 
1778f9b056b0SJohn McCall   if (helpers) {
177973064870SJohn McCall     llvm::Value *copy_helper = Builder.CreateStructGEP(addr, 4);
1780f9b056b0SJohn McCall     Builder.CreateStore(helpers->CopyHelper, copy_helper);
178173064870SJohn McCall 
178273064870SJohn McCall     llvm::Value *destroy_helper = Builder.CreateStructGEP(addr, 5);
1783f9b056b0SJohn McCall     Builder.CreateStore(helpers->DisposeHelper, destroy_helper);
178473064870SJohn McCall   }
178573064870SJohn McCall }
178673064870SJohn McCall 
1787ad7c5c16SJohn McCall void CodeGenFunction::BuildBlockRelease(llvm::Value *V, BlockFieldFlags flags) {
1788900546d2SDaniel Dunbar   llvm::Value *F = CGM.getBlockObjectDispose();
1789fbe25dd1SMike Stump   llvm::Value *N;
1790ad7c5c16SJohn McCall   V = Builder.CreateBitCast(V, Int8PtrTy);
1791ad7c5c16SJohn McCall   N = llvm::ConstantInt::get(Int32Ty, flags.getBitMask());
1792626aecc4SMike Stump   Builder.CreateCall2(F, V, N);
1793626aecc4SMike Stump }
179473064870SJohn McCall 
179573064870SJohn McCall namespace {
179673064870SJohn McCall   struct CallBlockRelease : EHScopeStack::Cleanup {
179773064870SJohn McCall     llvm::Value *Addr;
179873064870SJohn McCall     CallBlockRelease(llvm::Value *Addr) : Addr(Addr) {}
179973064870SJohn McCall 
180073064870SJohn McCall     void Emit(CodeGenFunction &CGF, bool IsForEH) {
180131168b07SJohn McCall       // Should we be passing FIELD_IS_WEAK here?
180273064870SJohn McCall       CGF.BuildBlockRelease(Addr, BLOCK_FIELD_IS_BYREF);
180373064870SJohn McCall     }
180473064870SJohn McCall   };
180573064870SJohn McCall }
180673064870SJohn McCall 
180773064870SJohn McCall /// Enter a cleanup to destroy a __block variable.  Note that this
180873064870SJohn McCall /// cleanup should be a no-op if the variable hasn't left the stack
180973064870SJohn McCall /// yet; if a cleanup is required for the variable itself, that needs
181073064870SJohn McCall /// to be done externally.
181173064870SJohn McCall void CodeGenFunction::enterByrefCleanup(const AutoVarEmission &emission) {
181273064870SJohn McCall   // We don't enter this cleanup if we're in pure-GC mode.
181373064870SJohn McCall   if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
181473064870SJohn McCall     return;
181573064870SJohn McCall 
181673064870SJohn McCall   EHStack.pushCleanup<CallBlockRelease>(NormalAndEHCleanup, emission.Address);
181773064870SJohn McCall }
1818