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