25 #include "llvm/ADT/SmallSet.h" 26 #include "llvm/IR/CallSite.h" 27 #include "llvm/IR/DataLayout.h" 28 #include "llvm/IR/Module.h" 29 #include "llvm/Support/ScopedPrinter.h" 33 using namespace clang;
34 using namespace CodeGen;
37 : Name(name), CXXThisIndex(0), CanBeGlobal(
false), NeedsCopyDispose(
false),
38 HasCXXObject(
false), UsesStret(
false), HasCapturedVariableLayout(
false),
39 CapturesNonExternalType(
false), LocalAddress(
Address::invalid()),
40 StructureType(nullptr), Block(block), DominatingIP(nullptr) {
44 if (!name.empty() && name[0] ==
'\01')
45 name = name.substr(1);
54 llvm::Constant *blockFn);
83 struct BlockCaptureManagedEntity {
95 : CopyKind(CopyType), DisposeKind(DisposeType), CopyFlags(CopyFlags),
96 DisposeFlags(DisposeFlags), CI(&CI), Capture(&Capture) {}
98 bool operator<(
const BlockCaptureManagedEntity &Other)
const {
99 return Capture->
getOffset() < Other.Capture->getOffset();
116 SmallVectorImpl<BlockCaptureManagedEntity> &ManagedCaptures);
125 std::string
Name =
"__block_descriptor_";
139 for (
const BlockCaptureManagedEntity &E : ManagedCaptures) {
140 Name += llvm::to_string(E.Capture->getOffset().getQuantity());
142 if (E.CopyKind == E.DisposeKind) {
146 "shouldn't see BlockCaptureManagedEntity that is None");
162 std::string TypeAtEncoding =
166 std::replace(TypeAtEncoding.begin(), TypeAtEncoding.end(),
'@',
'\1');
167 Name +=
"e" + llvm::to_string(TypeAtEncoding.size()) +
"_" + TypeAtEncoding;
190 llvm::IntegerType *
ulong =
192 llvm::PointerType *i8p =
nullptr;
195 llvm::Type::getInt8PtrTy(
200 std::string descName;
206 if (llvm::GlobalValue *desc = CGM.
getModule().getNamedValue(descName))
207 return llvm::ConstantExpr::getBitCast(desc,
217 elements.addInt(ulong, 0);
226 bool hasInternalHelper =
false;
230 elements.add(copyHelper);
234 elements.add(disposeHelper);
236 if (cast<llvm::Function>(copyHelper->getOperand(0))->hasInternalLinkage() ||
237 cast<llvm::Function>(disposeHelper->getOperand(0))
238 ->hasInternalLinkage())
239 hasInternalHelper =
true;
243 std::string typeAtEncoding =
245 elements.add(llvm::ConstantExpr::getBitCast(
256 elements.addNullPointer(i8p);
258 unsigned AddrSpace = 0;
262 llvm::GlobalValue::LinkageTypes linkage;
263 if (descName.empty()) {
265 descName =
"__block_descriptor_tmp";
266 }
else if (hasInternalHelper) {
271 linkage = llvm::GlobalValue::LinkOnceODRLinkage;
274 llvm::GlobalVariable *global =
276 true, linkage, AddrSpace);
278 if (linkage == llvm::GlobalValue::LinkOnceODRLinkage) {
280 global->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
299 struct objc_class *isa;
327 _ResultType (*invoke)(Block_literal *, _ParamTypes...);
330 struct Block_descriptor *block_descriptor;
333 _CapturesTypes captures...;
339 struct BlockLayoutChunk {
351 : Alignment(align), Size(size), Lifetime(lifetime),
352 Capture(capture),
Type(type), FieldType(fieldType) {}
368 bool operator<(
const BlockLayoutChunk &left,
const BlockLayoutChunk &right) {
369 if (left.Alignment != right.Alignment)
370 return left.Alignment > right.Alignment;
372 auto getPrefOrder = [](
const BlockLayoutChunk &chunk) {
373 if (chunk.Capture && chunk.Capture->isByRef())
382 return getPrefOrder(left) < getPrefOrder(right);
392 if (!recordType)
return true;
394 const auto *record = cast<CXXRecordDecl>(recordType->
getDecl());
397 if (!record->hasTrivialDestructor())
return false;
398 if (record->hasNonTrivialCopyConstructor())
return false;
402 return !record->hasMutableFields();
416 if (isa<ParmVarDecl>(var))
436 if (!init)
return nullptr;
448 SmallVectorImpl<llvm::Type*> &elementTypes) {
450 assert(elementTypes.empty());
462 assert((2 * CGM.
getIntSize()).isMultipleOf(GenPtrAlign));
463 elementTypes.push_back(CGM.
IntTy);
464 elementTypes.push_back(CGM.
IntTy);
465 elementTypes.push_back(
473 for (
auto I : Helper->getCustomFieldTypes()) {
477 if (BlockAlign < Align)
479 assert(Offset % Align == 0);
481 elementTypes.push_back(I);
495 elementTypes.push_back(CGM.
IntTy);
496 elementTypes.push_back(CGM.
IntTy);
511 return FD->getType();
528 bool hasNonConstantCustomFields =
false;
529 if (
auto *OpenCLHelper =
531 hasNonConstantCustomFields =
532 !OpenCLHelper->areAllCustomFieldValuesConstant(info);
533 if (!block->
hasCaptures() && !hasNonConstantCustomFields) {
553 "Can't capture 'this' outside a method");
559 std::pair<CharUnits,CharUnits> tinfo
561 maxFieldAlign =
std::max(maxFieldAlign, tinfo.second);
563 layout.push_back(BlockLayoutChunk(tinfo.second, tinfo.first,
565 nullptr, llvmType, thisType));
569 for (
const auto &CI : block->
captures()) {
570 const VarDecl *variable = CI.getVariable();
572 if (CI.isEscapingByref()) {
578 maxFieldAlign =
std::max(maxFieldAlign, align);
583 "capture type differs from the variable type");
627 }
else if (CI.hasCopyExpr()) {
642 if (!record->hasTrivialDestructor()) {
645 if (!record->isExternallyVisible())
654 maxFieldAlign =
std::max(maxFieldAlign, align);
660 BlockLayoutChunk(align, size, lifetime, &CI, llvmType, VT));
664 if (layout.empty()) {
674 std::stable_sort(layout.begin(), layout.end());
698 if (endAlign < maxFieldAlign) {
699 SmallVectorImpl<BlockLayoutChunk>::iterator
700 li = layout.begin() + 1, le = layout.end();
704 for (; li != le && endAlign < li->Alignment; ++li)
710 SmallVectorImpl<BlockLayoutChunk>::iterator first = li;
711 for (; li != le; ++li) {
712 assert(endAlign >= li->Alignment);
714 li->setIndex(info, elementTypes.size(), blockSize);
715 elementTypes.push_back(li->Type);
716 blockSize += li->Size;
720 if (endAlign >= maxFieldAlign) {
725 layout.erase(first, li);
729 assert(endAlign ==
getLowBit(blockSize));
733 if (endAlign < maxFieldAlign) {
735 CharUnits padding = newBlockSize - blockSize;
743 elementTypes.push_back(llvm::ArrayType::get(CGM.
Int8Ty,
745 blockSize = newBlockSize;
749 assert(endAlign >= maxFieldAlign);
750 assert(endAlign ==
getLowBit(blockSize));
754 for (SmallVectorImpl<BlockLayoutChunk>::iterator
755 li = layout.begin(), le = layout.end(); li != le; ++li) {
756 if (endAlign < li->Alignment) {
760 CharUnits padding = li->Alignment - endAlign;
761 elementTypes.push_back(llvm::ArrayType::get(CGM.
Int8Ty,
763 blockSize += padding;
766 assert(endAlign >= li->Alignment);
767 li->setIndex(info, elementTypes.size(), blockSize);
768 elementTypes.push_back(li->Type);
769 blockSize += li->Size;
794 if (blockInfo.CanBeGlobal)
return;
798 blockInfo.BlockAlign,
"block");
801 if (!blockInfo.NeedsCopyDispose)
return;
805 for (
const auto &CI : block->
captures()) {
808 if (CI.isByRef())
continue;
811 const VarDecl *variable = CI.getVariable();
832 "expected ObjC ARC to be enabled");
846 if (!blockInfo.DominatingIP)
847 blockInfo.DominatingIP = cast<llvm::Instruction>(addr.
getPointer());
851 if (useArrayEHCleanup)
855 destroyer, useArrayEHCleanup);
866 if (
const auto EWC = dyn_cast<ExprWithCleanups>(E)) {
867 assert(EWC->getNumObjects() != 0);
877 assert(head && *head);
892 assert(head &&
"destroying an empty chain");
897 }
while (head !=
nullptr);
913 return EmitBlockLiteral(blockInfo);
917 std::unique_ptr<CGBlockInfo> blockInfo;
922 return EmitBlockLiteral(*blockInfo);
937 BlockCGF.SanOpts = SanOpts;
938 auto *InvokeFn = BlockCGF.GenerateBlockFunction(
939 CurGD, blockInfo, LocalDeclMap, isLambdaConv, blockInfo.
CanBeGlobal);
940 auto *blockFn = llvm::ConstantExpr::getPointerCast(InvokeFn, GenVoidPtrTy);
949 assert(blockAddr.
isValid() &&
"block has no address!");
952 llvm::Constant *descriptor;
961 isa = llvm::ConstantExpr::getBitCast(blockISA, VoidPtrTy);
982 return Builder.CreateStructGEP(blockAddr, index, offset, name);
987 Builder.CreateStore(value, projectField(index, offset, name));
995 auto addHeaderField =
997 storeField(value, index, offset, name);
1003 addHeaderField(isa, getPointerSize(),
"block.isa");
1004 addHeaderField(llvm::ConstantInt::get(IntTy, flags.
getBitMask()),
1005 getIntSize(),
"block.flags");
1006 addHeaderField(llvm::ConstantInt::get(IntTy, 0), getIntSize(),
1011 getIntSize(),
"block.size");
1014 getIntSize(),
"block.align");
1016 addHeaderField(blockFn, GenVoidPtrSize,
"block.invoke");
1018 addHeaderField(descriptor, getPointerSize(),
"block.descriptor");
1019 else if (
auto *Helper =
1021 for (
auto I : Helper->getCustomFieldValues(*
this, blockInfo)) {
1035 if (blockDecl->capturesCXXThis()) {
1037 "block.captured-this.addr");
1038 Builder.CreateStore(LoadCXXThis(), addr);
1042 for (
const auto &CI : blockDecl->captures()) {
1043 const VarDecl *variable = CI.getVariable();
1060 if (blockDecl->isConversionFromLambda()) {
1064 }
else if (CI.isEscapingByref()) {
1065 if (BlockInfo && CI.isNested()) {
1071 src = Builder.CreateStructGEP(LoadBlockStruct(),
1074 "block.capture.addr");
1076 auto I = LocalDeclMap.find(variable);
1077 assert(I != LocalDeclMap.end());
1081 DeclRefExpr declRef(getContext(), const_cast<VarDecl *>(variable),
1085 src = EmitDeclRefLValue(&declRef).getAddress();
1092 if (CI.isEscapingByref()) {
1096 byrefPointer = Builder.CreateLoad(src,
"byref.capture");
1098 byrefPointer = Builder.CreateBitCast(src.getPointer(), VoidPtrTy);
1101 Builder.CreateStore(byrefPointer, blockField);
1104 }
else if (
const Expr *copyExpr = CI.getCopyExpr()) {
1105 if (blockDecl->isConversionFromLambda()) {
1114 EmitAggExpr(copyExpr, Slot);
1116 EmitSynthesizedCXXCopyCtor(blockField, src, copyExpr);
1121 Builder.CreateStore(src.getPointer(), blockField);
1127 llvm::Value *value = Builder.CreateLoad(src,
"captured");
1128 Builder.CreateStore(value, blockField);
1140 llvm::Value *value = Builder.CreateLoad(src,
"block.captured_block");
1141 value = EmitARCRetainNonBlock(value);
1144 Builder.CreateStore(value, blockField);
1155 DeclRefExpr declRef(getContext(), const_cast<VarDecl *>(variable),
1164 EmitExprAsInit(&l2r, &BlockFieldPseudoVar,
1170 if (!CI.isByRef()) {
1192 if (BlockDescriptorType)
1193 return BlockDescriptorType;
1196 getTypes().ConvertType(getContext().UnsignedLongTy);
1213 "struct.__block_descriptor", UnsignedLongTy, UnsignedLongTy);
1216 unsigned AddrSpace = 0;
1217 if (getLangOpts().OpenCL)
1219 BlockDescriptorType = llvm::PointerType::get(BlockDescriptorType, AddrSpace);
1220 return BlockDescriptorType;
1224 if (GenericBlockLiteralType)
1225 return GenericBlockLiteralType;
1227 llvm::Type *BlockDescPtrTy = getBlockDescriptorType();
1229 if (getLangOpts().OpenCL) {
1237 {IntTy, IntTy, getOpenCLRuntime().getGenericVoidPointerType()});
1238 if (
auto *Helper = getTargetCodeGenInfo().getTargetOpenCLBlockHelper()) {
1239 for (
auto I : Helper->getCustomFieldTypes())
1240 StructFields.push_back(I);
1243 StructFields,
"struct.__opencl_block_literal_generic");
1252 GenericBlockLiteralType =
1254 IntTy, IntTy, VoidPtrTy, BlockDescPtrTy);
1257 return GenericBlockLiteralType;
1272 unsigned AddrSpace = 0;
1273 if (getLangOpts().OpenCL)
1281 Builder.CreatePointerCast(BlockPtr, BlockLiteralTy,
"block.literal");
1291 QualType VoidPtrQualTy = getContext().VoidPtrTy;
1293 if (getLangOpts().OpenCL) {
1296 getContext().getPointerType(getContext().getAddrSpaceQualType(
1300 BlockPtr = Builder.CreatePointerCast(BlockPtr, GenericVoidPtrTy);
1309 llvm::Value *Func = Builder.CreateAlignedLoad(FuncPtr, getPointerAlign());
1318 llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
1319 Func = Builder.CreatePointerCast(Func, BlockFTyPtr);
1325 return EmitCall(FnInfo, Callee, ReturnValue, Args);
1329 assert(BlockInfo &&
"evaluating block ref without block information?");
1333 if (capture.
isConstant())
return LocalDeclMap.find(variable)->second;
1336 Builder.CreateStructGEP(LoadBlockStruct(), capture.
getIndex(),
1337 capture.
getOffset(),
"block.capture.addr");
1343 auto &byrefInfo = getBlockByrefInfo(variable);
1344 addr =
Address(Builder.CreateLoad(addr), byrefInfo.ByrefAlignment);
1346 auto byrefPointerType = llvm::PointerType::get(byrefInfo.Type, 0);
1347 addr = Builder.CreateBitCast(addr, byrefPointerType,
"byref.addr");
1349 addr = emitBlockByrefAddress(addr, byrefInfo,
true,
1355 "the capture field of a non-escaping variable should have a " 1358 addr = EmitLoadOfReference(MakeAddrLValue(addr, capture.
fieldType()));
1364 llvm::Constant *Addr) {
1365 bool Ok = EmittedGlobalBlocks.insert(std::make_pair(BE, Addr)).second;
1367 assert(Ok &&
"Trying to replace an already-existing global block!");
1373 if (llvm::Constant *
Block = getAddrOfGlobalBlockIfEmitted(BE))
1390 return getAddrOfGlobalBlockIfEmitted(BE);
1395 llvm::Constant *blockFn) {
1401 "Refusing to re-emit a global block.");
1405 auto fields = builder.beginStruct();
1424 fields.addInt(CGM.
IntTy, 0);
1431 fields.add(blockFn);
1436 }
else if (
auto *Helper =
1438 for (
auto I : Helper->getCustomFieldValues(CGM, blockInfo)) {
1443 unsigned AddrSpace = 0;
1447 llvm::Constant *literal = fields.finishAndCreateGlobal(
1448 "__block_literal_global", blockInfo.
BlockAlign,
1464 auto *InitVar =
new llvm::GlobalVariable(CGM.
getModule(), Init->getType(),
1466 Init,
".block_isa_init_ptr");
1467 InitVar->setSection(
".CRT$XCLa");
1474 llvm::Constant *Result =
1475 llvm::ConstantExpr::getPointerCast(literal, RequiredType);
1480 cast<llvm::Function>(blockFn->stripPointerCasts()), Result);
1487 assert(BlockInfo &&
"not emitting prologue of block invocation function?!");
1492 Builder.CreateStore(arg, alloc);
1497 DI->EmitDeclareOfBlockLiteralArgVariable(
1498 *BlockInfo, D->
getName(), argNum,
1499 cast<llvm::AllocaInst>(alloc.
getPointer()), Builder);
1508 BlockPointer = Builder.CreatePointerCast(
1511 getContext().getLangOpts().OpenCL
1518 assert(BlockInfo &&
"not in a block invocation function!");
1519 assert(BlockPointer &&
"no block pointer set!");
1527 bool IsLambdaConversionToBlock,
1528 bool BuildGlobalBlock) {
1535 BlockInfo = &blockInfo;
1540 for (DeclMapTy::const_iterator i = ldm.begin(), e = ldm.end(); i != e; ++i) {
1541 const auto *var = dyn_cast<
VarDecl>(i->first);
1542 if (var && !var->hasLocalStorage())
1543 setAddrOfLocalVar(var, i->second);
1553 QualType selfTy = getContext().VoidPtrTy;
1559 if (getLangOpts().OpenCL)
1560 selfTy = getContext().getPointerType(getContext().getAddrSpaceQualType(
1568 args.push_back(&SelfDecl);
1587 if (BuildGlobalBlock) {
1588 auto GenVoidPtrTy = getContext().getLangOpts().OpenCL
1592 llvm::ConstantExpr::getPointerCast(fn, GenVoidPtrTy));
1596 StartFunction(blockDecl, fnType->
getReturnType(), fn, fnInfo, args,
1607 Address Alloca = CreateTempAlloca(BlockPointer->getType(),
1613 Builder.CreateStore(BlockPointer, Alloca);
1621 Builder.CreateStructGEP(LoadBlockStruct(), blockInfo.
CXXThisIndex,
1623 CXXThisValue = Builder.CreateLoad(addr,
"this");
1627 for (
const auto &CI : blockDecl->
captures()) {
1628 const VarDecl *variable = CI.getVariable();
1632 CharUnits align = getContext().getDeclAlign(variable);
1634 CreateMemTemp(variable->
getType(), align,
"block.captured-const");
1636 Builder.CreateStore(capture.
getConstant(), alloca);
1638 setAddrOfLocalVar(variable, alloca);
1642 llvm::BasicBlock *entry = Builder.GetInsertBlock();
1643 llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint();
1646 if (IsLambdaConversionToBlock)
1647 EmitLambdaBlockInvokeBody();
1649 PGO.assignRegionCounters(
GlobalDecl(blockDecl), fn);
1650 incrementProfileCounter(blockDecl->
getBody());
1651 EmitStmt(blockDecl->
getBody());
1655 llvm::BasicBlock *resume = Builder.GetInsertBlock();
1659 Builder.SetInsertPoint(entry, entry_ptr);
1664 for (
const auto &CI : blockDecl->
captures()) {
1665 const VarDecl *variable = CI.getVariable();
1666 DI->EmitLocation(Builder, variable->
getLocation());
1672 auto addr = LocalDeclMap.find(variable)->second;
1673 (void)DI->EmitDeclareOfAutoVariable(variable, addr.getPointer(),
1678 DI->EmitDeclareOfBlockDeclRefVariable(
1679 variable, BlockPointerDbgLoc, Builder, blockInfo,
1680 entry_ptr == entry->end() ? nullptr : &*entry_ptr);
1684 DI->EmitLocation(Builder,
1685 cast<CompoundStmt>(blockDecl->
getBody())->getRBracLoc());
1689 if (resume ==
nullptr)
1690 Builder.ClearInsertionPoint();
1692 Builder.SetInsertPoint(resume);
1694 FinishFunction(cast<CompoundStmt>(blockDecl->
getBody())->getRBracLoc());
1699 static std::pair<BlockCaptureEntityKind, BlockFieldFlags>
1705 return std::make_pair(BlockCaptureEntityKind::CXXRecord,
BlockFieldFlags());
1712 return std::make_pair(BlockCaptureEntityKind::BlockObject, Flags);
1722 return std::make_pair(BlockCaptureEntityKind::NonTrivialCStruct,
1726 return std::make_pair(BlockCaptureEntityKind::ARCWeak, Flags);
1732 return std::make_pair(!isBlockPointer ? BlockCaptureEntityKind::ARCStrong
1733 : BlockCaptureEntityKind::BlockObject,
1747 return std::make_pair(BlockCaptureEntityKind::BlockObject, Flags);
1753 llvm_unreachable(
"after exhaustive PrimitiveCopyKind switch");
1756 static std::pair<BlockCaptureEntityKind, BlockFieldFlags>
1763 SmallVectorImpl<BlockCaptureManagedEntity> &ManagedCaptures) {
1775 ManagedCaptures.emplace_back(CopyInfo.first, DisposeInfo.first,
1776 CopyInfo.second, DisposeInfo.second, CI,
1781 llvm::sort(ManagedCaptures);
1793 : Addr(Addr), FieldFlags(Flags), LoadBlockVarAddr(LoadValue),
1798 if (LoadBlockVarAddr) {
1833 assert((StrKind != CaptureStrKind::Merged ||
1834 (E.CopyKind == E.DisposeKind && E.CopyFlags == E.DisposeFlags)) &&
1835 "different operations and flags");
1837 if (StrKind == CaptureStrKind::DisposeHelper) {
1838 Kind = E.DisposeKind;
1839 Flags = E.DisposeFlags;
1842 Flags = E.CopyFlags;
1846 case BlockCaptureEntityKind::CXXRecord: {
1849 llvm::raw_svector_ostream Out(TyStr);
1851 Str += llvm::to_string(TyStr.size()) + TyStr.c_str();
1854 case BlockCaptureEntityKind::ARCWeak:
1857 case BlockCaptureEntityKind::ARCStrong:
1860 case BlockCaptureEntityKind::BlockObject: {
1870 if (StrKind != CaptureStrKind::DisposeHelper) {
1874 if (StrKind != CaptureStrKind::CopyHelper) {
1888 case BlockCaptureEntityKind::NonTrivialCStruct: {
1894 std::string FuncStr;
1895 if (StrKind == CaptureStrKind::DisposeHelper)
1897 CaptureTy, Alignment, IsVolatile, Ctx);
1902 CaptureTy, Alignment, IsVolatile, Ctx);
1905 Str += llvm::to_string(FuncStr.size()) +
"_" + FuncStr;
1916 const SmallVectorImpl<BlockCaptureManagedEntity> &
Captures,
1918 assert((StrKind == CaptureStrKind::CopyHelper ||
1919 StrKind == CaptureStrKind::DisposeHelper) &&
1920 "unexpected CaptureStrKind");
1921 std::string
Name = StrKind == CaptureStrKind::CopyHelper
1922 ?
"__copy_helper_block_" 1923 :
"__destroy_helper_block_";
1928 Name += llvm::to_string(BlockAlignment.
getQuantity()) +
"_";
1930 for (
const BlockCaptureManagedEntity &E : Captures) {
1931 Name += llvm::to_string(E.Capture->getOffset().getQuantity());
1942 bool EHOnly = ForCopyHelper;
1944 switch (CaptureKind) {
1945 case BlockCaptureEntityKind::CXXRecord:
1946 case BlockCaptureEntityKind::ARCWeak:
1947 case BlockCaptureEntityKind::NonTrivialCStruct:
1948 case BlockCaptureEntityKind::ARCStrong: {
1952 CaptureKind == BlockCaptureEntityKind::ARCStrong
1962 case BlockCaptureEntityKind::BlockObject: {
1984 if (CapturesNonExternalType) {
1988 Fn->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
2005 std::string FuncName =
2007 CaptureStrKind::CopyHelper, CGM);
2009 if (llvm::GlobalValue *Func = CGM.
getModule().getNamedValue(FuncName))
2010 return llvm::ConstantExpr::getBitCast(Func, VoidPtrTy);
2018 args.push_back(&DstDecl);
2020 args.push_back(&SrcDecl);
2029 llvm::Function *Fn =
2042 FunctionTy,
nullptr,
SC_Static,
false,
false);
2046 StartFunction(FD, ReturnTy, Fn, FI, args);
2050 Address src = GetAddrOfLocalVar(&SrcDecl);
2052 src = Builder.CreateBitCast(src, structPtrTy,
"block.source");
2054 Address dst = GetAddrOfLocalVar(&DstDecl);
2056 dst = Builder.CreateBitCast(dst, structPtrTy,
"block.dest");
2058 for (
const auto &CopiedCapture : CopiedCaptures) {
2064 unsigned index = capture.
getIndex();
2065 Address srcField = Builder.CreateStructGEP(src, index, capture.
getOffset());
2066 Address dstField = Builder.CreateStructGEP(dst, index, capture.
getOffset());
2068 switch (CopiedCapture.CopyKind) {
2069 case BlockCaptureEntityKind::CXXRecord:
2071 assert(CI.
getCopyExpr() &&
"copy expression for variable is missing");
2072 EmitSynthesizedCXXCopyCtor(dstField, srcField, CI.
getCopyExpr());
2074 case BlockCaptureEntityKind::ARCWeak:
2075 EmitARCCopyWeak(dstField, srcField);
2077 case BlockCaptureEntityKind::NonTrivialCStruct: {
2081 callCStructCopyConstructor(MakeAddrLValue(dstField, varType),
2082 MakeAddrLValue(srcField, varType));
2085 case BlockCaptureEntityKind::ARCStrong: {
2086 llvm::Value *srcValue = Builder.CreateLoad(srcField,
"blockcopy.src");
2091 auto *ty = cast<llvm::PointerType>(srcValue->getType());
2092 llvm::Value *null = llvm::ConstantPointerNull::get(ty);
2093 Builder.CreateStore(null, dstField);
2094 EmitARCStoreStrongCall(dstField, srcValue,
true);
2100 EmitARCRetainNonBlock(srcValue);
2106 cast<llvm::Instruction>(dstField.
getPointer())->eraseFromParent();
2110 case BlockCaptureEntityKind::BlockObject: {
2111 llvm::Value *srcValue = Builder.CreateLoad(srcField,
"blockcopy.src");
2112 srcValue = Builder.CreateBitCast(srcValue, VoidPtrTy);
2114 Builder.CreateBitCast(dstField.
getPointer(), VoidPtrTy);
2116 dstAddr, srcValue, llvm::ConstantInt::get(Int32Ty, flags.
getBitMask())
2137 return llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy);
2149 static std::pair<BlockCaptureEntityKind, BlockFieldFlags>
2156 return std::make_pair(BlockCaptureEntityKind::BlockObject, Flags);
2161 return std::make_pair(BlockCaptureEntityKind::CXXRecord,
BlockFieldFlags());
2165 return std::make_pair(BlockCaptureEntityKind::ARCStrong,
2169 return std::make_pair(BlockCaptureEntityKind::ARCWeak,
2172 return std::make_pair(BlockCaptureEntityKind::NonTrivialCStruct,
2177 !LangOpts.ObjCAutoRefCount)
2178 return std::make_pair(BlockCaptureEntityKind::BlockObject,
2184 llvm_unreachable(
"after exhaustive DestructionKind switch");
2198 std::string FuncName =
2200 CaptureStrKind::DisposeHelper, CGM);
2202 if (llvm::GlobalValue *Func = CGM.
getModule().getNamedValue(FuncName))
2203 return llvm::ConstantExpr::getBitCast(Func, VoidPtrTy);
2211 args.push_back(&SrcDecl);
2220 llvm::Function *Fn =
2232 FunctionTy,
nullptr,
SC_Static,
false,
false);
2236 StartFunction(FD, ReturnTy, Fn, FI, args);
2237 markAsIgnoreThreadCheckingAtRuntime(Fn);
2243 Address src = GetAddrOfLocalVar(&SrcDecl);
2245 src = Builder.CreateBitCast(src, structPtrTy,
"block");
2249 for (
const auto &DestroyedCapture : DestroyedCaptures) {
2266 return llvm::ConstantExpr::getBitCast(Fn, VoidPtrTy);
2302 void profileImpl(llvm::FoldingSetNodeID &
id)
const override {
2303 id.AddInteger(Flags.getBitMask());
2321 void profileImpl(llvm::FoldingSetNodeID &
id)
const override {
2341 llvm::ConstantPointerNull::get(cast<llvm::PointerType>(value->getType()));
2357 void profileImpl(llvm::FoldingSetNodeID &
id)
const override {
2367 ARCStrongBlockByrefHelpers(
CharUnits alignment)
2384 void profileImpl(llvm::FoldingSetNodeID &
id)
const override {
2394 const Expr *CopyExpr;
2398 const Expr *copyExpr)
2401 bool needsCopy()
const override {
return CopyExpr !=
nullptr; }
2404 if (!CopyExpr)
return;
2414 void profileImpl(llvm::FoldingSetNodeID &
id)
const override {
2415 id.AddPointer(VarType.getCanonicalType().getAsOpaquePtr());
2434 bool needsDispose()
const override {
2435 return VarType.isDestructedType();
2440 CGF.
pushDestroy(VarType.isDestructedType(), field, VarType);
2444 void profileImpl(llvm::FoldingSetNodeID &
id)
const override {
2445 id.AddPointer(VarType.getCanonicalType().getAsOpaquePtr());
2450 static llvm::Constant *
2459 args.push_back(&Dst);
2462 args.push_back(&Src);
2471 llvm::Function *Fn =
2476 = &Context.
Idents.
get(
"__Block_byref_object_copy_");
2510 generator.
emitCopy(CGF, destField, srcField);
2515 return llvm::ConstantExpr::getBitCast(Fn, CGF.
Int8PtrTy);
2527 static llvm::Constant *
2537 args.push_back(&Src);
2546 llvm::Function *Fn =
2548 "__Block_byref_object_dispose_",
2552 = &Context.
Idents.
get(
"__Block_byref_object_dispose_");
2569 auto byrefPtrType = byrefInfo.
Type->getPointerTo(0);
2578 return llvm::ConstantExpr::getBitCast(Fn, CGF.
Int8PtrTy);
2594 llvm::FoldingSetNodeID
id;
2595 generator.Profile(
id);
2600 if (node)
return static_cast<T*
>(node);
2605 T *copy =
new (CGM.
getContext()) T(std::forward<T>(generator));
2614 CodeGenFunction::buildByrefHelpers(llvm::StructType &byrefType,
2615 const AutoVarEmission &emission) {
2616 const VarDecl &var = *emission.Variable;
2618 "only escaping __block variables need byref helpers");
2622 auto &byrefInfo = getBlockByrefInfo(&var);
2629 if (
const CXXRecordDecl *record = type->getAsCXXRecordDecl()) {
2630 const Expr *copyExpr =
2632 if (!copyExpr && record->hasTrivialDestructor())
return nullptr;
2635 CGM, byrefInfo, CXXByrefHelpers(valueAlignment, type, copyExpr));
2643 CGM, byrefInfo, NonTrivialCStructByrefHelpers(valueAlignment, type));
2647 if (!type->isObjCRetainableType())
return nullptr;
2665 ARCWeakByrefHelpers(valueAlignment));
2671 if (type->isBlockPointerType()) {
2673 ARCStrongBlockByrefHelpers(valueAlignment));
2679 ARCStrongByrefHelpers(valueAlignment));
2682 llvm_unreachable(
"fell out of lifetime switch!");
2686 if (type->isBlockPointerType()) {
2689 type->isObjCObjectPointerType()) {
2695 if (type.isObjCGCWeak())
2699 ObjectByrefHelpers(valueAlignment, flags));
2704 bool followForward) {
2705 auto &info = getBlockByrefInfo(var);
2706 return emitBlockByrefAddress(baseAddr, info, followForward, var->
getName());
2712 const llvm::Twine &name) {
2714 if (followForward) {
2716 Builder.CreateStructGEP(baseAddr, 1, getPointerSize(),
"forwarding");
2720 return Builder.CreateStructGEP(baseAddr, info.
FieldIndex,
2740 auto it = BlockByrefInfos.find(D);
2741 if (it != BlockByrefInfos.end())
2744 llvm::StructType *byrefType =
2754 types.push_back(Int8PtrTy);
2755 size += getPointerSize();
2758 types.push_back(llvm::PointerType::getUnqual(byrefType));
2759 size += getPointerSize();
2762 types.push_back(Int32Ty);
2766 types.push_back(Int32Ty);
2770 bool hasCopyAndDispose = getContext().BlockRequiresCopying(Ty, D);
2771 if (hasCopyAndDispose) {
2773 types.push_back(Int8PtrTy);
2774 size += getPointerSize();
2777 types.push_back(Int8PtrTy);
2778 size += getPointerSize();
2781 bool HasByrefExtendedLayout =
false;
2783 if (getContext().getByrefLifetime(Ty, Lifetime, HasByrefExtendedLayout) &&
2784 HasByrefExtendedLayout) {
2786 types.push_back(Int8PtrTy);
2793 bool packed =
false;
2794 CharUnits varAlign = getContext().getDeclAlign(D);
2798 if (varOffset != size) {
2800 llvm::ArrayType::get(Int8Ty, (varOffset - size).getQuantity());
2802 types.push_back(paddingTy);
2810 types.push_back(varTy);
2812 byrefType->setBody(types, packed);
2815 info.
Type = byrefType;
2820 auto pair = BlockByrefInfos.insert({D, info});
2821 assert(pair.second &&
"info was inserted recursively?");
2822 return pair.first->second;
2832 llvm::StructType *byrefType = cast<llvm::StructType>(
2833 cast<llvm::PointerType>(addr.
getPointer()->getType())->getElementType());
2835 unsigned nextHeaderIndex = 0;
2838 const Twine &name) {
2839 auto fieldAddr = Builder.CreateStructGEP(addr, nextHeaderIndex,
2840 nextHeaderOffset, name);
2841 Builder.CreateStore(value, fieldAddr);
2844 nextHeaderOffset += fieldSize;
2850 const VarDecl &D = *emission.Variable;
2853 bool HasByrefExtendedLayout;
2855 bool ByRefHasLifetime =
2856 getContext().getByrefLifetime(type, ByrefLifetime, HasByrefExtendedLayout);
2864 V = Builder.CreateIntToPtr(Builder.getInt32(isa), Int8PtrTy,
"isa");
2865 storeHeaderField(V, getPointerSize(),
"byref.isa");
2868 storeHeaderField(addr.
getPointer(), getPointerSize(),
"byref.forwarding");
2875 if (ByRefHasLifetime) {
2877 else switch (ByrefLifetime) {
2895 printf(
"\n Inline flag for BYREF variable layout (%d):", flags.getBitMask());
2897 printf(
" BLOCK_BYREF_HAS_COPY_DISPOSE");
2901 printf(
" BLOCK_BYREF_LAYOUT_EXTENDED");
2903 printf(
" BLOCK_BYREF_LAYOUT_STRONG");
2905 printf(
" BLOCK_BYREF_LAYOUT_WEAK");
2907 printf(
" BLOCK_BYREF_LAYOUT_UNRETAINED");
2909 printf(
" BLOCK_BYREF_LAYOUT_NON_OBJECT");
2914 storeHeaderField(llvm::ConstantInt::get(IntTy, flags.getBitMask()),
2915 getIntSize(),
"byref.flags");
2918 V = llvm::ConstantInt::get(IntTy, byrefSize.getQuantity());
2919 storeHeaderField(V, getIntSize(),
"byref.size");
2922 storeHeaderField(helpers->
CopyHelper, getPointerSize(),
2923 "byref.copyHelper");
2925 "byref.disposeHelper");
2928 if (ByRefHasLifetime && HasByrefExtendedLayout) {
2930 storeHeaderField(layoutInfo, getPointerSize(),
"byref.layout");
2938 Builder.CreateBitCast(V, Int8PtrTy),
2939 llvm::ConstantInt::get(Int32Ty, flags.
getBitMask())
2943 EmitRuntimeCallOrInvoke(F, args);
2945 EmitNounwindRuntimeCall(F, args);
2950 bool LoadBlockVarAddr,
bool CanThrow) {
2951 EHStack.pushCleanup<CallBlockRelease>(
Kind, Addr, Flags, LoadBlockVarAddr,
2957 llvm::Constant *C) {
2958 auto *GV = cast<llvm::GlobalValue>(C->stripPointerCasts());
2965 assert((isa<llvm::Function>(C->stripPointerCasts()) ||
2966 isa<llvm::GlobalVariable>(C->stripPointerCasts())) &&
2967 "expected Function or GlobalVariable");
2970 for (
const auto &Result : DC->
lookup(&II))
2971 if ((ND = dyn_cast<FunctionDecl>(Result)) ||
2972 (ND = dyn_cast<
VarDecl>(Result)))
2976 if (GV->isDeclaration() && (!ND || !ND->hasAttr<DLLExportAttr>())) {
2977 GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
2980 GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
2985 if (CGM.
getLangOpts().BlocksRuntimeOptional && GV->isDeclaration() &&
2986 GV->hasExternalLinkage())
2987 GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
2993 if (BlockObjectDispose)
2994 return BlockObjectDispose;
2997 llvm::FunctionType *fty
2998 = llvm::FunctionType::get(VoidTy, args,
false);
2999 BlockObjectDispose = CreateRuntimeFunction(fty,
"_Block_object_dispose");
3001 return BlockObjectDispose;
3005 if (BlockObjectAssign)
3006 return BlockObjectAssign;
3008 llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, Int32Ty };
3009 llvm::FunctionType *fty
3010 = llvm::FunctionType::get(VoidTy, args,
false);
3011 BlockObjectAssign = CreateRuntimeFunction(fty,
"_Block_object_assign");
3013 return BlockObjectAssign;
3017 if (NSConcreteGlobalBlock)
3018 return NSConcreteGlobalBlock;
3020 NSConcreteGlobalBlock = GetOrCreateLLVMGlobal(
"_NSConcreteGlobalBlock",
3021 Int8PtrTy->getPointerTo(),
3024 return NSConcreteGlobalBlock;
3028 if (NSConcreteStackBlock)
3029 return NSConcreteStackBlock;
3031 NSConcreteStackBlock = GetOrCreateLLVMGlobal(
"_NSConcreteStackBlock",
3032 Int8PtrTy->getPointerTo(),
3035 return NSConcreteStackBlock;
const CGFunctionInfo & arrangeBuiltinFunctionDeclaration(QualType resultType, const FunctionArgList &args)
A builtin function is a freestanding function using the default C conventions.
llvm::PointerType * Int8PtrPtrTy
const llvm::DataLayout & getDataLayout() const
CGOpenCLRuntime & getOpenCLRuntime()
Return a reference to the configured OpenCL runtime.
ReturnValueSlot - Contains the address where the return value of a function can be stored...
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const BlockDecl * getBlockDecl() const
Information about the layout of a __block variable.
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 ...
const Capture & getCapture(const VarDecl *var) const
llvm::Constant * GenerateCopyHelperFunction(const CGBlockInfo &blockInfo)
Generate the copy-helper function for a block closure object: static void block_copy_helper(block_t *...
static llvm::Constant * generateByrefDisposeHelper(CodeGenFunction &CGF, const BlockByrefInfo &byrefInfo, BlockByrefHelpers &generator)
Generate code for a __block variable's dispose helper.
Represents a function declaration or definition.
llvm::IntegerType * IntTy
int
llvm::Type * getGenericBlockLiteralType()
The type of a generic block literal.
CharUnits getIntAlign() const
const CGFunctionInfo & arrangeBlockFunctionDeclaration(const FunctionProtoType *type, const FunctionArgList &args)
Block invocation functions are C functions with an implicit parameter.
External linkage, which indicates that the entity can be referred to from other translation units...
Other implicit parameter.
CharUnits BlockHeaderForcedGapOffset
const FunctionProtoType * getFunctionType() const
getFunctionType - Return the underlying function type for this block.
Expr * getCopyExpr() const
static llvm::Constant * buildByrefDisposeHelper(CodeGenModule &CGM, const BlockByrefInfo &byrefInfo, BlockByrefHelpers &generator)
Build the dispose helper for a __block variable.
void SetLLVMFunctionAttributes(GlobalDecl GD, const CGFunctionInfo &Info, llvm::Function *F)
Set the LLVM function attributes (sext, zext, etc).
A class which contains all the information about a particular captured value.
Destroyer * getDestroyer(QualType::DestructionKind destructionKind)
A (possibly-)qualified type.
bool isBlockPointerType() const
CodeGenTypes & getTypes()
const CodeGenOptions & getCodeGenOpts() const
bool hasCaptures() const
True if this block (or its nested blocks) captures anything of local storage from its enclosing scope...
bool HaveInsertPoint() const
HaveInsertPoint - True if an insertion point is defined.
capture_const_iterator capture_begin() const
llvm::LLVMContext & getLLVMContext()
void EmitARCDestroyWeak(Address addr)
void @objc_destroyWeak(i8** addr) Essentially objc_storeWeak(addr, nil).
llvm::Constant * CopyHelper
The standard implementation of ConstantInitBuilder used in Clang.
BlockVarCopyInit getBlockVarCopyInit(const VarDecl *VD) const
Get the copy initialization expression of the VarDecl VD, or nullptr if none exists.
FunctionType - C99 6.7.5.3 - Function Declarators.
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
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.
QualType getLValueReferenceType(QualType T, bool SpelledAsLValue=true) const
Return the uniqued reference to the type for an lvalue reference to the specified type...
CharUnits getPointerSize() const
param_iterator param_end()
static llvm::Constant * buildBlockDescriptor(CodeGenModule &CGM, const CGBlockInfo &blockInfo)
buildBlockDescriptor - Build the block descriptor meta-data for a block.
QualType getNonReferenceType() const
If Type is a reference type (e.g., const int&), returns the type that the reference refers to ("const...
stable_iterator stable_begin() const
Create a stable reference to the top of the EH stack.
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
capture_const_iterator capture_end() const
The type would be trivial except that it is volatile-qualified.
Address GetAddrOfLocalVar(const VarDecl *VD)
GetAddrOfLocalVar - Return the address of a local variable.
Represents a variable declaration or definition.
Objects with "hidden" visibility are not seen by the dynamic linker.
CGBlockInfo(const BlockDecl *blockDecl, StringRef Name)
const T * getAs() const
Member-template getAs<specific type>'.
EHScopeStack::stable_iterator getCleanup() const
static void setBlockHelperAttributesVisibility(bool CapturesNonExternalType, llvm::Function *Fn, const CGFunctionInfo &FI, CodeGenModule &CGM)
LangAS
Defines the address space values used by the address space qualifier of QualType. ...
This class gathers all debug information during compilation and is responsible for emitting to llvm g...
static std::pair< BlockCaptureEntityKind, BlockFieldFlags > computeCopyInfoForBlockCapture(const BlockDecl::Capture &CI, QualType T, const LangOptions &LangOpts)
virtual llvm::Constant * BuildRCBlockLayout(CodeGen::CodeGenModule &CGM, const CodeGen::CGBlockInfo &blockInfo)=0
llvm::Value * getPointer() const
llvm::Type * ConvertTypeForMem(QualType T)
ConvertTypeForMem - Convert type T into a llvm::Type.
const internal::VariadicDynCastAllOfMatcher< Stmt, BlockExpr > blockExpr
Matches a reference to a block.
static void pushCaptureCleanup(BlockCaptureEntityKind CaptureKind, Address Field, QualType CaptureType, BlockFieldFlags Flags, bool ForCopyHelper, VarDecl *Var, CodeGenFunction &CGF)
static void destroyBlockInfos(CGBlockInfo *info)
Destroy a chain of block layouts.
The collection of all-type qualifiers we support.
void add(RValue rvalue, QualType type)
const AstTypeMatcher< RecordType > recordType
Matches record types (e.g.
llvm::DenseMap< const VarDecl *, FieldDecl * > LambdaCaptureFields
uint64_t getPointerWidth(unsigned AddrSpace) const
Return the width of pointers on this target, for the specified address space.
static std::string getBlockCaptureStr(const BlockCaptureManagedEntity &E, CaptureStrKind StrKind, CharUnits BlockAlignment, CodeGenModule &CGM)
One of these records is kept for each identifier that is lexed.
bool doesNotEscape() const
void emitByrefStructureInit(const AutoVarEmission &emission)
Initialize the structural components of a __block variable, i.e.
CGBlockInfo * FirstBlockInfo
FirstBlockInfo - The head of a singly-linked-list of block layouts.
QualType getPointeeType() const
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
llvm::Constant * tryEmitAbstractForInitializer(const VarDecl &D)
Try to emit the initializer of the given declaration as an abstract constant.
bool isObjCInertUnsafeUnretainedType() const
Was this type written with the special inert-in-ARC __unsafe_unretained qualifier?
bool HasCapturedVariableLayout
HasCapturedVariableLayout : True if block has captured variables and their layout meta-data has been ...
FullExpr - Represents a "full-expression" node.
SourceLocation getBeginLoc() const LLVM_READONLY
bool isReferenceType() const
Denotes a cleanup that should run when a scope is exited using exceptional control flow (a throw stat...
std::string getObjCEncodingForBlock(const BlockExpr *blockExpr) const
Return the encoded type for this block declaration.
void recordBlockInfo(const BlockExpr *E, llvm::Function *InvokeF, llvm::Value *Block)
Record invoke function and block literal emitted during normal codegen for a block expression...
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.
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
static llvm::Constant * generateByrefCopyHelper(CodeGenFunction &CGF, const BlockByrefInfo &byrefInfo, BlockByrefHelpers &generator)
Stmt * getBody() const override
getBody - If this Decl represents a declaration for a body of code, such as a function or method defi...
static bool isSafeForCXXConstantCapture(QualType type)
Determines if the given type is safe for constant capture in C++.
CleanupKind getCleanupKind(QualType::DestructionKind kind)
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
An r-value expression (a pr-value in the C++11 taxonomy) produces a temporary value.
CharUnits GetTargetTypeStoreSize(llvm::Type *Ty) const
Return the store size, in character units, of the given LLVM type.
llvm::Value * EmitARCStoreStrongCall(Address addr, llvm::Value *value, bool resultIgnored)
Store into a strong object.
Expr * getCopyExpr() const
virtual std::string getRCBlockLayoutStr(CodeGen::CodeGenModule &CGM, const CGBlockInfo &blockInfo)
CharUnits - This is an opaque type for sizes expressed in character units.
void EmitARCDestroyStrong(Address addr, ARCPreciseLifetime_t precise)
Destroy a __strong variable.
const BlockDecl * getBlockDecl() const
void setDSOLocal(llvm::GlobalValue *GV) const
virtual TargetOpenCLBlockHelper * getTargetOpenCLBlockHelper() const
bool HasCXXObject
HasCXXObject - True if the block's custom copy/dispose functions need to be run even in GC mode...
llvm::Value * EmitBlockLiteral(const BlockExpr *)
Emit block literal.
llvm::PointerType * VoidPtrTy
uint32_t getBitMask() const
bool isByRef() const
Whether this is a "by ref" capture, i.e.
bool isVolatileQualified() const
Determine whether this type is volatile-qualified.
bool needsEHCleanup(QualType::DestructionKind kind)
Determines whether an EH cleanup is required to destroy a type with the given destruction kind...
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...
The type is an Objective-C retainable pointer type that is qualified with the ARC __strong qualifier...
CharUnits getDeclAlign(const Decl *D, bool ForAlignof=false) const
Return a conservative estimate of the alignment of the specified decl D.
Scope - A scope is a transient data structure that is used while parsing the program.
llvm::PointerType * VoidPtrPtrTy
void ForceCleanup(std::initializer_list< llvm::Value **> ValuesToReload={})
Force the emission of cleanups now, instead of waiting until this object is destroyed.
static Capture makeConstant(llvm::Value *value)
CharUnits getPointerAlign() const
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...
void callCStructMoveConstructor(LValue Dst, LValue Src)
const Stmt * getBody() const
llvm::Constant * getNSConcreteStackBlock()
static std::string getNonTrivialCopyConstructorStr(QualType QT, CharUnits Alignment, bool IsVolatile, ASTContext &Ctx)
The type does not fall into any of the following categories.
static FunctionDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, SourceLocation NLoc, DeclarationName N, QualType T, TypeSourceInfo *TInfo, StorageClass SC, bool isInlineSpecified=false, bool hasWrittenPrototype=true, bool isConstexprSpecified=false)
This object can be modified without requiring retains or releases.
StringRef Name
Name - The name of the block, kindof.
bool isEscapingByref() const
bool NeedsCopyDispose
True if the block has captures that would necessitate custom copy or dispose helper functions if the ...
lookup_result lookup(DeclarationName Name) const
lookup - Find the declarations (if any) with the given Name in this context.
const BlockExpr * BlockExpression
unsigned getIndex() const
static llvm::Constant * buildCopyHelper(CodeGenModule &CGM, const CGBlockInfo &blockInfo)
Build the helper function to copy a block.
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
ASTContext & getContext() const
Represents a prototype with parameter type info, e.g.
llvm::CallInst * EmitNounwindRuntimeCall(llvm::Value *callee, const Twine &name="")
uint32_t getBitMask() const
const CodeGen::CGBlockInfo * BlockInfo
const TargetCodeGenInfo & getTargetCodeGenInfo()
CGBlockInfo - Information to generate a block literal.
virtual void emitCopy(CodeGenFunction &CGF, Address dest, Address src)=0
RValue - This trivial value class is used to represent the result of an expression that is evaluated...
QualType fieldType() const
bool CanBeGlobal
CanBeGlobal - True if the block can be global, i.e.
StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD)
static void findBlockCapturedManagedEntities(const CGBlockInfo &BlockInfo, const LangOptions &LangOpts, SmallVectorImpl< BlockCaptureManagedEntity > &ManagedCaptures)
Find the set of block captures that need to be explicitly copied or destroy.
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
static void initializeForBlockHeader(CodeGenModule &CGM, CGBlockInfo &info, SmallVectorImpl< llvm::Type *> &elementTypes)
CGBlockInfo * NextBlockInfo
The next block in the block-info chain.
CGObjCRuntime & getObjCRuntime()
Return a reference to the configured Objective-C runtime.
Pepresents a block literal declaration, which is like an unnamed FunctionDecl.
This represents one expression.
void EmitARCMoveWeak(Address dst, Address src)
void @objc_moveWeak(i8** dest, i8** src) Disregards the current value in dest.
Emit only debug info necessary for generating line number tables (-gline-tables-only).
Enters a new scope for capturing cleanups, all of which will be executed once the scope is exited...
CharUnits getOffset() const
llvm::Constant * DisposeHelper
const T * castAs() const
Member-template castAs<specific type>.
bool isObjCRetainableType() const
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Represents a C++ destructor within a class.
BlockCaptureEntityKind
Represents a type of copy/destroy operation that should be performed for an entity that's captured by...
static llvm::Constant * buildGlobalBlock(CodeGenModule &CGM, const CGBlockInfo &blockInfo, llvm::Constant *blockFn)
Build the given block as a global block.
llvm::Constant * GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo)
Generate the destroy-helper function for a block closure object: static void block_destroy_helper(blo...
ObjCLifetime getObjCLifetime() const
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
internal::Matcher< T > id(StringRef ID, const internal::BindableMatcher< T > &InnerMatcher)
If the provided matcher matches a node, binds the node to ID.
bool needsCopyDisposeHelpers() const
bool ReturnSlotInterferesWithArgs(const CGFunctionInfo &FI)
Return true iff the given type uses an argument slot when 'sret' is used as a return type...
llvm::IntegerType * Int32Ty
static Capture makeIndex(unsigned index, CharUnits offset, QualType FieldType)
bool isa(CodeGen::Address addr)
QualType getFunctionType(QualType ResultTy, ArrayRef< QualType > Args, const FunctionProtoType::ExtProtoInfo &EPI) const
Return a normal function type with a typed argument list.
static bool CanThrow(Expr *E, ASTContext &Ctx)
static CharUnits getLowBit(CharUnits v)
Get the low bit of a nonzero character count.
const Type * getBaseElementTypeUnsafe() const
Get the base element type of this type, potentially discarding type qualifiers.
const TargetInfo & getTarget() const
const LangOptions & getLangOpts() const
ASTContext & getContext() const
static BlockFieldFlags getBlockFieldFlagsForObjCObjectPointer(const BlockDecl::Capture &CI, QualType T)
Address GetAddrOfBlockDecl(const VarDecl *var)
static QualType getCaptureFieldType(const CodeGenFunction &CGF, const BlockDecl::Capture &CI)
GlobalDecl - represents a global declaration.
static std::string getBlockDescriptorName(const CGBlockInfo &BlockInfo, CodeGenModule &CGM)
virtual bool needsCopy() const
virtual llvm::Constant * BuildByrefLayout(CodeGen::CodeGenModule &CGM, QualType T)=0
Returns an i8* which points to the byref layout information.
bool isConstQualified() const
Determine whether this type is const-qualified.
param_iterator param_begin()
The l-value was considered opaque, so the alignment was determined from a type.
RecordDecl * getDecl() const
void setBlockContextParameter(const ImplicitParamDecl *D, unsigned argNum, llvm::Value *ptr)
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.
bool UsesStret
UsesStret : True if the block uses an stret return.
There is no lifetime qualification on this type.
Address CreateBitCast(Address Addr, llvm::Type *Ty, const llvm::Twine &Name="")
Assigning into this object requires the old value to be released and the new value to be retained...
void PushDestructorCleanup(QualType T, Address Addr)
PushDestructorCleanup - Push a cleanup to call the complete-object destructor of an object of the giv...
void pushDestroy(QualType::DestructionKind dtorKind, Address addr, QualType type)
pushDestroy - Push the standard destructor for the given type as at least a normal cleanup...
Encodes a location in the source.
static DeclContext * castToDeclContext(const TranslationUnitDecl *D)
void BuildBlockRelease(llvm::Value *DeclPtr, BlockFieldFlags flags, bool CanThrow)
QualType getReturnType() const
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
SourceLocation getBeginLoc() const LLVM_READONLY
A saved depth on the scope stack.
llvm::StructType * StructureType
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...
static std::string getCopyDestroyHelperFuncName(const SmallVectorImpl< BlockCaptureManagedEntity > &Captures, CharUnits BlockAlignment, CaptureStrKind StrKind, CodeGenModule &CGM)
static void configureBlocksRuntimeObject(CodeGenModule &CGM, llvm::Constant *C)
Adjust the declaration of something from the blocks API.
The type is a struct containing a field whose type is neither PCK_Trivial nor PCK_VolatileTrivial.
const BlockByrefInfo & getBlockByrefInfo(const VarDecl *var)
BuildByrefInfo - This routine changes a __block variable declared as T x into:
A scoped helper to set the current debug location to the specified location or preferred location of ...
bool CapturesNonExternalType
Indicates whether an object of a non-external C++ class is captured.
static bool cxxDestructorCanThrow(QualType T)
Check if T is a C++ class that has a destructor that can throw.
bool isConversionFromLambda() const
void enterNonTrivialFullExpression(const FullExpr *E)
Enter a full-expression with a non-trivial number of objects to clean up.
llvm::DenseMap< const VarDecl *, Capture > Captures
The mapping of allocated indexes within the block.
bool isNonEscapingByref() const
Indicates the capture is a __block variable that is never captured by an escaping block...
bool isObjCObjectPointerType() const
static bool isBlockPointer(Expr *Arg)
llvm::Constant * getBlockObjectDispose()
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.
void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F)
Set the LLVM function attributes which only apply to a function definition.
ImplicitCastExpr - Allows us to explicitly represent implicit type conversions, which have no direct ...
DestructionKind isDestructedType() const
Returns a nonzero value if objects of this type require non-trivial work to clean up after...
llvm::Value * getConstant() const
static Destroyer emitARCIntrinsicUse
const BlockExpr * getBlockExpr() const
All available information about a concrete callee.
MangleContext & getMangleContext()
Gets the mangle context.
void enterByrefCleanup(CleanupKind Kind, Address Addr, BlockFieldFlags Flags, bool LoadBlockVarAddr, bool CanThrow)
Enter a cleanup to destroy a __block variable.
virtual bool needsDispose() const
bool operator<(DeclarationName LHS, DeclarationName RHS)
Ordering on two declaration names.
VarDecl * getVariable() const
The variable being captured.
PrimitiveCopyKind isNonTrivialToPrimitiveCopy() const
Check if this is a non-trivial type that would cause a C struct transitively containing this type to ...
unsigned CXXThisIndex
The field index of 'this' within the block, if there is one.
Assigning into this object requires a lifetime extension.
static Destroyer destroyARCStrongImprecise
void FinishFunction(SourceLocation EndLoc=SourceLocation())
FinishFunction - Complete IR generation of the current function.
virtual void mangleTypeName(QualType T, raw_ostream &)=0
Generates a unique string for an externally visible type for use with TBAA or type uniquing...
The type is an Objective-C retainable pointer type that is qualified with the ARC __weak qualifier...
const CGFunctionInfo & arrangeBlockFunctionCall(const CallArgList &args, const FunctionType *type)
A block function is essentially a free function with an extra implicit argument.
std::pair< CharUnits, CharUnits > getTypeInfoInChars(const Type *T) const
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...
void addUsedGlobal(llvm::GlobalValue *GV)
Add a global to a list to be added to the llvm.used metadata.
FunctionArgList - Type for representing both the decl and type of parameters to a function...
bool isObjCGCWeak() const
true when Type is objc's weak.
static llvm::Constant * tryCaptureAsConstant(CodeGenModule &CGM, CodeGenFunction *CGF, const VarDecl *var)
It is illegal to modify a const object after initialization.
CGFunctionInfo - Class to encapsulate the information about a function definition.
This class organizes the cross-function state that is used while generating LLVM code.
CharUnits alignmentAtOffset(CharUnits offset) const
Given that this is a non-zero alignment value, what is the alignment at the given offset...
Dataflow Directional Tag Classes.
virtual llvm::Constant * BuildGCBlockLayout(CodeGen::CodeGenModule &CGM, const CodeGen::CGBlockInfo &blockInfo)=0
Address LoadBlockStruct()
static void computeBlockInfo(CodeGenModule &CGM, CodeGenFunction *CGF, CGBlockInfo &info)
Compute the layout of the given block.
llvm::FoldingSet< BlockByrefHelpers > ByrefHelpersCache
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
ArrayRef< Capture > captures() const
bool isNested() const
Whether this is a nested capture, i.e.
Address CreateStructGEP(Address Addr, unsigned Index, CharUnits Offset, const llvm::Twine &Name="")
Parameter for Objective-C 'self' argument.
static llvm::Constant * buildDisposeHelper(CodeGenModule &CGM, const CGBlockInfo &blockInfo)
Build the helper function to dispose of a block.
const Expr * getInit() const
llvm::Constant * getPointer() const
std::unique_ptr< DiagnosticConsumer > create(StringRef OutputFile, DiagnosticOptions *Diags, bool MergeChildRecords=false)
Returns a DiagnosticConsumer that serializes diagnostics to a bitcode file.
llvm::LoadInst * CreateLoad(Address Addr, const llvm::Twine &Name="")
int printf(__constant const char *st,...)
bool hasObjCLifetime() const
llvm::StoreInst * CreateStore(llvm::Value *Val, Address Addr, bool IsVolatile=false)
bool isEscapingByref() const
Indicates the capture is a __block variable that is captured by a block that can potentially escape (...
llvm::Module & getModule() const
LValue MakeAddrLValue(Address Addr, QualType T, AlignmentSource Source=AlignmentSource::Type)
llvm::DenseMap< const Decl *, Address > DeclMapTy
static void enterBlockScope(CodeGenFunction &CGF, BlockDecl *block)
Enter the scope of a block.
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
StructBuilder beginStruct(llvm::StructType *structTy=nullptr)
CanQualType UnsignedLongTy
static llvm::Constant * buildByrefCopyHelper(CodeGenModule &CGM, const BlockByrefInfo &byrefInfo, BlockByrefHelpers &generator)
Build the copy helper for a __block variable.
llvm::PointerType * Int8PtrTy
void EmitSynthesizedCXXCopyCtor(Address Dest, Address Src, const Expr *Exp)
static CGBlockInfo * findAndRemoveBlockInfo(CGBlockInfo **head, const BlockDecl *block)
Find the layout for the given block in a linked list and remove it.
void SetInternalFunctionAttributes(GlobalDecl GD, llvm::Function *F, const CGFunctionInfo &FI)
Set the attributes on the LLVM function for the given decl and function info.
virtual void emitDispose(CodeGenFunction &CGF, Address field)=0
Internal linkage, which indicates that the entity can be referred to from within the translation unit...
llvm::Constant * getBlockObjectAssign()
const Decl * CurFuncDecl
CurFuncDecl - Holds the Decl for the current outermost non-closure context.
A pair of helper functions for a __block variable.
bool capturesCXXThis() const
CharUnits getIntSize() const
Reading or writing from this object requires a barrier call.
TranslationUnitDecl * getTranslationUnitDecl() const
Represents a C++ struct/union/class.
static std::string getNonTrivialDestructorStr(QualType QT, CharUnits Alignment, bool IsVolatile, ASTContext &Ctx)
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
void setAddrOfGlobalBlock(const BlockExpr *BE, llvm::Constant *Addr)
Notes that BE's global block is available via Addr.
virtual ~BlockByrefHelpers()
CharUnits BlockHeaderForcedGapSize
SourceLocation getEndLoc() const LLVM_READONLY
llvm::Value * EmitARCRetainBlock(llvm::Value *value, bool mandatory)
Retain the given block, with _Block_copy semantics.
uint64_t getPointerAlign(unsigned AddrSpace) const
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
CGCXXABI & getCXXABI() const
__DEVICE__ int max(int __a, int __b)
llvm::Function * GenerateBlockFunction(GlobalDecl GD, const CGBlockInfo &Info, const DeclMapTy &ldm, bool IsLambdaConversionToBlock, bool BuildGlobalBlock)
The top declaration context.
A reference to a declared variable, function, enum, etc.
static RValue get(llvm::Value *V)
RValue EmitBlockCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue)
const internal::VariadicDynCastAllOfMatcher< Decl, BlockDecl > blockDecl
Matches block declarations.
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
static ApplyDebugLocation CreateEmpty(CodeGenFunction &CGF)
Set the IRBuilder to not attach debug locations.
llvm::Constant * getNSConcreteGlobalBlock()
llvm::Type * getBlockDescriptorType()
Fetches the type of a generic block descriptor.
llvm::Constant * GetAddrOfGlobalBlock(const BlockExpr *BE, StringRef Name)
Gets the address of a block which requires no captures.
An l-value expression is a reference to an object with independent storage.
llvm::PointerType * getGenericVoidPointerType()
Information for lazily generating a cleanup.
This represents a decl that may have a name.
llvm::Instruction * DominatingIP
An instruction which dominates the full-expression that the block is inside.
unsigned long ulong
An unsigned 64-bit integer.
const LangOptions & getLangOpts() const
unsigned getTargetAddressSpace(QualType T) const
static bool isObjCNSObjectType(QualType Ty)
Return true if this is an NSObject object with its NSObject attribute set.
CallArgList - Type for representing both the value and type of arguments in a call.
const LangOptions & getLangOpts() const
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...
Abstract information about a function or function prototype.
SourceLocation getLocation() const
static std::pair< BlockCaptureEntityKind, BlockFieldFlags > computeDestroyInfoForBlockCapture(const BlockDecl::Capture &CI, QualType T, const LangOptions &LangOpts)
bool isExternallyVisible() const
void setCleanup(EHScopeStack::stable_iterator cleanup)
static OMPLinearClause * Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc, OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc, SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef< Expr *> VL, ArrayRef< Expr *> PL, ArrayRef< Expr *> IL, Expr *Step, Expr *CalcStep, Stmt *PreInit, Expr *PostUpdate)
Creates clause with a list of variables VL and a linear step Step.
void Destroyer(CodeGenFunction &CGF, Address addr, QualType ty)
Qualifiers::ObjCLifetime getObjCLifetime() const
Returns lifetime attribute of this type.
llvm::FunctionType * GetFunctionType(const CGFunctionInfo &Info)
GetFunctionType - Get the LLVM function type for.