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