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