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