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