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 
622192fe50SChris Lattner   llvm::Type *ulong = CGM.getTypes().ConvertType(C.UnsignedLongTy);
632192fe50SChris Lattner   llvm::Type *i8p = CGM.getTypes().ConvertType(C.VoidPtrTy);
64351762cdSJohn McCall 
650e62c1ccSChris Lattner   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,
2460e62c1ccSChris Lattner                              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 
2830e62c1ccSChris Lattner   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.
2940e62c1ccSChris Lattner   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) {
4250e62c1ccSChris Lattner     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) {
4360e62c1ccSChris Lattner       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.
4710e62c1ccSChris Lattner   for (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 
5102192fe50SChris Lattner   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()) {
6304bd0fb1fSJohn McCall       switch (QualType::DestructionKind dtorKind = type.isDestructedType()) {
63131168b07SJohn McCall       case QualType::DK_none:
63231168b07SJohn McCall         break;
6334bd0fb1fSJohn McCall 
6344bd0fb1fSJohn McCall       // Block captures count as local values and have imprecise semantics.
6354bd0fb1fSJohn McCall       // They also can't be arrays, so need to worry about that.
6365fa8e208SJohn McCall       case QualType::DK_objc_strong_lifetime: {
6375fa8e208SJohn McCall         // This local is a GCC and MSVC compiler workaround.
6385fa8e208SJohn McCall         Destroyer *destroyer = &destroyARCStrongImprecise;
6394bd0fb1fSJohn McCall         pushDestroy(getCleanupKind(dtorKind), blockField, type,
6405fa8e208SJohn McCall                     *destroyer, /*useEHCleanupForArray*/ false);
64131168b07SJohn McCall         break;
6425fa8e208SJohn McCall       }
6434bd0fb1fSJohn McCall 
64431168b07SJohn McCall       case QualType::DK_objc_weak_lifetime:
6454bd0fb1fSJohn McCall       case QualType::DK_cxx_destructor:
6464bd0fb1fSJohn McCall         pushDestroy(dtorKind, blockField, type);
64731168b07SJohn McCall         break;
64831168b07SJohn McCall       }
64931168b07SJohn McCall     }
650351762cdSJohn McCall   }
651351762cdSJohn McCall 
652351762cdSJohn McCall   // Cast to the converted block-pointer type, which happens (somewhat
653351762cdSJohn McCall   // unfortunately) to be a pointer to function type.
654351762cdSJohn McCall   llvm::Value *result =
655351762cdSJohn McCall     Builder.CreateBitCast(blockAddr,
656351762cdSJohn McCall                           ConvertType(blockInfo.getBlockExpr()->getType()));
6573882ace2SJohn McCall 
658351762cdSJohn McCall   return result;
659b750d928SMike Stump }
66085284bacSMike Stump 
66185284bacSMike Stump 
662a5f58b05SChris Lattner llvm::Type *CodeGenModule::getBlockDescriptorType() {
663650c932dSMike Stump   if (BlockDescriptorType)
664650c932dSMike Stump     return BlockDescriptorType;
6652437cbfaSAnders Carlsson 
666a5f58b05SChris Lattner   llvm::Type *UnsignedLongTy =
667650c932dSMike Stump     getTypes().ConvertType(getContext().UnsignedLongTy);
6682437cbfaSAnders Carlsson 
6692437cbfaSAnders Carlsson   // struct __block_descriptor {
6702437cbfaSAnders Carlsson   //   unsigned long reserved;
6712437cbfaSAnders Carlsson   //   unsigned long block_size;
672fc83aa04SBlaine Garst   //
673fc83aa04SBlaine Garst   //   // later, the following will be added
674fc83aa04SBlaine Garst   //
675fc83aa04SBlaine Garst   //   struct {
676fc83aa04SBlaine Garst   //     void (*copyHelper)();
677fc83aa04SBlaine Garst   //     void (*copyHelper)();
678fc83aa04SBlaine Garst   //   } helpers;                // !!! optional
679fc83aa04SBlaine Garst   //
680fc83aa04SBlaine Garst   //   const char *signature;   // the block signature
681fc83aa04SBlaine Garst   //   const char *layout;      // reserved
6822437cbfaSAnders Carlsson   // };
683845511feSChris Lattner   BlockDescriptorType =
684a5f58b05SChris Lattner     llvm::StructType::createNamed("struct.__block_descriptor",
685a5f58b05SChris Lattner                                   UnsignedLongTy, UnsignedLongTy, NULL);
6862437cbfaSAnders Carlsson 
687351762cdSJohn McCall   // Now form a pointer to that.
688351762cdSJohn McCall   BlockDescriptorType = llvm::PointerType::getUnqual(BlockDescriptorType);
689650c932dSMike Stump   return BlockDescriptorType;
6902437cbfaSAnders Carlsson }
6912437cbfaSAnders Carlsson 
692a5f58b05SChris Lattner llvm::Type *CodeGenModule::getGenericBlockLiteralType() {
693005c9a62SMike Stump   if (GenericBlockLiteralType)
694005c9a62SMike Stump     return GenericBlockLiteralType;
6952437cbfaSAnders Carlsson 
696a5f58b05SChris Lattner   llvm::Type *BlockDescPtrTy = getBlockDescriptorType();
6972437cbfaSAnders Carlsson 
6982437cbfaSAnders Carlsson   // struct __block_literal_generic {
6995d2534adSMike Stump   //   void *__isa;
7005d2534adSMike Stump   //   int __flags;
7015d2534adSMike Stump   //   int __reserved;
7025d2534adSMike Stump   //   void (*__invoke)(void *);
7035d2534adSMike Stump   //   struct __block_descriptor *__descriptor;
7042437cbfaSAnders Carlsson   // };
705a5f58b05SChris Lattner   GenericBlockLiteralType =
706a5f58b05SChris Lattner     llvm::StructType::createNamed("struct.__block_literal_generic",
707a5f58b05SChris Lattner                                   VoidPtrTy,
70892bbd6d4SMike Stump                                   IntTy,
70992bbd6d4SMike Stump                                   IntTy,
710e3dc1707SJohn McCall                                   VoidPtrTy,
7112437cbfaSAnders Carlsson                                   BlockDescPtrTy,
7122437cbfaSAnders Carlsson                                   NULL);
7132437cbfaSAnders Carlsson 
714005c9a62SMike Stump   return GenericBlockLiteralType;
7152437cbfaSAnders Carlsson }
7162437cbfaSAnders Carlsson 
7175d2534adSMike Stump 
718bfb3671bSAnders Carlsson RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E,
719bfb3671bSAnders Carlsson                                           ReturnValueSlot ReturnValue) {
7202437cbfaSAnders Carlsson   const BlockPointerType *BPT =
721c23c7e6aSTed Kremenek     E->getCallee()->getType()->getAs<BlockPointerType>();
7222437cbfaSAnders Carlsson 
7232437cbfaSAnders Carlsson   llvm::Value *Callee = EmitScalarExpr(E->getCallee());
7242437cbfaSAnders Carlsson 
7252437cbfaSAnders Carlsson   // Get a pointer to the generic block literal.
7262192fe50SChris Lattner   llvm::Type *BlockLiteralTy =
7279793f0e4SOwen Anderson     llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
7282437cbfaSAnders Carlsson 
7292437cbfaSAnders Carlsson   // Bitcast the callee to a block literal.
7302437cbfaSAnders Carlsson   llvm::Value *BlockLiteral =
7312437cbfaSAnders Carlsson     Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
7322437cbfaSAnders Carlsson 
7332437cbfaSAnders Carlsson   // Get the function pointer from the literal.
7342437cbfaSAnders Carlsson   llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
7352437cbfaSAnders Carlsson 
736e3dc1707SJohn McCall   BlockLiteral = Builder.CreateBitCast(BlockLiteral, VoidPtrTy, "tmp");
7372437cbfaSAnders Carlsson 
7382437cbfaSAnders Carlsson   // Add the block literal.
7392437cbfaSAnders Carlsson   CallArgList Args;
7409dc0db21SJohn McCall   Args.add(RValue::get(BlockLiteral), getContext().VoidPtrTy);
7412437cbfaSAnders Carlsson 
742479e6fc7SAnders Carlsson   QualType FnType = BPT->getPointeeType();
743479e6fc7SAnders Carlsson 
7442437cbfaSAnders Carlsson   // And the rest of the arguments.
7459dd450bbSJohn McCall   EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(),
746479e6fc7SAnders Carlsson                E->arg_begin(), E->arg_end());
7472437cbfaSAnders Carlsson 
7485f50c651SAnders Carlsson   // Load the function.
749c76493a6SDaniel Dunbar   llvm::Value *Func = Builder.CreateLoad(FuncPtr, "tmp");
7505f50c651SAnders Carlsson 
75185915250SJohn McCall   const FunctionType *FuncTy = FnType->castAs<FunctionType>();
752*f481ccafSEli Friedman   const CGFunctionInfo &FnInfo = CGM.getTypes().getFunctionInfo(Args, FuncTy);
7535f50c651SAnders Carlsson 
7545f50c651SAnders Carlsson   // Cast the function pointer to the right type.
7552192fe50SChris Lattner   llvm::Type *BlockFTy =
756a60cbcdfSAnders Carlsson     CGM.getTypes().GetFunctionType(FnInfo, false);
7575f50c651SAnders Carlsson 
7582192fe50SChris Lattner   llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
7595f50c651SAnders Carlsson   Func = Builder.CreateBitCast(Func, BlockFTyPtr);
7605f50c651SAnders Carlsson 
7612437cbfaSAnders Carlsson   // And call the block.
762bfb3671bSAnders Carlsson   return EmitCall(FnInfo, Func, ReturnValue, Args);
7632437cbfaSAnders Carlsson }
7646a60fa24SAnders Carlsson 
765351762cdSJohn McCall llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const VarDecl *variable,
766351762cdSJohn McCall                                                  bool isByRef) {
767351762cdSJohn McCall   assert(BlockInfo && "evaluating block ref without block information?");
768351762cdSJohn McCall   const CGBlockInfo::Capture &capture = BlockInfo->getCapture(variable);
76987fe5d56SJohn McCall 
770351762cdSJohn McCall   // Handle constant captures.
771351762cdSJohn McCall   if (capture.isConstant()) return LocalDeclMap[variable];
77287fe5d56SJohn McCall 
773351762cdSJohn McCall   llvm::Value *addr =
774351762cdSJohn McCall     Builder.CreateStructGEP(LoadBlockStruct(), capture.getIndex(),
775351762cdSJohn McCall                             "block.capture.addr");
776351762cdSJohn McCall 
777351762cdSJohn McCall   if (isByRef) {
778351762cdSJohn McCall     // addr should be a void** right now.  Load, then cast the result
779351762cdSJohn McCall     // to byref*.
780351762cdSJohn McCall 
781351762cdSJohn McCall     addr = Builder.CreateLoad(addr);
7822192fe50SChris Lattner     llvm::PointerType *byrefPointerType
783351762cdSJohn McCall       = llvm::PointerType::get(BuildByRefType(variable), 0);
784351762cdSJohn McCall     addr = Builder.CreateBitCast(addr, byrefPointerType,
785351762cdSJohn McCall                                  "byref.addr");
786351762cdSJohn McCall 
787351762cdSJohn McCall     // Follow the forwarding pointer.
788351762cdSJohn McCall     addr = Builder.CreateStructGEP(addr, 1, "byref.forwarding");
789351762cdSJohn McCall     addr = Builder.CreateLoad(addr, "byref.addr.forwarded");
790351762cdSJohn McCall 
791351762cdSJohn McCall     // Cast back to byref* and GEP over to the actual object.
792351762cdSJohn McCall     addr = Builder.CreateBitCast(addr, byrefPointerType);
793351762cdSJohn McCall     addr = Builder.CreateStructGEP(addr, getByRefValueLLVMField(variable),
794351762cdSJohn McCall                                    variable->getNameAsString());
79587fe5d56SJohn McCall   }
79687fe5d56SJohn McCall 
797351762cdSJohn McCall   if (variable->getType()->isReferenceType())
798351762cdSJohn McCall     addr = Builder.CreateLoad(addr, "ref.tmp");
7990168f4b2SAnders Carlsson 
800351762cdSJohn McCall   return addr;
80197d01d50SMike Stump }
80297d01d50SMike Stump 
8032d5a2878SMike Stump llvm::Constant *
804ad7c5c16SJohn McCall CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *blockExpr,
805351762cdSJohn McCall                                     const char *name) {
806351762cdSJohn McCall   CGBlockInfo blockInfo(blockExpr, name);
8076a60fa24SAnders Carlsson 
808351762cdSJohn McCall   // Compute information about the layout, etc., of this block.
809ad7c5c16SJohn McCall   computeBlockInfo(*this, blockInfo);
8106a60fa24SAnders Carlsson 
811351762cdSJohn McCall   // Using that metadata, generate the actual block function.
812351762cdSJohn McCall   llvm::Constant *blockFn;
813351762cdSJohn McCall   {
8145469f29aSMike Stump     llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
815ad7c5c16SJohn McCall     blockFn = CodeGenFunction(*this).GenerateBlockFunction(GlobalDecl(),
816351762cdSJohn McCall                                                            blockInfo,
817351762cdSJohn McCall                                                            0, LocalDeclMap);
818351762cdSJohn McCall   }
819e3dc1707SJohn McCall   blockFn = llvm::ConstantExpr::getBitCast(blockFn, VoidPtrTy);
8206a60fa24SAnders Carlsson 
821ad7c5c16SJohn McCall   return buildGlobalBlock(*this, blockInfo, blockFn);
8226a60fa24SAnders Carlsson }
8236a60fa24SAnders Carlsson 
824351762cdSJohn McCall static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM,
825351762cdSJohn McCall                                         const CGBlockInfo &blockInfo,
826351762cdSJohn McCall                                         llvm::Constant *blockFn) {
827351762cdSJohn McCall   assert(blockInfo.CanBeGlobal);
828351762cdSJohn McCall 
829351762cdSJohn McCall   // Generate the constants for the block literal initializer.
830351762cdSJohn McCall   llvm::Constant *fields[BlockHeaderSize];
831351762cdSJohn McCall 
832351762cdSJohn McCall   // isa
833351762cdSJohn McCall   fields[0] = CGM.getNSConcreteGlobalBlock();
834351762cdSJohn McCall 
835351762cdSJohn McCall   // __flags
83685915250SJohn McCall   BlockFlags flags = BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE;
83785915250SJohn McCall   if (blockInfo.UsesStret) flags |= BLOCK_USE_STRET;
83885915250SJohn McCall 
839e3dc1707SJohn McCall   fields[1] = llvm::ConstantInt::get(CGM.IntTy, flags.getBitMask());
840351762cdSJohn McCall 
841351762cdSJohn McCall   // Reserved
842e3dc1707SJohn McCall   fields[2] = llvm::Constant::getNullValue(CGM.IntTy);
843351762cdSJohn McCall 
844351762cdSJohn McCall   // Function
845351762cdSJohn McCall   fields[3] = blockFn;
846351762cdSJohn McCall 
847351762cdSJohn McCall   // Descriptor
848351762cdSJohn McCall   fields[4] = buildBlockDescriptor(CGM, blockInfo);
849351762cdSJohn McCall 
850e64d7ba1SChris Lattner   llvm::Constant *init = llvm::ConstantStruct::getAnon(fields);
851351762cdSJohn McCall 
852351762cdSJohn McCall   llvm::GlobalVariable *literal =
853351762cdSJohn McCall     new llvm::GlobalVariable(CGM.getModule(),
854351762cdSJohn McCall                              init->getType(),
855351762cdSJohn McCall                              /*constant*/ true,
856351762cdSJohn McCall                              llvm::GlobalVariable::InternalLinkage,
857351762cdSJohn McCall                              init,
858351762cdSJohn McCall                              "__block_literal_global");
859351762cdSJohn McCall   literal->setAlignment(blockInfo.BlockAlign.getQuantity());
860351762cdSJohn McCall 
861351762cdSJohn McCall   // Return a constant of the appropriately-casted type.
8622192fe50SChris Lattner   llvm::Type *requiredType =
863351762cdSJohn McCall     CGM.getTypes().ConvertType(blockInfo.getBlockExpr()->getType());
864351762cdSJohn McCall   return llvm::ConstantExpr::getBitCast(literal, requiredType);
865cb2fbcb0SMike Stump }
866cb2fbcb0SMike Stump 
8674446dcf0SMike Stump llvm::Function *
868351762cdSJohn McCall CodeGenFunction::GenerateBlockFunction(GlobalDecl GD,
869351762cdSJohn McCall                                        const CGBlockInfo &blockInfo,
870351762cdSJohn McCall                                        const Decl *outerFnDecl,
871351762cdSJohn McCall                                        const DeclMapTy &ldm) {
872351762cdSJohn McCall   const BlockDecl *blockDecl = blockInfo.getBlockDecl();
8739074ed8cSDevang Patel 
8746e40c615SDevang Patel   // Check if we should generate debug info for this block function.
8756e40c615SDevang Patel   if (CGM.getModuleDebugInfo())
8766e40c615SDevang Patel     DebugInfo = CGM.getModuleDebugInfo();
8776e40c615SDevang Patel 
878351762cdSJohn McCall   BlockInfo = &blockInfo;
8799074ed8cSDevang Patel 
8805469f29aSMike Stump   // Arrange for local static and local extern declarations to appear
881351762cdSJohn McCall   // to be local to this function as well, in case they're directly
882351762cdSJohn McCall   // referenced in a block.
883351762cdSJohn McCall   for (DeclMapTy::const_iterator i = ldm.begin(), e = ldm.end(); i != e; ++i) {
884351762cdSJohn McCall     const VarDecl *var = dyn_cast<VarDecl>(i->first);
885351762cdSJohn McCall     if (var && !var->hasLocalStorage())
886351762cdSJohn McCall       LocalDeclMap[var] = i->second;
8875469f29aSMike Stump   }
8885469f29aSMike Stump 
889351762cdSJohn McCall   // Begin building the function declaration.
89009a9b6e3SEli Friedman 
891351762cdSJohn McCall   // Build the argument list.
892351762cdSJohn McCall   FunctionArgList args;
8936a60fa24SAnders Carlsson 
894351762cdSJohn McCall   // The first argument is the block pointer.  Just take it as a void*
895351762cdSJohn McCall   // and cast it later.
896351762cdSJohn McCall   QualType selfTy = getContext().VoidPtrTy;
8977fe9cc1dSMike Stump   IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor");
898d015328fSMike Stump 
899147d021bSJohn McCall   ImplicitParamDecl selfDecl(const_cast<BlockDecl*>(blockDecl),
900351762cdSJohn McCall                              SourceLocation(), II, selfTy);
901a738c25fSJohn McCall   args.push_back(&selfDecl);
9027fe9cc1dSMike Stump 
903351762cdSJohn McCall   // Now add the rest of the parameters.
904351762cdSJohn McCall   for (BlockDecl::param_const_iterator i = blockDecl->param_begin(),
905351762cdSJohn McCall        e = blockDecl->param_end(); i != e; ++i)
906a738c25fSJohn McCall     args.push_back(*i);
907afa3c0a8SFariborz Jahanian 
908351762cdSJohn McCall   // Create the function declaration.
909351762cdSJohn McCall   const FunctionProtoType *fnType =
910351762cdSJohn McCall     cast<FunctionProtoType>(blockInfo.getBlockExpr()->getFunctionType());
911351762cdSJohn McCall   const CGFunctionInfo &fnInfo =
912351762cdSJohn McCall     CGM.getTypes().getFunctionInfo(fnType->getResultType(), args,
913351762cdSJohn McCall                                    fnType->getExtInfo());
91485915250SJohn McCall   if (CGM.ReturnTypeUsesSRet(fnInfo))
91585915250SJohn McCall     blockInfo.UsesStret = true;
91685915250SJohn McCall 
9172192fe50SChris Lattner   llvm::FunctionType *fnLLVMType =
918351762cdSJohn McCall     CGM.getTypes().GetFunctionType(fnInfo, fnType->isVariadic());
91987fe5d56SJohn McCall 
920351762cdSJohn McCall   MangleBuffer name;
921351762cdSJohn McCall   CGM.getBlockMangledName(GD, name, blockDecl);
922351762cdSJohn McCall   llvm::Function *fn =
923351762cdSJohn McCall     llvm::Function::Create(fnLLVMType, llvm::GlobalValue::InternalLinkage,
924351762cdSJohn McCall                            name.getString(), &CGM.getModule());
925351762cdSJohn McCall   CGM.SetInternalFunctionAttributes(blockDecl, fn, fnInfo);
9266a60fa24SAnders Carlsson 
927351762cdSJohn McCall   // Begin generating the function.
928a738c25fSJohn McCall   StartFunction(blockDecl, fnType->getResultType(), fn, fnInfo, args,
9295f070a51SDevang Patel                 blockInfo.getBlockExpr()->getBody()->getLocStart());
930351762cdSJohn McCall   CurFuncDecl = outerFnDecl; // StartFunction sets this to blockDecl
9316a60fa24SAnders Carlsson 
932147d021bSJohn McCall   // Okay.  Undo some of what StartFunction did.
933147d021bSJohn McCall 
934147d021bSJohn McCall   // Pull the 'self' reference out of the local decl map.
935147d021bSJohn McCall   llvm::Value *blockAddr = LocalDeclMap[&selfDecl];
936147d021bSJohn McCall   LocalDeclMap.erase(&selfDecl);
937351762cdSJohn McCall   BlockPointer = Builder.CreateBitCast(blockAddr,
938351762cdSJohn McCall                                        blockInfo.StructureType->getPointerTo(),
939351762cdSJohn McCall                                        "block");
940017460abSMike Stump 
94187fe5d56SJohn McCall   // If we have a C++ 'this' reference, go ahead and force it into
94287fe5d56SJohn McCall   // existence now.
943351762cdSJohn McCall   if (blockDecl->capturesCXXThis()) {
944351762cdSJohn McCall     llvm::Value *addr = Builder.CreateStructGEP(BlockPointer,
945351762cdSJohn McCall                                                 blockInfo.CXXThisIndex,
946351762cdSJohn McCall                                                 "block.captured-this");
947351762cdSJohn McCall     CXXThisValue = Builder.CreateLoad(addr, "this");
94887fe5d56SJohn McCall   }
94987fe5d56SJohn McCall 
950351762cdSJohn McCall   // LoadObjCSelf() expects there to be an entry for 'self' in LocalDeclMap;
951351762cdSJohn McCall   // appease it.
952351762cdSJohn McCall   if (const ObjCMethodDecl *method
953351762cdSJohn McCall         = dyn_cast_or_null<ObjCMethodDecl>(CurFuncDecl)) {
954351762cdSJohn McCall     const VarDecl *self = method->getSelfDecl();
955351762cdSJohn McCall 
956351762cdSJohn McCall     // There might not be a capture for 'self', but if there is...
957351762cdSJohn McCall     if (blockInfo.Captures.count(self)) {
958351762cdSJohn McCall       const CGBlockInfo::Capture &capture = blockInfo.getCapture(self);
959351762cdSJohn McCall       llvm::Value *selfAddr = Builder.CreateStructGEP(BlockPointer,
960351762cdSJohn McCall                                                       capture.getIndex(),
961351762cdSJohn McCall                                                       "block.captured-self");
962351762cdSJohn McCall       LocalDeclMap[self] = selfAddr;
963351762cdSJohn McCall     }
964351762cdSJohn McCall   }
965351762cdSJohn McCall 
966351762cdSJohn McCall   // Also force all the constant captures.
967351762cdSJohn McCall   for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
968351762cdSJohn McCall          ce = blockDecl->capture_end(); ci != ce; ++ci) {
969351762cdSJohn McCall     const VarDecl *variable = ci->getVariable();
970351762cdSJohn McCall     const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
971351762cdSJohn McCall     if (!capture.isConstant()) continue;
972351762cdSJohn McCall 
973351762cdSJohn McCall     unsigned align = getContext().getDeclAlign(variable).getQuantity();
974351762cdSJohn McCall 
975351762cdSJohn McCall     llvm::AllocaInst *alloca =
976351762cdSJohn McCall       CreateMemTemp(variable->getType(), "block.captured-const");
977351762cdSJohn McCall     alloca->setAlignment(align);
978351762cdSJohn McCall 
979351762cdSJohn McCall     Builder.CreateStore(capture.getConstant(), alloca, align);
980351762cdSJohn McCall 
981351762cdSJohn McCall     LocalDeclMap[variable] = alloca;
9829d42f0f1SJohn McCall   }
9839d42f0f1SJohn McCall 
984017460abSMike Stump   // Save a spot to insert the debug information for all the BlockDeclRefDecls.
985017460abSMike Stump   llvm::BasicBlock *entry = Builder.GetInsertBlock();
986017460abSMike Stump   llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint();
987017460abSMike Stump   --entry_ptr;
988017460abSMike Stump 
989351762cdSJohn McCall   EmitStmt(blockDecl->getBody());
990017460abSMike Stump 
9917d69911aSMike Stump   // Remember where we were...
9927d69911aSMike Stump   llvm::BasicBlock *resume = Builder.GetInsertBlock();
993017460abSMike Stump 
9947d69911aSMike Stump   // Go back to the entry.
995017460abSMike Stump   ++entry_ptr;
996017460abSMike Stump   Builder.SetInsertPoint(entry, entry_ptr);
997017460abSMike Stump 
9982e722b91SMike Stump   // Emit debug information for all the BlockDeclRefDecls.
99987fe5d56SJohn McCall   // FIXME: also for 'this'
1000351762cdSJohn McCall   if (CGDebugInfo *DI = getDebugInfo()) {
1001351762cdSJohn McCall     for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1002351762cdSJohn McCall            ce = blockDecl->capture_end(); ci != ce; ++ci) {
1003351762cdSJohn McCall       const VarDecl *variable = ci->getVariable();
1004351762cdSJohn McCall       DI->setLocation(variable->getLocation());
1005351762cdSJohn McCall 
1006351762cdSJohn McCall       const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1007351762cdSJohn McCall       if (capture.isConstant()) {
1008351762cdSJohn McCall         DI->EmitDeclareOfAutoVariable(variable, LocalDeclMap[variable],
1009351762cdSJohn McCall                                       Builder);
1010351762cdSJohn McCall         continue;
1011351762cdSJohn McCall       }
1012351762cdSJohn McCall 
1013147d021bSJohn McCall       DI->EmitDeclareOfBlockDeclRefVariable(variable, BlockPointer,
1014351762cdSJohn McCall                                             Builder, blockInfo);
10152e722b91SMike Stump     }
10162e722b91SMike Stump   }
1017351762cdSJohn McCall 
10187d69911aSMike Stump   // And resume where we left off.
10197d69911aSMike Stump   if (resume == 0)
10207d69911aSMike Stump     Builder.ClearInsertionPoint();
10217d69911aSMike Stump   else
10227d69911aSMike Stump     Builder.SetInsertPoint(resume);
10232e722b91SMike Stump 
1024351762cdSJohn McCall   FinishFunction(cast<CompoundStmt>(blockDecl->getBody())->getRBracLoc());
10256a60fa24SAnders Carlsson 
1026351762cdSJohn McCall   return fn;
10276a60fa24SAnders Carlsson }
10281db7d04bSMike Stump 
1029351762cdSJohn McCall /*
1030351762cdSJohn McCall     notes.push_back(HelperInfo());
1031351762cdSJohn McCall     HelperInfo &note = notes.back();
1032351762cdSJohn McCall     note.index = capture.getIndex();
1033351762cdSJohn McCall     note.RequiresCopying = (ci->hasCopyExpr() || BlockRequiresCopying(type));
1034351762cdSJohn McCall     note.cxxbar_import = ci->getCopyExpr();
10351db7d04bSMike Stump 
1036351762cdSJohn McCall     if (ci->isByRef()) {
1037351762cdSJohn McCall       note.flag = BLOCK_FIELD_IS_BYREF;
1038351762cdSJohn McCall       if (type.isObjCGCWeak())
1039351762cdSJohn McCall         note.flag |= BLOCK_FIELD_IS_WEAK;
1040351762cdSJohn McCall     } else if (type->isBlockPointerType()) {
1041351762cdSJohn McCall       note.flag = BLOCK_FIELD_IS_BLOCK;
1042351762cdSJohn McCall     } else {
1043351762cdSJohn McCall       note.flag = BLOCK_FIELD_IS_OBJECT;
10441db7d04bSMike Stump     }
1045351762cdSJohn McCall  */
10461db7d04bSMike Stump 
104797d01d50SMike Stump 
10480c743277SMike Stump 
1049351762cdSJohn McCall llvm::Constant *
1050ad7c5c16SJohn McCall CodeGenFunction::GenerateCopyHelperFunction(const CGBlockInfo &blockInfo) {
1051351762cdSJohn McCall   ASTContext &C = getContext();
1052351762cdSJohn McCall 
1053351762cdSJohn McCall   FunctionArgList args;
1054a738c25fSJohn McCall   ImplicitParamDecl dstDecl(0, SourceLocation(), 0, C.VoidPtrTy);
1055a738c25fSJohn McCall   args.push_back(&dstDecl);
1056a738c25fSJohn McCall   ImplicitParamDecl srcDecl(0, SourceLocation(), 0, C.VoidPtrTy);
1057a738c25fSJohn McCall   args.push_back(&srcDecl);
10580c743277SMike Stump 
10590c743277SMike Stump   const CGFunctionInfo &FI =
1060351762cdSJohn McCall       CGM.getTypes().getFunctionInfo(C.VoidTy, args, FunctionType::ExtInfo());
10610c743277SMike Stump 
1062351762cdSJohn McCall   // FIXME: it would be nice if these were mergeable with things with
1063351762cdSJohn McCall   // identical semantics.
10642192fe50SChris Lattner   llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI, false);
10650c743277SMike Stump 
10660c743277SMike Stump   llvm::Function *Fn =
10670c743277SMike Stump     llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
1068d6b28fc6SBenjamin Kramer                            "__copy_helper_block_", &CGM.getModule());
10690c743277SMike Stump 
10700c743277SMike Stump   IdentifierInfo *II
10710c743277SMike Stump     = &CGM.getContext().Idents.get("__copy_helper_block_");
10720c743277SMike Stump 
10732a7c2194SDevang Patel   // Check if we should generate debug info for this block helper function.
10742a7c2194SDevang Patel   if (CGM.getModuleDebugInfo())
10752a7c2194SDevang Patel     DebugInfo = CGM.getModuleDebugInfo();
10762a7c2194SDevang Patel 
1077351762cdSJohn McCall   FunctionDecl *FD = FunctionDecl::Create(C,
1078351762cdSJohn McCall                                           C.getTranslationUnitDecl(),
1079dff1930bSAbramo Bagnara                                           SourceLocation(),
1080351762cdSJohn McCall                                           SourceLocation(), II, C.VoidTy, 0,
10818e7d656aSJohn McCall                                           SC_Static,
10828e7d656aSJohn McCall                                           SC_None,
1083c4df4076SDouglas Gregor                                           false,
10840c743277SMike Stump                                           true);
1085a738c25fSJohn McCall   StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation());
1086aeb0ffd5SMike Stump 
10872192fe50SChris Lattner   llvm::Type *structPtrTy = blockInfo.StructureType->getPointerTo();
108867645930SMike Stump 
1089a738c25fSJohn McCall   llvm::Value *src = GetAddrOfLocalVar(&srcDecl);
1090ad7c5c16SJohn McCall   src = Builder.CreateLoad(src);
1091ad7c5c16SJohn McCall   src = Builder.CreateBitCast(src, structPtrTy, "block.source");
109267645930SMike Stump 
1093a738c25fSJohn McCall   llvm::Value *dst = GetAddrOfLocalVar(&dstDecl);
1094ad7c5c16SJohn McCall   dst = Builder.CreateLoad(dst);
1095ad7c5c16SJohn McCall   dst = Builder.CreateBitCast(dst, structPtrTy, "block.dest");
1096aeb0ffd5SMike Stump 
1097351762cdSJohn McCall   const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1098aeb0ffd5SMike Stump 
1099351762cdSJohn McCall   for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1100351762cdSJohn McCall          ce = blockDecl->capture_end(); ci != ce; ++ci) {
1101351762cdSJohn McCall     const VarDecl *variable = ci->getVariable();
1102351762cdSJohn McCall     QualType type = variable->getType();
1103aeb0ffd5SMike Stump 
1104351762cdSJohn McCall     const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1105351762cdSJohn McCall     if (capture.isConstant()) continue;
1106351762cdSJohn McCall 
1107351762cdSJohn McCall     const Expr *copyExpr = ci->getCopyExpr();
110831168b07SJohn McCall     BlockFieldFlags flags;
110931168b07SJohn McCall 
111031168b07SJohn McCall     bool isARCWeakCapture = false;
1111351762cdSJohn McCall 
1112351762cdSJohn McCall     if (copyExpr) {
1113351762cdSJohn McCall       assert(!ci->isByRef());
1114351762cdSJohn McCall       // don't bother computing flags
111531168b07SJohn McCall 
1116351762cdSJohn McCall     } else if (ci->isByRef()) {
1117351762cdSJohn McCall       flags = BLOCK_FIELD_IS_BYREF;
111831168b07SJohn McCall       if (type.isObjCGCWeak())
111931168b07SJohn McCall         flags |= BLOCK_FIELD_IS_WEAK;
1120351762cdSJohn McCall 
112131168b07SJohn McCall     } else if (type->isObjCRetainableType()) {
112231168b07SJohn McCall       flags = BLOCK_FIELD_IS_OBJECT;
112331168b07SJohn McCall       if (type->isBlockPointerType())
112431168b07SJohn McCall         flags = BLOCK_FIELD_IS_BLOCK;
112531168b07SJohn McCall 
112631168b07SJohn McCall       // Special rules for ARC captures:
112731168b07SJohn McCall       if (getLangOptions().ObjCAutoRefCount) {
112831168b07SJohn McCall         Qualifiers qs = type.getQualifiers();
112931168b07SJohn McCall 
113031168b07SJohn McCall         // Don't generate special copy logic for a captured object
113131168b07SJohn McCall         // unless it's __strong or __weak.
113231168b07SJohn McCall         if (!qs.hasStrongOrWeakObjCLifetime())
113331168b07SJohn McCall           continue;
113431168b07SJohn McCall 
113531168b07SJohn McCall         // Support __weak direct captures.
113631168b07SJohn McCall         if (qs.getObjCLifetime() == Qualifiers::OCL_Weak)
113731168b07SJohn McCall           isARCWeakCapture = true;
113831168b07SJohn McCall       }
113931168b07SJohn McCall     } else {
114031168b07SJohn McCall       continue;
114131168b07SJohn McCall     }
1142351762cdSJohn McCall 
1143351762cdSJohn McCall     unsigned index = capture.getIndex();
1144ad7c5c16SJohn McCall     llvm::Value *srcField = Builder.CreateStructGEP(src, index);
1145ad7c5c16SJohn McCall     llvm::Value *dstField = Builder.CreateStructGEP(dst, index);
1146351762cdSJohn McCall 
1147351762cdSJohn McCall     // If there's an explicit copy expression, we do that.
1148351762cdSJohn McCall     if (copyExpr) {
1149ad7c5c16SJohn McCall       EmitSynthesizedCXXCopyCtor(dstField, srcField, copyExpr);
115031168b07SJohn McCall     } else if (isARCWeakCapture) {
115131168b07SJohn McCall       EmitARCCopyWeak(dstField, srcField);
1152351762cdSJohn McCall     } else {
1153351762cdSJohn McCall       llvm::Value *srcValue = Builder.CreateLoad(srcField, "blockcopy.src");
1154e3dc1707SJohn McCall       srcValue = Builder.CreateBitCast(srcValue, VoidPtrTy);
1155e3dc1707SJohn McCall       llvm::Value *dstAddr = Builder.CreateBitCast(dstField, VoidPtrTy);
1156351762cdSJohn McCall       Builder.CreateCall3(CGM.getBlockObjectAssign(), dstAddr, srcValue,
115731168b07SJohn McCall                           llvm::ConstantInt::get(Int32Ty, flags.getBitMask()));
115867645930SMike Stump     }
1159e988bdacSFariborz Jahanian   }
1160aeb0ffd5SMike Stump 
1161ad7c5c16SJohn McCall   FinishFunction();
11620c743277SMike Stump 
1163e3dc1707SJohn McCall   return llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy);
116497d01d50SMike Stump }
116597d01d50SMike Stump 
1166351762cdSJohn McCall llvm::Constant *
1167ad7c5c16SJohn McCall CodeGenFunction::GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo) {
1168351762cdSJohn McCall   ASTContext &C = getContext();
11690c743277SMike Stump 
1170351762cdSJohn McCall   FunctionArgList args;
1171a738c25fSJohn McCall   ImplicitParamDecl srcDecl(0, SourceLocation(), 0, C.VoidPtrTy);
1172a738c25fSJohn McCall   args.push_back(&srcDecl);
11730c743277SMike Stump 
11740c743277SMike Stump   const CGFunctionInfo &FI =
1175351762cdSJohn McCall       CGM.getTypes().getFunctionInfo(C.VoidTy, args, FunctionType::ExtInfo());
11760c743277SMike Stump 
1177cbc2bcafSMike Stump   // FIXME: We'd like to put these into a mergable by content, with
1178cbc2bcafSMike Stump   // internal linkage.
11792192fe50SChris Lattner   llvm::FunctionType *LTy = CGM.getTypes().GetFunctionType(FI, false);
11800c743277SMike Stump 
11810c743277SMike Stump   llvm::Function *Fn =
11820c743277SMike Stump     llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
1183d6b28fc6SBenjamin Kramer                            "__destroy_helper_block_", &CGM.getModule());
11840c743277SMike Stump 
11852a7c2194SDevang Patel   // Check if we should generate debug info for this block destroy function.
11862a7c2194SDevang Patel   if (CGM.getModuleDebugInfo())
11872a7c2194SDevang Patel     DebugInfo = CGM.getModuleDebugInfo();
11882a7c2194SDevang Patel 
11890c743277SMike Stump   IdentifierInfo *II
11900c743277SMike Stump     = &CGM.getContext().Idents.get("__destroy_helper_block_");
11910c743277SMike Stump 
1192351762cdSJohn McCall   FunctionDecl *FD = FunctionDecl::Create(C, C.getTranslationUnitDecl(),
1193dff1930bSAbramo Bagnara                                           SourceLocation(),
1194351762cdSJohn McCall                                           SourceLocation(), II, C.VoidTy, 0,
11958e7d656aSJohn McCall                                           SC_Static,
11968e7d656aSJohn McCall                                           SC_None,
1197c4df4076SDouglas Gregor                                           false, true);
1198a738c25fSJohn McCall   StartFunction(FD, C.VoidTy, Fn, FI, args, SourceLocation());
11996f7d9f87SMike Stump 
12002192fe50SChris Lattner   llvm::Type *structPtrTy = blockInfo.StructureType->getPointerTo();
120167645930SMike Stump 
1202a738c25fSJohn McCall   llvm::Value *src = GetAddrOfLocalVar(&srcDecl);
1203ad7c5c16SJohn McCall   src = Builder.CreateLoad(src);
1204ad7c5c16SJohn McCall   src = Builder.CreateBitCast(src, structPtrTy, "block");
12056f7d9f87SMike Stump 
1206351762cdSJohn McCall   const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1207351762cdSJohn McCall 
1208ad7c5c16SJohn McCall   CodeGenFunction::RunCleanupsScope cleanups(*this);
1209351762cdSJohn McCall 
1210351762cdSJohn McCall   for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1211351762cdSJohn McCall          ce = blockDecl->capture_end(); ci != ce; ++ci) {
1212351762cdSJohn McCall     const VarDecl *variable = ci->getVariable();
1213351762cdSJohn McCall     QualType type = variable->getType();
1214351762cdSJohn McCall 
1215351762cdSJohn McCall     const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1216351762cdSJohn McCall     if (capture.isConstant()) continue;
1217351762cdSJohn McCall 
1218ad7c5c16SJohn McCall     BlockFieldFlags flags;
1219351762cdSJohn McCall     const CXXDestructorDecl *dtor = 0;
1220351762cdSJohn McCall 
122131168b07SJohn McCall     bool isARCWeakCapture = false;
122231168b07SJohn McCall 
1223351762cdSJohn McCall     if (ci->isByRef()) {
1224351762cdSJohn McCall       flags = BLOCK_FIELD_IS_BYREF;
122531168b07SJohn McCall       if (type.isObjCGCWeak())
122631168b07SJohn McCall         flags |= BLOCK_FIELD_IS_WEAK;
122731168b07SJohn McCall     } else if (const CXXRecordDecl *record = type->getAsCXXRecordDecl()) {
122831168b07SJohn McCall       if (record->hasTrivialDestructor())
122931168b07SJohn McCall         continue;
1230351762cdSJohn McCall       dtor = record->getDestructor();
123131168b07SJohn McCall     } else if (type->isObjCRetainableType()) {
123231168b07SJohn McCall       flags = BLOCK_FIELD_IS_OBJECT;
123331168b07SJohn McCall       if (type->isBlockPointerType())
123431168b07SJohn McCall         flags = BLOCK_FIELD_IS_BLOCK;
1235351762cdSJohn McCall 
123631168b07SJohn McCall       // Special rules for ARC captures.
123731168b07SJohn McCall       if (getLangOptions().ObjCAutoRefCount) {
123831168b07SJohn McCall         Qualifiers qs = type.getQualifiers();
123931168b07SJohn McCall 
124031168b07SJohn McCall         // Don't generate special dispose logic for a captured object
124131168b07SJohn McCall         // unless it's __strong or __weak.
124231168b07SJohn McCall         if (!qs.hasStrongOrWeakObjCLifetime())
124331168b07SJohn McCall           continue;
124431168b07SJohn McCall 
124531168b07SJohn McCall         // Support __weak direct captures.
124631168b07SJohn McCall         if (qs.getObjCLifetime() == Qualifiers::OCL_Weak)
124731168b07SJohn McCall           isARCWeakCapture = true;
124831168b07SJohn McCall       }
124931168b07SJohn McCall     } else {
125031168b07SJohn McCall       continue;
125131168b07SJohn McCall     }
1252351762cdSJohn McCall 
1253351762cdSJohn McCall     unsigned index = capture.getIndex();
1254ad7c5c16SJohn McCall     llvm::Value *srcField = Builder.CreateStructGEP(src, index);
1255351762cdSJohn McCall 
1256351762cdSJohn McCall     // If there's an explicit copy expression, we do that.
1257351762cdSJohn McCall     if (dtor) {
1258ad7c5c16SJohn McCall       PushDestructorCleanup(dtor, srcField);
1259351762cdSJohn McCall 
126031168b07SJohn McCall     // If this is a __weak capture, emit the release directly.
126131168b07SJohn McCall     } else if (isARCWeakCapture) {
126231168b07SJohn McCall       EmitARCDestroyWeak(srcField);
126331168b07SJohn McCall 
1264351762cdSJohn McCall     // Otherwise we call _Block_object_dispose.  It wouldn't be too
1265351762cdSJohn McCall     // hard to just emit this as a cleanup if we wanted to make sure
1266351762cdSJohn McCall     // that things were done in reverse.
1267351762cdSJohn McCall     } else {
1268351762cdSJohn McCall       llvm::Value *value = Builder.CreateLoad(srcField);
1269e3dc1707SJohn McCall       value = Builder.CreateBitCast(value, VoidPtrTy);
1270351762cdSJohn McCall       BuildBlockRelease(value, flags);
12716f7d9f87SMike Stump     }
12726f7d9f87SMike Stump   }
1273351762cdSJohn McCall 
1274351762cdSJohn McCall   cleanups.ForceCleanup();
12756f7d9f87SMike Stump 
1276ad7c5c16SJohn McCall   FinishFunction();
12770c743277SMike Stump 
1278e3dc1707SJohn McCall   return llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy);
12790c743277SMike Stump }
12800c743277SMike Stump 
1281f9b056b0SJohn McCall namespace {
1282f9b056b0SJohn McCall 
1283f9b056b0SJohn McCall /// Emits the copy/dispose helper functions for a __block object of id type.
1284f9b056b0SJohn McCall class ObjectByrefHelpers : public CodeGenModule::ByrefHelpers {
1285f9b056b0SJohn McCall   BlockFieldFlags Flags;
1286f9b056b0SJohn McCall 
1287f9b056b0SJohn McCall public:
1288f9b056b0SJohn McCall   ObjectByrefHelpers(CharUnits alignment, BlockFieldFlags flags)
1289f9b056b0SJohn McCall     : ByrefHelpers(alignment), Flags(flags) {}
1290f9b056b0SJohn McCall 
12917c623641SJohn McCall   void emitCopy(CodeGenFunction &CGF, llvm::Value *destField,
12927c623641SJohn McCall                 llvm::Value *srcField) {
1293f9b056b0SJohn McCall     destField = CGF.Builder.CreateBitCast(destField, CGF.VoidPtrTy);
1294f9b056b0SJohn McCall 
1295f9b056b0SJohn McCall     srcField = CGF.Builder.CreateBitCast(srcField, CGF.VoidPtrPtrTy);
1296f9b056b0SJohn McCall     llvm::Value *srcValue = CGF.Builder.CreateLoad(srcField);
1297f9b056b0SJohn McCall 
1298f9b056b0SJohn McCall     unsigned flags = (Flags | BLOCK_BYREF_CALLER).getBitMask();
1299f9b056b0SJohn McCall 
1300f9b056b0SJohn McCall     llvm::Value *flagsVal = llvm::ConstantInt::get(CGF.Int32Ty, flags);
1301f9b056b0SJohn McCall     llvm::Value *fn = CGF.CGM.getBlockObjectAssign();
1302f9b056b0SJohn McCall     CGF.Builder.CreateCall3(fn, destField, srcValue, flagsVal);
1303f9b056b0SJohn McCall   }
1304f9b056b0SJohn McCall 
1305f9b056b0SJohn McCall   void emitDispose(CodeGenFunction &CGF, llvm::Value *field) {
1306f9b056b0SJohn McCall     field = CGF.Builder.CreateBitCast(field, CGF.Int8PtrTy->getPointerTo(0));
1307f9b056b0SJohn McCall     llvm::Value *value = CGF.Builder.CreateLoad(field);
1308f9b056b0SJohn McCall 
1309f9b056b0SJohn McCall     CGF.BuildBlockRelease(value, Flags | BLOCK_BYREF_CALLER);
1310f9b056b0SJohn McCall   }
1311f9b056b0SJohn McCall 
1312f9b056b0SJohn McCall   void profileImpl(llvm::FoldingSetNodeID &id) const {
1313f9b056b0SJohn McCall     id.AddInteger(Flags.getBitMask());
1314f9b056b0SJohn McCall   }
1315f9b056b0SJohn McCall };
1316f9b056b0SJohn McCall 
131731168b07SJohn McCall /// Emits the copy/dispose helpers for an ARC __block __weak variable.
131831168b07SJohn McCall class ARCWeakByrefHelpers : public CodeGenModule::ByrefHelpers {
131931168b07SJohn McCall public:
132031168b07SJohn McCall   ARCWeakByrefHelpers(CharUnits alignment) : ByrefHelpers(alignment) {}
132131168b07SJohn McCall 
132231168b07SJohn McCall   void emitCopy(CodeGenFunction &CGF, llvm::Value *destField,
132331168b07SJohn McCall                 llvm::Value *srcField) {
132431168b07SJohn McCall     CGF.EmitARCMoveWeak(destField, srcField);
132531168b07SJohn McCall   }
132631168b07SJohn McCall 
132731168b07SJohn McCall   void emitDispose(CodeGenFunction &CGF, llvm::Value *field) {
132831168b07SJohn McCall     CGF.EmitARCDestroyWeak(field);
132931168b07SJohn McCall   }
133031168b07SJohn McCall 
133131168b07SJohn McCall   void profileImpl(llvm::FoldingSetNodeID &id) const {
133231168b07SJohn McCall     // 0 is distinguishable from all pointers and byref flags
133331168b07SJohn McCall     id.AddInteger(0);
133431168b07SJohn McCall   }
133531168b07SJohn McCall };
133631168b07SJohn McCall 
133731168b07SJohn McCall /// Emits the copy/dispose helpers for an ARC __block __strong variable
133831168b07SJohn McCall /// that's not of block-pointer type.
133931168b07SJohn McCall class ARCStrongByrefHelpers : public CodeGenModule::ByrefHelpers {
134031168b07SJohn McCall public:
134131168b07SJohn McCall   ARCStrongByrefHelpers(CharUnits alignment) : ByrefHelpers(alignment) {}
134231168b07SJohn McCall 
134331168b07SJohn McCall   void emitCopy(CodeGenFunction &CGF, llvm::Value *destField,
134431168b07SJohn McCall                 llvm::Value *srcField) {
134531168b07SJohn McCall     // Do a "move" by copying the value and then zeroing out the old
134631168b07SJohn McCall     // variable.
134731168b07SJohn McCall 
134831168b07SJohn McCall     llvm::Value *value = CGF.Builder.CreateLoad(srcField);
134931168b07SJohn McCall     llvm::Value *null =
135031168b07SJohn McCall       llvm::ConstantPointerNull::get(cast<llvm::PointerType>(value->getType()));
135131168b07SJohn McCall     CGF.Builder.CreateStore(value, destField);
135231168b07SJohn McCall     CGF.Builder.CreateStore(null, srcField);
135331168b07SJohn McCall   }
135431168b07SJohn McCall 
135531168b07SJohn McCall   void emitDispose(CodeGenFunction &CGF, llvm::Value *field) {
135631168b07SJohn McCall     llvm::Value *value = CGF.Builder.CreateLoad(field);
135731168b07SJohn McCall     CGF.EmitARCRelease(value, /*precise*/ false);
135831168b07SJohn McCall   }
135931168b07SJohn McCall 
136031168b07SJohn McCall   void profileImpl(llvm::FoldingSetNodeID &id) const {
136131168b07SJohn McCall     // 1 is distinguishable from all pointers and byref flags
136231168b07SJohn McCall     id.AddInteger(1);
136331168b07SJohn McCall   }
136431168b07SJohn McCall };
136531168b07SJohn McCall 
1366f9b056b0SJohn McCall /// Emits the copy/dispose helpers for a __block variable with a
1367f9b056b0SJohn McCall /// nontrivial copy constructor or destructor.
1368f9b056b0SJohn McCall class CXXByrefHelpers : public CodeGenModule::ByrefHelpers {
1369f9b056b0SJohn McCall   QualType VarType;
1370f9b056b0SJohn McCall   const Expr *CopyExpr;
1371f9b056b0SJohn McCall 
1372f9b056b0SJohn McCall public:
1373f9b056b0SJohn McCall   CXXByrefHelpers(CharUnits alignment, QualType type,
1374f9b056b0SJohn McCall                   const Expr *copyExpr)
1375f9b056b0SJohn McCall     : ByrefHelpers(alignment), VarType(type), CopyExpr(copyExpr) {}
1376f9b056b0SJohn McCall 
1377f9b056b0SJohn McCall   bool needsCopy() const { return CopyExpr != 0; }
1378f9b056b0SJohn McCall   void emitCopy(CodeGenFunction &CGF, llvm::Value *destField,
1379f9b056b0SJohn McCall                 llvm::Value *srcField) {
1380f9b056b0SJohn McCall     if (!CopyExpr) return;
1381f9b056b0SJohn McCall     CGF.EmitSynthesizedCXXCopyCtor(destField, srcField, CopyExpr);
1382f9b056b0SJohn McCall   }
1383f9b056b0SJohn McCall 
1384f9b056b0SJohn McCall   void emitDispose(CodeGenFunction &CGF, llvm::Value *field) {
1385f9b056b0SJohn McCall     EHScopeStack::stable_iterator cleanupDepth = CGF.EHStack.stable_begin();
1386f9b056b0SJohn McCall     CGF.PushDestructorCleanup(VarType, field);
1387f9b056b0SJohn McCall     CGF.PopCleanupBlocks(cleanupDepth);
1388f9b056b0SJohn McCall   }
1389f9b056b0SJohn McCall 
1390f9b056b0SJohn McCall   void profileImpl(llvm::FoldingSetNodeID &id) const {
1391f9b056b0SJohn McCall     id.AddPointer(VarType.getCanonicalType().getAsOpaquePtr());
1392f9b056b0SJohn McCall   }
1393f9b056b0SJohn McCall };
1394f9b056b0SJohn McCall } // end anonymous namespace
1395f9b056b0SJohn McCall 
1396f9b056b0SJohn McCall static llvm::Constant *
1397f9b056b0SJohn McCall generateByrefCopyHelper(CodeGenFunction &CGF,
13982192fe50SChris Lattner                         llvm::StructType &byrefType,
1399f9b056b0SJohn McCall                         CodeGenModule::ByrefHelpers &byrefInfo) {
1400f9b056b0SJohn McCall   ASTContext &Context = CGF.getContext();
1401f9b056b0SJohn McCall 
1402f9b056b0SJohn McCall   QualType R = Context.VoidTy;
1403ee2a5ee5SMike Stump 
1404a738c25fSJohn McCall   FunctionArgList args;
1405f9b056b0SJohn McCall   ImplicitParamDecl dst(0, SourceLocation(), 0, Context.VoidPtrTy);
1406a738c25fSJohn McCall   args.push_back(&dst);
1407f89230d9SMike Stump 
1408f9b056b0SJohn McCall   ImplicitParamDecl src(0, SourceLocation(), 0, Context.VoidPtrTy);
1409a738c25fSJohn McCall   args.push_back(&src);
1410ee2a5ee5SMike Stump 
1411ee2a5ee5SMike Stump   const CGFunctionInfo &FI =
1412f9b056b0SJohn McCall     CGF.CGM.getTypes().getFunctionInfo(R, args, FunctionType::ExtInfo());
1413ee2a5ee5SMike Stump 
1414f9b056b0SJohn McCall   CodeGenTypes &Types = CGF.CGM.getTypes();
14152192fe50SChris Lattner   llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1416ee2a5ee5SMike Stump 
1417cbc2bcafSMike Stump   // FIXME: We'd like to put these into a mergable by content, with
1418cbc2bcafSMike Stump   // internal linkage.
1419ee2a5ee5SMike Stump   llvm::Function *Fn =
1420ee2a5ee5SMike Stump     llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
1421f9b056b0SJohn McCall                            "__Block_byref_object_copy_", &CGF.CGM.getModule());
1422ee2a5ee5SMike Stump 
1423ee2a5ee5SMike Stump   IdentifierInfo *II
1424f9b056b0SJohn McCall     = &Context.Idents.get("__Block_byref_object_copy_");
1425ee2a5ee5SMike Stump 
1426f9b056b0SJohn McCall   FunctionDecl *FD = FunctionDecl::Create(Context,
1427f9b056b0SJohn McCall                                           Context.getTranslationUnitDecl(),
1428dff1930bSAbramo Bagnara                                           SourceLocation(),
142960ed5604SArgyrios Kyrtzidis                                           SourceLocation(), II, R, 0,
14308e7d656aSJohn McCall                                           SC_Static,
14318e7d656aSJohn McCall                                           SC_None,
1432c4df4076SDouglas Gregor                                           false, true);
143331168b07SJohn McCall 
1434f9b056b0SJohn McCall   CGF.StartFunction(FD, R, Fn, FI, args, SourceLocation());
1435f9b056b0SJohn McCall 
1436f9b056b0SJohn McCall   if (byrefInfo.needsCopy()) {
14372192fe50SChris Lattner     llvm::Type *byrefPtrType = byrefType.getPointerTo(0);
1438f89230d9SMike Stump 
1439f89230d9SMike Stump     // dst->x
1440f9b056b0SJohn McCall     llvm::Value *destField = CGF.GetAddrOfLocalVar(&dst);
1441f9b056b0SJohn McCall     destField = CGF.Builder.CreateLoad(destField);
1442f9b056b0SJohn McCall     destField = CGF.Builder.CreateBitCast(destField, byrefPtrType);
1443f9b056b0SJohn McCall     destField = CGF.Builder.CreateStructGEP(destField, 6, "x");
1444f89230d9SMike Stump 
1445f89230d9SMike Stump     // src->x
1446f9b056b0SJohn McCall     llvm::Value *srcField = CGF.GetAddrOfLocalVar(&src);
1447f9b056b0SJohn McCall     srcField = CGF.Builder.CreateLoad(srcField);
1448f9b056b0SJohn McCall     srcField = CGF.Builder.CreateBitCast(srcField, byrefPtrType);
1449f9b056b0SJohn McCall     srcField = CGF.Builder.CreateStructGEP(srcField, 6, "x");
145050198098SFariborz Jahanian 
1451f9b056b0SJohn McCall     byrefInfo.emitCopy(CGF, destField, srcField);
145250198098SFariborz Jahanian   }
1453f89230d9SMike Stump 
1454f9b056b0SJohn McCall   CGF.FinishFunction();
1455ee2a5ee5SMike Stump 
1456f9b056b0SJohn McCall   return llvm::ConstantExpr::getBitCast(Fn, CGF.Int8PtrTy);
1457ee2a5ee5SMike Stump }
1458ee2a5ee5SMike Stump 
1459f9b056b0SJohn McCall /// Build the copy helper for a __block variable.
1460f9b056b0SJohn McCall static llvm::Constant *buildByrefCopyHelper(CodeGenModule &CGM,
14612192fe50SChris Lattner                                             llvm::StructType &byrefType,
1462f9b056b0SJohn McCall                                             CodeGenModule::ByrefHelpers &info) {
1463f9b056b0SJohn McCall   CodeGenFunction CGF(CGM);
1464f9b056b0SJohn McCall   return generateByrefCopyHelper(CGF, byrefType, info);
1465f9b056b0SJohn McCall }
1466f9b056b0SJohn McCall 
1467f9b056b0SJohn McCall /// Generate code for a __block variable's dispose helper.
1468f9b056b0SJohn McCall static llvm::Constant *
1469f9b056b0SJohn McCall generateByrefDisposeHelper(CodeGenFunction &CGF,
14702192fe50SChris Lattner                            llvm::StructType &byrefType,
1471f9b056b0SJohn McCall                            CodeGenModule::ByrefHelpers &byrefInfo) {
1472f9b056b0SJohn McCall   ASTContext &Context = CGF.getContext();
1473f9b056b0SJohn McCall   QualType R = Context.VoidTy;
1474ee2a5ee5SMike Stump 
1475a738c25fSJohn McCall   FunctionArgList args;
1476f9b056b0SJohn McCall   ImplicitParamDecl src(0, SourceLocation(), 0, Context.VoidPtrTy);
1477a738c25fSJohn McCall   args.push_back(&src);
1478ee2a5ee5SMike Stump 
1479ee2a5ee5SMike Stump   const CGFunctionInfo &FI =
1480f9b056b0SJohn McCall     CGF.CGM.getTypes().getFunctionInfo(R, args, FunctionType::ExtInfo());
1481ee2a5ee5SMike Stump 
1482f9b056b0SJohn McCall   CodeGenTypes &Types = CGF.CGM.getTypes();
14832192fe50SChris Lattner   llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
1484ee2a5ee5SMike Stump 
1485cbc2bcafSMike Stump   // FIXME: We'd like to put these into a mergable by content, with
1486cbc2bcafSMike Stump   // internal linkage.
1487ee2a5ee5SMike Stump   llvm::Function *Fn =
1488ee2a5ee5SMike Stump     llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
148950198098SFariborz Jahanian                            "__Block_byref_object_dispose_",
1490f9b056b0SJohn McCall                            &CGF.CGM.getModule());
1491ee2a5ee5SMike Stump 
1492ee2a5ee5SMike Stump   IdentifierInfo *II
1493f9b056b0SJohn McCall     = &Context.Idents.get("__Block_byref_object_dispose_");
1494ee2a5ee5SMike Stump 
1495f9b056b0SJohn McCall   FunctionDecl *FD = FunctionDecl::Create(Context,
1496f9b056b0SJohn McCall                                           Context.getTranslationUnitDecl(),
1497dff1930bSAbramo Bagnara                                           SourceLocation(),
149860ed5604SArgyrios Kyrtzidis                                           SourceLocation(), II, R, 0,
14998e7d656aSJohn McCall                                           SC_Static,
15008e7d656aSJohn McCall                                           SC_None,
1501c4df4076SDouglas Gregor                                           false, true);
1502f9b056b0SJohn McCall   CGF.StartFunction(FD, R, Fn, FI, args, SourceLocation());
1503fbe25dd1SMike Stump 
1504f9b056b0SJohn McCall   if (byrefInfo.needsDispose()) {
1505f9b056b0SJohn McCall     llvm::Value *V = CGF.GetAddrOfLocalVar(&src);
1506f9b056b0SJohn McCall     V = CGF.Builder.CreateLoad(V);
1507f9b056b0SJohn McCall     V = CGF.Builder.CreateBitCast(V, byrefType.getPointerTo(0));
1508f9b056b0SJohn McCall     V = CGF.Builder.CreateStructGEP(V, 6, "x");
1509ad7c5c16SJohn McCall 
1510f9b056b0SJohn McCall     byrefInfo.emitDispose(CGF, V);
1511f9b056b0SJohn McCall   }
1512ad7c5c16SJohn McCall 
1513f9b056b0SJohn McCall   CGF.FinishFunction();
1514f9b056b0SJohn McCall 
1515f9b056b0SJohn McCall   return llvm::ConstantExpr::getBitCast(Fn, CGF.Int8PtrTy);
1516f9b056b0SJohn McCall }
1517f9b056b0SJohn McCall 
1518f9b056b0SJohn McCall /// Build the dispose helper for a __block variable.
1519f9b056b0SJohn McCall static llvm::Constant *buildByrefDisposeHelper(CodeGenModule &CGM,
15202192fe50SChris Lattner                                               llvm::StructType &byrefType,
1521f9b056b0SJohn McCall                                             CodeGenModule::ByrefHelpers &info) {
1522f9b056b0SJohn McCall   CodeGenFunction CGF(CGM);
1523f9b056b0SJohn McCall   return generateByrefDisposeHelper(CGF, byrefType, info);
1524f9b056b0SJohn McCall }
1525f9b056b0SJohn McCall 
1526f9b056b0SJohn McCall ///
1527f9b056b0SJohn McCall template <class T> static T *buildByrefHelpers(CodeGenModule &CGM,
15282192fe50SChris Lattner                                                llvm::StructType &byrefTy,
1529f9b056b0SJohn McCall                                                T &byrefInfo) {
1530f9b056b0SJohn McCall   // Increase the field's alignment to be at least pointer alignment,
1531f9b056b0SJohn McCall   // since the layout of the byref struct will guarantee at least that.
1532f9b056b0SJohn McCall   byrefInfo.Alignment = std::max(byrefInfo.Alignment,
1533f9b056b0SJohn McCall                               CharUnits::fromQuantity(CGM.PointerAlignInBytes));
1534f9b056b0SJohn McCall 
1535f9b056b0SJohn McCall   llvm::FoldingSetNodeID id;
1536f9b056b0SJohn McCall   byrefInfo.Profile(id);
1537f9b056b0SJohn McCall 
1538f9b056b0SJohn McCall   void *insertPos;
1539f9b056b0SJohn McCall   CodeGenModule::ByrefHelpers *node
1540f9b056b0SJohn McCall     = CGM.ByrefHelpersCache.FindNodeOrInsertPos(id, insertPos);
1541f9b056b0SJohn McCall   if (node) return static_cast<T*>(node);
1542f9b056b0SJohn McCall 
1543f9b056b0SJohn McCall   byrefInfo.CopyHelper = buildByrefCopyHelper(CGM, byrefTy, byrefInfo);
1544f9b056b0SJohn McCall   byrefInfo.DisposeHelper = buildByrefDisposeHelper(CGM, byrefTy, byrefInfo);
1545f9b056b0SJohn McCall 
1546f9b056b0SJohn McCall   T *copy = new (CGM.getContext()) T(byrefInfo);
1547f9b056b0SJohn McCall   CGM.ByrefHelpersCache.InsertNode(copy, insertPos);
1548f9b056b0SJohn McCall   return copy;
1549f9b056b0SJohn McCall }
1550f9b056b0SJohn McCall 
1551f9b056b0SJohn McCall CodeGenModule::ByrefHelpers *
15522192fe50SChris Lattner CodeGenFunction::buildByrefHelpers(llvm::StructType &byrefType,
1553f9b056b0SJohn McCall                                    const AutoVarEmission &emission) {
1554f9b056b0SJohn McCall   const VarDecl &var = *emission.Variable;
1555f9b056b0SJohn McCall   QualType type = var.getType();
1556f9b056b0SJohn McCall 
1557f9b056b0SJohn McCall   if (const CXXRecordDecl *record = type->getAsCXXRecordDecl()) {
1558f9b056b0SJohn McCall     const Expr *copyExpr = CGM.getContext().getBlockVarCopyInits(&var);
1559f9b056b0SJohn McCall     if (!copyExpr && record->hasTrivialDestructor()) return 0;
1560f9b056b0SJohn McCall 
1561f9b056b0SJohn McCall     CXXByrefHelpers byrefInfo(emission.Alignment, type, copyExpr);
1562f9b056b0SJohn McCall     return ::buildByrefHelpers(CGM, byrefType, byrefInfo);
1563f9b056b0SJohn McCall   }
1564f9b056b0SJohn McCall 
156531168b07SJohn McCall   // Otherwise, if we don't have a retainable type, there's nothing to do.
156631168b07SJohn McCall   // that the runtime does extra copies.
156731168b07SJohn McCall   if (!type->isObjCRetainableType()) return 0;
156831168b07SJohn McCall 
156931168b07SJohn McCall   Qualifiers qs = type.getQualifiers();
157031168b07SJohn McCall 
157131168b07SJohn McCall   // If we have lifetime, that dominates.
157231168b07SJohn McCall   if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) {
157331168b07SJohn McCall     assert(getLangOptions().ObjCAutoRefCount);
157431168b07SJohn McCall 
157531168b07SJohn McCall     switch (lifetime) {
157631168b07SJohn McCall     case Qualifiers::OCL_None: llvm_unreachable("impossible");
157731168b07SJohn McCall 
157831168b07SJohn McCall     // These are just bits as far as the runtime is concerned.
157931168b07SJohn McCall     case Qualifiers::OCL_ExplicitNone:
158031168b07SJohn McCall     case Qualifiers::OCL_Autoreleasing:
158131168b07SJohn McCall       return 0;
158231168b07SJohn McCall 
158331168b07SJohn McCall     // Tell the runtime that this is ARC __weak, called by the
158431168b07SJohn McCall     // byref routines.
158531168b07SJohn McCall     case Qualifiers::OCL_Weak: {
158631168b07SJohn McCall       ARCWeakByrefHelpers byrefInfo(emission.Alignment);
158731168b07SJohn McCall       return ::buildByrefHelpers(CGM, byrefType, byrefInfo);
158831168b07SJohn McCall     }
158931168b07SJohn McCall 
159031168b07SJohn McCall     // ARC __strong __block variables need to be retained.
159131168b07SJohn McCall     case Qualifiers::OCL_Strong:
159231168b07SJohn McCall       // Block-pointers need to be _Block_copy'ed, so we let the
159331168b07SJohn McCall       // runtime be in charge.  But we can't use the code below
159431168b07SJohn McCall       // because we don't want to set BYREF_CALLER, which will
159531168b07SJohn McCall       // just make the runtime ignore us.
159631168b07SJohn McCall       if (type->isBlockPointerType()) {
159731168b07SJohn McCall         BlockFieldFlags flags = BLOCK_FIELD_IS_BLOCK;
159831168b07SJohn McCall         ObjectByrefHelpers byrefInfo(emission.Alignment, flags);
159931168b07SJohn McCall         return ::buildByrefHelpers(CGM, byrefType, byrefInfo);
160031168b07SJohn McCall 
160131168b07SJohn McCall       // Otherwise, we transfer ownership of the retain from the stack
160231168b07SJohn McCall       // to the heap.
160331168b07SJohn McCall       } else {
160431168b07SJohn McCall         ARCStrongByrefHelpers byrefInfo(emission.Alignment);
160531168b07SJohn McCall         return ::buildByrefHelpers(CGM, byrefType, byrefInfo);
160631168b07SJohn McCall       }
160731168b07SJohn McCall     }
160831168b07SJohn McCall     llvm_unreachable("fell out of lifetime switch!");
160931168b07SJohn McCall   }
161031168b07SJohn McCall 
1611f9b056b0SJohn McCall   BlockFieldFlags flags;
1612f9b056b0SJohn McCall   if (type->isBlockPointerType()) {
1613f9b056b0SJohn McCall     flags |= BLOCK_FIELD_IS_BLOCK;
1614f9b056b0SJohn McCall   } else if (CGM.getContext().isObjCNSObjectType(type) ||
1615f9b056b0SJohn McCall              type->isObjCObjectPointerType()) {
1616f9b056b0SJohn McCall     flags |= BLOCK_FIELD_IS_OBJECT;
1617ad7c5c16SJohn McCall   } else {
1618f9b056b0SJohn McCall     return 0;
1619ee2a5ee5SMike Stump   }
1620ee2a5ee5SMike Stump 
1621f9b056b0SJohn McCall   if (type.isObjCGCWeak())
1622f9b056b0SJohn McCall     flags |= BLOCK_FIELD_IS_WEAK;
1623ad7c5c16SJohn McCall 
1624f9b056b0SJohn McCall   ObjectByrefHelpers byrefInfo(emission.Alignment, flags);
1625f9b056b0SJohn McCall   return ::buildByrefHelpers(CGM, byrefType, byrefInfo);
1626ee2a5ee5SMike Stump }
1627ee2a5ee5SMike Stump 
162873064870SJohn McCall unsigned CodeGenFunction::getByRefValueLLVMField(const ValueDecl *VD) const {
162973064870SJohn McCall   assert(ByRefValueInfo.count(VD) && "Did not find value!");
163073064870SJohn McCall 
163173064870SJohn McCall   return ByRefValueInfo.find(VD)->second.second;
163273064870SJohn McCall }
163373064870SJohn McCall 
163473064870SJohn McCall llvm::Value *CodeGenFunction::BuildBlockByrefAddress(llvm::Value *BaseAddr,
163573064870SJohn McCall                                                      const VarDecl *V) {
163673064870SJohn McCall   llvm::Value *Loc = Builder.CreateStructGEP(BaseAddr, 1, "forwarding");
163773064870SJohn McCall   Loc = Builder.CreateLoad(Loc);
163873064870SJohn McCall   Loc = Builder.CreateStructGEP(Loc, getByRefValueLLVMField(V),
163973064870SJohn McCall                                 V->getNameAsString());
164073064870SJohn McCall   return Loc;
164173064870SJohn McCall }
164273064870SJohn McCall 
164373064870SJohn McCall /// BuildByRefType - This routine changes a __block variable declared as T x
164473064870SJohn McCall ///   into:
164573064870SJohn McCall ///
164673064870SJohn McCall ///      struct {
164773064870SJohn McCall ///        void *__isa;
164873064870SJohn McCall ///        void *__forwarding;
164973064870SJohn McCall ///        int32_t __flags;
165073064870SJohn McCall ///        int32_t __size;
165173064870SJohn McCall ///        void *__copy_helper;       // only if needed
165273064870SJohn McCall ///        void *__destroy_helper;    // only if needed
165373064870SJohn McCall ///        char padding[X];           // only if needed
165473064870SJohn McCall ///        T x;
165573064870SJohn McCall ///      } x
165673064870SJohn McCall ///
16572192fe50SChris Lattner llvm::Type *CodeGenFunction::BuildByRefType(const VarDecl *D) {
16582192fe50SChris Lattner   std::pair<llvm::Type *, unsigned> &Info = ByRefValueInfo[D];
165973064870SJohn McCall   if (Info.first)
166073064870SJohn McCall     return Info.first;
166173064870SJohn McCall 
166273064870SJohn McCall   QualType Ty = D->getType();
166373064870SJohn McCall 
16640e62c1ccSChris Lattner   SmallVector<llvm::Type *, 8> types;
166573064870SJohn McCall 
1666a5f58b05SChris Lattner   llvm::StructType *ByRefType =
1667a5f58b05SChris Lattner     llvm::StructType::createNamed(getLLVMContext(),
1668a5f58b05SChris Lattner                                 "struct.__block_byref_" + D->getNameAsString());
166973064870SJohn McCall 
167073064870SJohn McCall   // void *__isa;
16719dc0db21SJohn McCall   types.push_back(Int8PtrTy);
167273064870SJohn McCall 
167373064870SJohn McCall   // void *__forwarding;
1674a5f58b05SChris Lattner   types.push_back(llvm::PointerType::getUnqual(ByRefType));
167573064870SJohn McCall 
167673064870SJohn McCall   // int32_t __flags;
16779dc0db21SJohn McCall   types.push_back(Int32Ty);
167873064870SJohn McCall 
167973064870SJohn McCall   // int32_t __size;
16809dc0db21SJohn McCall   types.push_back(Int32Ty);
168173064870SJohn McCall 
168273064870SJohn McCall   bool HasCopyAndDispose = getContext().BlockRequiresCopying(Ty);
168373064870SJohn McCall   if (HasCopyAndDispose) {
168473064870SJohn McCall     /// void *__copy_helper;
16859dc0db21SJohn McCall     types.push_back(Int8PtrTy);
168673064870SJohn McCall 
168773064870SJohn McCall     /// void *__destroy_helper;
16889dc0db21SJohn McCall     types.push_back(Int8PtrTy);
168973064870SJohn McCall   }
169073064870SJohn McCall 
169173064870SJohn McCall   bool Packed = false;
169273064870SJohn McCall   CharUnits Align = getContext().getDeclAlign(D);
169373064870SJohn McCall   if (Align > getContext().toCharUnitsFromBits(Target.getPointerAlign(0))) {
169473064870SJohn McCall     // We have to insert padding.
169573064870SJohn McCall 
169673064870SJohn McCall     // The struct above has 2 32-bit integers.
169773064870SJohn McCall     unsigned CurrentOffsetInBytes = 4 * 2;
169873064870SJohn McCall 
169973064870SJohn McCall     // And either 2 or 4 pointers.
170073064870SJohn McCall     CurrentOffsetInBytes += (HasCopyAndDispose ? 4 : 2) *
170173064870SJohn McCall       CGM.getTargetData().getTypeAllocSize(Int8PtrTy);
170273064870SJohn McCall 
170373064870SJohn McCall     // Align the offset.
170473064870SJohn McCall     unsigned AlignedOffsetInBytes =
170573064870SJohn McCall       llvm::RoundUpToAlignment(CurrentOffsetInBytes, Align.getQuantity());
170673064870SJohn McCall 
170773064870SJohn McCall     unsigned NumPaddingBytes = AlignedOffsetInBytes - CurrentOffsetInBytes;
170873064870SJohn McCall     if (NumPaddingBytes > 0) {
1709a5f58b05SChris Lattner       llvm::Type *Ty = llvm::Type::getInt8Ty(getLLVMContext());
171073064870SJohn McCall       // FIXME: We need a sema error for alignment larger than the minimum of
17119dc0db21SJohn McCall       // the maximal stack alignment and the alignment of malloc on the system.
171273064870SJohn McCall       if (NumPaddingBytes > 1)
171373064870SJohn McCall         Ty = llvm::ArrayType::get(Ty, NumPaddingBytes);
171473064870SJohn McCall 
17159dc0db21SJohn McCall       types.push_back(Ty);
171673064870SJohn McCall 
171773064870SJohn McCall       // We want a packed struct.
171873064870SJohn McCall       Packed = true;
171973064870SJohn McCall     }
172073064870SJohn McCall   }
172173064870SJohn McCall 
172273064870SJohn McCall   // T x;
17239dc0db21SJohn McCall   types.push_back(ConvertTypeForMem(Ty));
172473064870SJohn McCall 
1725a5f58b05SChris Lattner   ByRefType->setBody(types, Packed);
172673064870SJohn McCall 
1727a5f58b05SChris Lattner   Info.first = ByRefType;
172873064870SJohn McCall 
17299dc0db21SJohn McCall   Info.second = types.size() - 1;
173073064870SJohn McCall 
173173064870SJohn McCall   return Info.first;
173273064870SJohn McCall }
173373064870SJohn McCall 
173473064870SJohn McCall /// Initialize the structural components of a __block variable, i.e.
173573064870SJohn McCall /// everything but the actual object.
173673064870SJohn McCall void CodeGenFunction::emitByrefStructureInit(const AutoVarEmission &emission) {
1737f9b056b0SJohn McCall   // Find the address of the local.
1738f9b056b0SJohn McCall   llvm::Value *addr = emission.Address;
173973064870SJohn McCall 
1740f9b056b0SJohn McCall   // That's an alloca of the byref structure type.
17412192fe50SChris Lattner   llvm::StructType *byrefType = cast<llvm::StructType>(
1742f9b056b0SJohn McCall                  cast<llvm::PointerType>(addr->getType())->getElementType());
1743f9b056b0SJohn McCall 
1744f9b056b0SJohn McCall   // Build the byref helpers if necessary.  This is null if we don't need any.
1745f9b056b0SJohn McCall   CodeGenModule::ByrefHelpers *helpers =
1746f9b056b0SJohn McCall     buildByrefHelpers(*byrefType, emission);
174773064870SJohn McCall 
174873064870SJohn McCall   const VarDecl &D = *emission.Variable;
174973064870SJohn McCall   QualType type = D.getType();
175073064870SJohn McCall 
1751f9b056b0SJohn McCall   llvm::Value *V;
175273064870SJohn McCall 
175373064870SJohn McCall   // Initialize the 'isa', which is just 0 or 1.
175473064870SJohn McCall   int isa = 0;
1755f9b056b0SJohn McCall   if (type.isObjCGCWeak())
175673064870SJohn McCall     isa = 1;
175773064870SJohn McCall   V = Builder.CreateIntToPtr(Builder.getInt32(isa), Int8PtrTy, "isa");
175873064870SJohn McCall   Builder.CreateStore(V, Builder.CreateStructGEP(addr, 0, "byref.isa"));
175973064870SJohn McCall 
176073064870SJohn McCall   // Store the address of the variable into its own forwarding pointer.
176173064870SJohn McCall   Builder.CreateStore(addr,
176273064870SJohn McCall                       Builder.CreateStructGEP(addr, 1, "byref.forwarding"));
176373064870SJohn McCall 
176473064870SJohn McCall   // Blocks ABI:
176573064870SJohn McCall   //   c) the flags field is set to either 0 if no helper functions are
176673064870SJohn McCall   //      needed or BLOCK_HAS_COPY_DISPOSE if they are,
176773064870SJohn McCall   BlockFlags flags;
1768f9b056b0SJohn McCall   if (helpers) flags |= BLOCK_HAS_COPY_DISPOSE;
176973064870SJohn McCall   Builder.CreateStore(llvm::ConstantInt::get(IntTy, flags.getBitMask()),
177073064870SJohn McCall                       Builder.CreateStructGEP(addr, 2, "byref.flags"));
177173064870SJohn McCall 
1772f9b056b0SJohn McCall   CharUnits byrefSize = CGM.GetTargetTypeStoreSize(byrefType);
1773f9b056b0SJohn McCall   V = llvm::ConstantInt::get(IntTy, byrefSize.getQuantity());
177473064870SJohn McCall   Builder.CreateStore(V, Builder.CreateStructGEP(addr, 3, "byref.size"));
177573064870SJohn McCall 
1776f9b056b0SJohn McCall   if (helpers) {
177773064870SJohn McCall     llvm::Value *copy_helper = Builder.CreateStructGEP(addr, 4);
1778f9b056b0SJohn McCall     Builder.CreateStore(helpers->CopyHelper, copy_helper);
177973064870SJohn McCall 
178073064870SJohn McCall     llvm::Value *destroy_helper = Builder.CreateStructGEP(addr, 5);
1781f9b056b0SJohn McCall     Builder.CreateStore(helpers->DisposeHelper, destroy_helper);
178273064870SJohn McCall   }
178373064870SJohn McCall }
178473064870SJohn McCall 
1785ad7c5c16SJohn McCall void CodeGenFunction::BuildBlockRelease(llvm::Value *V, BlockFieldFlags flags) {
1786900546d2SDaniel Dunbar   llvm::Value *F = CGM.getBlockObjectDispose();
1787fbe25dd1SMike Stump   llvm::Value *N;
1788ad7c5c16SJohn McCall   V = Builder.CreateBitCast(V, Int8PtrTy);
1789ad7c5c16SJohn McCall   N = llvm::ConstantInt::get(Int32Ty, flags.getBitMask());
1790626aecc4SMike Stump   Builder.CreateCall2(F, V, N);
1791626aecc4SMike Stump }
179273064870SJohn McCall 
179373064870SJohn McCall namespace {
179473064870SJohn McCall   struct CallBlockRelease : EHScopeStack::Cleanup {
179573064870SJohn McCall     llvm::Value *Addr;
179673064870SJohn McCall     CallBlockRelease(llvm::Value *Addr) : Addr(Addr) {}
179773064870SJohn McCall 
179830317fdaSJohn McCall     void Emit(CodeGenFunction &CGF, Flags flags) {
179931168b07SJohn McCall       // Should we be passing FIELD_IS_WEAK here?
180073064870SJohn McCall       CGF.BuildBlockRelease(Addr, BLOCK_FIELD_IS_BYREF);
180173064870SJohn McCall     }
180273064870SJohn McCall   };
180373064870SJohn McCall }
180473064870SJohn McCall 
180573064870SJohn McCall /// Enter a cleanup to destroy a __block variable.  Note that this
180673064870SJohn McCall /// cleanup should be a no-op if the variable hasn't left the stack
180773064870SJohn McCall /// yet; if a cleanup is required for the variable itself, that needs
180873064870SJohn McCall /// to be done externally.
180973064870SJohn McCall void CodeGenFunction::enterByrefCleanup(const AutoVarEmission &emission) {
181073064870SJohn McCall   // We don't enter this cleanup if we're in pure-GC mode.
181173064870SJohn McCall   if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
181273064870SJohn McCall     return;
181373064870SJohn McCall 
181473064870SJohn McCall   EHStack.pushCleanup<CallBlockRelease>(NormalAndEHCleanup, emission.Address);
181573064870SJohn McCall }
1816