Coverage Report

Created: 2019-07-24 05:18

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