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