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