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