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