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