From ebe69fe11e48d322045d5949c83283927a0d790b Mon Sep 17 00:00:00 2001 From: Stephen Hines Date: Mon, 23 Mar 2015 12:10:34 -0700 Subject: Update aosp/master LLVM for rebase to r230699. Change-Id: I2b5be30509658cb8266be782de0ab24f9099f9b9 --- lib/IR/Android.mk | 4 +- lib/IR/AsmWriter.cpp | 835 +++++++++++++++++++++++++++++---- lib/IR/AsmWriter.h | 4 +- lib/IR/AttributeImpl.h | 12 +- lib/IR/Attributes.cpp | 7 + lib/IR/AutoUpgrade.cpp | 272 ++++++++++- lib/IR/BasicBlock.cpp | 24 +- lib/IR/CMakeLists.txt | 7 +- lib/IR/ConstantFold.cpp | 148 ++++-- lib/IR/Constants.cpp | 146 ++++-- lib/IR/ConstantsContext.h | 18 +- lib/IR/Core.cpp | 156 +++++-- lib/IR/DIBuilder.cpp | 800 +++++++++++++++++-------------- lib/IR/DataLayout.cpp | 62 ++- lib/IR/DebugInfo.cpp | 284 ++++++----- lib/IR/DebugInfoMetadata.cpp | 418 +++++++++++++++++ lib/IR/DebugLoc.cpp | 296 ++---------- lib/IR/DiagnosticInfo.cpp | 3 +- lib/IR/Dominators.cpp | 38 +- lib/IR/Function.cpp | 124 ++++- lib/IR/GCOV.cpp | 332 +++++++------ lib/IR/Globals.cpp | 14 - lib/IR/IRBuilder.cpp | 119 ++++- lib/IR/IRPrintingPasses.cpp | 12 +- lib/IR/InlineAsm.cpp | 11 +- lib/IR/Instruction.cpp | 11 - lib/IR/Instructions.cpp | 48 +- lib/IR/IntrinsicInst.cpp | 22 +- lib/IR/LLVMContext.cpp | 22 +- lib/IR/LLVMContextImpl.cpp | 111 ++++- lib/IR/LLVMContextImpl.h | 864 ++++++++++++++++++++++++++++------ lib/IR/LeakDetector.cpp | 69 --- lib/IR/LeaksContext.h | 98 ---- lib/IR/LegacyPassManager.cpp | 55 +-- lib/IR/MDBuilder.cpp | 47 +- lib/IR/Metadata.cpp | 1061 ++++++++++++++++++++++++++++-------------- lib/IR/MetadataImpl.h | 46 ++ lib/IR/MetadataTracking.cpp | 55 +++ lib/IR/Module.cpp | 80 ++-- lib/IR/Pass.cpp | 4 +- lib/IR/PassManager.cpp | 165 +------ lib/IR/Statepoint.cpp | 77 +++ lib/IR/Type.cpp | 21 +- lib/IR/TypeFinder.cpp | 30 +- lib/IR/Value.cpp | 129 ++--- lib/IR/ValueSymbolTable.cpp | 10 +- lib/IR/Verifier.cpp | 658 +++++++++++++++++++++----- 47 files changed, 5493 insertions(+), 2336 deletions(-) create mode 100644 lib/IR/DebugInfoMetadata.cpp delete mode 100644 lib/IR/LeakDetector.cpp delete mode 100644 lib/IR/LeaksContext.h create mode 100644 lib/IR/MetadataImpl.h create mode 100644 lib/IR/MetadataTracking.cpp create mode 100644 lib/IR/Statepoint.cpp (limited to 'lib/IR') diff --git a/lib/IR/Android.mk b/lib/IR/Android.mk index a3632cf..2ca02f7 100644 --- a/lib/IR/Android.mk +++ b/lib/IR/Android.mk @@ -12,6 +12,7 @@ vmcore_SRC_FILES := \ Core.cpp \ DataLayout.cpp \ DebugInfo.cpp \ + DebugInfoMetadata.cpp \ DebugLoc.cpp \ DiagnosticInfo.cpp \ DiagnosticPrinter.cpp \ @@ -29,15 +30,16 @@ vmcore_SRC_FILES := \ IntrinsicInst.cpp \ LLVMContext.cpp \ LLVMContextImpl.cpp \ - LeakDetector.cpp \ LegacyPassManager.cpp \ Mangler.cpp \ MDBuilder.cpp \ Metadata.cpp \ + MetadataTracking.cpp \ Module.cpp \ Pass.cpp \ PassManager.cpp \ PassRegistry.cpp \ + Statepoint.cpp \ Type.cpp \ TypeFinder.cpp \ Use.cpp \ diff --git a/lib/IR/AsmWriter.cpp b/lib/IR/AsmWriter.cpp index 1961a20..de0e614 100644 --- a/lib/IR/AsmWriter.cpp +++ b/lib/IR/AsmWriter.cpp @@ -101,6 +101,11 @@ static OrderMap orderModule(const Module *M) { if (F.hasPrefixData()) if (!isa(F.getPrefixData())) orderValue(F.getPrefixData(), OM); + + if (F.hasPrologueData()) + if (!isa(F.getPrologueData())) + orderValue(F.getPrologueData(), OM); + orderValue(&F, OM); if (F.isDeclaration()) @@ -282,6 +287,7 @@ static void PrintCallingConv(unsigned cc, raw_ostream &Out) { case CallingConv::AnyReg: Out << "anyregcc"; break; case CallingConv::PreserveMost: Out << "preserve_mostcc"; break; case CallingConv::PreserveAll: Out << "preserve_allcc"; break; + case CallingConv::GHC: Out << "ghccc"; break; case CallingConv::X86_StdCall: Out << "x86_stdcallcc"; break; case CallingConv::X86_FastCall: Out << "x86_fastcallcc"; break; case CallingConv::X86_ThisCall: Out << "x86_thiscallcc"; break; @@ -600,8 +606,8 @@ private: /// Add all of the functions arguments, basic blocks, and instructions. void processFunction(); - SlotTracker(const SlotTracker &) LLVM_DELETED_FUNCTION; - void operator=(const SlotTracker &) LLVM_DELETED_FUNCTION; + SlotTracker(const SlotTracker &) = delete; + void operator=(const SlotTracker &) = delete; }; SlotTracker *createSlotTracker(const Module *M) { @@ -628,13 +634,6 @@ static SlotTracker *createSlotTracker(const Value *V) { if (const Function *Func = dyn_cast(V)) return new SlotTracker(Func); - if (const MDNode *MD = dyn_cast(V)) { - if (!MD->isFunctionLocal()) - return new SlotTracker(MD->getFunction()); - - return new SlotTracker((Function *)nullptr); - } - return nullptr; } @@ -647,16 +646,14 @@ static SlotTracker *createSlotTracker(const Value *V) { // Module level constructor. Causes the contents of the Module (sans functions) // to be added to the slot table. SlotTracker::SlotTracker(const Module *M) - : TheModule(M), TheFunction(nullptr), FunctionProcessed(false), - mNext(0), fNext(0), mdnNext(0), asNext(0) { -} + : TheModule(M), TheFunction(nullptr), FunctionProcessed(false), mNext(0), + fNext(0), mdnNext(0), asNext(0) {} // Function level constructor. Causes the contents of the Module and the one // function provided to be added to the slot table. SlotTracker::SlotTracker(const Function *F) - : TheModule(F ? F->getParent() : nullptr), TheFunction(F), - FunctionProcessed(false), mNext(0), fNext(0), mdnNext(0), asNext(0) { -} + : TheModule(F ? F->getParent() : nullptr), TheFunction(F), + FunctionProcessed(false), mNext(0), fNext(0), mdnNext(0), asNext(0) {} inline void SlotTracker::initialize() { if (TheModule) { @@ -738,8 +735,9 @@ void SlotTracker::processFunction() { if (Function *F = CI->getCalledFunction()) if (F->isIntrinsic()) for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) - if (MDNode *N = dyn_cast_or_null(I->getOperand(i))) - CreateMetadataSlot(N); + if (auto *V = dyn_cast_or_null(I->getOperand(i))) + if (MDNode *N = dyn_cast(V->getMetadata())) + CreateMetadataSlot(N); // Add all the call attributes to the table. AttributeSet Attrs = CI->getAttributes().getFnAttributes(); @@ -850,16 +848,10 @@ void SlotTracker::CreateFunctionSlot(const Value *V) { void SlotTracker::CreateMetadataSlot(const MDNode *N) { assert(N && "Can't insert a null Value into SlotTracker!"); - // Don't insert if N is a function-local metadata, these are always printed - // inline. - if (!N->isFunctionLocal()) { - mdn_iterator I = mdnMap.find(N); - if (I != mdnMap.end()) - return; - - unsigned DestSlot = mdnNext++; - mdnMap[N] = DestSlot; - } + unsigned DestSlot = mdnNext; + if (!mdnMap.insert(std::make_pair(N, DestSlot)).second) + return; + ++mdnNext; // Recursively add any MDNodes referenced by operands. for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) @@ -888,6 +880,11 @@ static void WriteAsOperandInternal(raw_ostream &Out, const Value *V, SlotTracker *Machine, const Module *Context); +static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD, + TypePrinting *TypePrinter, + SlotTracker *Machine, const Module *Context, + bool FromValue = false); + static const char *getPredicateText(unsigned predicate) { const char * pred = "unknown"; switch (predicate) { @@ -1252,20 +1249,21 @@ static void WriteConstantInternal(raw_ostream &Out, const Constant *CV, Out << ""; } -static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node, - TypePrinting *TypePrinter, - SlotTracker *Machine, - const Module *Context) { +static void writeMDTuple(raw_ostream &Out, const MDTuple *Node, + TypePrinting *TypePrinter, SlotTracker *Machine, + const Module *Context) { Out << "!{"; for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) { - const Value *V = Node->getOperand(mi); - if (!V) + const Metadata *MD = Node->getOperand(mi); + if (!MD) Out << "null"; - else { + else if (auto *MDV = dyn_cast(MD)) { + Value *V = MDV->getValue(); TypePrinter->print(V->getType(), Out); Out << ' '; - WriteAsOperandInternal(Out, Node->getOperand(mi), - TypePrinter, Machine, Context); + WriteAsOperandInternal(Out, V, TypePrinter, Machine, Context); + } else { + WriteAsOperandInternal(Out, MD, TypePrinter, Machine, Context); } if (mi + 1 != me) Out << ", "; @@ -1274,6 +1272,618 @@ static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node, Out << "}"; } +namespace { +struct FieldSeparator { + bool Skip; + const char *Sep; + FieldSeparator(const char *Sep = ", ") : Skip(true), Sep(Sep) {} +}; +raw_ostream &operator<<(raw_ostream &OS, FieldSeparator &FS) { + if (FS.Skip) { + FS.Skip = false; + return OS; + } + return OS << FS.Sep; +} +} // end namespace + +static void writeMetadataAsOperand(raw_ostream &Out, const Metadata *MD, + TypePrinting *TypePrinter, + SlotTracker *Machine, + const Module *Context) { + if (!MD) { + Out << "null"; + return; + } + WriteAsOperandInternal(Out, MD, TypePrinter, Machine, Context); +} + +static void writeTag(raw_ostream &Out, FieldSeparator &FS, const DebugNode *N) { + Out << FS << "tag: "; + if (const char *Tag = dwarf::TagString(N->getTag())) + Out << Tag; + else + Out << N->getTag(); +} + +static void writeGenericDebugNode(raw_ostream &Out, const GenericDebugNode *N, + TypePrinting *TypePrinter, + SlotTracker *Machine, const Module *Context) { + Out << "!GenericDebugNode("; + FieldSeparator FS; + writeTag(Out, FS, N); + if (!N->getHeader().empty()) { + Out << FS << "header: \""; + PrintEscapedString(N->getHeader(), Out); + Out << "\""; + } + if (N->getNumDwarfOperands()) { + Out << FS << "operands: {"; + FieldSeparator IFS; + for (auto &I : N->dwarf_operands()) { + Out << IFS; + writeMetadataAsOperand(Out, I, TypePrinter, Machine, Context); + } + Out << "}"; + } + Out << ")"; +} + +static void writeMDLocation(raw_ostream &Out, const MDLocation *DL, + TypePrinting *TypePrinter, SlotTracker *Machine, + const Module *Context) { + Out << "!MDLocation("; + FieldSeparator FS; + // Always output the line, since 0 is a relevant and important value for it. + Out << FS << "line: " << DL->getLine(); + if (DL->getColumn()) + Out << FS << "column: " << DL->getColumn(); + Out << FS << "scope: "; + WriteAsOperandInternal(Out, DL->getScope(), TypePrinter, Machine, Context); + if (DL->getInlinedAt()) { + Out << FS << "inlinedAt: "; + WriteAsOperandInternal(Out, DL->getInlinedAt(), TypePrinter, Machine, + Context); + } + Out << ")"; +} + +static void writeMDSubrange(raw_ostream &Out, const MDSubrange *N, + TypePrinting *, SlotTracker *, const Module *) { + Out << "!MDSubrange("; + FieldSeparator FS; + Out << FS << "count: " << N->getCount(); + if (N->getLo()) + Out << FS << "lowerBound: " << N->getLo(); + Out << ")"; +} + +static void writeMDEnumerator(raw_ostream &Out, const MDEnumerator *N, + TypePrinting *, SlotTracker *, const Module *) { + Out << "!MDEnumerator("; + FieldSeparator FS; + Out << FS << "name: \"" << N->getName() << "\""; + Out << FS << "value: " << N->getValue(); + Out << ")"; +} + +static void writeMDBasicType(raw_ostream &Out, const MDBasicType *N, + TypePrinting *, SlotTracker *, const Module *) { + Out << "!MDBasicType("; + FieldSeparator FS; + writeTag(Out, FS, N); + if (!N->getName().empty()) + Out << FS << "name: \"" << N->getName() << "\""; + if (N->getSizeInBits()) + Out << FS << "size: " << N->getSizeInBits(); + if (N->getAlignInBits()) + Out << FS << "align: " << N->getAlignInBits(); + if (unsigned Encoding = N->getEncoding()) { + Out << FS << "encoding: "; + if (const char *S = dwarf::AttributeEncodingString(Encoding)) + Out << S; + else + Out << Encoding; + } + Out << ")"; +} + +static void writeDIFlags(raw_ostream &Out, unsigned Flags) { + SmallVector SplitFlags; + unsigned Extra = DIDescriptor::splitFlags(Flags, SplitFlags); + + FieldSeparator FS(" | "); + for (unsigned F : SplitFlags) { + const char *StringF = DIDescriptor::getFlagString(F); + assert(StringF && "Expected valid flag"); + Out << FS << StringF; + } + if (Extra || SplitFlags.empty()) + Out << FS << Extra; +} + +static void writeMDDerivedType(raw_ostream &Out, const MDDerivedType *N, + TypePrinting *TypePrinter, SlotTracker *Machine, + const Module *Context) { + Out << "!MDDerivedType("; + FieldSeparator FS; + writeTag(Out, FS, N); + if (!N->getName().empty()) + Out << FS << "name: \"" << N->getName() << "\""; + if (N->getFile()) { + Out << FS << "file: "; + writeMetadataAsOperand(Out, N->getFile(), TypePrinter, Machine, + Context); + } + if (N->getLine()) + Out << FS << "line: " << N->getLine(); + if (N->getScope()) { + Out << FS << "scope: "; + writeMetadataAsOperand(Out, N->getScope(), TypePrinter, Machine, Context); + } + Out << FS << "baseType: "; + writeMetadataAsOperand(Out, N->getBaseType(), TypePrinter, Machine, Context); + if (N->getSizeInBits()) + Out << FS << "size: " << N->getSizeInBits(); + if (N->getAlignInBits()) + Out << FS << "align: " << N->getAlignInBits(); + if (N->getOffsetInBits()) + Out << FS << "offset: " << N->getOffsetInBits(); + if (auto Flags = N->getFlags()) { + Out << FS << "flags: "; + writeDIFlags(Out, Flags); + } + if (N->getExtraData()) { + Out << FS << "extraData: "; + writeMetadataAsOperand(Out, N->getExtraData(), TypePrinter, Machine, + Context); + } + Out << ")"; +} + +static void writeMDCompositeType(raw_ostream &Out, const MDCompositeType *N, + TypePrinting *TypePrinter, + SlotTracker *Machine, const Module *Context) { + Out << "!MDCompositeType("; + FieldSeparator FS; + writeTag(Out, FS, N); + if (!N->getName().empty()) + Out << FS << "name: \"" << N->getName() << "\""; + if (N->getFile()) { + Out << FS << "file: "; + writeMetadataAsOperand(Out, N->getFile(), TypePrinter, Machine, + Context); + } + if (N->getLine()) + Out << FS << "line: " << N->getLine(); + if (N->getScope()) { + Out << FS << "scope: "; + writeMetadataAsOperand(Out, N->getScope(), TypePrinter, Machine, Context); + } + if (N->getBaseType()) { + Out << FS << "baseType: "; + writeMetadataAsOperand(Out, N->getBaseType(), TypePrinter, Machine, + Context); + } + if (N->getSizeInBits()) + Out << FS << "size: " << N->getSizeInBits(); + if (N->getAlignInBits()) + Out << FS << "align: " << N->getAlignInBits(); + if (N->getOffsetInBits()) + Out << FS << "offset: " << N->getOffsetInBits(); + if (auto Flags = N->getFlags()) { + Out << FS << "flags: "; + writeDIFlags(Out, Flags); + } + if (N->getElements()) { + Out << FS << "elements: "; + writeMetadataAsOperand(Out, N->getElements(), TypePrinter, Machine, + Context); + } + if (unsigned Lang = N->getRuntimeLang()) { + Out << FS << "runtimeLang: "; + if (const char *S = dwarf::LanguageString(Lang)) + Out << S; + else + Out << Lang; + } + + if (N->getVTableHolder()) { + Out << FS << "vtableHolder: "; + writeMetadataAsOperand(Out, N->getVTableHolder(), TypePrinter, Machine, + Context); + } + if (N->getTemplateParams()) { + Out << FS << "templateParams: "; + writeMetadataAsOperand(Out, N->getTemplateParams(), TypePrinter, Machine, + Context); + } + if (!N->getIdentifier().empty()) + Out << FS << "identifier: \"" << N->getIdentifier() << "\""; + Out << ")"; +} + +static void writeMDSubroutineType(raw_ostream &Out, const MDSubroutineType *N, + TypePrinting *TypePrinter, + SlotTracker *Machine, const Module *Context) { + Out << "!MDSubroutineType("; + FieldSeparator FS; + if (auto Flags = N->getFlags()) { + Out << FS << "flags: "; + writeDIFlags(Out, Flags); + } + Out << FS << "types: "; + writeMetadataAsOperand(Out, N->getTypeArray(), TypePrinter, Machine, Context); + Out << ")"; +} + +static void writeMDFile(raw_ostream &Out, const MDFile *N, TypePrinting *, + SlotTracker *, const Module *) { + Out << "!MDFile("; + FieldSeparator FS; + Out << FS << "filename: \"" << N->getFilename() << "\""; + Out << FS << "directory: \"" << N->getDirectory() << "\""; + Out << ")"; +} + +static void writeMDCompileUnit(raw_ostream &Out, const MDCompileUnit *N, + TypePrinting *TypePrinter, SlotTracker *Machine, + const Module *Context) { + Out << "!MDCompileUnit("; + FieldSeparator FS; + Out << FS << "language: "; + if (const char *Lang = dwarf::LanguageString(N->getSourceLanguage())) + Out << Lang; + else + Out << N->getSourceLanguage(); + Out << FS << "file: "; + writeMetadataAsOperand(Out, N->getFile(), TypePrinter, Machine, Context); + if (!N->getProducer().empty()) + Out << FS << "producer: \"" << N->getProducer() << "\""; + Out << FS << "isOptimized: " << (N->isOptimized() ? "true" : "false"); + if (!N->getFlags().empty()) + Out << FS << "flags: \"" << N->getFlags() << "\""; + Out << FS << "runtimeVersion: " << N->getRuntimeVersion(); + if (!N->getSplitDebugFilename().empty()) + Out << FS << "splitDebugFilename: \"" << N->getSplitDebugFilename() << "\""; + Out << FS << "emissionKind: " << N->getEmissionKind(); + if (N->getEnumTypes()) { + Out << FS << "enums: "; + writeMetadataAsOperand(Out, N->getEnumTypes(), TypePrinter, Machine, + Context); + } + if (N->getRetainedTypes()) { + Out << FS << "retainedTypes: "; + writeMetadataAsOperand(Out, N->getRetainedTypes(), TypePrinter, Machine, + Context); + } + if (N->getSubprograms()) { + Out << FS << "subprograms: "; + writeMetadataAsOperand(Out, N->getSubprograms(), TypePrinter, Machine, + Context); + } + if (N->getGlobalVariables()) { + Out << FS << "globals: "; + writeMetadataAsOperand(Out, N->getGlobalVariables(), TypePrinter, Machine, + Context); + } + if (N->getImportedEntities()) { + Out << FS << "imports: "; + writeMetadataAsOperand(Out, N->getImportedEntities(), TypePrinter, Machine, + Context); + } + Out << ")"; +} + +static void writeMDSubprogram(raw_ostream &Out, const MDSubprogram *N, + TypePrinting *TypePrinter, SlotTracker *Machine, + const Module *Context) { + Out << "!MDSubprogram("; + FieldSeparator FS; + Out << FS << "scope: "; + writeMetadataAsOperand(Out, N->getScope(), TypePrinter, Machine, Context); + Out << FS << "name: \"" << N->getName() << "\""; + if (!N->getLinkageName().empty()) + Out << FS << "linkageName: \"" << N->getLinkageName() << "\""; + if (N->getFile()) { + Out << FS << "file: "; + writeMetadataAsOperand(Out, N->getFile(), TypePrinter, Machine, + Context); + } + if (N->getLine()) + Out << FS << "line: " << N->getLine(); + if (N->getType()) { + Out << FS << "type: "; + writeMetadataAsOperand(Out, N->getType(), TypePrinter, Machine, + Context); + } + Out << FS << "isLocal: " << (N->isLocalToUnit() ? "true" : "false"); + Out << FS << "isDefinition: " << (N->isDefinition() ? "true" : "false"); + if (N->getScopeLine()) + Out << FS << "scopeLine: " << N->getScopeLine(); + if (N->getContainingType()) { + Out << FS << "containingType: "; + writeMetadataAsOperand(Out, N->getContainingType(), TypePrinter, Machine, + Context); + } + if (unsigned V = N->getVirtuality()) { + Out << FS << "virtuality: "; + if (const char *S = dwarf::VirtualityString(V)) + Out << S; + else + Out << V; + } + if (N->getVirtualIndex()) + Out << FS << "virtualIndex: " << N->getVirtualIndex(); + if (auto Flags = N->getFlags()) { + Out << FS << "flags: "; + writeDIFlags(Out, Flags); + } + Out << FS << "isOptimized: " << (N->isOptimized() ? "true" : "false"); + if (N->getFunction()) { + Out << FS << "function: "; + writeMetadataAsOperand(Out, N->getFunction(), TypePrinter, Machine, + Context); + } + if (N->getTemplateParams()) { + Out << FS << "templateParams: "; + writeMetadataAsOperand(Out, N->getTemplateParams(), TypePrinter, Machine, + Context); + } + if (N->getDeclaration()) { + Out << FS << "declaration: "; + writeMetadataAsOperand(Out, N->getDeclaration(), TypePrinter, Machine, + Context); + } + if (N->getVariables()) { + Out << FS << "variables: "; + writeMetadataAsOperand(Out, N->getVariables(), TypePrinter, Machine, + Context); + } + Out << ")"; +} + +static void writeMDLexicalBlock(raw_ostream &Out, const MDLexicalBlock *N, + TypePrinting *TypePrinter, SlotTracker *Machine, + const Module *Context) { + Out << "!MDLexicalBlock("; + FieldSeparator FS; + Out << FS << "scope: "; + writeMetadataAsOperand(Out, N->getScope(), TypePrinter, Machine, Context); + if (N->getFile()) { + Out << FS << "file: "; + writeMetadataAsOperand(Out, N->getFile(), TypePrinter, Machine, + Context); + } + if (N->getLine()) + Out << FS << "line: " << N->getLine(); + if (N->getColumn()) + Out << FS << "column: " << N->getColumn(); + Out << ")"; +} + +static void writeMDLexicalBlockFile(raw_ostream &Out, + const MDLexicalBlockFile *N, + TypePrinting *TypePrinter, + SlotTracker *Machine, + const Module *Context) { + Out << "!MDLexicalBlockFile("; + FieldSeparator FS; + Out << FS << "scope: "; + writeMetadataAsOperand(Out, N->getScope(), TypePrinter, Machine, Context); + if (N->getFile()) { + Out << FS << "file: "; + writeMetadataAsOperand(Out, N->getFile(), TypePrinter, Machine, + Context); + } + Out << FS << "discriminator: " << N->getDiscriminator(); + Out << ")"; +} + +static void writeMDNamespace(raw_ostream &Out, const MDNamespace *N, + TypePrinting *TypePrinter, SlotTracker *Machine, + const Module *Context) { + Out << "!MDNamespace("; + FieldSeparator FS; + Out << FS << "scope: "; + writeMetadataAsOperand(Out, N->getScope(), TypePrinter, Machine, Context); + if (N->getFile()) { + Out << FS << "file: "; + writeMetadataAsOperand(Out, N->getFile(), TypePrinter, Machine, Context); + } + if (!N->getName().empty()) + Out << FS << "name: \"" << N->getName() << "\""; + if (N->getLine()) + Out << FS << "line: " << N->getLine(); + Out << ")"; +} + +static void writeMDTemplateTypeParameter(raw_ostream &Out, + const MDTemplateTypeParameter *N, + TypePrinting *TypePrinter, + SlotTracker *Machine, + const Module *Context) { + Out << "!MDTemplateTypeParameter("; + FieldSeparator FS; + Out << FS << "name: \"" << N->getName() << "\""; + Out << FS << "type: "; + writeMetadataAsOperand(Out, N->getType(), TypePrinter, Machine, Context); + Out << ")"; +} + +static void writeMDTemplateValueParameter(raw_ostream &Out, + const MDTemplateValueParameter *N, + TypePrinting *TypePrinter, + SlotTracker *Machine, + const Module *Context) { + Out << "!MDTemplateValueParameter("; + FieldSeparator FS; + writeTag(Out, FS, N); + Out << FS << "name: \"" << N->getName() << "\""; + Out << FS << "type: "; + writeMetadataAsOperand(Out, N->getType(), TypePrinter, Machine, Context); + Out << FS << "value: "; + writeMetadataAsOperand(Out, N->getValue(), TypePrinter, Machine, Context); + Out << ")"; +} + +static void writeMDGlobalVariable(raw_ostream &Out, const MDGlobalVariable *N, + TypePrinting *TypePrinter, + SlotTracker *Machine, const Module *Context) { + Out << "!MDGlobalVariable("; + FieldSeparator FS; + Out << FS << "scope: "; + writeMetadataAsOperand(Out, N->getScope(), TypePrinter, Machine, Context); + Out << FS << "name: \"" << N->getName() << "\""; + if (!N->getLinkageName().empty()) + Out << FS << "linkageName: \"" << N->getLinkageName() << "\""; + if (N->getFile()) { + Out << FS << "file: "; + writeMetadataAsOperand(Out, N->getFile(), TypePrinter, Machine, + Context); + } + if (N->getLine()) + Out << FS << "line: " << N->getLine(); + if (N->getType()) { + Out << FS << "type: "; + writeMetadataAsOperand(Out, N->getType(), TypePrinter, Machine, + Context); + } + Out << FS << "isLocal: " << (N->isLocalToUnit() ? "true" : "false"); + Out << FS << "isDefinition: " << (N->isDefinition() ? "true" : "false"); + if (N->getVariable()) { + Out << FS << "variable: "; + writeMetadataAsOperand(Out, N->getVariable(), TypePrinter, Machine, + Context); + } + if (N->getStaticDataMemberDeclaration()) { + Out << FS << "declaration: "; + writeMetadataAsOperand(Out, N->getStaticDataMemberDeclaration(), + TypePrinter, Machine, Context); + } + Out << ")"; +} + +static void writeMDLocalVariable(raw_ostream &Out, const MDLocalVariable *N, + TypePrinting *TypePrinter, + SlotTracker *Machine, const Module *Context) { + Out << "!MDLocalVariable("; + FieldSeparator FS; + writeTag(Out, FS, N); + Out << FS << "scope: "; + writeMetadataAsOperand(Out, N->getScope(), TypePrinter, Machine, Context); + Out << FS << "name: \"" << N->getName() << "\""; + if (N->getFile()) { + Out << FS << "file: "; + writeMetadataAsOperand(Out, N->getFile(), TypePrinter, Machine, + Context); + } + if (N->getLine()) + Out << FS << "line: " << N->getLine(); + if (N->getType()) { + Out << FS << "type: "; + writeMetadataAsOperand(Out, N->getType(), TypePrinter, Machine, + Context); + } + if (N->getTag() == dwarf::DW_TAG_arg_variable || N->getArg()) + Out << FS << "arg: " << N->getArg(); + if (auto Flags = N->getFlags()) { + Out << FS << "flags: "; + writeDIFlags(Out, Flags); + } + if (N->getInlinedAt()) { + Out << FS << "inlinedAt: "; + writeMetadataAsOperand(Out, N->getInlinedAt(), TypePrinter, Machine, + Context); + } + Out << ")"; +} + +static void writeMDExpression(raw_ostream &Out, const MDExpression *N, + TypePrinting *TypePrinter, SlotTracker *Machine, + const Module *Context) { + Out << "!MDExpression("; + FieldSeparator FS; + if (N->isValid()) { + for (auto I = N->expr_op_begin(), E = N->expr_op_end(); I != E; ++I) { + const char *OpStr = dwarf::OperationEncodingString(I->getOp()); + assert(OpStr && "Expected valid opcode"); + + Out << FS << OpStr; + for (unsigned A = 0, AE = I->getNumArgs(); A != AE; ++A) + Out << FS << I->getArg(A); + } + } else { + for (const auto &I : N->getElements()) + Out << FS << I; + } + Out << ")"; +} + +static void writeMDObjCProperty(raw_ostream &Out, const MDObjCProperty *N, + TypePrinting *TypePrinter, SlotTracker *Machine, + const Module *Context) { + Out << "!MDObjCProperty("; + FieldSeparator FS; + Out << FS << "name: \"" << N->getName() << "\""; + if (N->getFile()) { + Out << FS << "file: "; + writeMetadataAsOperand(Out, N->getFile(), TypePrinter, Machine, Context); + } + if (N->getLine()) + Out << FS << "line: " << N->getLine(); + if (!N->getSetterName().empty()) + Out << FS << "setter: \"" << N->getSetterName() << "\""; + if (!N->getGetterName().empty()) + Out << FS << "getter: \"" << N->getGetterName() << "\""; + if (N->getAttributes()) + Out << FS << "attributes: " << N->getAttributes(); + if (N->getType()) { + Out << FS << "type: "; + writeMetadataAsOperand(Out, N->getType(), TypePrinter, Machine, Context); + } + Out << ")"; +} + +static void writeMDImportedEntity(raw_ostream &Out, const MDImportedEntity *N, + TypePrinting *TypePrinter, + SlotTracker *Machine, const Module *Context) { + Out << "!MDImportedEntity("; + FieldSeparator FS; + writeTag(Out, FS, N); + Out << FS << "scope: "; + writeMetadataAsOperand(Out, N->getScope(), TypePrinter, Machine, Context); + if (N->getEntity()) { + Out << FS << "entity: "; + writeMetadataAsOperand(Out, N->getEntity(), TypePrinter, Machine, Context); + } + if (N->getLine()) + Out << FS << "line: " << N->getLine(); + Out << FS << "name: \"" << N->getName() << "\""; + Out << ")"; +} + + +static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node, + TypePrinting *TypePrinter, + SlotTracker *Machine, + const Module *Context) { + assert(!Node->isTemporary() && "Unexpected forward declaration"); + + if (Node->isDistinct()) + Out << "distinct "; + + switch (Node->getMetadataID()) { + default: + llvm_unreachable("Expected uniquable MDNode"); +#define HANDLE_MDNODE_LEAF(CLASS) \ + case Metadata::CLASS##Kind: \ + write##CLASS(Out, cast(Node), TypePrinter, Machine, Context); \ + break; +#include "llvm/IR/Metadata.def" + } +} + // Full implementation of printing a Value as an operand with support for // TypePrinting, etc. static void WriteAsOperandInternal(raw_ostream &Out, const Value *V, @@ -1309,31 +1919,9 @@ static void WriteAsOperandInternal(raw_ostream &Out, const Value *V, return; } - if (const MDNode *N = dyn_cast(V)) { - if (N->isFunctionLocal()) { - // Print metadata inline, not via slot reference number. - WriteMDNodeBodyInternal(Out, N, TypePrinter, Machine, Context); - return; - } - - if (!Machine) { - if (N->isFunctionLocal()) - Machine = new SlotTracker(N->getFunction()); - else - Machine = new SlotTracker(Context); - } - int Slot = Machine->getMetadataSlot(N); - if (Slot == -1) - Out << ""; - else - Out << '!' << Slot; - return; - } - - if (const MDString *MDS = dyn_cast(V)) { - Out << "!\""; - PrintEscapedString(MDS->getString(), Out); - Out << '"'; + if (auto *MD = dyn_cast(V)) { + WriteAsOperandInternal(Out, MD->getMetadata(), TypePrinter, Machine, + Context, /* FromValue */ true); return; } @@ -1376,6 +1964,40 @@ static void WriteAsOperandInternal(raw_ostream &Out, const Value *V, Out << ""; } +static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD, + TypePrinting *TypePrinter, + SlotTracker *Machine, const Module *Context, + bool FromValue) { + if (const MDNode *N = dyn_cast(MD)) { + if (!Machine) + Machine = new SlotTracker(Context); + int Slot = Machine->getMetadataSlot(N); + if (Slot == -1) + // Give the pointer value instead of "badref", since this comes up all + // the time when debugging. + Out << "<" << N << ">"; + else + Out << '!' << Slot; + return; + } + + if (const MDString *MDS = dyn_cast(MD)) { + Out << "!\""; + PrintEscapedString(MDS->getString(), Out); + Out << '"'; + return; + } + + auto *V = cast(MD); + assert(TypePrinter && "TypePrinter required for metadata values"); + assert((FromValue || !isa(V)) && + "Unexpected function-local metadata outside of value argument"); + + TypePrinter->print(V->getValue()->getType(), Out); + Out << ' '; + WriteAsOperandInternal(Out, V->getValue(), TypePrinter, Machine, Context); +} + void AssemblyWriter::init() { if (!TheModule) return; @@ -1672,6 +2294,24 @@ static void PrintThreadLocalModel(GlobalVariable::ThreadLocalMode TLM, } } +static void maybePrintComdat(formatted_raw_ostream &Out, + const GlobalObject &GO) { + const Comdat *C = GO.getComdat(); + if (!C) + return; + + if (isa(GO)) + Out << ','; + Out << " comdat"; + + if (GO.getName() == C->getName()) + return; + + Out << '('; + PrintLLVMName(Out, C->getName(), ComdatPrefix); + Out << ')'; +} + void AssemblyWriter::printGlobal(const GlobalVariable *GV) { if (GV->isMaterializable()) Out << "; Materializable\n"; @@ -1705,10 +2345,7 @@ void AssemblyWriter::printGlobal(const GlobalVariable *GV) { PrintEscapedString(GV->getSection(), Out); Out << '"'; } - if (GV->hasComdat()) { - Out << ", comdat "; - PrintLLVMName(Out, GV->getComdat()->getName(), ComdatPrefix); - } + maybePrintComdat(Out, *GV); if (GV->getAlignment()) Out << ", align " << GV->getAlignment(); @@ -1889,10 +2526,7 @@ void AssemblyWriter::printFunction(const Function *F) { PrintEscapedString(F->getSection(), Out); Out << '"'; } - if (F->hasComdat()) { - Out << " comdat "; - PrintLLVMName(Out, F->getComdat()->getName(), ComdatPrefix); - } + maybePrintComdat(Out, *F); if (F->getAlignment()) Out << " align " << F->getAlignment(); if (F->hasGC()) @@ -1901,6 +2535,11 @@ void AssemblyWriter::printFunction(const Function *F) { Out << " prefix "; writeOperand(F->getPrefixData(), true); } + if (F->hasPrologueData()) { + Out << " prologue "; + writeOperand(F->getPrologueData(), true); + } + if (F->isDeclaration()) { Out << '\n'; } else { @@ -2340,7 +2979,7 @@ static void WriteMDNodeComment(const MDNode *Node, if (Node->getNumOperands() < 1) return; - Value *Op = Node->getOperand(0); + Metadata *Op = Node->getOperand(0); if (!Op || !isa(Op)) return; @@ -2359,8 +2998,9 @@ static void WriteMDNodeComment(const MDNode *Node, } void AssemblyWriter::writeMDNode(unsigned Slot, const MDNode *Node) { - Out << '!' << Slot << " = metadata "; + Out << '!' << Slot << " = "; printMDNodeBody(Node); + Out << "\n"; } void AssemblyWriter::writeAllMDNodes() { @@ -2378,7 +3018,6 @@ void AssemblyWriter::writeAllMDNodes() { void AssemblyWriter::printMDNodeBody(const MDNode *Node) { WriteMDNodeBodyInternal(Out, Node, &TypePrinter, &Machine, TheModule); WriteMDNodeComment(Node, Out); - Out << "\n"; } void AssemblyWriter::writeAllAttributeGroups() { @@ -2511,18 +3150,14 @@ void Value::print(raw_ostream &ROS) const { W.printFunction(F); else W.printAlias(cast(GV)); - } else if (const MDNode *N = dyn_cast(this)) { - const Function *F = N->getFunction(); - SlotTracker SlotTable(F); - AssemblyWriter W(OS, SlotTable, F ? F->getParent() : nullptr, nullptr); - W.printMDNodeBody(N); + } else if (const MetadataAsValue *V = dyn_cast(this)) { + V->getMetadata()->print(ROS); } else if (const Constant *C = dyn_cast(this)) { TypePrinting TypePrinter; TypePrinter.print(C->getType(), OS); OS << ' '; WriteConstantInternal(OS, C, TypePrinter, nullptr, nullptr); - } else if (isa(this) || isa(this) || - isa(this)) { + } else if (isa(this) || isa(this)) { this->printAsOperand(OS); } else { llvm_unreachable("Unknown value to print out!"); @@ -2532,9 +3167,8 @@ void Value::print(raw_ostream &ROS) const { void Value::printAsOperand(raw_ostream &O, bool PrintType, const Module *M) const { // Fast path: Don't construct and populate a TypePrinting object if we // won't be needing any types printed. - if (!PrintType && - ((!isa(this) && !isa(this)) || - hasName() || isa(this))) { + if (!PrintType && ((!isa(this) && !isa(this)) || + hasName() || isa(this))) { WriteAsOperandInternal(O, this, nullptr, nullptr, M); return; } @@ -2553,17 +3187,54 @@ void Value::printAsOperand(raw_ostream &O, bool PrintType, const Module *M) cons WriteAsOperandInternal(O, this, &TypePrinter, nullptr, M); } +void Metadata::print(raw_ostream &ROS) const { + formatted_raw_ostream OS(ROS); + if (auto *N = dyn_cast(this)) { + SlotTracker SlotTable(static_cast(nullptr)); + AssemblyWriter W(OS, SlotTable, nullptr, nullptr); + W.printMDNodeBody(N); + + return; + } + printAsOperand(OS); +} + +void Metadata::printAsOperand(raw_ostream &ROS, bool PrintType, + const Module *M) const { + formatted_raw_ostream OS(ROS); + + std::unique_ptr TypePrinter; + if (PrintType) { + TypePrinter.reset(new TypePrinting); + if (M) + TypePrinter->incorporateTypes(*M); + } + WriteAsOperandInternal(OS, this, TypePrinter.get(), nullptr, M, + /* FromValue */ true); +} + // Value::dump - allow easy printing of Values from the debugger. +LLVM_DUMP_METHOD void Value::dump() const { print(dbgs()); dbgs() << '\n'; } // Type::dump - allow easy printing of Types from the debugger. +LLVM_DUMP_METHOD void Type::dump() const { print(dbgs()); dbgs() << '\n'; } // Module::dump() - Allow printing of Modules from the debugger. +LLVM_DUMP_METHOD void Module::dump() const { print(dbgs(), nullptr); } // \brief Allow printing of Comdats from the debugger. +LLVM_DUMP_METHOD void Comdat::dump() const { print(dbgs()); } // NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger. +LLVM_DUMP_METHOD void NamedMDNode::dump() const { print(dbgs()); } + +LLVM_DUMP_METHOD +void Metadata::dump() const { + print(dbgs()); + dbgs() << '\n'; +} diff --git a/lib/IR/AsmWriter.h b/lib/IR/AsmWriter.h index 60da5ad..7716fa6 100644 --- a/lib/IR/AsmWriter.h +++ b/lib/IR/AsmWriter.h @@ -42,8 +42,8 @@ SlotTracker *createSlotTracker(const Module *M); //===----------------------------------------------------------------------===// class TypePrinting { - TypePrinting(const TypePrinting &) LLVM_DELETED_FUNCTION; - void operator=(const TypePrinting&) LLVM_DELETED_FUNCTION; + TypePrinting(const TypePrinting &) = delete; + void operator=(const TypePrinting&) = delete; public: /// NamedTypes - The named types that are used by the current module. diff --git a/lib/IR/AttributeImpl.h b/lib/IR/AttributeImpl.h index 0448dc1..199c318 100644 --- a/lib/IR/AttributeImpl.h +++ b/lib/IR/AttributeImpl.h @@ -33,8 +33,8 @@ class AttributeImpl : public FoldingSetNode { unsigned char KindID; ///< Holds the AttrEntryKind of the attribute // AttributesImpl is uniqued, these should not be publicly available. - void operator=(const AttributeImpl &) LLVM_DELETED_FUNCTION; - AttributeImpl(const AttributeImpl &) LLVM_DELETED_FUNCTION; + void operator=(const AttributeImpl &) = delete; + AttributeImpl(const AttributeImpl &) = delete; protected: enum AttrEntryKind { @@ -151,8 +151,8 @@ class AttributeSetNode : public FoldingSetNode { } // AttributesSetNode is uniqued, these should not be publicly available. - void operator=(const AttributeSetNode &) LLVM_DELETED_FUNCTION; - AttributeSetNode(const AttributeSetNode &) LLVM_DELETED_FUNCTION; + void operator=(const AttributeSetNode &) = delete; + AttributeSetNode(const AttributeSetNode &) = delete; public: static AttributeSetNode *get(LLVMContext &C, ArrayRef Attrs); @@ -199,8 +199,8 @@ class AttributeSetImpl : public FoldingSetNode { } // AttributesSet is uniqued, these should not be publicly available. - void operator=(const AttributeSetImpl &) LLVM_DELETED_FUNCTION; - AttributeSetImpl(const AttributeSetImpl &) LLVM_DELETED_FUNCTION; + void operator=(const AttributeSetImpl &) = delete; + AttributeSetImpl(const AttributeSetImpl &) = delete; public: AttributeSetImpl(LLVMContext &C, ArrayRef > Attrs) diff --git a/lib/IR/Attributes.cpp b/lib/IR/Attributes.cpp index 04545ea..daac6b5 100644 --- a/lib/IR/Attributes.cpp +++ b/lib/IR/Attributes.cpp @@ -835,6 +835,13 @@ AttributeSet AttributeSet::removeAttributes(LLVMContext &C, unsigned Index, return get(C, AttrSet); } +AttributeSet AttributeSet::addDereferenceableAttr(LLVMContext &C, unsigned Index, + uint64_t Bytes) const { + llvm::AttrBuilder B; + B.addDereferenceableAttr(Bytes); + return addAttributes(C, Index, AttributeSet::get(C, Index, B)); +} + //===----------------------------------------------------------------------===// // AttributeSet Accessor Methods //===----------------------------------------------------------------------===// diff --git a/lib/IR/AutoUpgrade.cpp b/lib/IR/AutoUpgrade.cpp index c24dfea..0da7784 100644 --- a/lib/IR/AutoUpgrade.cpp +++ b/lib/IR/AutoUpgrade.cpp @@ -15,9 +15,9 @@ #include "llvm/IR/CFG.h" #include "llvm/IR/CallSite.h" #include "llvm/IR/Constants.h" +#include "llvm/IR/DIBuilder.h" #include "llvm/IR/DebugInfo.h" #include "llvm/IR/DiagnosticInfo.h" -#include "llvm/IR/DIBuilder.h" #include "llvm/IR/Function.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/Instruction.h" @@ -60,6 +60,21 @@ static bool UpgradeX86IntrinsicsWith8BitMask(Function *F, Intrinsic::ID IID, return true; } +// Upgrade the declarations of AVX-512 cmp intrinsic functions whose 8-bit +// immediates have changed their type from i32 to i8. +static bool UpgradeAVX512CmpIntrinsic(Function *F, Intrinsic::ID IID, + Function *&NewFn) { + // Check that the last argument is an i32. + Type *LastArgType = F->getFunctionType()->getParamType(2); + if (!LastArgType->isIntegerTy(32)) + return false; + + // Move this function aside and map down. + F->setName(F->getName() + ".old"); + NewFn = Intrinsic::getDeclaration(F->getParent(), IID); + return true; +} + static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) { assert(F && "Illegal to upgrade a non-existent Function."); @@ -148,6 +163,14 @@ static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) { Name == "x86.avx.vbroadcast.ss" || Name == "x86.avx.vbroadcast.ss.256" || Name == "x86.avx.vbroadcast.sd.256" || + Name == "x86.sse2.psll.dq" || + Name == "x86.sse2.psrl.dq" || + Name == "x86.avx2.psll.dq" || + Name == "x86.avx2.psrl.dq" || + Name == "x86.sse2.psll.dq.bs" || + Name == "x86.sse2.psrl.dq.bs" || + Name == "x86.avx2.psll.dq.bs" || + Name == "x86.avx2.psrl.dq.bs" || (Name.startswith("x86.xop.vpcom") && F->arg_size() == 2)) { NewFn = nullptr; return true; @@ -206,6 +229,88 @@ static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) { return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx2_mpsadbw, NewFn); + if (Name == "x86.avx512.mask.cmp.ps.512") + return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_ps_512, + NewFn); + if (Name == "x86.avx512.mask.cmp.pd.512") + return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_pd_512, + NewFn); + + if (Name == "x86.avx512.mask.cmp.b.512") + return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_b_512, + NewFn); + if (Name == "x86.avx512.mask.cmp.w.512") + return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_w_512, + NewFn); + if (Name == "x86.avx512.mask.cmp.d.512") + return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_d_512, + NewFn); + if (Name == "x86.avx512.mask.cmp.q.512") + return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_q_512, + NewFn); + if (Name == "x86.avx512.mask.ucmp.b.512") + return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_b_512, + NewFn); + if (Name == "x86.avx512.mask.ucmp.w.512") + return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_w_512, + NewFn); + if (Name == "x86.avx512.mask.ucmp.d.512") + return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_d_512, + NewFn); + if (Name == "x86.avx512.mask.ucmp.q.512") + return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_q_512, + NewFn); + + if (Name == "x86.avx512.mask.cmp.b.256") + return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_b_256, + NewFn); + if (Name == "x86.avx512.mask.cmp.w.256") + return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_w_256, + NewFn); + if (Name == "x86.avx512.mask.cmp.d.256") + return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_d_256, + NewFn); + if (Name == "x86.avx512.mask.cmp.q.256") + return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_q_256, + NewFn); + if (Name == "x86.avx512.mask.ucmp.b.256") + return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_b_256, + NewFn); + if (Name == "x86.avx512.mask.ucmp.w.256") + return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_w_256, + NewFn); + if (Name == "x86.avx512.mask.ucmp.d.256") + return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_d_256, + NewFn); + if (Name == "x86.avx512.mask.ucmp.q.256") + return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_q_256, + NewFn); + + if (Name == "x86.avx512.mask.cmp.b.128") + return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_b_128, + NewFn); + if (Name == "x86.avx512.mask.cmp.w.128") + return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_w_128, + NewFn); + if (Name == "x86.avx512.mask.cmp.d.128") + return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_d_128, + NewFn); + if (Name == "x86.avx512.mask.cmp.q.128") + return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_q_128, + NewFn); + if (Name == "x86.avx512.mask.ucmp.b.128") + return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_b_128, + NewFn); + if (Name == "x86.avx512.mask.ucmp.w.128") + return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_w_128, + NewFn); + if (Name == "x86.avx512.mask.ucmp.d.128") + return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_d_128, + NewFn); + if (Name == "x86.avx512.mask.ucmp.q.128") + return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_q_128, + NewFn); + // frcz.ss/sd may need to have an argument dropped if (Name.startswith("x86.xop.vfrcz.ss") && F->arg_size() == 2) { F->setName(Name + ".old"); @@ -260,14 +365,89 @@ static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) { return dyn_cast_or_null(DbgNode->getOperand(Elt)); } -static DIExpression getExpression(Value *VarOperand, Function *F) { +static MetadataAsValue *getExpression(Value *VarOperand, Function *F) { // Old-style DIVariables have an optional expression as the 8th element. - DIExpression Expr(getNodeField(cast(VarOperand), 8)); + DIExpression Expr(getNodeField( + cast(cast(VarOperand)->getMetadata()), 8)); if (!Expr) { - DIBuilder DIB(*F->getParent()); + DIBuilder DIB(*F->getParent(), /*AllowUnresolved*/ false); Expr = DIB.createExpression(); } - return Expr; + return MetadataAsValue::get(F->getContext(), Expr); +} + +// Handles upgrading SSE2 and AVX2 PSLLDQ intrinsics by converting them +// to byte shuffles. +static Value *UpgradeX86PSLLDQIntrinsics(IRBuilder<> &Builder, LLVMContext &C, + Value *Op, unsigned NumLanes, + unsigned Shift) { + // Each lane is 16 bytes. + unsigned NumElts = NumLanes * 16; + + // Bitcast from a 64-bit element type to a byte element type. + Op = Builder.CreateBitCast(Op, + VectorType::get(Type::getInt8Ty(C), NumElts), + "cast"); + // We'll be shuffling in zeroes. + Value *Res = ConstantVector::getSplat(NumElts, Builder.getInt8(0)); + + // If shift is less than 16, emit a shuffle to move the bytes. Otherwise, + // we'll just return the zero vector. + if (Shift < 16) { + SmallVector Idxs; + // 256-bit version is split into two 16-byte lanes. + for (unsigned l = 0; l != NumElts; l += 16) + for (unsigned i = 0; i != 16; ++i) { + unsigned Idx = NumElts + i - Shift; + if (Idx < NumElts) + Idx -= NumElts - 16; // end of lane, switch operand. + Idxs.push_back(Builder.getInt32(Idx + l)); + } + + Res = Builder.CreateShuffleVector(Res, Op, ConstantVector::get(Idxs)); + } + + // Bitcast back to a 64-bit element type. + return Builder.CreateBitCast(Res, + VectorType::get(Type::getInt64Ty(C), 2*NumLanes), + "cast"); +} + +// Handles upgrading SSE2 and AVX2 PSRLDQ intrinsics by converting them +// to byte shuffles. +static Value *UpgradeX86PSRLDQIntrinsics(IRBuilder<> &Builder, LLVMContext &C, + Value *Op, unsigned NumLanes, + unsigned Shift) { + // Each lane is 16 bytes. + unsigned NumElts = NumLanes * 16; + + // Bitcast from a 64-bit element type to a byte element type. + Op = Builder.CreateBitCast(Op, + VectorType::get(Type::getInt8Ty(C), NumElts), + "cast"); + // We'll be shuffling in zeroes. + Value *Res = ConstantVector::getSplat(NumElts, Builder.getInt8(0)); + + // If shift is less than 16, emit a shuffle to move the bytes. Otherwise, + // we'll just return the zero vector. + if (Shift < 16) { + SmallVector Idxs; + // 256-bit version is split into two 16-byte lanes. + for (unsigned l = 0; l != NumElts; l += 16) + for (unsigned i = 0; i != 16; ++i) { + unsigned Idx = i + Shift; + if (Idx >= 16) + Idx += NumElts - 16; // end of lane, switch operand. + Idxs.push_back(Builder.getInt32(Idx + l)); + } + + Res = Builder.CreateShuffleVector(Op, Res, ConstantVector::get(Idxs)); + } + + // Bitcast back to a 64-bit element type. + return Builder.CreateBitCast(Res, + VectorType::get(Type::getInt64Ty(C), 2*NumLanes), + "cast"); } // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the @@ -306,8 +486,9 @@ void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) { Builder.SetInsertPoint(CI->getParent(), CI); Module *M = F->getParent(); - SmallVector Elts; - Elts.push_back(ConstantInt::get(Type::getInt32Ty(C), 1)); + SmallVector Elts; + Elts.push_back( + ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1))); MDNode *Node = MDNode::get(C, Elts); Value *Arg0 = CI->getArgOperand(0); @@ -359,9 +540,9 @@ void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) { Imm = 4; else if (Name.startswith("ne")) Imm = 5; - else if (Name.startswith("true")) - Imm = 6; else if (Name.startswith("false")) + Imm = 6; + else if (Name.startswith("true")) Imm = 7; else llvm_unreachable("Unknown condition"); @@ -388,6 +569,46 @@ void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) { for (unsigned I = 0; I < EltNum; ++I) Rep = Builder.CreateInsertElement(Rep, Load, ConstantInt::get(I32Ty, I)); + } else if (Name == "llvm.x86.sse2.psll.dq") { + // 128-bit shift left specified in bits. + unsigned Shift = cast(CI->getArgOperand(1))->getZExtValue(); + Rep = UpgradeX86PSLLDQIntrinsics(Builder, C, CI->getArgOperand(0), 1, + Shift / 8); // Shift is in bits. + } else if (Name == "llvm.x86.sse2.psrl.dq") { + // 128-bit shift right specified in bits. + unsigned Shift = cast(CI->getArgOperand(1))->getZExtValue(); + Rep = UpgradeX86PSRLDQIntrinsics(Builder, C, CI->getArgOperand(0), 1, + Shift / 8); // Shift is in bits. + } else if (Name == "llvm.x86.avx2.psll.dq") { + // 256-bit shift left specified in bits. + unsigned Shift = cast(CI->getArgOperand(1))->getZExtValue(); + Rep = UpgradeX86PSLLDQIntrinsics(Builder, C, CI->getArgOperand(0), 2, + Shift / 8); // Shift is in bits. + } else if (Name == "llvm.x86.avx2.psrl.dq") { + // 256-bit shift right specified in bits. + unsigned Shift = cast(CI->getArgOperand(1))->getZExtValue(); + Rep = UpgradeX86PSRLDQIntrinsics(Builder, C, CI->getArgOperand(0), 2, + Shift / 8); // Shift is in bits. + } else if (Name == "llvm.x86.sse2.psll.dq.bs") { + // 128-bit shift left specified in bytes. + unsigned Shift = cast(CI->getArgOperand(1))->getZExtValue(); + Rep = UpgradeX86PSLLDQIntrinsics(Builder, C, CI->getArgOperand(0), 1, + Shift); + } else if (Name == "llvm.x86.sse2.psrl.dq.bs") { + // 128-bit shift right specified in bytes. + unsigned Shift = cast(CI->getArgOperand(1))->getZExtValue(); + Rep = UpgradeX86PSRLDQIntrinsics(Builder, C, CI->getArgOperand(0), 1, + Shift); + } else if (Name == "llvm.x86.avx2.psll.dq.bs") { + // 256-bit shift left specified in bytes. + unsigned Shift = cast(CI->getArgOperand(1))->getZExtValue(); + Rep = UpgradeX86PSLLDQIntrinsics(Builder, C, CI->getArgOperand(0), 2, + Shift); + } else if (Name == "llvm.x86.avx2.psrl.dq.bs") { + // 256-bit shift right specified in bytes. + unsigned Shift = cast(CI->getArgOperand(1))->getZExtValue(); + Rep = UpgradeX86PSRLDQIntrinsics(Builder, C, CI->getArgOperand(0), 2, + Shift); } else { bool PD128 = false, PD256 = false, PS128 = false, PS256 = false; if (Name == "llvm.x86.avx.vpermil.pd.256") @@ -545,6 +766,21 @@ void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) { CI->eraseFromParent(); return; } + case Intrinsic::x86_avx512_mask_cmp_ps_512: + case Intrinsic::x86_avx512_mask_cmp_pd_512: { + // Need to truncate the last argument from i32 to i8 -- this argument models + // an inherently 8-bit immediate operand to these x86 instructions. + SmallVector Args(CI->arg_operands().begin(), + CI->arg_operands().end()); + + // Replace the last argument with a trunc. + Args[2] = Builder.CreateTrunc(Args[2], Type::getInt8Ty(C), "trunc"); + + CallInst *NewCall = Builder.CreateCall(NewFn, Args); + CI->replaceAllUsesWith(NewCall); + CI->eraseFromParent(); + return; + } } } @@ -578,22 +814,18 @@ void llvm::UpgradeInstWithTBAATag(Instruction *I) { return; if (MD->getNumOperands() == 3) { - Value *Elts[] = { - MD->getOperand(0), - MD->getOperand(1) - }; + Metadata *Elts[] = {MD->getOperand(0), MD->getOperand(1)}; MDNode *ScalarType = MDNode::get(I->getContext(), Elts); // Create a MDNode - Value *Elts2[] = { - ScalarType, ScalarType, - Constant::getNullValue(Type::getInt64Ty(I->getContext())), - MD->getOperand(2) - }; + Metadata *Elts2[] = {ScalarType, ScalarType, + ConstantAsMetadata::get(Constant::getNullValue( + Type::getInt64Ty(I->getContext()))), + MD->getOperand(2)}; I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2)); } else { // Create a MDNode - Value *Elts[] = {MD, MD, - Constant::getNullValue(Type::getInt64Ty(I->getContext()))}; + Metadata *Elts[] = {MD, MD, ConstantAsMetadata::get(Constant::getNullValue( + Type::getInt64Ty(I->getContext())))}; I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts)); } } diff --git a/lib/IR/BasicBlock.cpp b/lib/IR/BasicBlock.cpp index 5ed9bed..b3b3cbf 100644 --- a/lib/IR/BasicBlock.cpp +++ b/lib/IR/BasicBlock.cpp @@ -19,7 +19,6 @@ #include "llvm/IR/Instructions.h" #include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/LLVMContext.h" -#include "llvm/IR/LeakDetector.h" #include "llvm/IR/Type.h" #include using namespace llvm; @@ -47,9 +46,6 @@ BasicBlock::BasicBlock(LLVMContext &C, const Twine &Name, Function *NewParent, BasicBlock *InsertBefore) : Value(Type::getLabelTy(C), Value::BasicBlockVal), Parent(nullptr) { - // Make sure that we get added to a function - LeakDetector::addGarbageObject(this); - if (NewParent) insertInto(NewParent, InsertBefore); else @@ -94,14 +90,8 @@ BasicBlock::~BasicBlock() { } void BasicBlock::setParent(Function *parent) { - if (getParent()) - LeakDetector::addGarbageObject(this); - // Set Parent=parent, updating instruction symtab entries as appropriate. InstList.setSymTabObject(&Parent, parent); - - if (getParent()) - LeakDetector::removeGarbageObject(this); } void BasicBlock::removeFromParent() { @@ -249,6 +239,20 @@ BasicBlock *BasicBlock::getUniquePredecessor() { return PredBB; } +BasicBlock *BasicBlock::getUniqueSuccessor() { + succ_iterator SI = succ_begin(this), E = succ_end(this); + if (SI == E) return NULL; // No successors + BasicBlock *SuccBB = *SI; + ++SI; + for (;SI != E; ++SI) { + if (*SI != SuccBB) + return NULL; + // The same successor appears multiple times in the successor list. + // This is OK. + } + return SuccBB; +} + /// removePredecessor - This method is used to notify a BasicBlock that the /// specified Predecessor of the block is no longer able to reach it. This is /// actually not used to update the Predecessor list, but is actually used to diff --git a/lib/IR/CMakeLists.txt b/lib/IR/CMakeLists.txt index b3889e6..9fef0b2 100644 --- a/lib/IR/CMakeLists.txt +++ b/lib/IR/CMakeLists.txt @@ -11,6 +11,7 @@ add_llvm_library(LLVMCore DIBuilder.cpp DataLayout.cpp DebugInfo.cpp + DebugInfoMetadata.cpp DebugLoc.cpp DiagnosticInfo.cpp DiagnosticPrinter.cpp @@ -27,15 +28,16 @@ add_llvm_library(LLVMCore IntrinsicInst.cpp LLVMContext.cpp LLVMContextImpl.cpp - LeakDetector.cpp LegacyPassManager.cpp MDBuilder.cpp Mangler.cpp Metadata.cpp + MetadataTracking.cpp Module.cpp Pass.cpp PassManager.cpp PassRegistry.cpp + Statepoint.cpp Type.cpp TypeFinder.cpp Use.cpp @@ -45,6 +47,9 @@ add_llvm_library(LLVMCore ValueSymbolTable.cpp ValueTypes.cpp Verifier.cpp + + ADDITIONAL_HEADER_DIRS + ${LLVM_MAIN_INCLUDE_DIR}/llvm/IR ) add_dependencies(LLVMCore intrinsics_gen) diff --git a/lib/IR/ConstantFold.cpp b/lib/IR/ConstantFold.cpp index cdfb41f..a915d28 100644 --- a/lib/IR/ConstantFold.cpp +++ b/lib/IR/ConstantFold.cpp @@ -27,12 +27,14 @@ #include "llvm/IR/GlobalVariable.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/Operator.h" +#include "llvm/IR/PatternMatch.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/MathExtras.h" #include using namespace llvm; +using namespace llvm::PatternMatch; //===----------------------------------------------------------------------===// // ConstantFold*Instruction Implementations @@ -913,49 +915,70 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, return C1; return Constant::getNullValue(C1->getType()); // undef & X -> 0 case Instruction::Mul: { - ConstantInt *CI; - // X * undef -> undef if X is odd or undef - if (((CI = dyn_cast(C1)) && CI->getValue()[0]) || - ((CI = dyn_cast(C2)) && CI->getValue()[0]) || - (isa(C1) && isa(C2))) - return UndefValue::get(C1->getType()); + // undef * undef -> undef + if (isa(C1) && isa(C2)) + return C1; + const APInt *CV; + // X * undef -> undef if X is odd + if (match(C1, m_APInt(CV)) || match(C2, m_APInt(CV))) + if ((*CV)[0]) + return UndefValue::get(C1->getType()); // X * undef -> 0 otherwise return Constant::getNullValue(C1->getType()); } - case Instruction::UDiv: case Instruction::SDiv: + case Instruction::UDiv: + // X / undef -> undef + if (match(C1, m_Zero())) + return C2; + // undef / 0 -> undef // undef / 1 -> undef - if (Opcode == Instruction::UDiv || Opcode == Instruction::SDiv) - if (ConstantInt *CI2 = dyn_cast(C2)) - if (CI2->isOne()) - return C1; - // FALL THROUGH + if (match(C2, m_Zero()) || match(C2, m_One())) + return C1; + // undef / X -> 0 otherwise + return Constant::getNullValue(C1->getType()); case Instruction::URem: case Instruction::SRem: - if (!isa(C2)) // undef / X -> 0 - return Constant::getNullValue(C1->getType()); - return C2; // X / undef -> undef + // X % undef -> undef + if (match(C2, m_Undef())) + return C2; + // undef % 0 -> undef + if (match(C2, m_Zero())) + return C1; + // undef % X -> 0 otherwise + return Constant::getNullValue(C1->getType()); case Instruction::Or: // X | undef -> -1 if (isa(C1) && isa(C2)) // undef | undef -> undef return C1; return Constant::getAllOnesValue(C1->getType()); // undef | X -> ~0 case Instruction::LShr: - if (isa(C2) && isa(C1)) - return C1; // undef lshr undef -> undef - return Constant::getNullValue(C1->getType()); // X lshr undef -> 0 - // undef lshr X -> 0 + // X >>l undef -> undef + if (isa(C2)) + return C2; + // undef >>l 0 -> undef + if (match(C2, m_Zero())) + return C1; + // undef >>l X -> 0 + return Constant::getNullValue(C1->getType()); case Instruction::AShr: - if (!isa(C2)) // undef ashr X --> all ones - return Constant::getAllOnesValue(C1->getType()); - else if (isa(C1)) - return C1; // undef ashr undef -> undef - else - return C1; // X ashr undef --> X + // X >>a undef -> undef + if (isa(C2)) + return C2; + // undef >>a 0 -> undef + if (match(C2, m_Zero())) + return C1; + // TODO: undef >>a X -> undef if the shift is exact + // undef >>a X -> 0 + return Constant::getNullValue(C1->getType()); case Instruction::Shl: - if (isa(C2) && isa(C1)) - return C1; // undef shl undef -> undef - // undef << X -> 0 or X << undef -> 0 + // X << undef -> undef + if (isa(C2)) + return C2; + // undef << 0 -> undef + if (match(C2, m_Zero())) + return C1; + // undef << X -> 0 return Constant::getNullValue(C1->getType()); } } @@ -1259,15 +1282,17 @@ static int IdxCompare(Constant *C1, Constant *C2, Type *ElTy) { if (!isa(C1) || !isa(C2)) return -2; // don't know! - // Ok, we have two differing integer indices. Sign extend them to be the same - // type. Long is always big enough, so we use it. - if (!C1->getType()->isIntegerTy(64)) - C1 = ConstantExpr::getSExt(C1, Type::getInt64Ty(C1->getContext())); + // We cannot compare the indices if they don't fit in an int64_t. + if (cast(C1)->getValue().getActiveBits() > 64 || + cast(C2)->getValue().getActiveBits() > 64) + return -2; // don't know! - if (!C2->getType()->isIntegerTy(64)) - C2 = ConstantExpr::getSExt(C2, Type::getInt64Ty(C1->getContext())); + // Ok, we have two differing integer indices. Sign extend them to be the same + // type. + int64_t C1Val = cast(C1)->getSExtValue(); + int64_t C2Val = cast(C2)->getSExtValue(); - if (C1 == C2) return 0; // They are equal + if (C1Val == C2Val) return 0; // They are equal // If the type being indexed over is really just a zero sized type, there is // no pointer difference being made here. @@ -1276,8 +1301,7 @@ static int IdxCompare(Constant *C1, Constant *C2, Type *ElTy) { // If they are really different, now that they are the same type, then we // found a difference! - if (cast(C1)->getSExtValue() < - cast(C2)->getSExtValue()) + if (C1Val < C2Val) return -1; else return 1; @@ -1348,9 +1372,24 @@ static FCmpInst::Predicate evaluateFCmpRelation(Constant *V1, Constant *V2) { static ICmpInst::Predicate areGlobalsPotentiallyEqual(const GlobalValue *GV1, const GlobalValue *GV2) { + auto isGlobalUnsafeForEquality = [](const GlobalValue *GV) { + if (GV->hasExternalWeakLinkage() || GV->hasWeakAnyLinkage()) + return true; + if (const auto *GVar = dyn_cast(GV)) { + Type *Ty = GVar->getType()->getPointerElementType(); + // A global with opaque type might end up being zero sized. + if (!Ty->isSized()) + return true; + // A global with an empty type might lie at the address of any other + // global. + if (Ty->isEmptyTy()) + return true; + } + return false; + }; // Don't try to decide equality of aliases. if (!isa(GV1) && !isa(GV2)) - if (!GV1->hasExternalWeakLinkage() || !GV2->hasExternalWeakLinkage()) + if (!isGlobalUnsafeForEquality(GV1) && !isGlobalUnsafeForEquality(GV2)) return ICmpInst::ICMP_NE; return ICmpInst::BAD_ICMP_PREDICATE; } @@ -2040,8 +2079,7 @@ static Constant *ConstantFoldGetElementPtrImpl(Constant *C, if (PerformFold) { SmallVector NewIndices; NewIndices.reserve(Idxs.size() + CE->getNumOperands()); - for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i) - NewIndices.push_back(CE->getOperand(i)); + NewIndices.append(CE->op_begin() + 1, CE->op_end() - 1); // Add the last index of the source with the first index of the new GEP. // Make sure to handle the case when they are actually different types. @@ -2050,9 +2088,15 @@ static Constant *ConstantFoldGetElementPtrImpl(Constant *C, if (!Idx0->isNullValue()) { Type *IdxTy = Combined->getType(); if (IdxTy != Idx0->getType()) { - Type *Int64Ty = Type::getInt64Ty(IdxTy->getContext()); - Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, Int64Ty); - Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined, Int64Ty); + unsigned CommonExtendedWidth = + std::max(IdxTy->getIntegerBitWidth(), + Idx0->getType()->getIntegerBitWidth()); + CommonExtendedWidth = std::max(CommonExtendedWidth, 64U); + + Type *CommonTy = + Type::getIntNTy(IdxTy->getContext(), CommonExtendedWidth); + Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, CommonTy); + Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined, CommonTy); Combined = ConstantExpr::get(Instruction::Add, C1, C2); } else { Combined = @@ -2125,14 +2169,20 @@ static Constant *ConstantFoldGetElementPtrImpl(Constant *C, Constant *PrevIdx = cast(Idxs[i-1]); Constant *Div = ConstantExpr::getSDiv(CI, Factor); + unsigned CommonExtendedWidth = + std::max(PrevIdx->getType()->getIntegerBitWidth(), + Div->getType()->getIntegerBitWidth()); + CommonExtendedWidth = std::max(CommonExtendedWidth, 64U); + // Before adding, extend both operands to i64 to avoid // overflow trouble. - if (!PrevIdx->getType()->isIntegerTy(64)) - PrevIdx = ConstantExpr::getSExt(PrevIdx, - Type::getInt64Ty(Div->getContext())); - if (!Div->getType()->isIntegerTy(64)) - Div = ConstantExpr::getSExt(Div, - Type::getInt64Ty(Div->getContext())); + if (!PrevIdx->getType()->isIntegerTy(CommonExtendedWidth)) + PrevIdx = ConstantExpr::getSExt( + PrevIdx, + Type::getIntNTy(Div->getContext(), CommonExtendedWidth)); + if (!Div->getType()->isIntegerTy(CommonExtendedWidth)) + Div = ConstantExpr::getSExt( + Div, Type::getIntNTy(Div->getContext(), CommonExtendedWidth)); NewIdxs[i-1] = ConstantExpr::getAdd(PrevIdx, Div); } else { diff --git a/lib/IR/Constants.cpp b/lib/IR/Constants.cpp index e0cb835..0bf61a7 100644 --- a/lib/IR/Constants.cpp +++ b/lib/IR/Constants.cpp @@ -257,11 +257,11 @@ Constant *Constant::getAggregateElement(unsigned Elt) const { if (const ConstantVector *CV = dyn_cast(this)) return Elt < CV->getNumOperands() ? CV->getOperand(Elt) : nullptr; - if (const ConstantAggregateZero *CAZ =dyn_cast(this)) - return CAZ->getElementValue(Elt); + if (const ConstantAggregateZero *CAZ = dyn_cast(this)) + return Elt < CAZ->getNumElements() ? CAZ->getElementValue(Elt) : nullptr; if (const UndefValue *UV = dyn_cast(this)) - return UV->getElementValue(Elt); + return Elt < UV->getNumElements() ? UV->getElementValue(Elt) : nullptr; if (const ConstantDataSequential *CDS =dyn_cast(this)) return Elt < CDS->getNumElements() ? CDS->getElementAsConstant(Elt) @@ -554,19 +554,17 @@ Constant *ConstantInt::getFalse(Type *Ty) { ConstantInt::getFalse(Ty->getContext())); } - -// Get a ConstantInt from an APInt. Note that the value stored in the DenseMap -// as the key, is a DenseMapAPIntKeyInfo::KeyTy which has provided the -// operator== and operator!= to ensure that the DenseMap doesn't attempt to -// compare APInt's of different widths, which would violate an APInt class -// invariant which generates an assertion. +// Get a ConstantInt from an APInt. ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt &V) { - // Get the corresponding integer type for the bit width of the value. - IntegerType *ITy = IntegerType::get(Context, V.getBitWidth()); // get an existing value or the insertion position LLVMContextImpl *pImpl = Context.pImpl; - ConstantInt *&Slot = pImpl->IntConstants[DenseMapAPIntKeyInfo::KeyTy(V, ITy)]; - if (!Slot) Slot = new ConstantInt(ITy, V); + ConstantInt *&Slot = pImpl->IntConstants[V]; + if (!Slot) { + // Get the corresponding integer type for the bit width of the value. + IntegerType *ITy = IntegerType::get(Context, V.getBitWidth()); + Slot = new ConstantInt(ITy, V); + } + assert(Slot->getType() == IntegerType::get(Context, V.getBitWidth())); return Slot; } @@ -689,7 +687,7 @@ Constant *ConstantFP::getZeroValueForNegation(Type *Ty) { ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) { LLVMContextImpl* pImpl = Context.pImpl; - ConstantFP *&Slot = pImpl->FPConstants[DenseMapAPFloatKeyInfo::KeyTy(V)]; + ConstantFP *&Slot = pImpl->FPConstants[V]; if (!Slot) { Type *Ty; @@ -766,6 +764,14 @@ Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const { return getStructElement(Idx); } +unsigned ConstantAggregateZero::getNumElements() const { + const Type *Ty = getType(); + if (const auto *AT = dyn_cast(Ty)) + return AT->getNumElements(); + if (const auto *VT = dyn_cast(Ty)) + return VT->getNumElements(); + return Ty->getStructNumElements(); +} //===----------------------------------------------------------------------===// // UndefValue Implementation @@ -799,7 +805,14 @@ UndefValue *UndefValue::getElementValue(unsigned Idx) const { return getStructElement(Idx); } - +unsigned UndefValue::getNumElements() const { + const Type *Ty = getType(); + if (const auto *AT = dyn_cast(Ty)) + return AT->getNumElements(); + if (const auto *VT = dyn_cast(Ty)) + return VT->getNumElements(); + return Ty->getStructNumElements(); +} //===----------------------------------------------------------------------===// // ConstantXXX Classes @@ -898,23 +911,25 @@ Constant *ConstantArray::getImpl(ArrayType *Ty, ArrayRef V) { if (ConstantFP *CFP = dyn_cast(C)) { if (CFP->getType()->isFloatTy()) { - SmallVector Elts; + SmallVector Elts; for (unsigned i = 0, e = V.size(); i != e; ++i) if (ConstantFP *CFP = dyn_cast(V[i])) - Elts.push_back(CFP->getValueAPF().convertToFloat()); + Elts.push_back( + CFP->getValueAPF().bitcastToAPInt().getLimitedValue()); else break; if (Elts.size() == V.size()) - return ConstantDataArray::get(C->getContext(), Elts); + return ConstantDataArray::getFP(C->getContext(), Elts); } else if (CFP->getType()->isDoubleTy()) { - SmallVector Elts; + SmallVector Elts; for (unsigned i = 0, e = V.size(); i != e; ++i) if (ConstantFP *CFP = dyn_cast(V[i])) - Elts.push_back(CFP->getValueAPF().convertToDouble()); + Elts.push_back( + CFP->getValueAPF().bitcastToAPInt().getLimitedValue()); else break; if (Elts.size() == V.size()) - return ConstantDataArray::get(C->getContext(), Elts); + return ConstantDataArray::getFP(C->getContext(), Elts); } } } @@ -1084,23 +1099,25 @@ Constant *ConstantVector::getImpl(ArrayRef V) { if (ConstantFP *CFP = dyn_cast(C)) { if (CFP->getType()->isFloatTy()) { - SmallVector Elts; + SmallVector Elts; for (unsigned i = 0, e = V.size(); i != e; ++i) if (ConstantFP *CFP = dyn_cast(V[i])) - Elts.push_back(CFP->getValueAPF().convertToFloat()); + Elts.push_back( + CFP->getValueAPF().bitcastToAPInt().getLimitedValue()); else break; if (Elts.size() == V.size()) - return ConstantDataVector::get(C->getContext(), Elts); + return ConstantDataVector::getFP(C->getContext(), Elts); } else if (CFP->getType()->isDoubleTy()) { - SmallVector Elts; + SmallVector Elts; for (unsigned i = 0, e = V.size(); i != e; ++i) if (ConstantFP *CFP = dyn_cast(V[i])) - Elts.push_back(CFP->getValueAPF().convertToDouble()); + Elts.push_back( + CFP->getValueAPF().bitcastToAPInt().getLimitedValue()); else break; if (Elts.size() == V.size()) - return ConstantDataVector::get(C->getContext(), Elts); + return ConstantDataVector::getFP(C->getContext(), Elts); } } } @@ -2531,7 +2548,31 @@ Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef Elts) { Constant *ConstantDataArray::get(LLVMContext &Context, ArrayRef Elts) { Type *Ty = ArrayType::get(Type::getDoubleTy(Context), Elts.size()); const char *Data = reinterpret_cast(Elts.data()); - return getImpl(StringRef(const_cast(Data), Elts.size()*8), Ty); + return getImpl(StringRef(const_cast(Data), Elts.size() * 8), Ty); +} + +/// getFP() constructors - Return a constant with array type with an element +/// count and element type of float with precision matching the number of +/// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits, +/// double for 64bits) Note that this can return a ConstantAggregateZero +/// object. +Constant *ConstantDataArray::getFP(LLVMContext &Context, + ArrayRef Elts) { + Type *Ty = VectorType::get(Type::getHalfTy(Context), Elts.size()); + const char *Data = reinterpret_cast(Elts.data()); + return getImpl(StringRef(const_cast(Data), Elts.size() * 2), Ty); +} +Constant *ConstantDataArray::getFP(LLVMContext &Context, + ArrayRef Elts) { + Type *Ty = ArrayType::get(Type::getFloatTy(Context), Elts.size()); + const char *Data = reinterpret_cast(Elts.data()); + return getImpl(StringRef(const_cast(Data), Elts.size() * 4), Ty); +} +Constant *ConstantDataArray::getFP(LLVMContext &Context, + ArrayRef Elts) { + Type *Ty = ArrayType::get(Type::getDoubleTy(Context), Elts.size()); + const char *Data = reinterpret_cast(Elts.data()); + return getImpl(StringRef(const_cast(Data), Elts.size() * 8), Ty); } /// getString - This method constructs a CDS and initializes it with a text @@ -2584,7 +2625,31 @@ Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef Elts) { Constant *ConstantDataVector::get(LLVMContext &Context, ArrayRef Elts) { Type *Ty = VectorType::get(Type::getDoubleTy(Context), Elts.size()); const char *Data = reinterpret_cast(Elts.data()); - return getImpl(StringRef(const_cast(Data), Elts.size()*8), Ty); + return getImpl(StringRef(const_cast(Data), Elts.size() * 8), Ty); +} + +/// getFP() constructors - Return a constant with vector type with an element +/// count and element type of float with the precision matching the number of +/// bits in the ArrayRef passed in. (i.e. half for 16bits, float for 32bits, +/// double for 64bits) Note that this can return a ConstantAggregateZero +/// object. +Constant *ConstantDataVector::getFP(LLVMContext &Context, + ArrayRef Elts) { + Type *Ty = VectorType::get(Type::getHalfTy(Context), Elts.size()); + const char *Data = reinterpret_cast(Elts.data()); + return getImpl(StringRef(const_cast(Data), Elts.size() * 2), Ty); +} +Constant *ConstantDataVector::getFP(LLVMContext &Context, + ArrayRef Elts) { + Type *Ty = VectorType::get(Type::getFloatTy(Context), Elts.size()); + const char *Data = reinterpret_cast(Elts.data()); + return getImpl(StringRef(const_cast(Data), Elts.size() * 4), Ty); +} +Constant *ConstantDataVector::getFP(LLVMContext &Context, + ArrayRef Elts) { + Type *Ty = VectorType::get(Type::getDoubleTy(Context), Elts.size()); + const char *Data = reinterpret_cast(Elts.data()); + return getImpl(StringRef(const_cast(Data), Elts.size() * 8), Ty); } Constant *ConstantDataVector::getSplat(unsigned NumElts, Constant *V) { @@ -2610,13 +2675,14 @@ Constant *ConstantDataVector::getSplat(unsigned NumElts, Constant *V) { if (ConstantFP *CFP = dyn_cast(V)) { if (CFP->getType()->isFloatTy()) { - SmallVector Elts(NumElts, CFP->getValueAPF().convertToFloat()); - return get(V->getContext(), Elts); + SmallVector Elts( + NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue()); + return getFP(V->getContext(), Elts); } if (CFP->getType()->isDoubleTy()) { - SmallVector Elts(NumElts, - CFP->getValueAPF().convertToDouble()); - return get(V->getContext(), Elts); + SmallVector Elts( + NumElts, CFP->getValueAPF().bitcastToAPInt().getLimitedValue()); + return getFP(V->getContext(), Elts); } } return ConstantVector::getSplat(NumElts, V); @@ -2654,13 +2720,13 @@ APFloat ConstantDataSequential::getElementAsAPFloat(unsigned Elt) const { default: llvm_unreachable("Accessor can only be used when element is float/double!"); case Type::FloatTyID: { - const float *FloatPrt = reinterpret_cast(EltPtr); - return APFloat(*const_cast(FloatPrt)); - } + auto EltVal = *reinterpret_cast(EltPtr); + return APFloat(APFloat::IEEEsingle, APInt(32, EltVal)); + } case Type::DoubleTyID: { - const double *DoublePtr = reinterpret_cast(EltPtr); - return APFloat(*const_cast(DoublePtr)); - } + auto EltVal = *reinterpret_cast(EltPtr); + return APFloat(APFloat::IEEEdouble, APInt(64, EltVal)); + } } } diff --git a/lib/IR/ConstantsContext.h b/lib/IR/ConstantsContext.h index 571dec2..c1dfcf1 100644 --- a/lib/IR/ConstantsContext.h +++ b/lib/IR/ConstantsContext.h @@ -34,7 +34,7 @@ namespace llvm { /// behind the scenes to implement unary constant exprs. class UnaryConstantExpr : public ConstantExpr { void anchor() override; - void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; + void *operator new(size_t, unsigned) = delete; public: // allocate space for exactly one operand void *operator new(size_t s) { @@ -51,7 +51,7 @@ public: /// behind the scenes to implement binary constant exprs. class BinaryConstantExpr : public ConstantExpr { void anchor() override; - void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; + void *operator new(size_t, unsigned) = delete; public: // allocate space for exactly two operands void *operator new(size_t s) { @@ -72,7 +72,7 @@ public: /// behind the scenes to implement select constant exprs. class SelectConstantExpr : public ConstantExpr { void anchor() override; - void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; + void *operator new(size_t, unsigned) = delete; public: // allocate space for exactly three operands void *operator new(size_t s) { @@ -93,7 +93,7 @@ public: /// extractelement constant exprs. class ExtractElementConstantExpr : public ConstantExpr { void anchor() override; - void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; + void *operator new(size_t, unsigned) = delete; public: // allocate space for exactly two operands void *operator new(size_t s) { @@ -114,7 +114,7 @@ public: /// insertelement constant exprs. class InsertElementConstantExpr : public ConstantExpr { void anchor() override; - void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; + void *operator new(size_t, unsigned) = delete; public: // allocate space for exactly three operands void *operator new(size_t s) { @@ -136,7 +136,7 @@ public: /// shufflevector constant exprs. class ShuffleVectorConstantExpr : public ConstantExpr { void anchor() override; - void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; + void *operator new(size_t, unsigned) = delete; public: // allocate space for exactly three operands void *operator new(size_t s) { @@ -161,7 +161,7 @@ public: /// extractvalue constant exprs. class ExtractValueConstantExpr : public ConstantExpr { void anchor() override; - void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; + void *operator new(size_t, unsigned) = delete; public: // allocate space for exactly one operand void *operator new(size_t s) { @@ -186,7 +186,7 @@ public: /// insertvalue constant exprs. class InsertValueConstantExpr : public ConstantExpr { void anchor() override; - void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; + void *operator new(size_t, unsigned) = delete; public: // allocate space for exactly one operand void *operator new(size_t s) { @@ -233,7 +233,7 @@ public: // needed in order to store the predicate value for these instructions. class CompareConstantExpr : public ConstantExpr { void anchor() override; - void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION; + void *operator new(size_t, unsigned) = delete; public: // allocate space for exactly two operands void *operator new(size_t s) { diff --git a/lib/IR/Core.cpp b/lib/IR/Core.cpp index 3576137..f007616 100644 --- a/lib/IR/Core.cpp +++ b/lib/IR/Core.cpp @@ -26,8 +26,8 @@ #include "llvm/IR/InlineAsm.h" #include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/LLVMContext.h" +#include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/Module.h" -#include "llvm/PassManager.h" #include "llvm/Support/Debug.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/FileSystem.h" @@ -556,12 +556,31 @@ int LLVMHasMetadata(LLVMValueRef Inst) { } LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) { - return wrap(unwrap(Inst)->getMetadata(KindID)); + auto *I = unwrap(Inst); + assert(I && "Expected instruction"); + if (auto *MD = I->getMetadata(KindID)) + return wrap(MetadataAsValue::get(I->getContext(), MD)); + return nullptr; +} + +// MetadataAsValue uses a canonical format which strips the actual MDNode for +// MDNode with just a single constant value, storing just a ConstantAsMetadata +// This undoes this canonicalization, reconstructing the MDNode. +static MDNode *extractMDNode(MetadataAsValue *MAV) { + Metadata *MD = MAV->getMetadata(); + assert((isa(MD) || isa(MD)) && + "Expected a metadata node or a canonicalized constant"); + + if (MDNode *N = dyn_cast(MD)) + return N; + + return MDNode::get(MAV->getContext(), MD); } -void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) { - unwrap(Inst) - ->setMetadata(KindID, MD ? unwrap(MD) : nullptr); +void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) { + MDNode *N = Val ? extractMDNode(unwrap(Val)) : nullptr; + + unwrap(Inst)->setMetadata(KindID, N); } /*--.. Conversion functions ................................................--*/ @@ -573,6 +592,21 @@ void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) { LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST) +LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) { + if (auto *MD = dyn_cast_or_null(unwrap(Val))) + if (isa(MD->getMetadata()) || + isa(MD->getMetadata())) + return Val; + return nullptr; +} + +LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) { + if (auto *MD = dyn_cast_or_null(unwrap(Val))) + if (isa(MD->getMetadata())) + return Val; + return nullptr; +} + /*--.. Operations on Uses ..................................................--*/ LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) { Value *V = unwrap(Val); @@ -598,10 +632,28 @@ LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) { } /*--.. Operations on Users .................................................--*/ + +static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N, + unsigned Index) { + Metadata *Op = N->getOperand(Index); + if (!Op) + return nullptr; + if (auto *C = dyn_cast(Op)) + return wrap(C->getValue()); + return wrap(MetadataAsValue::get(Context, Op)); +} + LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) { Value *V = unwrap(Val); - if (MDNode *MD = dyn_cast(V)) - return wrap(MD->getOperand(Index)); + if (auto *MD = dyn_cast(V)) { + if (auto *L = dyn_cast(MD->getMetadata())) { + assert(Index == 0 && "Function-local metadata can only have one operand"); + return wrap(L->getValue()); + } + return getMDNodeOperandImpl(V->getContext(), + cast(MD->getMetadata()), Index); + } + return wrap(cast(V)->getOperand(Index)); } @@ -616,8 +668,9 @@ void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) { int LLVMGetNumOperands(LLVMValueRef Val) { Value *V = unwrap(Val); - if (MDNode *MD = dyn_cast(V)) - return MD->getNumOperands(); + if (isa(V)) + return LLVMGetMDNodeNumOperands(Val); + return cast(V)->getNumOperands(); } @@ -658,7 +711,9 @@ LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) { LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str, unsigned SLen) { - return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen))); + LLVMContext &Context = *unwrap(C); + return wrap(MetadataAsValue::get( + Context, MDString::get(Context, StringRef(Str, SLen)))); } LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) { @@ -667,8 +722,29 @@ LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) { LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals, unsigned Count) { - return wrap(MDNode::get(*unwrap(C), - makeArrayRef(unwrap(Vals, Count), Count))); + LLVMContext &Context = *unwrap(C); + SmallVector MDs; + for (auto *OV : makeArrayRef(Vals, Count)) { + Value *V = unwrap(OV); + Metadata *MD; + if (!V) + MD = nullptr; + else if (auto *C = dyn_cast(V)) + MD = ConstantAsMetadata::get(C); + else if (auto *MDV = dyn_cast(V)) { + MD = MDV->getMetadata(); + assert(!isa(MD) && "Unexpected function-local metadata " + "outside of direct argument to call"); + } else { + // This is function-local metadata. Pretend to make an MDNode. + assert(Count == 1 && + "Expected only one operand to function-local metadata"); + return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V))); + } + + MDs.push_back(MD); + } + return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs))); } LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) { @@ -676,25 +752,35 @@ LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) { } const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) { - if (const MDString *S = dyn_cast(unwrap(V))) { - *Len = S->getString().size(); - return S->getString().data(); - } + if (const auto *MD = dyn_cast(unwrap(V))) + if (const MDString *S = dyn_cast(MD->getMetadata())) { + *Len = S->getString().size(); + return S->getString().data(); + } *Len = 0; return nullptr; } unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) { - return cast(unwrap(V))->getNumOperands(); + auto *MD = cast(unwrap(V)); + if (isa(MD->getMetadata())) + return 1; + return cast(MD->getMetadata())->getNumOperands(); } void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) { - const MDNode *N = cast(unwrap(V)); + auto *MD = cast(unwrap(V)); + if (auto *MDV = dyn_cast(MD->getMetadata())) { + *Dest = wrap(MDV->getValue()); + return; + } + const auto *N = cast(MD->getMetadata()); const unsigned numOperands = N->getNumOperands(); + LLVMContext &Context = unwrap(V)->getContext(); for (unsigned i = 0; i < numOperands; i++) - Dest[i] = wrap(N->getOperand(i)); + Dest[i] = getMDNodeOperandImpl(Context, N, i); } unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name) @@ -710,8 +796,9 @@ void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRe NamedMDNode *N = unwrap(M)->getNamedMetadata(name); if (!N) return; + LLVMContext &Context = unwrap(M)->getContext(); for (unsigned i=0;igetNumOperands();i++) - Dest[i] = wrap(N->getOperand(i)); + Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i))); } void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name, @@ -720,9 +807,9 @@ void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name, NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name); if (!N) return; - MDNode *Op = Val ? unwrap(Val) : nullptr; - if (Op) - N->addOperand(Op); + if (!Val) + return; + N->addOperand(extractMDNode(unwrap(Val))); } /*--.. Operations on scalar constants ......................................--*/ @@ -1543,7 +1630,7 @@ LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee, auto *PTy = cast(unwrap(Ty)); return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(), GlobalValue::ExternalLinkage, Name, - unwrap(Aliasee), unwrap(M))); + unwrap(Aliasee), unwrap(M))); } /*--.. Operations on functions .............................................--*/ @@ -2092,13 +2179,16 @@ void LLVMDisposeBuilder(LLVMBuilderRef Builder) { /*--.. Metadata builders ...................................................--*/ void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) { - MDNode *Loc = L ? unwrap(L) : nullptr; + MDNode *Loc = + L ? cast(unwrap(L)->getMetadata()) : nullptr; unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc)); } LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) { - return wrap(unwrap(Builder)->getCurrentDebugLocation() - .getAsMDNode(unwrap(Builder)->getContext())); + LLVMContext &Context = unwrap(Builder)->getContext(); + return wrap(MetadataAsValue::get( + Context, + unwrap(Builder)->getCurrentDebugLocation().getAsMDNode(Context))); } void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) { @@ -2755,11 +2845,11 @@ LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) { /*===-- Pass Manager ------------------------------------------------------===*/ LLVMPassManagerRef LLVMCreatePassManager() { - return wrap(new PassManager()); + return wrap(new legacy::PassManager()); } LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) { - return wrap(new FunctionPassManager(unwrap(M))); + return wrap(new legacy::FunctionPassManager(unwrap(M))); } LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) { @@ -2768,19 +2858,19 @@ LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) { } LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) { - return unwrap(PM)->run(*unwrap(M)); + return unwrap(PM)->run(*unwrap(M)); } LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) { - return unwrap(FPM)->doInitialization(); + return unwrap(FPM)->doInitialization(); } LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) { - return unwrap(FPM)->run(*unwrap(F)); + return unwrap(FPM)->run(*unwrap(F)); } LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) { - return unwrap(FPM)->doFinalization(); + return unwrap(FPM)->doFinalization(); } void LLVMDisposePassManager(LLVMPassManagerRef PM) { diff --git a/lib/IR/DIBuilder.cpp b/lib/IR/DIBuilder.cpp index 4fe2be6..2cb27ca 100644 --- a/lib/IR/DIBuilder.cpp +++ b/lib/IR/DIBuilder.cpp @@ -25,15 +25,24 @@ using namespace llvm::dwarf; namespace { class HeaderBuilder { + /// \brief Whether there are any fields yet. + /// + /// Note that this is not equivalent to \c Chars.empty(), since \a concat() + /// may have been called already with an empty string. + bool IsEmpty; SmallVector Chars; public: - explicit HeaderBuilder(Twine T) { T.toVector(Chars); } - HeaderBuilder(const HeaderBuilder &X) : Chars(X.Chars) {} - HeaderBuilder(HeaderBuilder &&X) : Chars(std::move(X.Chars)) {} + HeaderBuilder() : IsEmpty(true) {} + HeaderBuilder(const HeaderBuilder &X) : IsEmpty(X.IsEmpty), Chars(X.Chars) {} + HeaderBuilder(HeaderBuilder &&X) + : IsEmpty(X.IsEmpty), Chars(std::move(X.Chars)) {} template HeaderBuilder &concat(Twineable &&X) { - Chars.push_back(0); + if (IsEmpty) + IsEmpty = false; + else + Chars.push_back(0); Twine(X).toVector(Chars); return *this; } @@ -43,26 +52,37 @@ public: } static HeaderBuilder get(unsigned Tag) { - return HeaderBuilder("0x" + Twine::utohexstr(Tag)); + return HeaderBuilder().concat("0x" + Twine::utohexstr(Tag)); } }; } -DIBuilder::DIBuilder(Module &m) +DIBuilder::DIBuilder(Module &m, bool AllowUnresolvedNodes) : M(m), VMContext(M.getContext()), TempEnumTypes(nullptr), TempRetainTypes(nullptr), TempSubprograms(nullptr), TempGVs(nullptr), - DeclareFn(nullptr), ValueFn(nullptr) {} + DeclareFn(nullptr), ValueFn(nullptr), + AllowUnresolvedNodes(AllowUnresolvedNodes) {} + +void DIBuilder::trackIfUnresolved(MDNode *N) { + if (!N) + return; + if (N->isResolved()) + return; + + assert(AllowUnresolvedNodes && "Cannot handle unresolved nodes"); + UnresolvedNodes.emplace_back(N); +} void DIBuilder::finalize() { DIArray Enums = getOrCreateArray(AllEnumTypes); DIType(TempEnumTypes).replaceAllUsesWith(Enums); - SmallVector RetainValues; + SmallVector RetainValues; // Declarations and definitions of the same type may be retained. Some // clients RAUW these pairs, leaving duplicates in the retained types // list. Use a set to remove the duplicates while we transform the // TrackingVHs back into Values. - SmallPtrSet RetainSet; + SmallPtrSet RetainSet; for (unsigned I = 0, E = AllRetainTypes.size(); I < E; I++) if (RetainSet.insert(AllRetainTypes[I]).second) RetainValues.push_back(AllRetainTypes[I]); @@ -74,9 +94,8 @@ void DIBuilder::finalize() { for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) { DISubprogram SP(SPs.getElement(i)); if (MDNode *Temp = SP.getVariablesNodes()) { - SmallVector Variables; - for (Value *V : PreservedVariables.lookup(SP)) - Variables.push_back(V); + const auto &PV = PreservedVariables.lookup(SP); + SmallVector Variables(PV.begin(), PV.end()); DIArray AV = getOrCreateArray(Variables); DIType(Temp).replaceAllUsesWith(AV); } @@ -85,11 +104,20 @@ void DIBuilder::finalize() { DIArray GVs = getOrCreateArray(AllGVs); DIType(TempGVs).replaceAllUsesWith(GVs); - SmallVector RetainValuesI; - for (unsigned I = 0, E = AllImportedModules.size(); I < E; I++) - RetainValuesI.push_back(AllImportedModules[I]); + SmallVector RetainValuesI(AllImportedModules.begin(), + AllImportedModules.end()); DIArray IMs = getOrCreateArray(RetainValuesI); DIType(TempImportedModules).replaceAllUsesWith(IMs); + + // Now that all temp nodes have been replaced or deleted, resolve remaining + // cycles. + for (const auto &N : UnresolvedNodes) + if (N && !N->isResolved()) + N->resolveCycles(); + UnresolvedNodes.clear(); + + // Can't handle unresolved nodes anymore. + AllowUnresolvedNodes = false; } /// If N is compile unit return NULL otherwise return N. @@ -102,10 +130,8 @@ static MDNode *getNonCompileUnitScope(MDNode *N) { static MDNode *createFilePathPair(LLVMContext &VMContext, StringRef Filename, StringRef Directory) { assert(!Filename.empty() && "Unable to create file without name"); - Value *Pair[] = { - MDString::get(VMContext, Filename), - MDString::get(VMContext, Directory) - }; + Metadata *Pair[] = {MDString::get(VMContext, Filename), + MDString::get(VMContext, Directory)}; return MDNode::get(VMContext, Pair); } @@ -117,35 +143,35 @@ DICompileUnit DIBuilder::createCompileUnit(unsigned Lang, StringRef Filename, DebugEmissionKind Kind, bool EmitDebugInfo) { - assert(((Lang <= dwarf::DW_LANG_OCaml && Lang >= dwarf::DW_LANG_C89) || + assert(((Lang <= dwarf::DW_LANG_Fortran08 && Lang >= dwarf::DW_LANG_C89) || (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) && "Invalid Language tag"); assert(!Filename.empty() && "Unable to create compile unit without filename"); - Value *TElts[] = {HeaderBuilder::get(DW_TAG_base_type).get(VMContext)}; - TempEnumTypes = MDNode::getTemporary(VMContext, TElts); - - TempRetainTypes = MDNode::getTemporary(VMContext, TElts); - - TempSubprograms = MDNode::getTemporary(VMContext, TElts); - - TempGVs = MDNode::getTemporary(VMContext, TElts); - - TempImportedModules = MDNode::getTemporary(VMContext, TElts); - - Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_compile_unit) - .concat(Lang) - .concat(Producer) - .concat(isOptimized) - .concat(Flags) - .concat(RunTimeVer) - .concat(SplitName) - .concat(Kind) - .get(VMContext), - createFilePathPair(VMContext, Filename, Directory), - TempEnumTypes, TempRetainTypes, TempSubprograms, TempGVs, - TempImportedModules}; + // TODO: Once we make MDCompileUnit distinct, stop using temporaries here + // (just start with operands assigned to nullptr). + TempEnumTypes = MDTuple::getTemporary(VMContext, None).release(); + TempRetainTypes = MDTuple::getTemporary(VMContext, None).release(); + TempSubprograms = MDTuple::getTemporary(VMContext, None).release(); + TempGVs = MDTuple::getTemporary(VMContext, None).release(); + TempImportedModules = MDTuple::getTemporary(VMContext, None).release(); + + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_compile_unit) + .concat(Lang) + .concat(Producer) + .concat(isOptimized) + .concat(Flags) + .concat(RunTimeVer) + .concat(SplitName) + .concat(Kind) + .get(VMContext), + createFilePathPair(VMContext, Filename, Directory), + TempEnumTypes, TempRetainTypes, TempSubprograms, TempGVs, + TempImportedModules}; + + // TODO: Switch to getDistinct(). We never want to merge compile units based + // on contents. MDNode *CUNode = MDNode::get(VMContext, Elts); // Create a named metadata so that it is easier to find cu in a module. @@ -158,20 +184,21 @@ DICompileUnit DIBuilder::createCompileUnit(unsigned Lang, StringRef Filename, NMD->addOperand(CUNode); } + trackIfUnresolved(CUNode); return DICompileUnit(CUNode); } static DIImportedEntity createImportedModule(LLVMContext &C, dwarf::Tag Tag, DIScope Context, - Value *NS, unsigned Line, StringRef Name, - SmallVectorImpl> &AllImportedModules) { + Metadata *NS, unsigned Line, StringRef Name, + SmallVectorImpl &AllImportedModules) { const MDNode *R; - Value *Elts[] = {HeaderBuilder::get(Tag).concat(Line).concat(Name).get(C), - Context, NS}; + Metadata *Elts[] = {HeaderBuilder::get(Tag).concat(Line).concat(Name).get(C), + Context, NS}; R = MDNode::get(C, Elts); DIImportedEntity M(R); assert(M.Verify() && "Imported module should be valid"); - AllImportedModules.push_back(TrackingVH(M)); + AllImportedModules.emplace_back(M.get()); return M; } @@ -194,7 +221,8 @@ DIImportedEntity DIBuilder::createImportedDeclaration(DIScope Context, unsigned Line, StringRef Name) { // Make sure to use the unique identifier based metadata reference for // types that have one. - Value *V = Decl.isType() ? static_cast(DIType(Decl).getRef()) : Decl; + Metadata *V = + Decl.isType() ? static_cast(DIType(Decl).getRef()) : Decl; return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration, Context, V, Line, Name, AllImportedModules); @@ -208,16 +236,18 @@ DIImportedEntity DIBuilder::createImportedDeclaration(DIScope Context, } DIFile DIBuilder::createFile(StringRef Filename, StringRef Directory) { - Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_file_type).get(VMContext), - createFilePathPair(VMContext, Filename, Directory)}; + Metadata *Elts[] = { + HeaderBuilder::get(dwarf::DW_TAG_file_type).get(VMContext), + createFilePathPair(VMContext, Filename, Directory)}; return DIFile(MDNode::get(VMContext, Elts)); } DIEnumerator DIBuilder::createEnumerator(StringRef Name, int64_t Val) { assert(!Name.empty() && "Unable to create enumerator without name"); - Value *Elts[] = { - HeaderBuilder::get(dwarf::DW_TAG_enumerator).concat(Name).concat(Val).get( - VMContext)}; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_enumerator) + .concat(Name) + .concat(Val) + .get(VMContext)}; return DIEnumerator(MDNode::get(VMContext, Elts)); } @@ -225,7 +255,7 @@ DIBasicType DIBuilder::createUnspecifiedType(StringRef Name) { assert(!Name.empty() && "Unable to create type without name"); // Unspecified types are encoded in DIBasicType format. Line number, filename, // size, alignment, offset and flags are always empty here. - Value *Elts[] = { + Metadata *Elts[] = { HeaderBuilder::get(dwarf::DW_TAG_unspecified_type) .concat(Name) .concat(0) @@ -251,7 +281,7 @@ DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits, assert(!Name.empty() && "Unable to create type without name"); // Basic types are encoded in DIBasicType format. Line number, filename, // offset and flags are always empty here. - Value *Elts[] = { + Metadata *Elts[] = { HeaderBuilder::get(dwarf::DW_TAG_base_type) .concat(Name) .concat(0) // Line @@ -269,17 +299,17 @@ DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits, DIDerivedType DIBuilder::createQualifiedType(unsigned Tag, DIType FromTy) { // Qualified types are encoded in DIDerivedType format. - Value *Elts[] = {HeaderBuilder::get(Tag) - .concat(StringRef()) // Name - .concat(0) // Line - .concat(0) // Size - .concat(0) // Align - .concat(0) // Offset - .concat(0) // Flags - .get(VMContext), - nullptr, // Filename - nullptr, // Unused - FromTy.getRef()}; + Metadata *Elts[] = {HeaderBuilder::get(Tag) + .concat(StringRef()) // Name + .concat(0) // Line + .concat(0) // Size + .concat(0) // Align + .concat(0) // Offset + .concat(0) // Flags + .get(VMContext), + nullptr, // Filename + nullptr, // Unused + FromTy.getRef()}; return DIDerivedType(MDNode::get(VMContext, Elts)); } @@ -287,68 +317,69 @@ DIDerivedType DIBuilder::createPointerType(DIType PointeeTy, uint64_t SizeInBits, uint64_t AlignInBits, StringRef Name) { // Pointer types are encoded in DIDerivedType format. - Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_pointer_type) - .concat(Name) - .concat(0) // Line - .concat(SizeInBits) - .concat(AlignInBits) - .concat(0) // Offset - .concat(0) // Flags - .get(VMContext), - nullptr, // Filename - nullptr, // Unused - PointeeTy.getRef()}; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_pointer_type) + .concat(Name) + .concat(0) // Line + .concat(SizeInBits) + .concat(AlignInBits) + .concat(0) // Offset + .concat(0) // Flags + .get(VMContext), + nullptr, // Filename + nullptr, // Unused + PointeeTy.getRef()}; return DIDerivedType(MDNode::get(VMContext, Elts)); } -DIDerivedType DIBuilder::createMemberPointerType(DIType PointeeTy, - DIType Base) { +DIDerivedType +DIBuilder::createMemberPointerType(DIType PointeeTy, DIType Base, + uint64_t SizeInBits, uint64_t AlignInBits) { // Pointer types are encoded in DIDerivedType format. - Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_ptr_to_member_type) - .concat(StringRef()) - .concat(0) // Line - .concat(0) // Size - .concat(0) // Align - .concat(0) // Offset - .concat(0) // Flags - .get(VMContext), - nullptr, // Filename - nullptr, // Unused - PointeeTy.getRef(), Base.getRef()}; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_ptr_to_member_type) + .concat(StringRef()) + .concat(0) // Line + .concat(SizeInBits) // Size + .concat(AlignInBits) // Align + .concat(0) // Offset + .concat(0) // Flags + .get(VMContext), + nullptr, // Filename + nullptr, // Unused + PointeeTy.getRef(), Base.getRef()}; return DIDerivedType(MDNode::get(VMContext, Elts)); } DIDerivedType DIBuilder::createReferenceType(unsigned Tag, DIType RTy) { assert(RTy.isType() && "Unable to create reference type"); // References are encoded in DIDerivedType format. - Value *Elts[] = {HeaderBuilder::get(Tag) - .concat(StringRef()) // Name - .concat(0) // Line - .concat(0) // Size - .concat(0) // Align - .concat(0) // Offset - .concat(0) // Flags - .get(VMContext), - nullptr, // Filename - nullptr, // TheCU, - RTy.getRef()}; + Metadata *Elts[] = {HeaderBuilder::get(Tag) + .concat(StringRef()) // Name + .concat(0) // Line + .concat(0) // Size + .concat(0) // Align + .concat(0) // Offset + .concat(0) // Flags + .get(VMContext), + nullptr, // Filename + nullptr, // TheCU, + RTy.getRef()}; return DIDerivedType(MDNode::get(VMContext, Elts)); } DIDerivedType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File, unsigned LineNo, DIDescriptor Context) { // typedefs are encoded in DIDerivedType format. - Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_typedef) - .concat(Name) - .concat(LineNo) - .concat(0) // Size - .concat(0) // Align - .concat(0) // Offset - .concat(0) // Flags - .get(VMContext), - File.getFileNode(), - DIScope(getNonCompileUnitScope(Context)).getRef(), - Ty.getRef()}; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_typedef) + .concat(Name) + .concat(LineNo) + .concat(0) // Size + .concat(0) // Align + .concat(0) // Offset + .concat(0) // Flags + .get(VMContext), + File.getFileNode(), + DIScope(getNonCompileUnitScope(Context)).getRef(), + Ty.getRef()}; return DIDerivedType(MDNode::get(VMContext, Elts)); } @@ -356,15 +387,15 @@ DIDerivedType DIBuilder::createFriend(DIType Ty, DIType FriendTy) { // typedefs are encoded in DIDerivedType format. assert(Ty.isType() && "Invalid type!"); assert(FriendTy.isType() && "Invalid friend type!"); - Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_friend) - .concat(StringRef()) // Name - .concat(0) // Line - .concat(0) // Size - .concat(0) // Align - .concat(0) // Offset - .concat(0) // Flags - .get(VMContext), - nullptr, Ty.getRef(), FriendTy.getRef()}; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_friend) + .concat(StringRef()) // Name + .concat(0) // Line + .concat(0) // Size + .concat(0) // Align + .concat(0) // Offset + .concat(0) // Flags + .get(VMContext), + nullptr, Ty.getRef(), FriendTy.getRef()}; return DIDerivedType(MDNode::get(VMContext, Elts)); } @@ -373,16 +404,17 @@ DIDerivedType DIBuilder::createInheritance(DIType Ty, DIType BaseTy, unsigned Flags) { assert(Ty.isType() && "Unable to create inheritance"); // TAG_inheritance is encoded in DIDerivedType format. - Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_inheritance) - .concat(StringRef()) // Name - .concat(0) // Line - .concat(0) // Size - .concat(0) // Align - .concat(BaseOffset) - .concat(Flags) - .get(VMContext), - nullptr, Ty.getRef(), BaseTy.getRef()}; - return DIDerivedType(MDNode::get(VMContext, Elts)); + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_inheritance) + .concat(StringRef()) // Name + .concat(0) // Line + .concat(0) // Size + .concat(0) // Align + .concat(BaseOffset) + .concat(Flags) + .get(VMContext), + nullptr, Ty.getRef(), BaseTy.getRef()}; + auto R = DIDerivedType(MDNode::get(VMContext, Elts)); + return R; } DIDerivedType DIBuilder::createMemberType(DIDescriptor Scope, StringRef Name, @@ -392,20 +424,26 @@ DIDerivedType DIBuilder::createMemberType(DIDescriptor Scope, StringRef Name, uint64_t OffsetInBits, unsigned Flags, DIType Ty) { // TAG_member is encoded in DIDerivedType format. - Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_member) - .concat(Name) - .concat(LineNumber) - .concat(SizeInBits) - .concat(AlignInBits) - .concat(OffsetInBits) - .concat(Flags) - .get(VMContext), - File.getFileNode(), - DIScope(getNonCompileUnitScope(Scope)).getRef(), - Ty.getRef()}; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_member) + .concat(Name) + .concat(LineNumber) + .concat(SizeInBits) + .concat(AlignInBits) + .concat(OffsetInBits) + .concat(Flags) + .get(VMContext), + File.getFileNode(), + DIScope(getNonCompileUnitScope(Scope)).getRef(), + Ty.getRef()}; return DIDerivedType(MDNode::get(VMContext, Elts)); } +static Metadata *getConstantOrNull(Constant *C) { + if (C) + return ConstantAsMetadata::get(C); + return nullptr; +} + DIDerivedType DIBuilder::createStaticMemberType(DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber, DIType Ty, @@ -413,17 +451,17 @@ DIDerivedType DIBuilder::createStaticMemberType(DIDescriptor Scope, llvm::Constant *Val) { // TAG_member is encoded in DIDerivedType format. Flags |= DIDescriptor::FlagStaticMember; - Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_member) - .concat(Name) - .concat(LineNumber) - .concat(0) // Size - .concat(0) // Align - .concat(0) // Offset - .concat(Flags) - .get(VMContext), - File.getFileNode(), - DIScope(getNonCompileUnitScope(Scope)).getRef(), Ty.getRef(), - Val}; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_member) + .concat(Name) + .concat(LineNumber) + .concat(0) // Size + .concat(0) // Align + .concat(0) // Offset + .concat(Flags) + .get(VMContext), + File.getFileNode(), + DIScope(getNonCompileUnitScope(Scope)).getRef(), + Ty.getRef(), getConstantOrNull(Val)}; return DIDerivedType(MDNode::get(VMContext, Elts)); } @@ -434,16 +472,16 @@ DIDerivedType DIBuilder::createObjCIVar(StringRef Name, DIFile File, uint64_t OffsetInBits, unsigned Flags, DIType Ty, MDNode *PropertyNode) { // TAG_member is encoded in DIDerivedType format. - Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_member) - .concat(Name) - .concat(LineNumber) - .concat(SizeInBits) - .concat(AlignInBits) - .concat(OffsetInBits) - .concat(Flags) - .get(VMContext), - File.getFileNode(), getNonCompileUnitScope(File), Ty, - PropertyNode}; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_member) + .concat(Name) + .concat(LineNumber) + .concat(SizeInBits) + .concat(AlignInBits) + .concat(OffsetInBits) + .concat(Flags) + .get(VMContext), + File.getFileNode(), getNonCompileUnitScope(File), Ty, + PropertyNode}; return DIDerivedType(MDNode::get(VMContext, Elts)); } @@ -451,69 +489,65 @@ DIObjCProperty DIBuilder::createObjCProperty(StringRef Name, DIFile File, unsigned LineNumber, StringRef GetterName, StringRef SetterName, unsigned PropertyAttributes, DIType Ty) { - Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_APPLE_property) - .concat(Name) - .concat(LineNumber) - .concat(GetterName) - .concat(SetterName) - .concat(PropertyAttributes) - .get(VMContext), - File, Ty}; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_APPLE_property) + .concat(Name) + .concat(LineNumber) + .concat(GetterName) + .concat(SetterName) + .concat(PropertyAttributes) + .get(VMContext), + File, Ty}; return DIObjCProperty(MDNode::get(VMContext, Elts)); } DITemplateTypeParameter DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name, - DIType Ty, MDNode *File, unsigned LineNo, - unsigned ColumnNo) { - Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_template_type_parameter) - .concat(Name) - .concat(LineNo) - .concat(ColumnNo) - .get(VMContext), - DIScope(getNonCompileUnitScope(Context)).getRef(), - Ty.getRef(), File}; + DIType Ty) { + assert(!DIScope(getNonCompileUnitScope(Context)).getRef() && + "Expected compile unit"); + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_template_type_parameter) + .concat(Name) + .concat(0) + .concat(0) + .get(VMContext), + nullptr, Ty.getRef(), nullptr}; return DITemplateTypeParameter(MDNode::get(VMContext, Elts)); } -static DITemplateValueParameter createTemplateValueParameterHelper( - LLVMContext &VMContext, unsigned Tag, DIDescriptor Context, StringRef Name, - DIType Ty, Value *Val, MDNode *File, unsigned LineNo, unsigned ColumnNo) { - Value *Elts[] = { - HeaderBuilder::get(Tag).concat(Name).concat(LineNo).concat(ColumnNo).get( - VMContext), - DIScope(getNonCompileUnitScope(Context)).getRef(), Ty.getRef(), Val, - File}; +static DITemplateValueParameter +createTemplateValueParameterHelper(LLVMContext &VMContext, unsigned Tag, + DIDescriptor Context, StringRef Name, + DIType Ty, Metadata *MD) { + assert(!DIScope(getNonCompileUnitScope(Context)).getRef() && + "Expected compile unit"); + Metadata *Elts[] = { + HeaderBuilder::get(Tag).concat(Name).concat(0).concat(0).get(VMContext), + nullptr, Ty.getRef(), MD, nullptr}; return DITemplateValueParameter(MDNode::get(VMContext, Elts)); } DITemplateValueParameter DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name, - DIType Ty, Constant *Val, MDNode *File, - unsigned LineNo, unsigned ColumnNo) { + DIType Ty, Constant *Val) { return createTemplateValueParameterHelper( - VMContext, dwarf::DW_TAG_template_value_parameter, Context, Name, Ty, Val, - File, LineNo, ColumnNo); + VMContext, dwarf::DW_TAG_template_value_parameter, Context, Name, Ty, + getConstantOrNull(Val)); } DITemplateValueParameter DIBuilder::createTemplateTemplateParameter(DIDescriptor Context, StringRef Name, - DIType Ty, StringRef Val, - MDNode *File, unsigned LineNo, - unsigned ColumnNo) { + DIType Ty, StringRef Val) { return createTemplateValueParameterHelper( VMContext, dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty, - MDString::get(VMContext, Val), File, LineNo, ColumnNo); + MDString::get(VMContext, Val)); } DITemplateValueParameter DIBuilder::createTemplateParameterPack(DIDescriptor Context, StringRef Name, - DIType Ty, DIArray Val, - MDNode *File, unsigned LineNo, - unsigned ColumnNo) { + DIType Ty, DIArray Val) { return createTemplateValueParameterHelper( VMContext, dwarf::DW_TAG_GNU_template_parameter_pack, Context, Name, Ty, - Val, File, LineNo, ColumnNo); + Val); } DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name, @@ -529,7 +563,7 @@ DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name, assert((!Context || Context.isScope() || Context.isType()) && "createClassType should be called with a valid Context"); // TAG_class_type is encoded in DICompositeType format. - Value *Elts[] = { + Metadata *Elts[] = { HeaderBuilder::get(dwarf::DW_TAG_class_type) .concat(Name) .concat(LineNumber) @@ -548,6 +582,7 @@ DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name, "createClassType should return a DICompositeType"); if (!UniqueIdentifier.empty()) retainType(R); + trackIfUnresolved(R); return R; } @@ -562,7 +597,7 @@ DICompositeType DIBuilder::createStructType(DIDescriptor Context, DIType VTableHolder, StringRef UniqueIdentifier) { // TAG_structure_type is encoded in DICompositeType format. - Value *Elts[] = { + Metadata *Elts[] = { HeaderBuilder::get(dwarf::DW_TAG_structure_type) .concat(Name) .concat(LineNumber) @@ -581,6 +616,7 @@ DICompositeType DIBuilder::createStructType(DIDescriptor Context, "createStructType should return a DICompositeType"); if (!UniqueIdentifier.empty()) retainType(R); + trackIfUnresolved(R); return R; } @@ -592,7 +628,7 @@ DICompositeType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name, unsigned RunTimeLang, StringRef UniqueIdentifier) { // TAG_union_type is encoded in DICompositeType format. - Value *Elts[] = { + Metadata *Elts[] = { HeaderBuilder::get(dwarf::DW_TAG_union_type) .concat(Name) .concat(LineNumber) @@ -609,6 +645,7 @@ DICompositeType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name, DICompositeType R(MDNode::get(VMContext, Elts)); if (!UniqueIdentifier.empty()) retainType(R); + trackIfUnresolved(R); return R; } @@ -616,7 +653,7 @@ DISubroutineType DIBuilder::createSubroutineType(DIFile File, DITypeArray ParameterTypes, unsigned Flags) { // TAG_subroutine_type is encoded in DICompositeType format. - Value *Elts[] = { + Metadata *Elts[] = { HeaderBuilder::get(dwarf::DW_TAG_subroutine_type) .concat(StringRef()) .concat(0) // Line @@ -637,7 +674,7 @@ DICompositeType DIBuilder::createEnumerationType( uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements, DIType UnderlyingType, StringRef UniqueIdentifier) { // TAG_enumeration_type is encoded in DICompositeType format. - Value *Elts[] = { + Metadata *Elts[] = { HeaderBuilder::get(dwarf::DW_TAG_enumeration_type) .concat(Name) .concat(LineNumber) @@ -655,13 +692,14 @@ DICompositeType DIBuilder::createEnumerationType( AllEnumTypes.push_back(CTy); if (!UniqueIdentifier.empty()) retainType(CTy); + trackIfUnresolved(CTy); return CTy; } DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits, DIType Ty, DIArray Subscripts) { // TAG_array_type is encoded in DICompositeType format. - Value *Elts[] = { + Metadata *Elts[] = { HeaderBuilder::get(dwarf::DW_TAG_array_type) .concat(StringRef()) .concat(0) // Line @@ -676,13 +714,15 @@ DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits, Ty.getRef(), Subscripts, nullptr, nullptr, nullptr // Type Identifer }; - return DICompositeType(MDNode::get(VMContext, Elts)); + DICompositeType R(MDNode::get(VMContext, Elts)); + trackIfUnresolved(R); + return R; } DICompositeType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits, DIType Ty, DIArray Subscripts) { // A vector is an array type with the FlagVector flag applied. - Value *Elts[] = { + Metadata *Elts[] = { HeaderBuilder::get(dwarf::DW_TAG_array_type) .concat("") .concat(0) // Line @@ -697,7 +737,9 @@ DICompositeType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits, Ty.getRef(), Subscripts, nullptr, nullptr, nullptr // Type Identifer }; - return DICompositeType(MDNode::get(VMContext, Elts)); + DICompositeType R(MDNode::get(VMContext, Elts)); + trackIfUnresolved(R); + return R; } static HeaderBuilder setTypeFlagsInHeader(StringRef Header, @@ -710,19 +752,20 @@ static HeaderBuilder setTypeFlagsInHeader(StringRef Header, Flags = 0; Flags |= FlagsToSet; - return HeaderBuilder(Twine(I.getPrefix())).concat(Flags).concat( - I.getSuffix()); + return HeaderBuilder() + .concat(I.getPrefix()) + .concat(Flags) + .concat(I.getSuffix()); } static DIType createTypeWithFlags(LLVMContext &Context, DIType Ty, unsigned FlagsToSet) { - SmallVector Elts; + SmallVector Elts; MDNode *N = Ty; assert(N && "Unexpected input DIType!"); // Update header field. Elts.push_back(setTypeFlagsInHeader(Ty.getHeader(), FlagsToSet).get(Context)); - for (unsigned I = 1, E = N->getNumOperands(); I != E; ++I) - Elts.push_back(N->getOperand(I)); + Elts.append(N->op_begin() + 1, N->op_end()); return DIType(MDNode::get(Context, Elts)); } @@ -740,9 +783,7 @@ DIType DIBuilder::createObjectPointerType(DIType Ty) { return createTypeWithFlags(VMContext, Ty, Flags); } -void DIBuilder::retainType(DIType T) { - AllRetainTypes.push_back(TrackingVH(T)); -} +void DIBuilder::retainType(DIType T) { AllRetainTypes.emplace_back(T); } DIBasicType DIBuilder::createUnspecifiedParameter() { return DIBasicType(); @@ -754,7 +795,7 @@ DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIDescriptor Scope, uint64_t SizeInBits, uint64_t AlignInBits, StringRef UniqueIdentifier) { // Create a temporary MDNode. - Value *Elts[] = { + Metadata *Elts[] = { HeaderBuilder::get(Tag) .concat(Name) .concat(Line) @@ -775,22 +816,23 @@ DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIDescriptor Scope, "createForwardDecl result should be a DIType"); if (!UniqueIdentifier.empty()) retainType(RetTy); + trackIfUnresolved(RetTy); return RetTy; } -DICompositeType DIBuilder::createReplaceableForwardDecl( +DICompositeType DIBuilder::createReplaceableCompositeType( unsigned Tag, StringRef Name, DIDescriptor Scope, DIFile F, unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits, uint64_t AlignInBits, - StringRef UniqueIdentifier) { + unsigned Flags, StringRef UniqueIdentifier) { // Create a temporary MDNode. - Value *Elts[] = { + Metadata *Elts[] = { HeaderBuilder::get(Tag) .concat(Name) .concat(Line) .concat(SizeInBits) .concat(AlignInBits) .concat(0) // Offset - .concat(DIDescriptor::FlagFwdDecl) + .concat(Flags) .concat(RuntimeLang) .get(VMContext), F.getFileNode(), DIScope(getNonCompileUnitScope(Scope)).getRef(), nullptr, @@ -798,21 +840,21 @@ DICompositeType DIBuilder::createReplaceableForwardDecl( nullptr, // TemplateParams UniqueIdentifier.empty() ? nullptr : MDString::get(VMContext, UniqueIdentifier)}; - MDNode *Node = MDNode::getTemporary(VMContext, Elts); - DICompositeType RetTy(Node); + DICompositeType RetTy(MDNode::getTemporary(VMContext, Elts).release()); assert(RetTy.isCompositeType() && "createReplaceableForwardDecl result should be a DIType"); if (!UniqueIdentifier.empty()) retainType(RetTy); + trackIfUnresolved(RetTy); return RetTy; } -DIArray DIBuilder::getOrCreateArray(ArrayRef Elements) { +DIArray DIBuilder::getOrCreateArray(ArrayRef Elements) { return DIArray(MDNode::get(VMContext, Elements)); } -DITypeArray DIBuilder::getOrCreateTypeArray(ArrayRef Elements) { - SmallVector Elts; +DITypeArray DIBuilder::getOrCreateTypeArray(ArrayRef Elements) { + SmallVector Elts; for (unsigned i = 0, e = Elements.size(); i != e; ++i) { if (Elements[i] && isa(Elements[i])) Elts.push_back(DIType(cast(Elements[i])).getRef()); @@ -823,10 +865,10 @@ DITypeArray DIBuilder::getOrCreateTypeArray(ArrayRef Elements) { } DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) { - Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_subrange_type) - .concat(Lo) - .concat(Count) - .get(VMContext)}; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_subrange_type) + .concat(Lo) + .concat(Count) + .get(VMContext)}; return DISubrange(MDNode::get(VMContext, Elts)); } @@ -835,17 +877,24 @@ static DIGlobalVariable createGlobalVariableHelper( LLVMContext &VMContext, DIDescriptor Context, StringRef Name, StringRef LinkageName, DIFile F, unsigned LineNumber, DITypeRef Ty, bool isLocalToUnit, Constant *Val, MDNode *Decl, bool isDefinition, - std::function)> CreateFunc) { - Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_variable) - .concat(Name) - .concat(Name) - .concat(LinkageName) - .concat(LineNumber) - .concat(isLocalToUnit) - .concat(isDefinition) - .get(VMContext), - DIScope(getNonCompileUnitScope(Context)).getRef(), F, Ty, Val, - DIDescriptor(Decl)}; + std::function)> CreateFunc) { + + MDNode *TheCtx = getNonCompileUnitScope(Context); + if (DIScope(TheCtx).isCompositeType()) { + assert(!DICompositeType(TheCtx).getIdentifier() && + "Context of a global variable should not be a type with identifier"); + } + + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_variable) + .concat(Name) + .concat(Name) + .concat(LinkageName) + .concat(LineNumber) + .concat(isLocalToUnit) + .concat(isDefinition) + .get(VMContext), + TheCtx, F, Ty, getConstantOrNull(Val), + DIDescriptor(Decl)}; return DIGlobalVariable(CreateFunc(Elts)); } @@ -854,13 +903,13 @@ DIGlobalVariable DIBuilder::createGlobalVariable( DIDescriptor Context, StringRef Name, StringRef LinkageName, DIFile F, unsigned LineNumber, DITypeRef Ty, bool isLocalToUnit, Constant *Val, MDNode *Decl) { - return createGlobalVariableHelper(VMContext, Context, Name, LinkageName, F, - LineNumber, Ty, isLocalToUnit, Val, Decl, true, - [&] (ArrayRef Elts) -> MDNode * { - MDNode *Node = MDNode::get(VMContext, Elts); - AllGVs.push_back(Node); - return Node; - }); + return createGlobalVariableHelper( + VMContext, Context, Name, LinkageName, F, LineNumber, Ty, isLocalToUnit, + Val, Decl, true, [&](ArrayRef Elts) -> MDNode *{ + MDNode *Node = MDNode::get(VMContext, Elts); + AllGVs.push_back(Node); + return Node; + }); } DIGlobalVariable DIBuilder::createTempGlobalVariableFwdDecl( @@ -868,10 +917,10 @@ DIGlobalVariable DIBuilder::createTempGlobalVariableFwdDecl( unsigned LineNumber, DITypeRef Ty, bool isLocalToUnit, Constant *Val, MDNode *Decl) { return createGlobalVariableHelper(VMContext, Context, Name, LinkageName, F, - LineNumber, Ty, isLocalToUnit, Val, Decl, false, - [&] (ArrayRef Elts) { - return MDNode::getTemporary(VMContext, Elts); - }); + LineNumber, Ty, isLocalToUnit, Val, Decl, + false, [&](ArrayRef Elts) { + return MDNode::getTemporary(VMContext, Elts).release(); + }); } DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope, @@ -882,12 +931,12 @@ DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope, DIDescriptor Context(getNonCompileUnitScope(Scope)); assert((!Context || Context.isScope()) && "createLocalVariable should be called with a valid Context"); - Value *Elts[] = {HeaderBuilder::get(Tag) - .concat(Name) - .concat(LineNo | (ArgNo << 24)) - .concat(Flags) - .get(VMContext), - getNonCompileUnitScope(Scope), File, Ty}; + Metadata *Elts[] = {HeaderBuilder::get(Tag) + .concat(Name) + .concat(LineNo | (ArgNo << 24)) + .concat(Flags) + .get(VMContext), + getNonCompileUnitScope(Scope), File, Ty}; MDNode *Node = MDNode::get(VMContext, Elts); if (AlwaysPreserve) { // The optimizer may remove local variable. If there is an interest @@ -895,7 +944,7 @@ DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope, // named mdnode. DISubprogram Fn(getDISubprogram(Scope)); assert(Fn && "Missing subprogram for local variable"); - PreservedVariables[Fn].push_back(Node); + PreservedVariables[Fn].emplace_back(Node); } DIVariable RetVar(Node); assert(RetVar.isVariable() && @@ -903,17 +952,23 @@ DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope, return RetVar; } -DIExpression DIBuilder::createExpression(ArrayRef Addr) { +DIExpression DIBuilder::createExpression(ArrayRef Addr) { auto Header = HeaderBuilder::get(DW_TAG_expression); - for (int64_t I : Addr) + for (uint64_t I : Addr) Header.concat(I); - Value *Elts[] = {Header.get(VMContext)}; + Metadata *Elts[] = {Header.get(VMContext)}; return DIExpression(MDNode::get(VMContext, Elts)); } -DIExpression DIBuilder::createPieceExpression(unsigned OffsetInBytes, - unsigned SizeInBytes) { - int64_t Addr[] = {dwarf::DW_OP_piece, OffsetInBytes, SizeInBytes}; +DIExpression DIBuilder::createExpression(ArrayRef Signed) { + // TODO: Remove the callers of this signed version and delete. + SmallVector Addr(Signed.begin(), Signed.end()); + return createExpression(Addr); +} + +DIExpression DIBuilder::createBitPieceExpression(unsigned OffsetInBits, + unsigned SizeInBits) { + int64_t Addr[] = {dwarf::DW_OP_bit_piece, OffsetInBits, SizeInBits}; return createExpression(Addr); } @@ -932,31 +987,30 @@ DISubprogram DIBuilder::createFunction(DIScopeRef Context, StringRef Name, Flags, isOptimized, Fn, TParams, Decl); } -static DISubprogram -createFunctionHelper(LLVMContext &VMContext, DIDescriptor Context, StringRef Name, - StringRef LinkageName, DIFile File, unsigned LineNo, - DICompositeType Ty, bool isLocalToUnit, bool isDefinition, - unsigned ScopeLine, unsigned Flags, bool isOptimized, - Function *Fn, MDNode *TParams, MDNode *Decl, MDNode *Vars, - std::function)> CreateFunc) { +static DISubprogram createFunctionHelper( + LLVMContext &VMContext, DIDescriptor Context, StringRef Name, + StringRef LinkageName, DIFile File, unsigned LineNo, DICompositeType Ty, + bool isLocalToUnit, bool isDefinition, unsigned ScopeLine, unsigned Flags, + bool isOptimized, Function *Fn, MDNode *TParams, MDNode *Decl, MDNode *Vars, + std::function)> CreateFunc) { assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type && "function types should be subroutines"); - Value *Elts[] = { - HeaderBuilder::get(dwarf::DW_TAG_subprogram) - .concat(Name) - .concat(Name) - .concat(LinkageName) - .concat(LineNo) - .concat(isLocalToUnit) - .concat(isDefinition) - .concat(0) - .concat(0) - .concat(Flags) - .concat(isOptimized) - .concat(ScopeLine) - .get(VMContext), - File.getFileNode(), DIScope(getNonCompileUnitScope(Context)).getRef(), Ty, - nullptr, Fn, TParams, Decl, Vars}; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_subprogram) + .concat(Name) + .concat(Name) + .concat(LinkageName) + .concat(LineNo) + .concat(isLocalToUnit) + .concat(isDefinition) + .concat(0) + .concat(0) + .concat(Flags) + .concat(isOptimized) + .concat(ScopeLine) + .get(VMContext), + File.getFileNode(), + DIScope(getNonCompileUnitScope(Context)).getRef(), Ty, + nullptr, getConstantOrNull(Fn), TParams, Decl, Vars}; DISubprogram S(CreateFunc(Elts)); assert(S.isSubprogram() && @@ -973,17 +1027,18 @@ DISubprogram DIBuilder::createFunction(DIDescriptor Context, StringRef Name, bool isOptimized, Function *Fn, MDNode *TParams, MDNode *Decl) { return createFunctionHelper(VMContext, Context, Name, LinkageName, File, - LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine, - Flags, isOptimized, Fn, TParams, Decl, - MDNode::getTemporary(VMContext, None), - [&] (ArrayRef Elts) -> MDNode *{ - MDNode *Node = MDNode::get(VMContext, Elts); - // Create a named metadata so that we - // do not lose this mdnode. - if (isDefinition) - AllSubprograms.push_back(Node); - return Node; - }); + LineNo, Ty, isLocalToUnit, isDefinition, + ScopeLine, Flags, isOptimized, Fn, TParams, Decl, + MDNode::getTemporary(VMContext, None).release(), + [&](ArrayRef Elts) -> MDNode *{ + MDNode *Node = MDNode::get(VMContext, Elts); + // Create a named metadata so that we + // do not lose this mdnode. + if (isDefinition) + AllSubprograms.push_back(Node); + trackIfUnresolved(Node); + return Node; + }); } DISubprogram @@ -995,11 +1050,11 @@ DIBuilder::createTempFunctionFwdDecl(DIDescriptor Context, StringRef Name, bool isOptimized, Function *Fn, MDNode *TParams, MDNode *Decl) { return createFunctionHelper(VMContext, Context, Name, LinkageName, File, - LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine, - Flags, isOptimized, Fn, TParams, Decl, nullptr, - [&] (ArrayRef Elts) { - return MDNode::getTemporary(VMContext, Elts); - }); + LineNo, Ty, isLocalToUnit, isDefinition, + ScopeLine, Flags, isOptimized, Fn, TParams, Decl, + nullptr, [&](ArrayRef Elts) { + return MDNode::getTemporary(VMContext, Elts).release(); + }); } DISubprogram DIBuilder::createMethod(DIDescriptor Context, StringRef Name, @@ -1015,37 +1070,39 @@ DISubprogram DIBuilder::createMethod(DIDescriptor Context, StringRef Name, assert(getNonCompileUnitScope(Context) && "Methods should have both a Context and a context that isn't " "the compile unit."); - Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_subprogram) - .concat(Name) - .concat(Name) - .concat(LinkageName) - .concat(LineNo) - .concat(isLocalToUnit) - .concat(isDefinition) - .concat(VK) - .concat(VIndex) - .concat(Flags) - .concat(isOptimized) - .concat(LineNo) - // FIXME: Do we want to use different scope/lines? - .get(VMContext), - F.getFileNode(), DIScope(Context).getRef(), Ty, - VTableHolder.getRef(), Fn, TParam, nullptr, nullptr}; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_subprogram) + .concat(Name) + .concat(Name) + .concat(LinkageName) + .concat(LineNo) + .concat(isLocalToUnit) + .concat(isDefinition) + .concat(VK) + .concat(VIndex) + .concat(Flags) + .concat(isOptimized) + .concat(LineNo) + // FIXME: Do we want to use different scope/lines? + .get(VMContext), + F.getFileNode(), DIScope(Context).getRef(), Ty, + VTableHolder.getRef(), getConstantOrNull(Fn), TParam, + nullptr, nullptr}; MDNode *Node = MDNode::get(VMContext, Elts); if (isDefinition) AllSubprograms.push_back(Node); DISubprogram S(Node); assert(S.isSubprogram() && "createMethod should return a valid DISubprogram"); + trackIfUnresolved(S); return S; } DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNo) { - Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_namespace) - .concat(Name) - .concat(LineNo) - .get(VMContext), - File.getFileNode(), getNonCompileUnitScope(Scope)}; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_namespace) + .concat(Name) + .concat(LineNo) + .get(VMContext), + File.getFileNode(), getNonCompileUnitScope(Scope)}; DINameSpace R(MDNode::get(VMContext, Elts)); assert(R.Verify() && "createNameSpace should return a verifiable DINameSpace"); @@ -1055,10 +1112,10 @@ DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name, DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope, DIFile File, unsigned Discriminator) { - Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_lexical_block) - .concat(Discriminator) - .get(VMContext), - File.getFileNode(), Scope}; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_lexical_block) + .concat(Discriminator) + .get(VMContext), + File.getFileNode(), Scope}; DILexicalBlockFile R(MDNode::get(VMContext, Elts)); assert( R.Verify() && @@ -1077,41 +1134,52 @@ DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File, // Defeat MDNode uniquing for lexical blocks by using unique id. static unsigned int unique_id = 0; - Value *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_lexical_block) - .concat(Line) - .concat(Col) - .concat(unique_id++) - .get(VMContext), - File.getFileNode(), getNonCompileUnitScope(Scope)}; + Metadata *Elts[] = {HeaderBuilder::get(dwarf::DW_TAG_lexical_block) + .concat(Line) + .concat(Col) + .concat(unique_id++) + .get(VMContext), + File.getFileNode(), getNonCompileUnitScope(Scope)}; DILexicalBlock R(MDNode::get(VMContext, Elts)); assert(R.Verify() && "createLexicalBlock should return a verifiable DILexicalBlock"); return R; } +static Value *getDbgIntrinsicValueImpl(LLVMContext &VMContext, Value *V) { + assert(V && "no value passed to dbg intrinsic"); + return MetadataAsValue::get(VMContext, ValueAsMetadata::get(V)); +} + Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo, DIExpression Expr, Instruction *InsertBefore) { - assert(Storage && "no storage passed to dbg.declare"); assert(VarInfo.isVariable() && "empty or invalid DIVariable passed to dbg.declare"); if (!DeclareFn) DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare); - Value *Args[] = {MDNode::get(Storage->getContext(), Storage), VarInfo, Expr}; + trackIfUnresolved(VarInfo); + trackIfUnresolved(Expr); + Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage), + MetadataAsValue::get(VMContext, VarInfo), + MetadataAsValue::get(VMContext, Expr)}; return CallInst::Create(DeclareFn, Args, "", InsertBefore); } Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo, DIExpression Expr, BasicBlock *InsertAtEnd) { - assert(Storage && "no storage passed to dbg.declare"); assert(VarInfo.isVariable() && "empty or invalid DIVariable passed to dbg.declare"); if (!DeclareFn) DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare); - Value *Args[] = {MDNode::get(Storage->getContext(), Storage), VarInfo, Expr}; + trackIfUnresolved(VarInfo); + trackIfUnresolved(Expr); + Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage), + MetadataAsValue::get(VMContext, VarInfo), + MetadataAsValue::get(VMContext, Expr)}; // If this block already has a terminator then insert this intrinsic // before the terminator. @@ -1131,9 +1199,12 @@ Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset, if (!ValueFn) ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value); - Value *Args[] = {MDNode::get(V->getContext(), V), - ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset), - VarInfo, Expr}; + trackIfUnresolved(VarInfo); + trackIfUnresolved(Expr); + Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V), + ConstantInt::get(Type::getInt64Ty(VMContext), Offset), + MetadataAsValue::get(VMContext, VarInfo), + MetadataAsValue::get(VMContext, Expr)}; return CallInst::Create(ValueFn, Args, "", InsertBefore); } @@ -1147,8 +1218,43 @@ Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset, if (!ValueFn) ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value); - Value *Args[] = {MDNode::get(V->getContext(), V), - ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset), - VarInfo, Expr}; + trackIfUnresolved(VarInfo); + trackIfUnresolved(Expr); + Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, V), + ConstantInt::get(Type::getInt64Ty(VMContext), Offset), + MetadataAsValue::get(VMContext, VarInfo), + MetadataAsValue::get(VMContext, Expr)}; return CallInst::Create(ValueFn, Args, "", InsertAtEnd); } + +void DIBuilder::replaceVTableHolder(DICompositeType &T, DICompositeType VTableHolder) { + T.setContainingType(VTableHolder); + + // If this didn't create a self-reference, just return. + if (T != VTableHolder) + return; + + // Look for unresolved operands. T will drop RAUW support, orphaning any + // cycles underneath it. + if (T->isResolved()) + for (const MDOperand &O : T->operands()) + if (auto *N = dyn_cast_or_null(O)) + trackIfUnresolved(N); +} + +void DIBuilder::replaceArrays(DICompositeType &T, DIArray Elements, + DIArray TParams) { + T.setArrays(Elements, TParams); + + // If T isn't resolved, there's no problem. + if (!T->isResolved()) + return; + + // If "T" is resolved, it may be due to a self-reference cycle. Track the + // arrays explicitly if they're unresolved, or else the cycles will be + // orphaned. + if (Elements) + trackIfUnresolved(Elements); + if (TParams) + trackIfUnresolved(TParams); +} diff --git a/lib/IR/DataLayout.cpp b/lib/IR/DataLayout.cpp index 8a057f5..9c1dee0 100644 --- a/lib/IR/DataLayout.cpp +++ b/lib/IR/DataLayout.cpp @@ -197,8 +197,10 @@ void DataLayout::reset(StringRef Desc) { static std::pair split(StringRef Str, char Separator) { assert(!Str.empty() && "parse error, string can't be empty here"); std::pair Split = Str.split(Separator); - assert((!Split.second.empty() || Split.first == Str) && - "a trailing separator is not allowed"); + if (Split.second.empty() && Split.first != Str) + report_fatal_error("Trailing separator in datalayout string"); + if (!Split.second.empty() && Split.first.empty()) + report_fatal_error("Expected token before separator in datalayout string"); return Split; } @@ -213,7 +215,8 @@ static unsigned getInt(StringRef R) { /// Convert bits into bytes. Assert if not a byte width multiple. static unsigned inBytes(unsigned Bits) { - assert(Bits % 8 == 0 && "number of bits must be a byte width multiple"); + if (Bits % 8) + report_fatal_error("number of bits must be a byte width multiple"); return Bits / 8; } @@ -247,14 +250,20 @@ void DataLayout::parseSpecifier(StringRef Desc) { case 'p': { // Address space. unsigned AddrSpace = Tok.empty() ? 0 : getInt(Tok); - assert(AddrSpace < 1 << 24 && - "Invalid address space, must be a 24bit integer"); + if (!isUInt<24>(AddrSpace)) + report_fatal_error("Invalid address space, must be a 24bit integer"); // Size. + if (Rest.empty()) + report_fatal_error( + "Missing size specification for pointer in datalayout string"); Split = split(Rest, ':'); unsigned PointerMemSize = inBytes(getInt(Tok)); // ABI alignment. + if (Rest.empty()) + report_fatal_error( + "Missing alignment specification for pointer in datalayout string"); Split = split(Rest, ':'); unsigned PointerABIAlign = inBytes(getInt(Tok)); @@ -285,10 +294,14 @@ void DataLayout::parseSpecifier(StringRef Desc) { // Bit size. unsigned Size = Tok.empty() ? 0 : getInt(Tok); - assert((AlignType != AGGREGATE_ALIGN || Size == 0) && - "These specifications don't have a size"); + if (AlignType == AGGREGATE_ALIGN && Size != 0) + report_fatal_error( + "Sized aggregate specification in datalayout string"); // ABI alignment. + if (Rest.empty()) + report_fatal_error( + "Missing alignment specification in datalayout string"); Split = split(Rest, ':'); unsigned ABIAlign = inBytes(getInt(Tok)); @@ -306,7 +319,9 @@ void DataLayout::parseSpecifier(StringRef Desc) { case 'n': // Native integer types. for (;;) { unsigned Width = getInt(Tok); - assert(Width != 0 && "width must be non-zero"); + if (Width == 0) + report_fatal_error( + "Zero width native integer type in datalayout string"); LegalIntWidths.push_back(Width); if (Rest.empty()) break; @@ -318,11 +333,15 @@ void DataLayout::parseSpecifier(StringRef Desc) { break; } case 'm': - assert(Tok.empty()); - assert(Rest.size() == 1); + if (!Tok.empty()) + report_fatal_error("Unexpected trailing characters after mangling specifier in datalayout string"); + if (Rest.empty()) + report_fatal_error("Expected mangling specifier in datalayout string"); + if (Rest.size() > 1) + report_fatal_error("Unknown mangling specifier in datalayout string"); switch(Rest[0]) { default: - llvm_unreachable("Unknown mangling in datalayout string"); + report_fatal_error("Unknown mangling in datalayout string"); case 'e': ManglingMode = MM_ELF; break; @@ -338,7 +357,7 @@ void DataLayout::parseSpecifier(StringRef Desc) { } break; default: - llvm_unreachable("Unknown specifier in datalayout string"); + report_fatal_error("Unknown specifier in datalayout string"); break; } } @@ -369,9 +388,17 @@ bool DataLayout::operator==(const DataLayout &Other) const { void DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align, unsigned pref_align, uint32_t bit_width) { - assert(abi_align <= pref_align && "Preferred alignment worse than ABI!"); - assert(pref_align < (1 << 16) && "Alignment doesn't fit in bitfield"); - assert(bit_width < (1 << 24) && "Bit width doesn't fit in bitfield"); + if (!isUInt<24>(bit_width)) + report_fatal_error("Invalid bit width, must be a 24bit integer"); + if (!isUInt<16>(abi_align)) + report_fatal_error("Invalid ABI alignment, must be a 16bit integer"); + if (!isUInt<16>(pref_align)) + report_fatal_error("Invalid preferred alignment, must be a 16bit integer"); + + if (pref_align < abi_align) + report_fatal_error( + "Preferred alignment cannot be less than the ABI alignment"); + for (LayoutAlignElem &Elem : Alignments) { if (Elem.AlignType == (unsigned)align_type && Elem.TypeBitWidth == bit_width) { @@ -397,7 +424,10 @@ DataLayout::findPointerLowerBound(uint32_t AddressSpace) { void DataLayout::setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign, unsigned PrefAlign, uint32_t TypeByteWidth) { - assert(ABIAlign <= PrefAlign && "Preferred alignment worse than ABI!"); + if (PrefAlign < ABIAlign) + report_fatal_error( + "Preferred alignment cannot be less than the ABI alignment"); + PointersTy::iterator I = findPointerLowerBound(AddrSpace); if (I == Pointers.end() || I->AddressSpace != AddrSpace) { Pointers.insert(I, PointerAlignElem::get(AddrSpace, ABIAlign, PrefAlign, diff --git a/lib/IR/DebugInfo.cpp b/lib/IR/DebugInfo.cpp index bb5161d..6590661 100644 --- a/lib/IR/DebugInfo.cpp +++ b/lib/IR/DebugInfo.cpp @@ -17,6 +17,7 @@ #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallString.h" +#include "llvm/ADT/StringSwitch.h" #include "llvm/Analysis/ValueTracking.h" #include "llvm/IR/Constants.h" #include "llvm/IR/DIBuilder.h" @@ -36,6 +37,48 @@ using namespace llvm::dwarf; // DIDescriptor //===----------------------------------------------------------------------===// +unsigned DIDescriptor::getFlag(StringRef Flag) { + return StringSwitch(Flag) +#define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME) +#include "llvm/IR/DebugInfoFlags.def" + .Default(0); +} + +const char *DIDescriptor::getFlagString(unsigned Flag) { + switch (Flag) { + default: + return ""; +#define HANDLE_DI_FLAG(ID, NAME) \ + case Flag##NAME: \ + return "DIFlag" #NAME; +#include "llvm/IR/DebugInfoFlags.def" + } +} + +unsigned DIDescriptor::splitFlags(unsigned Flags, + SmallVectorImpl &SplitFlags) { + // Accessibility flags need to be specially handled, since they're packed + // together. + if (unsigned A = Flags & FlagAccessibility) { + if (A == FlagPrivate) + SplitFlags.push_back(FlagPrivate); + else if (A == FlagProtected) + SplitFlags.push_back(FlagProtected); + else + SplitFlags.push_back(FlagPublic); + Flags &= ~A; + } + +#define HANDLE_DI_FLAG(ID, NAME) \ + if (unsigned Bit = Flags & ID) { \ + SplitFlags.push_back(Bit); \ + Flags &= ~Bit; \ + } +#include "llvm/IR/DebugInfoFlags.def" + + return Flags; +} + bool DIDescriptor::Verify() const { return DbgNode && (DIDerivedType(DbgNode).Verify() || @@ -52,7 +95,7 @@ bool DIDescriptor::Verify() const { DIImportedEntity(DbgNode).Verify() || DIExpression(DbgNode).Verify()); } -static Value *getField(const MDNode *DbgNode, unsigned Elt) { +static Metadata *getField(const MDNode *DbgNode, unsigned Elt) { if (!DbgNode || Elt >= DbgNode->getNumOperands()) return nullptr; return DbgNode->getOperand(Elt); @@ -73,25 +116,17 @@ StringRef DIDescriptor::getStringField(unsigned Elt) const { } uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const { - if (!DbgNode) - return 0; - - if (Elt < DbgNode->getNumOperands()) - if (ConstantInt *CI = - dyn_cast_or_null(DbgNode->getOperand(Elt))) + if (auto *C = getConstantField(Elt)) + if (ConstantInt *CI = dyn_cast(C)) return CI->getZExtValue(); return 0; } int64_t DIDescriptor::getInt64Field(unsigned Elt) const { - if (!DbgNode) - return 0; - - if (Elt < DbgNode->getNumOperands()) - if (ConstantInt *CI = - dyn_cast_or_null(DbgNode->getOperand(Elt))) - return CI->getSExtValue(); + if (auto *C = getConstantField(Elt)) + if (ConstantInt *CI = dyn_cast(C)) + return CI->getZExtValue(); return 0; } @@ -102,12 +137,7 @@ DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const { } GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const { - if (!DbgNode) - return nullptr; - - if (Elt < DbgNode->getNumOperands()) - return dyn_cast_or_null(DbgNode->getOperand(Elt)); - return nullptr; + return dyn_cast_or_null(getConstantField(Elt)); } Constant *DIDescriptor::getConstantField(unsigned Elt) const { @@ -115,17 +145,14 @@ Constant *DIDescriptor::getConstantField(unsigned Elt) const { return nullptr; if (Elt < DbgNode->getNumOperands()) - return dyn_cast_or_null(DbgNode->getOperand(Elt)); + if (auto *C = + dyn_cast_or_null(DbgNode->getOperand(Elt))) + return C->getValue(); return nullptr; } Function *DIDescriptor::getFunctionField(unsigned Elt) const { - if (!DbgNode) - return nullptr; - - if (Elt < DbgNode->getNumOperands()) - return dyn_cast_or_null(DbgNode->getOperand(Elt)); - return nullptr; + return dyn_cast_or_null(getConstantField(Elt)); } void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) { @@ -134,7 +161,7 @@ void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) { if (Elt < DbgNode->getNumOperands()) { MDNode *Node = const_cast(DbgNode); - Node->replaceOperandWith(Elt, F); + Node->replaceOperandWith(Elt, F ? ConstantAsMetadata::get(F) : nullptr); } } @@ -163,18 +190,32 @@ uint64_t DIExpression::getElement(unsigned Idx) const { return getHeaderFieldAs(I); } -bool DIExpression::isVariablePiece() const { - return getNumElements() && getElement(0) == dwarf::DW_OP_piece; +bool DIExpression::isBitPiece() const { + unsigned N = getNumElements(); + return N >=3 && getElement(N-3) == dwarf::DW_OP_bit_piece; +} + +uint64_t DIExpression::getBitPieceOffset() const { + assert(isBitPiece() && "not a piece"); + return getElement(getNumElements()-2); +} + +uint64_t DIExpression::getBitPieceSize() const { + assert(isBitPiece() && "not a piece"); + return getElement(getNumElements()-1); } -uint64_t DIExpression::getPieceOffset() const { - assert(isVariablePiece()); - return getElement(1); +DIExpression::iterator DIExpression::begin() const { + return DIExpression::iterator(*this); } -uint64_t DIExpression::getPieceSize() const { - assert(isVariablePiece()); - return getElement(2); +DIExpression::iterator DIExpression::end() const { + return DIExpression::iterator(); +} + +DIExpression::Operand DIExpression::Operand::getNext() const { + iterator it(I); + return *(++it); } //===----------------------------------------------------------------------===// @@ -182,7 +223,7 @@ uint64_t DIExpression::getPieceSize() const { //===----------------------------------------------------------------------===// bool DIDescriptor::isSubroutineType() const { - return isCompositeType() && getTag() == dwarf::DW_TAG_subroutine_type; + return DbgNode && getTag() == dwarf::DW_TAG_subroutine_type; } bool DIDescriptor::isBasicType() const { @@ -256,8 +297,7 @@ bool DIDescriptor::isSubprogram() const { } bool DIDescriptor::isGlobalVariable() const { - return DbgNode && (getTag() == dwarf::DW_TAG_variable || - getTag() == dwarf::DW_TAG_constant); + return DbgNode && getTag() == dwarf::DW_TAG_variable; } bool DIDescriptor::isScope() const { @@ -347,27 +387,23 @@ void DIDescriptor::replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D) { // itself. const MDNode *DN = D; if (DbgNode == DN) { - SmallVector Ops(DbgNode->getNumOperands()); - for (size_t i = 0; i != Ops.size(); ++i) - Ops[i] = DbgNode->getOperand(i); + SmallVector Ops(DbgNode->op_begin(), DbgNode->op_end()); DN = MDNode::get(VMContext, Ops); } - MDNode *Node = const_cast(DbgNode); - const Value *V = cast_or_null(DN); - Node->replaceAllUsesWith(const_cast(V)); + assert(DbgNode->isTemporary() && "Expected temporary node"); + auto *Node = const_cast(DbgNode); + Node->replaceAllUsesWith(const_cast(DN)); MDNode::deleteTemporary(Node); DbgNode = DN; } void DIDescriptor::replaceAllUsesWith(MDNode *D) { - assert(DbgNode && "Trying to replace an unverified type!"); assert(DbgNode != D && "This replacement should always happen"); - MDNode *Node = const_cast(DbgNode); - const MDNode *DN = D; - const Value *V = cast_or_null(DN); - Node->replaceAllUsesWith(const_cast(V)); + assert(DbgNode->isTemporary() && "Expected temporary node"); + auto *Node = const_cast(DbgNode); + Node->replaceAllUsesWith(D); MDNode::deleteTemporary(Node); } @@ -392,21 +428,14 @@ bool DIObjCProperty::Verify() const { } /// \brief Check if a field at position Elt of a MDNode is a MDNode. -/// -/// We currently allow an empty string and an integer. -/// But we don't allow a non-empty string in a MDNode field. static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) { - // FIXME: This function should return true, if the field is null or the field - // is indeed a MDNode: return !Fld || isa(Fld). - Value *Fld = getField(DbgNode, Elt); - if (Fld && isa(Fld) && !cast(Fld)->getString().empty()) - return false; - return true; + Metadata *Fld = getField(DbgNode, Elt); + return !Fld || isa(Fld); } /// \brief Check if a field at position Elt of a MDNode is a MDString. static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) { - Value *Fld = getField(DbgNode, Elt); + Metadata *Fld = getField(DbgNode, Elt); return !Fld || isa(Fld); } @@ -432,7 +461,9 @@ static bool isScopeRef(const Metadata *MD) { return true; if (auto *S = dyn_cast(MD)) return !S->getString().empty(); - return isa(MD); + if (auto *N = dyn_cast(MD)) + return DIScope(N).isScope(); + return false; } /// \brief Check if a field at position Elt of a MDNode can be a ScopeRef. @@ -440,6 +471,17 @@ static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) { return isScopeRef(dyn_cast_or_null(getField(DbgNode, Elt))); } +#ifndef NDEBUG +/// \brief Check if a value can be a DescriptorRef. +static bool isDescriptorRef(const Metadata *MD) { + if (!MD) + return true; + if (auto *S = dyn_cast(MD)) + return !S->getString().empty(); + return isa(MD); +} +#endif + bool DIType::Verify() const { if (!isType()) return false; @@ -533,7 +575,6 @@ bool DISubprogram::Verify() const { // If a DISubprogram has an llvm::Function*, then scope chains from all // instructions within the function should lead to this DISubprogram. if (auto *F = getFunction()) { - LLVMContext &Ctxt = F->getContext(); for (auto &BB : *F) { for (auto &I : BB) { DebugLoc DL = I.getDebugLoc(); @@ -543,15 +584,19 @@ bool DISubprogram::Verify() const { MDNode *Scope = nullptr; MDNode *IA = nullptr; // walk the inlined-at scopes - while (DL.getScopeAndInlinedAt(Scope, IA, F->getContext()), IA) + while ((IA = DL.getInlinedAt())) DL = DebugLoc::getFromDILocation(IA); - DL.getScopeAndInlinedAt(Scope, IA, Ctxt); + DL.getScopeAndInlinedAt(Scope, IA); + if (!Scope) + return false; assert(!IA); while (!DIDescriptor(Scope).isSubprogram()) { DILexicalBlockFile D(Scope); Scope = D.isLexicalBlockFile() ? D.getScope() - : DebugLoc::getFromDILexicalBlock(Scope).getScope(Ctxt); + : DebugLoc::getFromDILexicalBlock(Scope).getScope(); + if (!Scope) + return false; } if (!DISubprogram(Scope).describes(F)) return false; @@ -567,8 +612,8 @@ bool DIGlobalVariable::Verify() const { if (getDisplayName().empty()) return false; - // Make sure context @ field 1 is a ScopeRef. - if (!fieldIsScopeRef(DbgNode, 1)) + // Make sure context @ field 1 is an MDNode. + if (!fieldIsMDNode(DbgNode, 1)) return false; // Make sure that type @ field 3 is a DITypeRef. if (!fieldIsTypeRef(DbgNode, 3)) @@ -609,14 +654,29 @@ bool DIExpression::Verify() const { if (!DbgNode) return true; - return isExpression() && DbgNode->getNumOperands() == 1; + if (!(isExpression() && DbgNode->getNumOperands() == 1)) + return false; + + for (auto Op : *this) + switch (Op) { + case DW_OP_bit_piece: + // Must be the last element of the expression. + return std::distance(Op.getBase(), DIHeaderFieldIterator()) == 3; + case DW_OP_plus: + if (std::distance(Op.getBase(), DIHeaderFieldIterator()) < 2) + return false; + break; + case DW_OP_deref: + break; + default: + // Other operators are not yet supported by the backend. + return false; + } + return true; } bool DILocation::Verify() const { - if (!DbgNode) - return false; - - return DbgNode->getNumOperands() == 4; + return DbgNode && isa(DbgNode); } bool DINameSpace::Verify() const { @@ -678,19 +738,19 @@ MDString *DICompositeType::getIdentifier() const { static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) { for (unsigned i = 0; i != LHS->getNumOperands(); ++i) { // Skip the 'empty' list (that's a single i32 0, rather than truly empty). - if (i == 0 && isa(LHS->getOperand(i))) + if (i == 0 && mdconst::hasa(LHS->getOperand(i))) continue; const MDNode *E = cast(LHS->getOperand(i)); bool found = false; for (unsigned j = 0; !found && j != RHS->getNumOperands(); ++j) - found = E == RHS->getOperand(j); + found = (E == cast(RHS->getOperand(j))); assert(found && "Losing a member during member list replacement"); } } #endif void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) { - TrackingVH N(*this); + TrackingMDNodeRef N(*this); if (Elements) { #ifndef NDEBUG // Check that the new list of members contains all the old members as well. @@ -714,7 +774,7 @@ DIScopeRef DIScope::getRef() const { } void DICompositeType::setContainingType(DICompositeType ContainingType) { - TrackingVH N(*this); + TrackingMDNodeRef N(*this); N->replaceOperandWith(5, ContainingType.getRef()); DbgNode = N; } @@ -748,8 +808,8 @@ DIArray DISubprogram::getVariables() const { return DIArray(getNodeField(DbgNode, 8)); } -Value *DITemplateValueParameter::getValue() const { - return getField(DbgNode, 3); +Metadata *DITemplateValueParameter::getValue() const { + return DbgNode->getOperand(3); } DIScopeRef DIScope::getContext() const { @@ -851,16 +911,12 @@ void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) { DILocation DILocation::copyWithNewScope(LLVMContext &Ctx, DILexicalBlockFile NewScope) { - SmallVector Elts; assert(Verify()); - for (unsigned I = 0; I < DbgNode->getNumOperands(); ++I) { - if (I != 2) - Elts.push_back(DbgNode->getOperand(I)); - else - Elts.push_back(NewScope); - } - MDNode *NewDIL = MDNode::get(Ctx, Elts); - return DILocation(NewDIL); + assert(NewScope && "Expected valid scope"); + + const auto *Old = cast(DbgNode); + return DILocation(MDLocation::get(Ctx, Old->getLine(), Old->getColumn(), + NewScope, Old->getInlinedAt())); } unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) { @@ -875,9 +931,8 @@ DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope, return cleanseInlinedVariable(DV, VMContext); // Insert inlined scope. - SmallVector Elts; - for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I) - Elts.push_back(DV->getOperand(I)); + SmallVector Elts(DV->op_begin(), + DV->op_begin() + DIVariableInlinedAtIndex); Elts.push_back(InlinedScope); DIVariable Inlined(MDNode::get(VMContext, Elts)); @@ -891,9 +946,8 @@ DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) { return DIVariable(DV); // Remove inlined scope. - SmallVector Elts; - for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I) - Elts.push_back(DV->getOperand(I)); + SmallVector Elts(DV->op_begin(), + DV->op_begin() + DIVariableInlinedAtIndex); DIVariable Cleansed(MDNode::get(VMContext, Elts)); assert(Cleansed.Verify() && "Expected to create a DIVariable"); @@ -923,7 +977,7 @@ DISubprogram llvm::getDISubprogram(const Function *F) { if (Inst == BB.end()) continue; DebugLoc DLoc = Inst->getDebugLoc(); - const MDNode *Scope = DLoc.getScopeNode(F->getParent()->getContext()); + const MDNode *Scope = DLoc.getScopeNode(); DISubprogram Subprogram = getDISubprogram(Scope); return Subprogram.describes(F) ? Subprogram : DISubprogram(); } @@ -1005,7 +1059,7 @@ void DebugInfoFinder::processModule(const Module &M) { for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) { DIGlobalVariable DIG(GVs.getElement(i)); if (addGlobalVariable(DIG)) { - processScope(DIG.getContext().resolve(TypeIdentifierMap)); + processScope(DIG.getContext()); processType(DIG.getType().resolve(TypeIdentifierMap)); } } @@ -1106,11 +1160,9 @@ void DebugInfoFinder::processSubprogram(DISubprogram SP) { DIDescriptor Element = TParams.getElement(I); if (Element.isTemplateTypeParameter()) { DITemplateTypeParameter TType(Element); - processScope(TType.getContext().resolve(TypeIdentifierMap)); processType(TType.getType().resolve(TypeIdentifierMap)); } else if (Element.isTemplateValueParameter()) { DITemplateValueParameter TVal(Element); - processScope(TVal.getContext().resolve(TypeIdentifierMap)); processType(TVal.getType().resolve(TypeIdentifierMap)); } } @@ -1401,24 +1453,22 @@ void DIVariable::printInternal(raw_ostream &OS) const { } void DIExpression::printInternal(raw_ostream &OS) const { - for (unsigned I = 0; I < getNumElements(); ++I) { - uint64_t OpCode = getElement(I); - OS << " [" << OperationEncodingString(OpCode); - switch (OpCode) { + for (auto Op : *this) { + OS << " [" << OperationEncodingString(Op); + switch (Op) { case DW_OP_plus: { - OS << " " << getElement(++I); + OS << " " << Op.getArg(1); break; } - case DW_OP_piece: { - unsigned Offset = getElement(++I); - unsigned Size = getElement(++I); - OS << " offset=" << Offset << ", size=" << Size; + case DW_OP_bit_piece: { + OS << " offset=" << Op.getArg(1) << ", size=" << Op.getArg(2); break; } + case DW_OP_deref: + // No arguments. + break; default: - // Else bail out early. This may be a line table entry. - OS << "Unknown]"; - return; + llvm_unreachable("unhandled operation"); } OS << "]"; } @@ -1467,6 +1517,10 @@ void DIVariable::printExtendedName(raw_ostream &OS) const { } } +template <> DIRef::DIRef(const Metadata *V) : Val(V) { + assert(isDescriptorRef(V) && + "DIDescriptorRef should be a MDString or MDNode"); +} template <> DIRef::DIRef(const Metadata *V) : Val(V) { assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode"); } @@ -1475,6 +1529,10 @@ template <> DIRef::DIRef(const Metadata *V) : Val(V) { } template <> +DIDescriptorRef DIDescriptor::getFieldAs(unsigned Elt) const { + return DIDescriptorRef(cast_or_null(getField(DbgNode, Elt))); +} +template <> DIScopeRef DIDescriptor::getFieldAs(unsigned Elt) const { return DIScopeRef(cast_or_null(getField(DbgNode, Elt))); } @@ -1530,10 +1588,10 @@ bool llvm::StripDebugInfo(Module &M) { } unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) { - Value *Val = M.getModuleFlag("Debug Info Version"); - if (!Val) - return 0; - return cast(Val)->getZExtValue(); + if (auto *Val = mdconst::dyn_extract_or_null( + M.getModuleFlag("Debug Info Version"))) + return Val->getZExtValue(); + return 0; } llvm::DenseMap diff --git a/lib/IR/DebugInfoMetadata.cpp b/lib/IR/DebugInfoMetadata.cpp new file mode 100644 index 0000000..89ec1bc --- /dev/null +++ b/lib/IR/DebugInfoMetadata.cpp @@ -0,0 +1,418 @@ +//===- DebugInfoMetadata.cpp - Implement debug info metadata --------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements the debug info Metadata classes. +// +//===----------------------------------------------------------------------===// + +#include "llvm/IR/DebugInfoMetadata.h" +#include "LLVMContextImpl.h" +#include "MetadataImpl.h" +#include "llvm/IR/Function.h" + +using namespace llvm; + +MDLocation::MDLocation(LLVMContext &C, StorageType Storage, unsigned Line, + unsigned Column, ArrayRef MDs) + : MDNode(C, MDLocationKind, Storage, MDs) { + assert((MDs.size() == 1 || MDs.size() == 2) && + "Expected a scope and optional inlined-at"); + + // Set line and column. + assert(Column < (1u << 16) && "Expected 16-bit column"); + + SubclassData32 = Line; + SubclassData16 = Column; +} + +static void adjustColumn(unsigned &Column) { + // Set to unknown on overflow. We only have 16 bits to play with here. + if (Column >= (1u << 16)) + Column = 0; +} + +MDLocation *MDLocation::getImpl(LLVMContext &Context, unsigned Line, + unsigned Column, Metadata *Scope, + Metadata *InlinedAt, StorageType Storage, + bool ShouldCreate) { + // Fixup column. + adjustColumn(Column); + + if (Storage == Uniqued) { + if (auto *N = + getUniqued(Context.pImpl->MDLocations, + MDLocationInfo::KeyTy(Line, Column, Scope, InlinedAt))) + return N; + if (!ShouldCreate) + return nullptr; + } else { + assert(ShouldCreate && "Expected non-uniqued nodes to always be created"); + } + + SmallVector Ops; + Ops.push_back(Scope); + if (InlinedAt) + Ops.push_back(InlinedAt); + return storeImpl(new (Ops.size()) + MDLocation(Context, Storage, Line, Column, Ops), + Storage, Context.pImpl->MDLocations); +} + +static StringRef getString(const MDString *S) { + if (S) + return S->getString(); + return StringRef(); +} + +#ifndef NDEBUG +static bool isCanonical(const MDString *S) { + return !S || !S->getString().empty(); +} +#endif + +GenericDebugNode *GenericDebugNode::getImpl(LLVMContext &Context, unsigned Tag, + MDString *Header, + ArrayRef DwarfOps, + StorageType Storage, + bool ShouldCreate) { + unsigned Hash = 0; + if (Storage == Uniqued) { + GenericDebugNodeInfo::KeyTy Key(Tag, getString(Header), DwarfOps); + if (auto *N = getUniqued(Context.pImpl->GenericDebugNodes, Key)) + return N; + if (!ShouldCreate) + return nullptr; + Hash = Key.getHash(); + } else { + assert(ShouldCreate && "Expected non-uniqued nodes to always be created"); + } + + // Use a nullptr for empty headers. + assert(isCanonical(Header) && "Expected canonical MDString"); + Metadata *PreOps[] = {Header}; + return storeImpl(new (DwarfOps.size() + 1) GenericDebugNode( + Context, Storage, Hash, Tag, PreOps, DwarfOps), + Storage, Context.pImpl->GenericDebugNodes); +} + +void GenericDebugNode::recalculateHash() { + setHash(GenericDebugNodeInfo::KeyTy::calculateHash(this)); +} + +#define UNWRAP_ARGS_IMPL(...) __VA_ARGS__ +#define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS +#define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS) \ + do { \ + if (Storage == Uniqued) { \ + if (auto *N = getUniqued(Context.pImpl->CLASS##s, \ + CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS)))) \ + return N; \ + if (!ShouldCreate) \ + return nullptr; \ + } else { \ + assert(ShouldCreate && \ + "Expected non-uniqued nodes to always be created"); \ + } \ + } while (false) +#define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS) \ + return storeImpl(new (ArrayRef(OPS).size()) \ + CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \ + Storage, Context.pImpl->CLASS##s) +#define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS) \ + return storeImpl(new (0u) CLASS(Context, Storage, UNWRAP_ARGS(ARGS)), \ + Storage, Context.pImpl->CLASS##s) +#define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS) \ + return storeImpl(new (ArrayRef(OPS).size()) \ + CLASS(Context, Storage, OPS), \ + Storage, Context.pImpl->CLASS##s) + +MDSubrange *MDSubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo, + StorageType Storage, bool ShouldCreate) { + DEFINE_GETIMPL_LOOKUP(MDSubrange, (Count, Lo)); + DEFINE_GETIMPL_STORE_NO_OPS(MDSubrange, (Count, Lo)); +} + +MDEnumerator *MDEnumerator::getImpl(LLVMContext &Context, int64_t Value, + MDString *Name, StorageType Storage, + bool ShouldCreate) { + assert(isCanonical(Name) && "Expected canonical MDString"); + DEFINE_GETIMPL_LOOKUP(MDEnumerator, (Value, getString(Name))); + Metadata *Ops[] = {Name}; + DEFINE_GETIMPL_STORE(MDEnumerator, (Value), Ops); +} + +MDBasicType *MDBasicType::getImpl(LLVMContext &Context, unsigned Tag, + MDString *Name, uint64_t SizeInBits, + uint64_t AlignInBits, unsigned Encoding, + StorageType Storage, bool ShouldCreate) { + assert(isCanonical(Name) && "Expected canonical MDString"); + DEFINE_GETIMPL_LOOKUP( + MDBasicType, (Tag, getString(Name), SizeInBits, AlignInBits, Encoding)); + Metadata *Ops[] = {nullptr, nullptr, Name}; + DEFINE_GETIMPL_STORE(MDBasicType, (Tag, SizeInBits, AlignInBits, Encoding), + Ops); +} + +MDDerivedType *MDDerivedType::getImpl( + LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File, + unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits, + uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags, + Metadata *ExtraData, StorageType Storage, bool ShouldCreate) { + assert(isCanonical(Name) && "Expected canonical MDString"); + DEFINE_GETIMPL_LOOKUP(MDDerivedType, (Tag, getString(Name), File, Line, Scope, + BaseType, SizeInBits, AlignInBits, + OffsetInBits, Flags, ExtraData)); + Metadata *Ops[] = {File, Scope, Name, BaseType, ExtraData}; + DEFINE_GETIMPL_STORE( + MDDerivedType, (Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags), + Ops); +} + +MDCompositeType *MDCompositeType::getImpl( + LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File, + unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits, + uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags, + Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder, + Metadata *TemplateParams, MDString *Identifier, StorageType Storage, + bool ShouldCreate) { + assert(isCanonical(Name) && "Expected canonical MDString"); + DEFINE_GETIMPL_LOOKUP(MDCompositeType, + (Tag, getString(Name), File, Line, Scope, BaseType, + SizeInBits, AlignInBits, OffsetInBits, Flags, Elements, + RuntimeLang, VTableHolder, TemplateParams, + getString(Identifier))); + Metadata *Ops[] = {File, Scope, Name, BaseType, + Elements, VTableHolder, TemplateParams, Identifier}; + DEFINE_GETIMPL_STORE(MDCompositeType, (Tag, Line, RuntimeLang, SizeInBits, + AlignInBits, OffsetInBits, Flags), + Ops); +} + +MDSubroutineType *MDSubroutineType::getImpl(LLVMContext &Context, + unsigned Flags, Metadata *TypeArray, + StorageType Storage, + bool ShouldCreate) { + DEFINE_GETIMPL_LOOKUP(MDSubroutineType, (Flags, TypeArray)); + Metadata *Ops[] = {nullptr, nullptr, nullptr, nullptr, + TypeArray, nullptr, nullptr, nullptr}; + DEFINE_GETIMPL_STORE(MDSubroutineType, (Flags), Ops); +} + +MDFile *MDFile::getImpl(LLVMContext &Context, MDString *Filename, + MDString *Directory, StorageType Storage, + bool ShouldCreate) { + assert(isCanonical(Filename) && "Expected canonical MDString"); + assert(isCanonical(Directory) && "Expected canonical MDString"); + DEFINE_GETIMPL_LOOKUP(MDFile, (getString(Filename), getString(Directory))); + Metadata *Ops[] = {Filename, Directory}; + DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(MDFile, Ops); +} + +MDCompileUnit *MDCompileUnit::getImpl( + LLVMContext &Context, unsigned SourceLanguage, Metadata *File, + MDString *Producer, bool IsOptimized, MDString *Flags, + unsigned RuntimeVersion, MDString *SplitDebugFilename, + unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes, + Metadata *Subprograms, Metadata *GlobalVariables, + Metadata *ImportedEntities, StorageType Storage, bool ShouldCreate) { + assert(isCanonical(Producer) && "Expected canonical MDString"); + assert(isCanonical(Flags) && "Expected canonical MDString"); + assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString"); + DEFINE_GETIMPL_LOOKUP( + MDCompileUnit, + (SourceLanguage, File, getString(Producer), IsOptimized, getString(Flags), + RuntimeVersion, getString(SplitDebugFilename), EmissionKind, EnumTypes, + RetainedTypes, Subprograms, GlobalVariables, ImportedEntities)); + Metadata *Ops[] = {File, Producer, Flags, SplitDebugFilename, EnumTypes, + RetainedTypes, Subprograms, GlobalVariables, + ImportedEntities}; + DEFINE_GETIMPL_STORE( + MDCompileUnit, + (SourceLanguage, IsOptimized, RuntimeVersion, EmissionKind), Ops); +} + +MDSubprogram *MDSubprogram::getImpl( + LLVMContext &Context, Metadata *Scope, MDString *Name, + MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type, + bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine, + Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex, + unsigned Flags, bool IsOptimized, Metadata *Function, + Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables, + StorageType Storage, bool ShouldCreate) { + assert(isCanonical(Name) && "Expected canonical MDString"); + assert(isCanonical(LinkageName) && "Expected canonical MDString"); + DEFINE_GETIMPL_LOOKUP(MDSubprogram, + (Scope, getString(Name), getString(LinkageName), File, + Line, Type, IsLocalToUnit, IsDefinition, ScopeLine, + ContainingType, Virtuality, VirtualIndex, Flags, + IsOptimized, Function, TemplateParams, Declaration, + Variables)); + Metadata *Ops[] = {File, Scope, Name, Name, + LinkageName, Type, ContainingType, Function, + TemplateParams, Declaration, Variables}; + DEFINE_GETIMPL_STORE(MDSubprogram, + (Line, ScopeLine, Virtuality, VirtualIndex, Flags, + IsLocalToUnit, IsDefinition, IsOptimized), + Ops); +} + +void MDSubprogram::replaceFunction(Function *F) { + replaceFunction(F ? ConstantAsMetadata::get(F) + : static_cast(nullptr)); +} + +MDLexicalBlock *MDLexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope, + Metadata *File, unsigned Line, + unsigned Column, StorageType Storage, + bool ShouldCreate) { + DEFINE_GETIMPL_LOOKUP(MDLexicalBlock, (Scope, File, Line, Column)); + Metadata *Ops[] = {File, Scope}; + DEFINE_GETIMPL_STORE(MDLexicalBlock, (Line, Column), Ops); +} + +MDLexicalBlockFile *MDLexicalBlockFile::getImpl(LLVMContext &Context, + Metadata *Scope, Metadata *File, + unsigned Discriminator, + StorageType Storage, + bool ShouldCreate) { + DEFINE_GETIMPL_LOOKUP(MDLexicalBlockFile, (Scope, File, Discriminator)); + Metadata *Ops[] = {File, Scope}; + DEFINE_GETIMPL_STORE(MDLexicalBlockFile, (Discriminator), Ops); +} + +MDNamespace *MDNamespace::getImpl(LLVMContext &Context, Metadata *Scope, + Metadata *File, MDString *Name, unsigned Line, + StorageType Storage, bool ShouldCreate) { + assert(isCanonical(Name) && "Expected canonical MDString"); + DEFINE_GETIMPL_LOOKUP(MDNamespace, (Scope, File, getString(Name), Line)); + Metadata *Ops[] = {File, Scope, Name}; + DEFINE_GETIMPL_STORE(MDNamespace, (Line), Ops); +} + +MDTemplateTypeParameter *MDTemplateTypeParameter::getImpl(LLVMContext &Context, + MDString *Name, + Metadata *Type, + StorageType Storage, + bool ShouldCreate) { + assert(isCanonical(Name) && "Expected canonical MDString"); + DEFINE_GETIMPL_LOOKUP(MDTemplateTypeParameter, (getString(Name), Type)); + Metadata *Ops[] = {Name, Type}; + DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(MDTemplateTypeParameter, Ops); +} + +MDTemplateValueParameter *MDTemplateValueParameter::getImpl( + LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type, + Metadata *Value, StorageType Storage, bool ShouldCreate) { + assert(isCanonical(Name) && "Expected canonical MDString"); + DEFINE_GETIMPL_LOOKUP(MDTemplateValueParameter, + (Tag, getString(Name), Type, Value)); + Metadata *Ops[] = {Name, Type, Value}; + DEFINE_GETIMPL_STORE(MDTemplateValueParameter, (Tag), Ops); +} + +MDGlobalVariable * +MDGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name, + MDString *LinkageName, Metadata *File, unsigned Line, + Metadata *Type, bool IsLocalToUnit, bool IsDefinition, + Metadata *Variable, + Metadata *StaticDataMemberDeclaration, + StorageType Storage, bool ShouldCreate) { + assert(isCanonical(Name) && "Expected canonical MDString"); + assert(isCanonical(LinkageName) && "Expected canonical MDString"); + DEFINE_GETIMPL_LOOKUP(MDGlobalVariable, + (Scope, getString(Name), getString(LinkageName), File, + Line, Type, IsLocalToUnit, IsDefinition, Variable, + StaticDataMemberDeclaration)); + Metadata *Ops[] = {Scope, Name, File, Type, + Name, LinkageName, Variable, StaticDataMemberDeclaration}; + DEFINE_GETIMPL_STORE(MDGlobalVariable, (Line, IsLocalToUnit, IsDefinition), + Ops); +} + +MDLocalVariable *MDLocalVariable::getImpl( + LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name, + Metadata *File, unsigned Line, Metadata *Type, unsigned Arg, unsigned Flags, + Metadata *InlinedAt, StorageType Storage, bool ShouldCreate) { + // Truncate Arg to 8 bits. + // + // FIXME: This is gross (and should be changed to an assert or removed), but + // it matches historical behaviour for now. + Arg &= (1u << 8) - 1; + + assert(isCanonical(Name) && "Expected canonical MDString"); + DEFINE_GETIMPL_LOOKUP(MDLocalVariable, (Tag, Scope, getString(Name), File, + Line, Type, Arg, Flags, InlinedAt)); + Metadata *Ops[] = {Scope, Name, File, Type, InlinedAt}; + DEFINE_GETIMPL_STORE(MDLocalVariable, (Tag, Line, Arg, Flags), Ops); +} + +MDExpression *MDExpression::getImpl(LLVMContext &Context, + ArrayRef Elements, + StorageType Storage, bool ShouldCreate) { + DEFINE_GETIMPL_LOOKUP(MDExpression, (Elements)); + DEFINE_GETIMPL_STORE_NO_OPS(MDExpression, (Elements)); +} + +unsigned MDExpression::ExprOperand::getSize() const { + switch (getOp()) { + case dwarf::DW_OP_bit_piece: + return 3; + case dwarf::DW_OP_plus: + return 2; + default: + return 1; + } +} + +bool MDExpression::isValid() const { + for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) { + // Check that there's space for the operand. + if (I->get() + I->getSize() > E->get()) + return false; + + // Check that the operand is valid. + switch (I->getOp()) { + default: + return false; + case dwarf::DW_OP_bit_piece: + // Piece expressions must be at the end. + return I->get() + I->getSize() == E->get(); + case dwarf::DW_OP_plus: + case dwarf::DW_OP_deref: + break; + } + } + return true; +} + +MDObjCProperty *MDObjCProperty::getImpl( + LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line, + MDString *GetterName, MDString *SetterName, unsigned Attributes, + Metadata *Type, StorageType Storage, bool ShouldCreate) { + assert(isCanonical(Name) && "Expected canonical MDString"); + assert(isCanonical(GetterName) && "Expected canonical MDString"); + assert(isCanonical(SetterName) && "Expected canonical MDString"); + DEFINE_GETIMPL_LOOKUP(MDObjCProperty, + (getString(Name), File, Line, getString(GetterName), + getString(SetterName), Attributes, Type)); + Metadata *Ops[] = {Name, File, GetterName, SetterName, Type}; + DEFINE_GETIMPL_STORE(MDObjCProperty, (Line, Attributes), Ops); +} + +MDImportedEntity *MDImportedEntity::getImpl(LLVMContext &Context, unsigned Tag, + Metadata *Scope, Metadata *Entity, + unsigned Line, MDString *Name, + StorageType Storage, + bool ShouldCreate) { + assert(isCanonical(Name) && "Expected canonical MDString"); + DEFINE_GETIMPL_LOOKUP(MDImportedEntity, + (Tag, Scope, Entity, Line, getString(Name))); + Metadata *Ops[] = {Scope, Entity, Name}; + DEFINE_GETIMPL_STORE(MDImportedEntity, (Tag, Line), Ops); +} diff --git a/lib/IR/DebugLoc.cpp b/lib/IR/DebugLoc.cpp index 718da85..e1bf795 100644 --- a/lib/IR/DebugLoc.cpp +++ b/lib/IR/DebugLoc.cpp @@ -17,67 +17,29 @@ using namespace llvm; // DebugLoc Implementation //===----------------------------------------------------------------------===// -MDNode *DebugLoc::getScope(const LLVMContext &Ctx) const { - if (ScopeIdx == 0) return nullptr; - - if (ScopeIdx > 0) { - // Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at - // position specified. - assert(unsigned(ScopeIdx) <= Ctx.pImpl->ScopeRecords.size() && - "Invalid ScopeIdx!"); - return Ctx.pImpl->ScopeRecords[ScopeIdx-1].get(); - } - - // Otherwise, the index is in the ScopeInlinedAtRecords array. - assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() && - "Invalid ScopeIdx"); - return Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].first.get(); -} +unsigned DebugLoc::getLine() const { return DILocation(Loc).getLineNumber(); } +unsigned DebugLoc::getCol() const { return DILocation(Loc).getColumnNumber(); } + +MDNode *DebugLoc::getScope() const { return DILocation(Loc).getScope(); } -MDNode *DebugLoc::getInlinedAt(const LLVMContext &Ctx) const { - // Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at - // position specified. Zero is invalid. - if (ScopeIdx >= 0) return nullptr; - - // Otherwise, the index is in the ScopeInlinedAtRecords array. - assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() && - "Invalid ScopeIdx"); - return Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].second.get(); +MDNode *DebugLoc::getInlinedAt() const { + return DILocation(Loc).getOrigLocation(); } /// Return both the Scope and the InlinedAt values. -void DebugLoc::getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA, - const LLVMContext &Ctx) const { - if (ScopeIdx == 0) { - Scope = IA = nullptr; - return; - } - - if (ScopeIdx > 0) { - // Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at - // position specified. - assert(unsigned(ScopeIdx) <= Ctx.pImpl->ScopeRecords.size() && - "Invalid ScopeIdx!"); - Scope = Ctx.pImpl->ScopeRecords[ScopeIdx-1].get(); - IA = nullptr; - return; - } - - // Otherwise, the index is in the ScopeInlinedAtRecords array. - assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() && - "Invalid ScopeIdx"); - Scope = Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].first.get(); - IA = Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].second.get(); +void DebugLoc::getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA) const { + Scope = getScope(); + IA = getInlinedAt(); } -MDNode *DebugLoc::getScopeNode(const LLVMContext &Ctx) const { - if (MDNode *InlinedAt = getInlinedAt(Ctx)) - return DebugLoc::getFromDILocation(InlinedAt).getScopeNode(Ctx); - return getScope(Ctx); +MDNode *DebugLoc::getScopeNode() const { + if (MDNode *InlinedAt = getInlinedAt()) + return DebugLoc::getFromDILocation(InlinedAt).getScopeNode(); + return getScope(); } -DebugLoc DebugLoc::getFnDebugLoc(const LLVMContext &Ctx) const { - const MDNode *Scope = getScopeNode(Ctx); +DebugLoc DebugLoc::getFnDebugLoc() const { + const MDNode *Scope = getScopeNode(); DISubprogram SP = getDISubprogram(Scope); if (SP.isSubprogram()) return DebugLoc::get(SP.getScopeLineNumber(), 0, SP); @@ -87,53 +49,23 @@ DebugLoc DebugLoc::getFnDebugLoc(const LLVMContext &Ctx) const { DebugLoc DebugLoc::get(unsigned Line, unsigned Col, MDNode *Scope, MDNode *InlinedAt) { - DebugLoc Result; - // If no scope is available, this is an unknown location. - if (!Scope) return Result; + if (!Scope) + return DebugLoc(); - // Saturate line and col to "unknown". - if (Col > 255) Col = 0; - if (Line >= (1 << 24)) Line = 0; - Result.LineCol = Line | (Col << 24); - - LLVMContext &Ctx = Scope->getContext(); - - // If there is no inlined-at location, use the ScopeRecords array. - if (!InlinedAt) - Result.ScopeIdx = Ctx.pImpl->getOrAddScopeRecordIdxEntry(Scope, 0); - else - Result.ScopeIdx = Ctx.pImpl->getOrAddScopeInlinedAtIdxEntry(Scope, - InlinedAt, 0); - - return Result; + return getFromDILocation( + MDLocation::get(Scope->getContext(), Line, Col, Scope, InlinedAt)); } /// getAsMDNode - This method converts the compressed DebugLoc node into a /// DILocation-compatible MDNode. -MDNode *DebugLoc::getAsMDNode(const LLVMContext &Ctx) const { - if (isUnknown()) return nullptr; - - MDNode *Scope, *IA; - getScopeAndInlinedAt(Scope, IA, Ctx); - assert(Scope && "If scope is null, this should be isUnknown()"); - - LLVMContext &Ctx2 = Scope->getContext(); - Type *Int32 = Type::getInt32Ty(Ctx2); - Value *Elts[] = { - ConstantInt::get(Int32, getLine()), ConstantInt::get(Int32, getCol()), - Scope, IA - }; - return MDNode::get(Ctx2, Elts); -} +MDNode *DebugLoc::getAsMDNode() const { return Loc; } /// getFromDILocation - Translate the DILocation quad into a DebugLoc. DebugLoc DebugLoc::getFromDILocation(MDNode *N) { - DILocation Loc(N); - MDNode *Scope = Loc.getScope(); - if (!Scope) return DebugLoc(); - return get(Loc.getLineNumber(), Loc.getColumnNumber(), Scope, - Loc.getOrigLocation()); + DebugLoc Loc; + Loc.Loc.reset(N); + return Loc; } /// getFromDILexicalBlock - Translate the DILexicalBlock into a DebugLoc. @@ -145,26 +77,26 @@ DebugLoc DebugLoc::getFromDILexicalBlock(MDNode *N) { nullptr); } -void DebugLoc::dump(const LLVMContext &Ctx) const { +void DebugLoc::dump() const { #ifndef NDEBUG if (!isUnknown()) { dbgs() << getLine(); if (getCol() != 0) dbgs() << ',' << getCol(); - DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt(Ctx)); + DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt()); if (!InlinedAtDL.isUnknown()) { dbgs() << " @ "; - InlinedAtDL.dump(Ctx); + InlinedAtDL.dump(); } else dbgs() << "\n"; } #endif } -void DebugLoc::print(const LLVMContext &Ctx, raw_ostream &OS) const { +void DebugLoc::print(raw_ostream &OS) const { if (!isUnknown()) { // Print source line info. - DIScope Scope(getScope(Ctx)); + DIScope Scope(getScope()); assert((!Scope || Scope.isScope()) && "Scope of a DebugLoc should be null or a DIScope."); if (Scope) @@ -174,179 +106,11 @@ void DebugLoc::print(const LLVMContext &Ctx, raw_ostream &OS) const { OS << ':' << getLine(); if (getCol() != 0) OS << ':' << getCol(); - DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt(Ctx)); + DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt()); if (!InlinedAtDL.isUnknown()) { OS << " @[ "; - InlinedAtDL.print(Ctx, OS); + InlinedAtDL.print(OS); OS << " ]"; } } } - -//===----------------------------------------------------------------------===// -// DenseMap specialization -//===----------------------------------------------------------------------===// - -unsigned DenseMapInfo::getHashValue(const DebugLoc &Key) { - return static_cast(hash_combine(Key.LineCol, Key.ScopeIdx)); -} - -//===----------------------------------------------------------------------===// -// LLVMContextImpl Implementation -//===----------------------------------------------------------------------===// - -int LLVMContextImpl::getOrAddScopeRecordIdxEntry(MDNode *Scope, - int ExistingIdx) { - // If we already have an entry for this scope, return it. - int &Idx = ScopeRecordIdx[Scope]; - if (Idx) return Idx; - - // If we don't have an entry, but ExistingIdx is specified, use it. - if (ExistingIdx) - return Idx = ExistingIdx; - - // Otherwise add a new entry. - - // Start out ScopeRecords with a minimal reasonable size to avoid - // excessive reallocation starting out. - if (ScopeRecords.empty()) - ScopeRecords.reserve(128); - - // Index is biased by 1 for index. - Idx = ScopeRecords.size()+1; - ScopeRecords.push_back(DebugRecVH(Scope, this, Idx)); - return Idx; -} - -int LLVMContextImpl::getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA, - int ExistingIdx) { - // If we already have an entry, return it. - int &Idx = ScopeInlinedAtIdx[std::make_pair(Scope, IA)]; - if (Idx) return Idx; - - // If we don't have an entry, but ExistingIdx is specified, use it. - if (ExistingIdx) - return Idx = ExistingIdx; - - // Start out ScopeInlinedAtRecords with a minimal reasonable size to avoid - // excessive reallocation starting out. - if (ScopeInlinedAtRecords.empty()) - ScopeInlinedAtRecords.reserve(128); - - // Index is biased by 1 and negated. - Idx = -ScopeInlinedAtRecords.size()-1; - ScopeInlinedAtRecords.push_back(std::make_pair(DebugRecVH(Scope, this, Idx), - DebugRecVH(IA, this, Idx))); - return Idx; -} - - -//===----------------------------------------------------------------------===// -// DebugRecVH Implementation -//===----------------------------------------------------------------------===// - -/// deleted - The MDNode this is pointing to got deleted, so this pointer needs -/// to drop to null and we need remove our entry from the DenseMap. -void DebugRecVH::deleted() { - // If this is a non-canonical reference, just drop the value to null, we know - // it doesn't have a map entry. - if (Idx == 0) { - setValPtr(nullptr); - return; - } - - MDNode *Cur = get(); - - // If the index is positive, it is an entry in ScopeRecords. - if (Idx > 0) { - assert(Ctx->ScopeRecordIdx[Cur] == Idx && "Mapping out of date!"); - Ctx->ScopeRecordIdx.erase(Cur); - // Reset this VH to null and we're done. - setValPtr(nullptr); - Idx = 0; - return; - } - - // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it - // is the scope or the inlined-at record entry. - assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size()); - std::pair &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1]; - assert((this == &Entry.first || this == &Entry.second) && - "Mapping out of date!"); - - MDNode *OldScope = Entry.first.get(); - MDNode *OldInlinedAt = Entry.second.get(); - assert(OldScope && OldInlinedAt && - "Entry should be non-canonical if either val dropped to null"); - - // Otherwise, we do have an entry in it, nuke it and we're done. - assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&& - "Mapping out of date"); - Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt)); - - // Reset this VH to null. Drop both 'Idx' values to null to indicate that - // we're in non-canonical form now. - setValPtr(nullptr); - Entry.first.Idx = Entry.second.Idx = 0; -} - -void DebugRecVH::allUsesReplacedWith(Value *NewVa) { - // If being replaced with a non-mdnode value (e.g. undef) handle this as if - // the mdnode got deleted. - MDNode *NewVal = dyn_cast(NewVa); - if (!NewVal) return deleted(); - - // If this is a non-canonical reference, just change it, we know it already - // doesn't have a map entry. - if (Idx == 0) { - setValPtr(NewVa); - return; - } - - MDNode *OldVal = get(); - assert(OldVal != NewVa && "Node replaced with self?"); - - // If the index is positive, it is an entry in ScopeRecords. - if (Idx > 0) { - assert(Ctx->ScopeRecordIdx[OldVal] == Idx && "Mapping out of date!"); - Ctx->ScopeRecordIdx.erase(OldVal); - setValPtr(NewVal); - - int NewEntry = Ctx->getOrAddScopeRecordIdxEntry(NewVal, Idx); - - // If NewVal already has an entry, this becomes a non-canonical reference, - // just drop Idx to 0 to signify this. - if (NewEntry != Idx) - Idx = 0; - return; - } - - // Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it - // is the scope or the inlined-at record entry. - assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size()); - std::pair &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1]; - assert((this == &Entry.first || this == &Entry.second) && - "Mapping out of date!"); - - MDNode *OldScope = Entry.first.get(); - MDNode *OldInlinedAt = Entry.second.get(); - assert(OldScope && OldInlinedAt && - "Entry should be non-canonical if either val dropped to null"); - - // Otherwise, we do have an entry in it, nuke it and we're done. - assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&& - "Mapping out of date"); - Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt)); - - // Reset this VH to the new value. - setValPtr(NewVal); - - int NewIdx = Ctx->getOrAddScopeInlinedAtIdxEntry(Entry.first.get(), - Entry.second.get(), Idx); - // If NewVal already has an entry, this becomes a non-canonical reference, - // just drop Idx to 0 to signify this. - if (NewIdx != Idx) { - std::pair &Entry=Ctx->ScopeInlinedAtRecords[-Idx-1]; - Entry.first.Idx = Entry.second.Idx = 0; - } -} diff --git a/lib/IR/DiagnosticInfo.cpp b/lib/IR/DiagnosticInfo.cpp index 37cce2b..cfb699a 100644 --- a/lib/IR/DiagnosticInfo.cpp +++ b/lib/IR/DiagnosticInfo.cpp @@ -98,7 +98,8 @@ DiagnosticInfoInlineAsm::DiagnosticInfoInlineAsm(const Instruction &I, Instr(&I) { if (const MDNode *SrcLoc = I.getMetadata("srcloc")) { if (SrcLoc->getNumOperands() != 0) - if (const ConstantInt *CI = dyn_cast(SrcLoc->getOperand(0))) + if (const auto *CI = + mdconst::dyn_extract(SrcLoc->getOperand(0))) LocCookie = CI->getZExtValue(); } } diff --git a/lib/IR/Dominators.cpp b/lib/IR/Dominators.cpp index d6649d6..9b6ff1e 100644 --- a/lib/IR/Dominators.cpp +++ b/lib/IR/Dominators.cpp @@ -20,6 +20,7 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/IR/CFG.h" #include "llvm/IR/Instructions.h" +#include "llvm/IR/PassManager.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/Debug.h" @@ -298,10 +299,45 @@ void DominatorTree::verifyDomTree() const { } //===----------------------------------------------------------------------===// +// DominatorTreeAnalysis and related pass implementations +//===----------------------------------------------------------------------===// +// +// This implements the DominatorTreeAnalysis which is used with the new pass +// manager. It also implements some methods from utility passes. +// +//===----------------------------------------------------------------------===// + +DominatorTree DominatorTreeAnalysis::run(Function &F) { + DominatorTree DT; + DT.recalculate(F); + return DT; +} + +char DominatorTreeAnalysis::PassID; + +DominatorTreePrinterPass::DominatorTreePrinterPass(raw_ostream &OS) : OS(OS) {} + +PreservedAnalyses DominatorTreePrinterPass::run(Function &F, + FunctionAnalysisManager *AM) { + OS << "DominatorTree for function: " << F.getName() << "\n"; + AM->getResult(F).print(OS); + + return PreservedAnalyses::all(); +} + +PreservedAnalyses DominatorTreeVerifierPass::run(Function &F, + FunctionAnalysisManager *AM) { + AM->getResult(F).verifyDomTree(); + + return PreservedAnalyses::all(); +} + +//===----------------------------------------------------------------------===// // DominatorTreeWrapperPass Implementation //===----------------------------------------------------------------------===// // -// The implementation details of the wrapper pass that holds a DominatorTree. +// The implementation details of the wrapper pass that holds a DominatorTree +// suitable for use with the legacy pass manager. // //===----------------------------------------------------------------------===// diff --git a/lib/IR/Function.cpp b/lib/IR/Function.cpp index 32b2ec5..33e1526 100644 --- a/lib/IR/Function.cpp +++ b/lib/IR/Function.cpp @@ -23,7 +23,6 @@ #include "llvm/IR/InstIterator.h" #include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/LLVMContext.h" -#include "llvm/IR/LeakDetector.h" #include "llvm/IR/Module.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/RWMutex.h" @@ -46,20 +45,13 @@ Argument::Argument(Type *Ty, const Twine &Name, Function *Par) : Value(Ty, Value::ArgumentVal) { Parent = nullptr; - // Make sure that we get added to a function - LeakDetector::addGarbageObject(this); - if (Par) Par->getArgumentList().push_back(this); setName(Name); } void Argument::setParent(Function *parent) { - if (getParent()) - LeakDetector::addGarbageObject(this); Parent = parent; - if (getParent()) - LeakDetector::removeGarbageObject(this); } /// getArgNo - Return the index of this formal argument in its containing @@ -260,9 +252,6 @@ Function::Function(FunctionType *Ty, LinkageTypes Linkage, const Twine &name, if (Ty->getNumParams()) setValueSubclassData(1); // Set the "has lazy arguments" bit. - // Make sure that we get added to a function - LeakDetector::addGarbageObject(this); - if (ParentModule) ParentModule->getFunctionList().push_back(this); @@ -298,7 +287,7 @@ void Function::BuildLazyArguments() const { // Clear the lazy arguments bit. unsigned SDC = getSubclassDataFromValue(); - const_cast(this)->setValueSubclassData(SDC &= ~1); + const_cast(this)->setValueSubclassData(SDC &= ~(1<<0)); } size_t Function::arg_size() const { @@ -309,11 +298,7 @@ bool Function::arg_empty() const { } void Function::setParent(Module *parent) { - if (getParent()) - LeakDetector::addGarbageObject(this); Parent = parent; - if (getParent()) - LeakDetector::removeGarbageObject(this); } // dropAllReferences() - This function causes all the subinstructions to "let @@ -335,8 +320,9 @@ void Function::dropAllReferences() { while (!BasicBlocks.empty()) BasicBlocks.begin()->eraseFromParent(); - // Prefix data is stored in a side table. + // Prefix and prologue data are stored in a side table. setPrefixData(nullptr); + setPrologueData(nullptr); } void Function::addAttribute(unsigned i, Attribute::AttrKind attr) { @@ -357,6 +343,12 @@ void Function::removeAttributes(unsigned i, AttributeSet attrs) { setAttributes(PAL); } +void Function::addDereferenceableAttr(unsigned i, uint64_t Bytes) { + AttributeSet PAL = getAttributes(); + PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes); + setAttributes(PAL); +} + // Maintain the GC name for each function in an on-the-side table. This saves // allocating an additional word in Function for programs which do not use GC // (i.e., most programs) at the cost of increased overhead for clients which do @@ -416,6 +408,10 @@ void Function::copyAttributesFrom(const GlobalValue *Src) { setPrefixData(SrcF->getPrefixData()); else setPrefixData(nullptr); + if (SrcF->hasPrologueData()) + setPrologueData(SrcF->getPrologueData()); + else + setPrologueData(nullptr); } /// getIntrinsicID - This method returns the ID number of the specified @@ -456,7 +452,19 @@ unsigned Function::lookupIntrinsicID() const { } /// Returns a stable mangling for the type specified for use in the name -/// mangling scheme used by 'any' types in intrinsic signatures. +/// mangling scheme used by 'any' types in intrinsic signatures. The mangling +/// of named types is simply their name. Manglings for unnamed types consist +/// of a prefix ('p' for pointers, 'a' for arrays, 'f_' for functions) +/// combined with the mangling of their component types. A vararg function +/// type will have a suffix of 'vararg'. Since function types can contain +/// other function types, we close a function type mangling with suffix 'f' +/// which can't be confused with it's prefix. This ensures we don't have +/// collisions between two unrelated function types. Otherwise, you might +/// parse ffXX as f(fXX) or f(fX)X. (X is a placeholder for any other type.) +/// Manglings of integers, floats, and vectors ('i', 'f', and 'v' prefix in most +/// cases) fall back to the MVT codepath, where they could be mangled to +/// 'x86mmx', for example; matching on derived types is not sufficient to mangle +/// everything. static std::string getMangledTypeStr(Type* Ty) { std::string Result; if (PointerType* PTyp = dyn_cast(Ty)) { @@ -476,7 +484,8 @@ static std::string getMangledTypeStr(Type* Ty) { Result += getMangledTypeStr(FT->getParamType(i)); if (FT->isVarArg()) Result += "vararg"; - Result += "f"; //ensure distinguishable + // Ensure nested function types are distinguishable. + Result += "f"; } else if (Ty) Result += EVT::getEVT(Ty).getEVTString(); return Result; @@ -537,7 +546,10 @@ enum IIT_Info { IIT_ANYPTR = 26, IIT_V1 = 27, IIT_VARARG = 28, - IIT_HALF_VEC_ARG = 29 + IIT_HALF_VEC_ARG = 29, + IIT_SAME_VEC_WIDTH_ARG = 30, + IIT_PTR_TO_ARG = 31, + IIT_VEC_OF_PTRS_TO_ELT = 32 }; @@ -645,6 +657,24 @@ static void DecodeIITType(unsigned &NextElt, ArrayRef Infos, ArgInfo)); return; } + case IIT_SAME_VEC_WIDTH_ARG: { + unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); + OutputTable.push_back(IITDescriptor::get(IITDescriptor::SameVecWidthArgument, + ArgInfo)); + return; + } + case IIT_PTR_TO_ARG: { + unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); + OutputTable.push_back(IITDescriptor::get(IITDescriptor::PtrToArgument, + ArgInfo)); + return; + } + case IIT_VEC_OF_PTRS_TO_ELT: { + unsigned ArgInfo = (NextElt == Infos.size() ? 0 : Infos[NextElt++]); + OutputTable.push_back(IITDescriptor::get(IITDescriptor::VecOfPtrsToElt, + ArgInfo)); + return; + } case IIT_EMPTYSTRUCT: OutputTable.push_back(IITDescriptor::get(IITDescriptor::Struct, 0)); return; @@ -752,7 +782,28 @@ static Type *DecodeFixedType(ArrayRef &Infos, case IITDescriptor::HalfVecArgument: return VectorType::getHalfElementsVectorType(cast( Tys[D.getArgumentNumber()])); + case IITDescriptor::SameVecWidthArgument: { + Type *EltTy = DecodeFixedType(Infos, Tys, Context); + Type *Ty = Tys[D.getArgumentNumber()]; + if (VectorType *VTy = dyn_cast(Ty)) { + return VectorType::get(EltTy, VTy->getNumElements()); + } + llvm_unreachable("unhandled"); + } + case IITDescriptor::PtrToArgument: { + Type *Ty = Tys[D.getArgumentNumber()]; + return PointerType::getUnqual(Ty); + } + case IITDescriptor::VecOfPtrsToElt: { + Type *Ty = Tys[D.getArgumentNumber()]; + VectorType *VTy = dyn_cast(Ty); + if (!VTy) + llvm_unreachable("Expected an argument of Vector Type"); + Type *EltTy = VTy->getVectorElementType(); + return VectorType::get(PointerType::getUnqual(EltTy), + VTy->getNumElements()); } + } llvm_unreachable("unhandled"); } @@ -871,11 +922,40 @@ void Function::setPrefixData(Constant *PrefixData) { PDHolder->setOperand(0, PrefixData); else PDHolder = ReturnInst::Create(getContext(), PrefixData); - SCData |= 2; + SCData |= (1<<1); } else { delete PDHolder; PDMap.erase(this); - SCData &= ~2; + SCData &= ~(1<<1); } setValueSubclassData(SCData); } + +Constant *Function::getPrologueData() const { + assert(hasPrologueData()); + const LLVMContextImpl::PrologueDataMapTy &SOMap = + getContext().pImpl->PrologueDataMap; + assert(SOMap.find(this) != SOMap.end()); + return cast(SOMap.find(this)->second->getReturnValue()); +} + +void Function::setPrologueData(Constant *PrologueData) { + if (!PrologueData && !hasPrologueData()) + return; + + unsigned PDData = getSubclassDataFromValue(); + LLVMContextImpl::PrologueDataMapTy &PDMap = getContext().pImpl->PrologueDataMap; + ReturnInst *&PDHolder = PDMap[this]; + if (PrologueData) { + if (PDHolder) + PDHolder->setOperand(0, PrologueData); + else + PDHolder = ReturnInst::Create(getContext(), PrologueData); + PDData |= (1<<2); + } else { + delete PDHolder; + PDMap.erase(this); + PDData &= ~(1<<2); + } + setValueSubclassData(PDData); +} diff --git a/lib/IR/GCOV.cpp b/lib/IR/GCOV.cpp index 245c500..08f44e0 100644 --- a/lib/IR/GCOV.cpp +++ b/lib/IR/GCOV.cpp @@ -28,12 +28,16 @@ using namespace llvm; /// readGCNO - Read GCNO buffer. bool GCOVFile::readGCNO(GCOVBuffer &Buffer) { - if (!Buffer.readGCNOFormat()) return false; - if (!Buffer.readGCOVVersion(Version)) return false; + if (!Buffer.readGCNOFormat()) + return false; + if (!Buffer.readGCOVVersion(Version)) + return false; - if (!Buffer.readInt(Checksum)) return false; + if (!Buffer.readInt(Checksum)) + return false; while (true) { - if (!Buffer.readFunctionTag()) break; + if (!Buffer.readFunctionTag()) + break; auto GFun = make_unique(*this); if (!GFun->readGCNO(Buffer, Version)) return false; @@ -48,19 +52,22 @@ bool GCOVFile::readGCNO(GCOVBuffer &Buffer) { /// called after readGCNO(). bool GCOVFile::readGCDA(GCOVBuffer &Buffer) { assert(GCNOInitialized && "readGCDA() can only be called after readGCNO()"); - if (!Buffer.readGCDAFormat()) return false; + if (!Buffer.readGCDAFormat()) + return false; GCOV::GCOVVersion GCDAVersion; - if (!Buffer.readGCOVVersion(GCDAVersion)) return false; + if (!Buffer.readGCOVVersion(GCDAVersion)) + return false; if (Version != GCDAVersion) { errs() << "GCOV versions do not match.\n"; return false; } uint32_t GCDAChecksum; - if (!Buffer.readInt(GCDAChecksum)) return false; + if (!Buffer.readInt(GCDAChecksum)) + return false; if (Checksum != GCDAChecksum) { - errs() << "File checksums do not match: " << Checksum << " != " - << GCDAChecksum << ".\n"; + errs() << "File checksums do not match: " << Checksum + << " != " << GCDAChecksum << ".\n"; return false; } for (size_t i = 0, e = Functions.size(); i < e; ++i) { @@ -74,15 +81,20 @@ bool GCOVFile::readGCDA(GCOVBuffer &Buffer) { if (Buffer.readObjectTag()) { uint32_t Length; uint32_t Dummy; - if (!Buffer.readInt(Length)) return false; - if (!Buffer.readInt(Dummy)) return false; // checksum - if (!Buffer.readInt(Dummy)) return false; // num - if (!Buffer.readInt(RunCount)) return false; - Buffer.advanceCursor(Length-3); + if (!Buffer.readInt(Length)) + return false; + if (!Buffer.readInt(Dummy)) + return false; // checksum + if (!Buffer.readInt(Dummy)) + return false; // num + if (!Buffer.readInt(RunCount)) + return false; + Buffer.advanceCursor(Length - 3); } while (Buffer.readProgramTag()) { uint32_t Length; - if (!Buffer.readInt(Length)) return false; + if (!Buffer.readInt(Length)) + return false; Buffer.advanceCursor(Length); ++ProgramCount; } @@ -112,21 +124,28 @@ void GCOVFile::collectLineCounts(FileInfo &FI) { /// occurs. bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVVersion Version) { uint32_t Dummy; - if (!Buff.readInt(Dummy)) return false; // Function header length - if (!Buff.readInt(Ident)) return false; - if (!Buff.readInt(Checksum)) return false; + if (!Buff.readInt(Dummy)) + return false; // Function header length + if (!Buff.readInt(Ident)) + return false; + if (!Buff.readInt(Checksum)) + return false; if (Version != GCOV::V402) { uint32_t CfgChecksum; - if (!Buff.readInt(CfgChecksum)) return false; + if (!Buff.readInt(CfgChecksum)) + return false; if (Parent.getChecksum() != CfgChecksum) { errs() << "File checksums do not match: " << Parent.getChecksum() << " != " << CfgChecksum << " in (" << Name << ").\n"; return false; } } - if (!Buff.readString(Name)) return false; - if (!Buff.readString(Filename)) return false; - if (!Buff.readInt(LineNumber)) return false; + if (!Buff.readString(Name)) + return false; + if (!Buff.readString(Filename)) + return false; + if (!Buff.readInt(LineNumber)) + return false; // read blocks. if (!Buff.readBlockTag()) { @@ -134,19 +153,23 @@ bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVVersion Version) { return false; } uint32_t BlockCount; - if (!Buff.readInt(BlockCount)) return false; + if (!Buff.readInt(BlockCount)) + return false; for (uint32_t i = 0, e = BlockCount; i != e; ++i) { - if (!Buff.readInt(Dummy)) return false; // Block flags; + if (!Buff.readInt(Dummy)) + return false; // Block flags; Blocks.push_back(make_unique(*this, i)); } // read edges. while (Buff.readEdgeTag()) { uint32_t EdgeCount; - if (!Buff.readInt(EdgeCount)) return false; + if (!Buff.readInt(EdgeCount)) + return false; EdgeCount = (EdgeCount - 1) / 2; uint32_t BlockNo; - if (!Buff.readInt(BlockNo)) return false; + if (!Buff.readInt(BlockNo)) + return false; if (BlockNo >= BlockCount) { errs() << "Unexpected block number: " << BlockNo << " (in " << Name << ").\n"; @@ -154,12 +177,14 @@ bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVVersion Version) { } for (uint32_t i = 0, e = EdgeCount; i != e; ++i) { uint32_t Dst; - if (!Buff.readInt(Dst)) return false; + if (!Buff.readInt(Dst)) + return false; Edges.push_back(make_unique(*Blocks[BlockNo], *Blocks[Dst])); GCOVEdge *Edge = Edges.back().get(); Blocks[BlockNo]->addDstEdge(Edge); Blocks[Dst]->addSrcEdge(Edge); - if (!Buff.readInt(Dummy)) return false; // Edge flag + if (!Buff.readInt(Dummy)) + return false; // Edge flag } } @@ -167,11 +192,13 @@ bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVVersion Version) { while (Buff.readLineTag()) { uint32_t LineTableLength; // Read the length of this line table. - if (!Buff.readInt(LineTableLength)) return false; - uint32_t EndPos = Buff.getCursor() + LineTableLength*4; + if (!Buff.readInt(LineTableLength)) + return false; + uint32_t EndPos = Buff.getCursor() + LineTableLength * 4; uint32_t BlockNo; // Read the block number this table is associated with. - if (!Buff.readInt(BlockNo)) return false; + if (!Buff.readInt(BlockNo)) + return false; if (BlockNo >= BlockCount) { errs() << "Unexpected block number: " << BlockNo << " (in " << Name << ").\n"; @@ -180,13 +207,15 @@ bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVVersion Version) { GCOVBlock &Block = *Blocks[BlockNo]; // Read the word that pads the beginning of the line table. This may be a // flag of some sort, but seems to always be zero. - if (!Buff.readInt(Dummy)) return false; + if (!Buff.readInt(Dummy)) + return false; // Line information starts here and continues up until the last word. if (Buff.getCursor() != (EndPos - sizeof(uint32_t))) { StringRef F; // Read the source file name. - if (!Buff.readString(F)) return false; + if (!Buff.readString(F)) + return false; if (Filename != F) { errs() << "Multiple sources for a single basic block: " << Filename << " != " << F << " (in " << Name << ").\n"; @@ -195,17 +224,21 @@ bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVVersion Version) { // Read lines up to, but not including, the null terminator. while (Buff.getCursor() < (EndPos - 2 * sizeof(uint32_t))) { uint32_t Line; - if (!Buff.readInt(Line)) return false; + if (!Buff.readInt(Line)) + return false; // Line 0 means this instruction was injected by the compiler. Skip it. - if (!Line) continue; + if (!Line) + continue; Block.addLine(Line); } // Read the null terminator. - if (!Buff.readInt(Dummy)) return false; + if (!Buff.readInt(Dummy)) + return false; } // The last word is either a flag or padding, it isn't clear which. Skip // over it. - if (!Buff.readInt(Dummy)) return false; + if (!Buff.readInt(Dummy)) + return false; } return true; } @@ -214,27 +247,31 @@ bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVVersion Version) { /// occurs. bool GCOVFunction::readGCDA(GCOVBuffer &Buff, GCOV::GCOVVersion Version) { uint32_t Dummy; - if (!Buff.readInt(Dummy)) return false; // Function header length + if (!Buff.readInt(Dummy)) + return false; // Function header length uint32_t GCDAIdent; - if (!Buff.readInt(GCDAIdent)) return false; + if (!Buff.readInt(GCDAIdent)) + return false; if (Ident != GCDAIdent) { - errs() << "Function identifiers do not match: " << Ident << " != " - << GCDAIdent << " (in " << Name << ").\n"; + errs() << "Function identifiers do not match: " << Ident + << " != " << GCDAIdent << " (in " << Name << ").\n"; return false; } uint32_t GCDAChecksum; - if (!Buff.readInt(GCDAChecksum)) return false; + if (!Buff.readInt(GCDAChecksum)) + return false; if (Checksum != GCDAChecksum) { - errs() << "Function checksums do not match: " << Checksum << " != " - << GCDAChecksum << " (in " << Name << ").\n"; + errs() << "Function checksums do not match: " << Checksum + << " != " << GCDAChecksum << " (in " << Name << ").\n"; return false; } uint32_t CfgChecksum; if (Version != GCOV::V402) { - if (!Buff.readInt(CfgChecksum)) return false; + if (!Buff.readInt(CfgChecksum)) + return false; if (Parent.getChecksum() != CfgChecksum) { errs() << "File checksums do not match: " << Parent.getChecksum() << " != " << CfgChecksum << " (in " << Name << ").\n"; @@ -243,7 +280,8 @@ bool GCOVFunction::readGCDA(GCOVBuffer &Buff, GCOV::GCOVVersion Version) { } StringRef GCDAName; - if (!Buff.readString(GCDAName)) return false; + if (!Buff.readString(GCDAName)) + return false; if (Name != GCDAName) { errs() << "Function names do not match: " << Name << " != " << GCDAName << ".\n"; @@ -256,26 +294,28 @@ bool GCOVFunction::readGCDA(GCOVBuffer &Buff, GCOV::GCOVVersion Version) { } uint32_t Count; - if (!Buff.readInt(Count)) return false; + if (!Buff.readInt(Count)) + return false; Count /= 2; // This for loop adds the counts for each block. A second nested loop is // required to combine the edge counts that are contained in the GCDA file. for (uint32_t BlockNo = 0; Count > 0; ++BlockNo) { // The last block is always reserved for exit block - if (BlockNo >= Blocks.size()-1) { + if (BlockNo >= Blocks.size() - 1) { errs() << "Unexpected number of edges (in " << Name << ").\n"; return false; } GCOVBlock &Block = *Blocks[BlockNo]; for (size_t EdgeNo = 0, End = Block.getNumDstEdges(); EdgeNo < End; - ++EdgeNo) { + ++EdgeNo) { if (Count == 0) { errs() << "Unexpected number of edges (in " << Name << ").\n"; return false; } uint64_t ArcCount; - if (!Buff.readInt64(ArcCount)) return false; + if (!Buff.readInt64(ArcCount)) + return false; Block.addCount(EdgeNo, ArcCount); --Count; } @@ -349,9 +389,8 @@ void GCOVBlock::sortDstEdges() { /// collectLineCounts - Collect line counts. This must be used after /// reading .gcno and .gcda files. void GCOVBlock::collectLineCounts(FileInfo &FI) { - for (SmallVectorImpl::iterator I = Lines.begin(), - E = Lines.end(); I != E; ++I) - FI.addBlockLine(Parent.getFilename(), *I, this); + for (uint32_t N : Lines) + FI.addBlockLine(Parent.getFilename(), N, this); } /// dump - Dump GCOVBlock content to dbgs() for debugging purposes. @@ -359,25 +398,20 @@ void GCOVBlock::dump() const { dbgs() << "Block : " << Number << " Counter : " << Counter << "\n"; if (!SrcEdges.empty()) { dbgs() << "\tSource Edges : "; - for (EdgeIterator I = SrcEdges.begin(), E = SrcEdges.end(); I != E; ++I) { - const GCOVEdge *Edge = *I; + for (const GCOVEdge *Edge : SrcEdges) dbgs() << Edge->Src.Number << " (" << Edge->Count << "), "; - } dbgs() << "\n"; } if (!DstEdges.empty()) { dbgs() << "\tDestination Edges : "; - for (EdgeIterator I = DstEdges.begin(), E = DstEdges.end(); I != E; ++I) { - const GCOVEdge *Edge = *I; + for (const GCOVEdge *Edge : DstEdges) dbgs() << Edge->Dst.Number << " (" << Edge->Count << "), "; - } dbgs() << "\n"; } if (!Lines.empty()) { dbgs() << "\tLines : "; - for (SmallVectorImpl::const_iterator I = Lines.begin(), - E = Lines.end(); I != E; ++I) - dbgs() << (*I) << ","; + for (uint32_t N : Lines) + dbgs() << (N) << ","; dbgs() << "\n"; } } @@ -389,7 +423,7 @@ void GCOVBlock::dump() const { static uint32_t safeDiv(uint64_t Numerator, uint64_t Divisor) { if (!Numerator) return 0; - return Numerator/Divisor; + return Numerator / Divisor; } // This custom division function mimics gcov's branch ouputs: @@ -401,7 +435,7 @@ static uint32_t branchDiv(uint64_t Numerator, uint64_t Divisor) { if (Numerator == Divisor) return 100; - uint8_t Res = (Numerator*100+Divisor/2) / Divisor; + uint8_t Res = (Numerator * 100 + Divisor / 2) / Divisor; if (Res == 0) return 1; if (Res == 100) @@ -410,9 +444,8 @@ static uint32_t branchDiv(uint64_t Numerator, uint64_t Divisor) { } struct formatBranchInfo { - formatBranchInfo(const GCOVOptions &Options, uint64_t Count, - uint64_t Total) : - Options(Options), Count(Count), Total(Total) {} + formatBranchInfo(const GCOVOptions &Options, uint64_t Count, uint64_t Total) + : Options(Options), Count(Count), Total(Total) {} void print(raw_ostream &OS) const { if (!Total) @@ -437,6 +470,7 @@ namespace { class LineConsumer { std::unique_ptr Buffer; StringRef Remaining; + public: LineConsumer(StringRef Filename) { ErrorOr> BufferOrErr = @@ -508,8 +542,7 @@ std::string FileInfo::getCoveragePath(StringRef Filename, if (Options.LongFileNames && !Filename.equals(MainFilename)) CoveragePath = mangleCoveragePath(MainFilename, Options.PreservePaths) + "##"; - CoveragePath += - mangleCoveragePath(Filename, Options.PreservePaths) + ".gcov"; + CoveragePath += mangleCoveragePath(Filename, Options.PreservePaths) + ".gcov"; return CoveragePath; } @@ -529,47 +562,44 @@ FileInfo::openCoveragePath(StringRef CoveragePath) { } /// print - Print source files with collected line count information. -void FileInfo::print(StringRef MainFilename, StringRef GCNOFile, - StringRef GCDAFile) { - for (StringMap::const_iterator I = LineInfo.begin(), - E = LineInfo.end(); I != E; ++I) { - StringRef Filename = I->first(); +void FileInfo::print(raw_ostream &InfoOS, StringRef MainFilename, + StringRef GCNOFile, StringRef GCDAFile) { + for (const auto &LI : LineInfo) { + StringRef Filename = LI.first(); auto AllLines = LineConsumer(Filename); std::string CoveragePath = getCoveragePath(Filename, MainFilename); - std::unique_ptr S = openCoveragePath(CoveragePath); - raw_ostream &OS = *S; + std::unique_ptr CovStream = openCoveragePath(CoveragePath); + raw_ostream &CovOS = *CovStream; - OS << " -: 0:Source:" << Filename << "\n"; - OS << " -: 0:Graph:" << GCNOFile << "\n"; - OS << " -: 0:Data:" << GCDAFile << "\n"; - OS << " -: 0:Runs:" << RunCount << "\n"; - OS << " -: 0:Programs:" << ProgramCount << "\n"; + CovOS << " -: 0:Source:" << Filename << "\n"; + CovOS << " -: 0:Graph:" << GCNOFile << "\n"; + CovOS << " -: 0:Data:" << GCDAFile << "\n"; + CovOS << " -: 0:Runs:" << RunCount << "\n"; + CovOS << " -: 0:Programs:" << ProgramCount << "\n"; - const LineData &Line = I->second; + const LineData &Line = LI.second; GCOVCoverage FileCoverage(Filename); - for (uint32_t LineIndex = 0; - LineIndex < Line.LastLine || !AllLines.empty(); ++LineIndex) { + for (uint32_t LineIndex = 0; LineIndex < Line.LastLine || !AllLines.empty(); + ++LineIndex) { if (Options.BranchInfo) { FunctionLines::const_iterator FuncsIt = Line.Functions.find(LineIndex); if (FuncsIt != Line.Functions.end()) - printFunctionSummary(OS, FuncsIt->second); + printFunctionSummary(CovOS, FuncsIt->second); } BlockLines::const_iterator BlocksIt = Line.Blocks.find(LineIndex); if (BlocksIt == Line.Blocks.end()) { // No basic blocks are on this line. Not an executable line of code. - OS << " -:"; - AllLines.printNext(OS, LineIndex + 1); + CovOS << " -:"; + AllLines.printNext(CovOS, LineIndex + 1); } else { const BlockVector &Blocks = BlocksIt->second; // Add up the block counts to form line counts. DenseMap LineExecs; uint64_t LineCount = 0; - for (BlockVector::const_iterator I = Blocks.begin(), E = Blocks.end(); - I != E; ++I) { - const GCOVBlock *Block = *I; + for (const GCOVBlock *Block : Blocks) { if (Options.AllBlocks) { // Only take the highest block count for that line. uint64_t BlockCount = Block->getCount(); @@ -593,8 +623,8 @@ void FileInfo::print(StringRef MainFilename, StringRef GCNOFile, // one of the blocks are executed. const GCOVFunction *Function = &Block->getParent(); if (FuncCoverages.find(Function) == FuncCoverages.end()) { - std::pair - KeyValue(Function, GCOVCoverage(Function->getName())); + std::pair KeyValue( + Function, GCOVCoverage(Function->getName())); FuncCoverages.insert(KeyValue); } GCOVCoverage &FuncCoverage = FuncCoverages.find(Function)->second; @@ -615,32 +645,30 @@ void FileInfo::print(StringRef MainFilename, StringRef GCNOFile, } if (LineCount == 0) - OS << " #####:"; + CovOS << " #####:"; else { - OS << format("%9" PRIu64 ":", LineCount); + CovOS << format("%9" PRIu64 ":", LineCount); ++FileCoverage.LinesExec; } ++FileCoverage.LogicalLines; - AllLines.printNext(OS, LineIndex + 1); + AllLines.printNext(CovOS, LineIndex + 1); uint32_t BlockNo = 0; uint32_t EdgeNo = 0; - for (BlockVector::const_iterator I = Blocks.begin(), E = Blocks.end(); - I != E; ++I) { - const GCOVBlock *Block = *I; - + for (const GCOVBlock *Block : Blocks) { // Only print block and branch information at the end of the block. - if (Block->getLastLine() != LineIndex+1) + if (Block->getLastLine() != LineIndex + 1) continue; if (Options.AllBlocks) - printBlockInfo(OS, *Block, LineIndex, BlockNo); + printBlockInfo(CovOS, *Block, LineIndex, BlockNo); if (Options.BranchInfo) { size_t NumEdges = Block->getNumDstEdges(); if (NumEdges > 1) - printBranchInfo(OS, *Block, FileCoverage, EdgeNo); + printBranchInfo(CovOS, *Block, FileCoverage, EdgeNo); else if (Options.UncondBranch && NumEdges == 1) - printUncondBranchInfo(OS, EdgeNo, (*Block->dst_begin())->Count); + printUncondBranchInfo(CovOS, EdgeNo, + (*Block->dst_begin())->Count); } } } @@ -650,30 +678,25 @@ void FileInfo::print(StringRef MainFilename, StringRef GCNOFile, // FIXME: There is no way to detect calls given current instrumentation. if (Options.FuncCoverage) - printFuncCoverage(); - printFileCoverage(); + printFuncCoverage(InfoOS); + printFileCoverage(InfoOS); return; } /// printFunctionSummary - Print function and block summary. void FileInfo::printFunctionSummary(raw_ostream &OS, const FunctionVector &Funcs) const { - for (FunctionVector::const_iterator I = Funcs.begin(), E = Funcs.end(); - I != E; ++I) { - const GCOVFunction *Func = *I; + for (const GCOVFunction *Func : Funcs) { uint64_t EntryCount = Func->getEntryCount(); uint32_t BlocksExec = 0; - for (GCOVFunction::BlockIterator I = Func->block_begin(), - E = Func->block_end(); I != E; ++I) { - const GCOVBlock &Block = **I; + for (const GCOVBlock &Block : Func->blocks()) if (Block.getNumDstEdges() && Block.getCount()) - ++BlocksExec; - } + ++BlocksExec; OS << "function " << Func->getName() << " called " << EntryCount - << " returned " << safeDiv(Func->getExitCount()*100, EntryCount) + << " returned " << safeDiv(Func->getExitCount() * 100, EntryCount) << "% blocks executed " - << safeDiv(BlocksExec*100, Func->getNumBlocks()-1) << "%\n"; + << safeDiv(BlocksExec * 100, Func->getNumBlocks() - 1) << "%\n"; } } @@ -684,7 +707,7 @@ void FileInfo::printBlockInfo(raw_ostream &OS, const GCOVBlock &Block, OS << " $$$$$:"; else OS << format("%9" PRIu64 ":", Block.getCount()); - OS << format("%5u-block %2u\n", LineIndex+1, BlockNo++); + OS << format("%5u-block %2u\n", LineIndex + 1, BlockNo++); } /// printBranchInfo - Print conditional branch probabilities. @@ -692,29 +715,29 @@ void FileInfo::printBranchInfo(raw_ostream &OS, const GCOVBlock &Block, GCOVCoverage &Coverage, uint32_t &EdgeNo) { SmallVector BranchCounts; uint64_t TotalCounts = 0; - for (GCOVBlock::EdgeIterator I = Block.dst_begin(), E = Block.dst_end(); - I != E; ++I) { - const GCOVEdge *Edge = *I; + for (const GCOVEdge *Edge : Block.dsts()) { BranchCounts.push_back(Edge->Count); TotalCounts += Edge->Count; - if (Block.getCount()) ++Coverage.BranchesExec; - if (Edge->Count) ++Coverage.BranchesTaken; + if (Block.getCount()) + ++Coverage.BranchesExec; + if (Edge->Count) + ++Coverage.BranchesTaken; ++Coverage.Branches; if (Options.FuncCoverage) { const GCOVFunction *Function = &Block.getParent(); GCOVCoverage &FuncCoverage = FuncCoverages.find(Function)->second; - if (Block.getCount()) ++FuncCoverage.BranchesExec; - if (Edge->Count) ++FuncCoverage.BranchesTaken; + if (Block.getCount()) + ++FuncCoverage.BranchesExec; + if (Edge->Count) + ++FuncCoverage.BranchesTaken; ++FuncCoverage.Branches; } } - for (SmallVectorImpl::const_iterator I = BranchCounts.begin(), - E = BranchCounts.end(); I != E; ++I) { + for (uint64_t N : BranchCounts) OS << format("branch %2u ", EdgeNo++) - << formatBranchInfo(Options, *I, TotalCounts) << "\n"; - } + << formatBranchInfo(Options, N, TotalCounts) << "\n"; } /// printUncondBranchInfo - Print unconditional branch probabilities. @@ -726,46 +749,45 @@ void FileInfo::printUncondBranchInfo(raw_ostream &OS, uint32_t &EdgeNo, // printCoverage - Print generic coverage info used by both printFuncCoverage // and printFileCoverage. -void FileInfo::printCoverage(const GCOVCoverage &Coverage) const { - outs() << format("Lines executed:%.2f%% of %u\n", - double(Coverage.LinesExec)*100/Coverage.LogicalLines, - Coverage.LogicalLines); +void FileInfo::printCoverage(raw_ostream &OS, + const GCOVCoverage &Coverage) const { + OS << format("Lines executed:%.2f%% of %u\n", + double(Coverage.LinesExec) * 100 / Coverage.LogicalLines, + Coverage.LogicalLines); if (Options.BranchInfo) { if (Coverage.Branches) { - outs() << format("Branches executed:%.2f%% of %u\n", - double(Coverage.BranchesExec)*100/Coverage.Branches, - Coverage.Branches); - outs() << format("Taken at least once:%.2f%% of %u\n", - double(Coverage.BranchesTaken)*100/Coverage.Branches, - Coverage.Branches); + OS << format("Branches executed:%.2f%% of %u\n", + double(Coverage.BranchesExec) * 100 / Coverage.Branches, + Coverage.Branches); + OS << format("Taken at least once:%.2f%% of %u\n", + double(Coverage.BranchesTaken) * 100 / Coverage.Branches, + Coverage.Branches); } else { - outs() << "No branches\n"; + OS << "No branches\n"; } - outs() << "No calls\n"; // to be consistent with gcov + OS << "No calls\n"; // to be consistent with gcov } } // printFuncCoverage - Print per-function coverage info. -void FileInfo::printFuncCoverage() const { - for (FuncCoverageMap::const_iterator I = FuncCoverages.begin(), - E = FuncCoverages.end(); I != E; ++I) { - const GCOVCoverage &Coverage = I->second; - outs() << "Function '" << Coverage.Name << "'\n"; - printCoverage(Coverage); - outs() << "\n"; +void FileInfo::printFuncCoverage(raw_ostream &OS) const { + for (const auto &FC : FuncCoverages) { + const GCOVCoverage &Coverage = FC.second; + OS << "Function '" << Coverage.Name << "'\n"; + printCoverage(OS, Coverage); + OS << "\n"; } } // printFileCoverage - Print per-file coverage info. -void FileInfo::printFileCoverage() const { - for (FileCoverageList::const_iterator I = FileCoverages.begin(), - E = FileCoverages.end(); I != E; ++I) { - const std::string &Filename = I->first; - const GCOVCoverage &Coverage = I->second; - outs() << "File '" << Coverage.Name << "'\n"; - printCoverage(Coverage); +void FileInfo::printFileCoverage(raw_ostream &OS) const { + for (const auto &FC : FileCoverages) { + const std::string &Filename = FC.first; + const GCOVCoverage &Coverage = FC.second; + OS << "File '" << Coverage.Name << "'\n"; + printCoverage(OS, Coverage); if (!Options.NoOutput) - outs() << Coverage.Name << ":creating '" << Filename << "'\n"; - outs() << "\n"; + OS << Coverage.Name << ":creating '" << Filename << "'\n"; + OS << "\n"; } } diff --git a/lib/IR/Globals.cpp b/lib/IR/Globals.cpp index e181d62..54197d9 100644 --- a/lib/IR/Globals.cpp +++ b/lib/IR/Globals.cpp @@ -18,7 +18,6 @@ #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/GlobalAlias.h" #include "llvm/IR/GlobalVariable.h" -#include "llvm/IR/LeakDetector.h" #include "llvm/IR/Module.h" #include "llvm/IR/Operator.h" #include "llvm/Support/ErrorHandling.h" @@ -159,8 +158,6 @@ GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link, "Initializer should be the same type as the GlobalVariable!"); Op<0>() = InitVal; } - - LeakDetector::addGarbageObject(this); } GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant, @@ -180,8 +177,6 @@ GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant, Op<0>() = InitVal; } - LeakDetector::addGarbageObject(this); - if (Before) Before->getParent()->getGlobalList().insert(Before, this); else @@ -189,11 +184,7 @@ GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant, } void GlobalVariable::setParent(Module *parent) { - if (getParent()) - LeakDetector::addGarbageObject(this); Parent = parent; - if (getParent()) - LeakDetector::removeGarbageObject(this); } void GlobalVariable::removeFromParent() { @@ -259,7 +250,6 @@ GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link, Module *ParentModule) : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) { - LeakDetector::addGarbageObject(this); Op<0>() = Aliasee; if (ParentModule) @@ -296,11 +286,7 @@ GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) { } void GlobalAlias::setParent(Module *parent) { - if (getParent()) - LeakDetector::addGarbageObject(this); Parent = parent; - if (getParent()) - LeakDetector::removeGarbageObject(this); } void GlobalAlias::removeFromParent() { diff --git a/lib/IR/IRBuilder.cpp b/lib/IR/IRBuilder.cpp index a4c5d97..90303b2 100644 --- a/lib/IR/IRBuilder.cpp +++ b/lib/IR/IRBuilder.cpp @@ -53,8 +53,9 @@ Value *IRBuilderBase::getCastedInt8PtrValue(Value *Ptr) { } static CallInst *createCallHelper(Value *Callee, ArrayRef Ops, - IRBuilderBase *Builder) { - CallInst *CI = CallInst::Create(Callee, Ops, ""); + IRBuilderBase *Builder, + const Twine& Name="") { + CallInst *CI = CallInst::Create(Callee, Ops, Name); Builder->GetInsertBlock()->getInstList().insert(Builder->GetInsertPoint(),CI); Builder->SetInstDebugLocation(CI); return CI; @@ -183,3 +184,117 @@ CallInst *IRBuilderBase::CreateAssumption(Value *Cond) { return createCallHelper(FnAssume, Ops, this); } +/// Create a call to a Masked Load intrinsic. +/// Ptr - the base pointer for the load +/// Align - alignment of the source location +/// Mask - an vector of booleans which indicates what vector lanes should +/// be accessed in memory +/// PassThru - a pass-through value that is used to fill the masked-off lanes +/// of the result +/// Name - name of the result variable +CallInst *IRBuilderBase::CreateMaskedLoad(Value *Ptr, unsigned Align, + Value *Mask, Value *PassThru, + const Twine &Name) { + assert(Ptr->getType()->isPointerTy() && "Ptr must be of pointer type"); + // DataTy is the overloaded type + Type *DataTy = cast(Ptr->getType())->getElementType(); + assert(DataTy->isVectorTy() && "Ptr should point to a vector"); + if (!PassThru) + PassThru = UndefValue::get(DataTy); + Value *Ops[] = { Ptr, getInt32(Align), Mask, PassThru}; + return CreateMaskedIntrinsic(Intrinsic::masked_load, Ops, DataTy, Name); +} + +/// Create a call to a Masked Store intrinsic. +/// Val - the data to be stored, +/// Ptr - the base pointer for the store +/// Align - alignment of the destination location +/// Mask - an vector of booleans which indicates what vector lanes should +/// be accessed in memory +CallInst *IRBuilderBase::CreateMaskedStore(Value *Val, Value *Ptr, + unsigned Align, Value *Mask) { + Value *Ops[] = { Val, Ptr, getInt32(Align), Mask }; + // Type of the data to be stored - the only one overloaded type + return CreateMaskedIntrinsic(Intrinsic::masked_store, Ops, Val->getType()); +} + +/// Create a call to a Masked intrinsic, with given intrinsic Id, +/// an array of operands - Ops, and one overloaded type - DataTy +CallInst *IRBuilderBase::CreateMaskedIntrinsic(unsigned Id, + ArrayRef Ops, + Type *DataTy, + const Twine &Name) { + Module *M = BB->getParent()->getParent(); + Type *OverloadedTypes[] = { DataTy }; + Value *TheFn = Intrinsic::getDeclaration(M, (Intrinsic::ID)Id, OverloadedTypes); + return createCallHelper(TheFn, Ops, this, Name); +} + +CallInst *IRBuilderBase::CreateGCStatepoint(Value *ActualCallee, + ArrayRef CallArgs, + ArrayRef DeoptArgs, + ArrayRef GCArgs, + const Twine &Name) { + // Extract out the type of the callee. + PointerType *FuncPtrType = cast(ActualCallee->getType()); + assert(isa(FuncPtrType->getElementType()) && + "actual callee must be a callable value"); + + + Module *M = BB->getParent()->getParent(); + // Fill in the one generic type'd argument (the function is also vararg) + Type *ArgTypes[] = { FuncPtrType }; + Function *FnStatepoint = + Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_statepoint, + ArgTypes); + + std::vector args; + args.push_back(ActualCallee); + args.push_back(getInt32(CallArgs.size())); + args.push_back(getInt32(0 /*unused*/)); + args.insert(args.end(), CallArgs.begin(), CallArgs.end()); + args.push_back(getInt32(DeoptArgs.size())); + args.insert(args.end(), DeoptArgs.begin(), DeoptArgs.end()); + args.insert(args.end(), GCArgs.begin(), GCArgs.end()); + + return createCallHelper(FnStatepoint, args, this, Name); +} + +CallInst *IRBuilderBase::CreateGCStatepoint(Value *ActualCallee, + ArrayRef CallArgs, + ArrayRef DeoptArgs, + ArrayRef GCArgs, + const Twine &Name) { + std::vector VCallArgs; + for (auto &U : CallArgs) + VCallArgs.push_back(U.get()); + return CreateGCStatepoint(ActualCallee, VCallArgs, DeoptArgs, GCArgs, Name); +} + +CallInst *IRBuilderBase::CreateGCResult(Instruction *Statepoint, + Type *ResultType, + const Twine &Name) { + Intrinsic::ID ID = Intrinsic::experimental_gc_result; + Module *M = BB->getParent()->getParent(); + Type *Types[] = {ResultType}; + Value *FnGCResult = Intrinsic::getDeclaration(M, ID, Types); + + Value *Args[] = {Statepoint}; + return createCallHelper(FnGCResult, Args, this, Name); +} + +CallInst *IRBuilderBase::CreateGCRelocate(Instruction *Statepoint, + int BaseOffset, + int DerivedOffset, + Type *ResultType, + const Twine &Name) { + Module *M = BB->getParent()->getParent(); + Type *Types[] = {ResultType}; + Value *FnGCRelocate = + Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_relocate, Types); + + Value *Args[] = {Statepoint, + getInt32(BaseOffset), + getInt32(DerivedOffset)}; + return createCallHelper(FnGCRelocate, Args, this, Name); +} diff --git a/lib/IR/IRPrintingPasses.cpp b/lib/IR/IRPrintingPasses.cpp index c8a1747..91ccfbb 100644 --- a/lib/IR/IRPrintingPasses.cpp +++ b/lib/IR/IRPrintingPasses.cpp @@ -24,8 +24,8 @@ PrintModulePass::PrintModulePass() : OS(dbgs()) {} PrintModulePass::PrintModulePass(raw_ostream &OS, const std::string &Banner) : OS(OS), Banner(Banner) {} -PreservedAnalyses PrintModulePass::run(Module *M) { - OS << Banner << *M; +PreservedAnalyses PrintModulePass::run(Module &M) { + OS << Banner << M; return PreservedAnalyses::all(); } @@ -33,8 +33,8 @@ PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {} PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner) : OS(OS), Banner(Banner) {} -PreservedAnalyses PrintFunctionPass::run(Function *F) { - OS << Banner << static_cast(*F); +PreservedAnalyses PrintFunctionPass::run(Function &F) { + OS << Banner << static_cast(F); return PreservedAnalyses::all(); } @@ -50,7 +50,7 @@ public: : ModulePass(ID), P(OS, Banner) {} bool runOnModule(Module &M) override { - P.run(&M); + P.run(M); return false; } @@ -70,7 +70,7 @@ public: // This pass just prints a banner followed by the function as it's processed. bool runOnFunction(Function &F) override { - P.run(&F); + P.run(F); return false; } diff --git a/lib/IR/InlineAsm.cpp b/lib/IR/InlineAsm.cpp index 16d874f..5b73561 100644 --- a/lib/IR/InlineAsm.cpp +++ b/lib/IR/InlineAsm.cpp @@ -73,7 +73,7 @@ bool InlineAsm::ConstraintInfo::Parse(StringRef Str, unsigned multipleAlternativeCount = Str.count('|') + 1; unsigned multipleAlternativeIndex = 0; ConstraintCodeVector *pCodes = &Codes; - + // Initialize isMultipleAlternative = (multipleAlternativeCount > 1 ? true : false); if (isMultipleAlternative) { @@ -99,12 +99,12 @@ bool InlineAsm::ConstraintInfo::Parse(StringRef Str, ++I; Type = isOutput; } - + if (*I == '*') { isIndirect = true; ++I; } - + if (I == E) return true; // Just a prefix, like "==" or "~". // Parse the modifiers. @@ -228,7 +228,10 @@ InlineAsm::ParseConstraints(StringRef Constraints) { I = ConstraintEnd; if (I != E) { ++I; - if (I == E) { Result.clear(); break; } // don't allow "xyz," + if (I == E) { + Result.clear(); + break; + } // don't allow "xyz," } } diff --git a/lib/IR/Instruction.cpp b/lib/IR/Instruction.cpp index 3ee66f5..92c6e9f 100644 --- a/lib/IR/Instruction.cpp +++ b/lib/IR/Instruction.cpp @@ -15,7 +15,6 @@ #include "llvm/IR/CallSite.h" #include "llvm/IR/Constants.h" #include "llvm/IR/Instructions.h" -#include "llvm/IR/LeakDetector.h" #include "llvm/IR/Module.h" #include "llvm/IR/Operator.h" #include "llvm/IR/Type.h" @@ -24,8 +23,6 @@ using namespace llvm; Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps, Instruction *InsertBefore) : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) { - // Make sure that we get added to a basicblock - LeakDetector::addGarbageObject(this); // If requested, insert this instruction into a basic block... if (InsertBefore) { @@ -42,8 +39,6 @@ const DataLayout *Instruction::getDataLayout() const { Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps, BasicBlock *InsertAtEnd) : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(nullptr) { - // Make sure that we get added to a basicblock - LeakDetector::addGarbageObject(this); // append this instruction into the basic block assert(InsertAtEnd && "Basic block to append to may not be NULL!"); @@ -60,12 +55,6 @@ Instruction::~Instruction() { void Instruction::setParent(BasicBlock *P) { - if (getParent()) { - if (!P) LeakDetector::addGarbageObject(this); - } else { - if (P) LeakDetector::removeGarbageObject(this); - } - Parent = P; } diff --git a/lib/IR/Instructions.cpp b/lib/IR/Instructions.cpp index 57a4f0b..7136923 100644 --- a/lib/IR/Instructions.cpp +++ b/lib/IR/Instructions.cpp @@ -346,6 +346,12 @@ void CallInst::removeAttribute(unsigned i, Attribute attr) { setAttributes(PAL); } +void CallInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) { + AttributeSet PAL = getAttributes(); + PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes); + setAttributes(PAL); +} + bool CallInst::hasFnAttrImpl(Attribute::AttrKind A) const { if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A)) return true; @@ -605,6 +611,12 @@ void InvokeInst::removeAttribute(unsigned i, Attribute attr) { setAttributes(PAL); } +void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) { + AttributeSet PAL = getAttributes(); + PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes); + setAttributes(PAL); +} + LandingPadInst *InvokeInst::getLandingPadInst() const { return cast(getUnwindDest()->getFirstNonPHI()); } @@ -796,11 +808,8 @@ void BranchInst::swapSuccessors() { return; // The first operand is the name. Fetch them backwards and build a new one. - Value *Ops[] = { - ProfileData->getOperand(0), - ProfileData->getOperand(2), - ProfileData->getOperand(1) - }; + Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2), + ProfileData->getOperand(1)}; setMetadata(LLVMContext::MD_prof, MDNode::get(ProfileData->getContext(), Ops)); } @@ -2076,7 +2085,7 @@ float FPMathOperator::getFPAccuracy() const { cast(this)->getMetadata(LLVMContext::MD_fpmath); if (!MD) return 0.0; - ConstantFP *Accuracy = cast(MD->getOperand(0)); + ConstantFP *Accuracy = mdconst::extract(MD->getOperand(0)); return Accuracy->getValueAPF().convertToFloat(); } @@ -2559,6 +2568,17 @@ CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast( return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); } +CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty, + const Twine &Name, + Instruction *InsertBefore) { + if (S->getType()->isPointerTy() && Ty->isIntegerTy()) + return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); + if (S->getType()->isIntegerTy() && Ty->isPointerTy()) + return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore); + + return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); +} + CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, bool isSigned, const Twine &Name, Instruction *InsertBefore) { @@ -2716,6 +2736,18 @@ bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) { return true; } +bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy, + const DataLayout *DL) { + if (auto *PtrTy = dyn_cast(SrcTy)) + if (auto *IntTy = dyn_cast(DestTy)) + return DL && IntTy->getBitWidth() == DL->getPointerTypeSizeInBits(PtrTy); + if (auto *PtrTy = dyn_cast(DestTy)) + if (auto *IntTy = dyn_cast(SrcTy)) + return DL && IntTy->getBitWidth() == DL->getPointerTypeSizeInBits(PtrTy); + + return isBitCastable(SrcTy, DestTy); +} + // Provide a way to get a "cast" where the cast opcode is inferred from the // types and size of the operand. This, basically, is a parallel of the // logic in the castIsValid function below. This axiom should hold: @@ -2832,10 +2864,6 @@ CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) { // Check for type sanity on the arguments Type *SrcTy = S->getType(); - // If this is a cast to the same type then it's trivially true. - if (SrcTy == DstTy) - return true; - if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() || SrcTy->isAggregateType() || DstTy->isAggregateType()) return false; diff --git a/lib/IR/IntrinsicInst.cpp b/lib/IR/IntrinsicInst.cpp index 5725284..b9b5a29 100644 --- a/lib/IR/IntrinsicInst.cpp +++ b/lib/IR/IntrinsicInst.cpp @@ -49,15 +49,25 @@ Value *DbgInfoIntrinsic::StripCast(Value *C) { return dyn_cast(C); } +static Value *getValueImpl(Value *Op) { + auto *MD = cast(Op)->getMetadata(); + if (auto *V = dyn_cast(MD)) + return V->getValue(); + + // When the value goes to null, it gets replaced by an empty MDNode. + assert(!cast(MD)->getNumOperands() && "Expected an empty MDNode"); + return nullptr; +} + //===----------------------------------------------------------------------===// /// DbgDeclareInst - This represents the llvm.dbg.declare instruction. /// Value *DbgDeclareInst::getAddress() const { - if (MDNode* MD = cast_or_null(getArgOperand(0))) - return MD->getOperand(0); - else + if (!getArgOperand(0)) return nullptr; + + return getValueImpl(getArgOperand(0)); } //===----------------------------------------------------------------------===// @@ -65,9 +75,7 @@ Value *DbgDeclareInst::getAddress() const { /// const Value *DbgValueInst::getValue() const { - return cast(getArgOperand(0))->getOperand(0); + return const_cast(this)->getValue(); } -Value *DbgValueInst::getValue() { - return cast(getArgOperand(0))->getOperand(0); -} +Value *DbgValueInst::getValue() { return getValueImpl(getArgOperand(0)); } diff --git a/lib/IR/LLVMContext.cpp b/lib/IR/LLVMContext.cpp index c62bc09..b6d95c4 100644 --- a/lib/IR/LLVMContext.cpp +++ b/lib/IR/LLVMContext.cpp @@ -229,28 +229,10 @@ void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) { // Metadata Kind Uniquing //===----------------------------------------------------------------------===// -#ifndef NDEBUG -/// isValidName - Return true if Name is a valid custom metadata handler name. -static bool isValidName(StringRef MDName) { - if (MDName.empty()) - return false; - - if (!std::isalpha(static_cast(MDName[0]))) - return false; - - for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E; - ++I) { - if (!std::isalnum(static_cast(*I)) && *I != '_' && - *I != '-' && *I != '.') - return false; - } - return true; -} -#endif - /// getMDKindID - Return a unique non-zero ID for the specified metadata kind. unsigned LLVMContext::getMDKindID(StringRef Name) const { - assert(isValidName(Name) && "Invalid MDNode name"); + assert(!std::isdigit(Name.front()) && + "Named metadata may not start with a digit"); // If this is new, assign it its ID. return pImpl->CustomMDKindNames.insert(std::make_pair( diff --git a/lib/IR/LLVMContextImpl.cpp b/lib/IR/LLVMContextImpl.cpp index 3fd0bb3..d717b92 100644 --- a/lib/IR/LLVMContextImpl.cpp +++ b/lib/IR/LLVMContextImpl.cpp @@ -72,9 +72,31 @@ LLVMContextImpl::~LLVMContextImpl() { // the container. Avoid iterators during this operation: while (!OwnedModules.empty()) delete *OwnedModules.begin(); - - // Free the constants. This is important to do here to ensure that they are - // freed before the LeakDetector is torn down. + + // Drop references for MDNodes. Do this before Values get deleted to avoid + // unnecessary RAUW when nodes are still unresolved. + for (auto *I : DistinctMDNodes) + I->dropAllReferences(); +#define HANDLE_MDNODE_LEAF(CLASS) \ + for (auto *I : CLASS##s) \ + I->dropAllReferences(); +#include "llvm/IR/Metadata.def" + + // Also drop references that come from the Value bridges. + for (auto &Pair : ValuesAsMetadata) + Pair.second->dropUsers(); + for (auto &Pair : MetadataAsValues) + Pair.second->dropUse(); + + // Destroy MDNodes. + for (MDNode *I : DistinctMDNodes) + I->deleteAsSubclass(); +#define HANDLE_MDNODE_LEAF(CLASS) \ + for (CLASS *I : CLASS##s) \ + delete I; +#include "llvm/IR/Metadata.def" + + // Free the constants. std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(), DropFirst()); std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(), @@ -120,23 +142,81 @@ LLVMContextImpl::~LLVMContextImpl() { delete &*Elem; } - // Destroy MDNodes. ~MDNode can move and remove nodes between the MDNodeSet - // and the NonUniquedMDNodes sets, so copy the values out first. - SmallVector MDNodes; - MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size()); - MDNodes.append(MDNodeSet.begin(), MDNodeSet.end()); - MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end()); - for (GenericMDNode *I : MDNodes) - I->dropAllReferences(); - for (GenericMDNode *I : MDNodes) - delete I; - assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() && - "Destroying all MDNodes didn't empty the Context's sets."); + // Destroy MetadataAsValues. + { + SmallVector MDVs; + MDVs.reserve(MetadataAsValues.size()); + for (auto &Pair : MetadataAsValues) + MDVs.push_back(Pair.second); + MetadataAsValues.clear(); + for (auto *V : MDVs) + delete V; + } + + // Destroy ValuesAsMetadata. + for (auto &Pair : ValuesAsMetadata) + delete Pair.second; // Destroy MDStrings. MDStringCache.clear(); } +void LLVMContextImpl::dropTriviallyDeadConstantArrays() { + bool Changed; + do { + Changed = false; + + for (auto I = ArrayConstants.map_begin(), E = ArrayConstants.map_end(); + I != E; ) { + auto *C = I->first; + I++; + if (C->use_empty()) { + Changed = true; + C->destroyConstant(); + } + } + + } while (Changed); +} + +void Module::dropTriviallyDeadConstantArrays() { + Context.pImpl->dropTriviallyDeadConstantArrays(); +} + +namespace llvm { +/// \brief Make MDOperand transparent for hashing. +/// +/// This overload of an implementation detail of the hashing library makes +/// MDOperand hash to the same value as a \a Metadata pointer. +/// +/// Note that overloading \a hash_value() as follows: +/// +/// \code +/// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); } +/// \endcode +/// +/// does not cause MDOperand to be transparent. In particular, a bare pointer +/// doesn't get hashed before it's combined, whereas \a MDOperand would. +static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); } +} + +unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) { + unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end()); +#ifndef NDEBUG + { + SmallVector MDs(N->op_begin() + Offset, N->op_end()); + unsigned RawHash = calculateHash(MDs); + assert(Hash == RawHash && + "Expected hash of MDOperand to equal hash of Metadata*"); + } +#endif + return Hash; +} + +unsigned MDNodeOpsKey::calculateHash(ArrayRef Ops) { + return hash_combine_range(Ops.begin(), Ops.end()); +} + // ConstantsContext anchors void UnaryConstantExpr::anchor() { } @@ -157,3 +237,4 @@ void InsertValueConstantExpr::anchor() { } void GetElementPtrConstantExpr::anchor() { } void CompareConstantExpr::anchor() { } + diff --git a/lib/IR/LLVMContextImpl.h b/lib/IR/LLVMContextImpl.h index e743ec3..4631246 100644 --- a/lib/IR/LLVMContextImpl.h +++ b/lib/IR/LLVMContextImpl.h @@ -17,7 +17,6 @@ #include "AttributeImpl.h" #include "ConstantsContext.h" -#include "LeaksContext.h" #include "llvm/ADT/APFloat.h" #include "llvm/ADT/APInt.h" #include "llvm/ADT/ArrayRef.h" @@ -28,6 +27,7 @@ #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/StringMap.h" #include "llvm/IR/Constants.h" +#include "llvm/IR/DebugInfoMetadata.h" #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Metadata.h" @@ -41,60 +41,38 @@ class ConstantFP; class DiagnosticInfoOptimizationRemark; class DiagnosticInfoOptimizationRemarkMissed; class DiagnosticInfoOptimizationRemarkAnalysis; +class GCStrategy; class LLVMContext; class Type; class Value; struct DenseMapAPIntKeyInfo { - struct KeyTy { - APInt val; - Type* type; - KeyTy(const APInt& V, Type* Ty) : val(V), type(Ty) {} - bool operator==(const KeyTy& that) const { - return type == that.type && this->val == that.val; - } - bool operator!=(const KeyTy& that) const { - return !this->operator==(that); - } - friend hash_code hash_value(const KeyTy &Key) { - return hash_combine(Key.type, Key.val); - } - }; - static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), nullptr); } - static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), nullptr); } - static unsigned getHashValue(const KeyTy &Key) { + static inline APInt getEmptyKey() { + APInt V(nullptr, 0); + V.VAL = 0; + return V; + } + static inline APInt getTombstoneKey() { + APInt V(nullptr, 0); + V.VAL = 1; + return V; + } + static unsigned getHashValue(const APInt &Key) { return static_cast(hash_value(Key)); } - static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { - return LHS == RHS; + static bool isEqual(const APInt &LHS, const APInt &RHS) { + return LHS.getBitWidth() == RHS.getBitWidth() && LHS == RHS; } }; struct DenseMapAPFloatKeyInfo { - struct KeyTy { - APFloat val; - KeyTy(const APFloat& V) : val(V){} - bool operator==(const KeyTy& that) const { - return this->val.bitwiseIsEqual(that.val); - } - bool operator!=(const KeyTy& that) const { - return !this->operator==(that); - } - friend hash_code hash_value(const KeyTy &Key) { - return hash_combine(Key.val); - } - }; - static inline KeyTy getEmptyKey() { - return KeyTy(APFloat(APFloat::Bogus,1)); - } - static inline KeyTy getTombstoneKey() { - return KeyTy(APFloat(APFloat::Bogus,2)); - } - static unsigned getHashValue(const KeyTy &Key) { + static inline APFloat getEmptyKey() { return APFloat(APFloat::Bogus, 1); } + static inline APFloat getTombstoneKey() { return APFloat(APFloat::Bogus, 2); } + static unsigned getHashValue(const APFloat &Key) { return static_cast(hash_value(Key)); } - static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { - return LHS == RHS; + static bool isEqual(const APFloat &LHS, const APFloat &RHS) { + return LHS.bitwiseIsEqual(RHS); } }; @@ -104,9 +82,8 @@ struct AnonStructTypeKeyInfo { bool isPacked; KeyTy(const ArrayRef& E, bool P) : ETypes(E), isPacked(P) {} - KeyTy(const StructType* ST) : - ETypes(ArrayRef(ST->element_begin(), ST->element_end())), - isPacked(ST->isPacked()) {} + KeyTy(const StructType *ST) + : ETypes(ST->elements()), isPacked(ST->isPacked()) {} bool operator==(const KeyTy& that) const { if (isPacked != that.isPacked) return false; @@ -149,10 +126,9 @@ struct FunctionTypeKeyInfo { bool isVarArg; KeyTy(const Type* R, const ArrayRef& P, bool V) : ReturnType(R), Params(P), isVarArg(V) {} - KeyTy(const FunctionType* FT) : - ReturnType(FT->getReturnType()), - Params(makeArrayRef(FT->param_begin(), FT->param_end())), - isVarArg(FT->isVarArg()) {} + KeyTy(const FunctionType *FT) + : ReturnType(FT->getReturnType()), Params(FT->params()), + isVarArg(FT->isVarArg()) {} bool operator==(const KeyTy& that) const { if (ReturnType != that.ReturnType) return false; @@ -191,78 +167,690 @@ struct FunctionTypeKeyInfo { } }; -/// \brief DenseMapInfo for GenericMDNode. +/// \brief Structure for hashing arbitrary MDNode operands. +class MDNodeOpsKey { + ArrayRef RawOps; + ArrayRef Ops; + + unsigned Hash; + +protected: + MDNodeOpsKey(ArrayRef Ops) + : RawOps(Ops), Hash(calculateHash(Ops)) {} + + template + MDNodeOpsKey(const NodeTy *N, unsigned Offset = 0) + : Ops(N->op_begin() + Offset, N->op_end()), Hash(N->getHash()) {} + + template + bool compareOps(const NodeTy *RHS, unsigned Offset = 0) const { + if (getHash() != RHS->getHash()) + return false; + + assert((RawOps.empty() || Ops.empty()) && "Two sets of operands?"); + return RawOps.empty() ? compareOps(Ops, RHS, Offset) + : compareOps(RawOps, RHS, Offset); + } + + static unsigned calculateHash(MDNode *N, unsigned Offset = 0); + +private: + template + static bool compareOps(ArrayRef Ops, const MDNode *RHS, unsigned Offset) { + if (Ops.size() != RHS->getNumOperands() - Offset) + return false; + return std::equal(Ops.begin(), Ops.end(), RHS->op_begin() + Offset); + } + + static unsigned calculateHash(ArrayRef Ops); + +public: + unsigned getHash() const { return Hash; } +}; + +template struct MDNodeKeyImpl; +template struct MDNodeInfo; + +/// \brief DenseMapInfo for MDTuple. /// /// Note that we don't need the is-function-local bit, since that's implicit in /// the operands. -struct GenericMDNodeInfo { - struct KeyTy { - ArrayRef Ops; - unsigned Hash; - - KeyTy(ArrayRef Ops) - : Ops(Ops), Hash(hash_combine_range(Ops.begin(), Ops.end())) {} - - KeyTy(GenericMDNode *N, SmallVectorImpl &Storage) { - Storage.resize(N->getNumOperands()); - for (unsigned I = 0, E = N->getNumOperands(); I != E; ++I) - Storage[I] = N->getOperand(I); - Ops = Storage; - Hash = hash_combine_range(Ops.begin(), Ops.end()); - } +template <> struct MDNodeKeyImpl : MDNodeOpsKey { + MDNodeKeyImpl(ArrayRef Ops) : MDNodeOpsKey(Ops) {} + MDNodeKeyImpl(const MDTuple *N) : MDNodeOpsKey(N) {} - bool operator==(const GenericMDNode *RHS) const { - if (RHS == getEmptyKey() || RHS == getTombstoneKey()) - return false; - if (Hash != RHS->getHash() || Ops.size() != RHS->getNumOperands()) - return false; - for (unsigned I = 0, E = Ops.size(); I != E; ++I) - if (Ops[I] != RHS->getOperand(I)) - return false; - return true; - } - }; - static inline GenericMDNode *getEmptyKey() { - return DenseMapInfo::getEmptyKey(); + bool isKeyOf(const MDTuple *RHS) const { return compareOps(RHS); } + + unsigned getHashValue() const { return getHash(); } + + static unsigned calculateHash(MDTuple *N) { + return MDNodeOpsKey::calculateHash(N); } - static inline GenericMDNode *getTombstoneKey() { - return DenseMapInfo::getTombstoneKey(); +}; + +/// \brief DenseMapInfo for MDLocation. +template <> struct MDNodeKeyImpl { + unsigned Line; + unsigned Column; + Metadata *Scope; + Metadata *InlinedAt; + + MDNodeKeyImpl(unsigned Line, unsigned Column, Metadata *Scope, + Metadata *InlinedAt) + : Line(Line), Column(Column), Scope(Scope), InlinedAt(InlinedAt) {} + + MDNodeKeyImpl(const MDLocation *L) + : Line(L->getLine()), Column(L->getColumn()), Scope(L->getScope()), + InlinedAt(L->getInlinedAt()) {} + + bool isKeyOf(const MDLocation *RHS) const { + return Line == RHS->getLine() && Column == RHS->getColumn() && + Scope == RHS->getScope() && InlinedAt == RHS->getInlinedAt(); } - static unsigned getHashValue(const KeyTy &Key) { return Key.Hash; } - static unsigned getHashValue(const GenericMDNode *U) { - return U->getHash(); + unsigned getHashValue() const { + return hash_combine(Line, Column, Scope, InlinedAt); } - static bool isEqual(const KeyTy &LHS, const GenericMDNode *RHS) { - return LHS == RHS; +}; + +/// \brief DenseMapInfo for GenericDebugNode. +template <> struct MDNodeKeyImpl : MDNodeOpsKey { + unsigned Tag; + StringRef Header; + MDNodeKeyImpl(unsigned Tag, StringRef Header, ArrayRef DwarfOps) + : MDNodeOpsKey(DwarfOps), Tag(Tag), Header(Header) {} + MDNodeKeyImpl(const GenericDebugNode *N) + : MDNodeOpsKey(N, 1), Tag(N->getTag()), Header(N->getHeader()) {} + + bool isKeyOf(const GenericDebugNode *RHS) const { + return Tag == RHS->getTag() && Header == RHS->getHeader() && + compareOps(RHS, 1); } - static bool isEqual(const GenericMDNode *LHS, const GenericMDNode *RHS) { - return LHS == RHS; + + unsigned getHashValue() const { return hash_combine(getHash(), Tag, Header); } + + static unsigned calculateHash(GenericDebugNode *N) { + return MDNodeOpsKey::calculateHash(N, 1); } }; -/// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps -/// up to date as MDNodes mutate. This class is implemented in DebugLoc.cpp. -class DebugRecVH : public CallbackVH { - /// Ctx - This is the LLVM Context being referenced. - LLVMContextImpl *Ctx; - - /// Idx - The index into either ScopeRecordIdx or ScopeInlinedAtRecords that - /// this reference lives in. If this is zero, then it represents a - /// non-canonical entry that has no DenseMap value. This can happen due to - /// RAUW. - int Idx; -public: - DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx) - : CallbackVH(n), Ctx(ctx), Idx(idx) {} - - MDNode *get() const { - return cast_or_null(getValPtr()); +template <> struct MDNodeKeyImpl { + int64_t Count; + int64_t Lo; + + MDNodeKeyImpl(int64_t Count, int64_t Lo) : Count(Count), Lo(Lo) {} + MDNodeKeyImpl(const MDSubrange *N) : Count(N->getCount()), Lo(N->getLo()) {} + + bool isKeyOf(const MDSubrange *RHS) const { + return Count == RHS->getCount() && Lo == RHS->getLo(); } + unsigned getHashValue() const { return hash_combine(Count, Lo); } +}; + +template <> struct MDNodeKeyImpl { + int64_t Value; + StringRef Name; - void deleted() override; - void allUsesReplacedWith(Value *VNew) override; + MDNodeKeyImpl(int64_t Value, StringRef Name) : Value(Value), Name(Name) {} + MDNodeKeyImpl(const MDEnumerator *N) + : Value(N->getValue()), Name(N->getName()) {} + + bool isKeyOf(const MDEnumerator *RHS) const { + return Value == RHS->getValue() && Name == RHS->getName(); + } + unsigned getHashValue() const { return hash_combine(Value, Name); } }; - + +template <> struct MDNodeKeyImpl { + unsigned Tag; + StringRef Name; + uint64_t SizeInBits; + uint64_t AlignInBits; + unsigned Encoding; + + MDNodeKeyImpl(unsigned Tag, StringRef Name, uint64_t SizeInBits, + uint64_t AlignInBits, unsigned Encoding) + : Tag(Tag), Name(Name), SizeInBits(SizeInBits), AlignInBits(AlignInBits), + Encoding(Encoding) {} + MDNodeKeyImpl(const MDBasicType *N) + : Tag(N->getTag()), Name(N->getName()), SizeInBits(N->getSizeInBits()), + AlignInBits(N->getAlignInBits()), Encoding(N->getEncoding()) {} + + bool isKeyOf(const MDBasicType *RHS) const { + return Tag == RHS->getTag() && Name == RHS->getName() && + SizeInBits == RHS->getSizeInBits() && + AlignInBits == RHS->getAlignInBits() && + Encoding == RHS->getEncoding(); + } + unsigned getHashValue() const { + return hash_combine(Tag, Name, SizeInBits, AlignInBits, Encoding); + } +}; + +template <> struct MDNodeKeyImpl { + unsigned Tag; + StringRef Name; + Metadata *File; + unsigned Line; + Metadata *Scope; + Metadata *BaseType; + uint64_t SizeInBits; + uint64_t AlignInBits; + uint64_t OffsetInBits; + unsigned Flags; + Metadata *ExtraData; + + MDNodeKeyImpl(unsigned Tag, StringRef Name, Metadata *File, unsigned Line, + Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits, + uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags, + Metadata *ExtraData) + : Tag(Tag), Name(Name), File(File), Line(Line), Scope(Scope), + BaseType(BaseType), SizeInBits(SizeInBits), AlignInBits(AlignInBits), + OffsetInBits(OffsetInBits), Flags(Flags), ExtraData(ExtraData) {} + MDNodeKeyImpl(const MDDerivedType *N) + : Tag(N->getTag()), Name(N->getName()), File(N->getFile()), + Line(N->getLine()), Scope(N->getScope()), BaseType(N->getBaseType()), + SizeInBits(N->getSizeInBits()), AlignInBits(N->getAlignInBits()), + OffsetInBits(N->getOffsetInBits()), Flags(N->getFlags()), + ExtraData(N->getExtraData()) {} + + bool isKeyOf(const MDDerivedType *RHS) const { + return Tag == RHS->getTag() && Name == RHS->getName() && + File == RHS->getFile() && Line == RHS->getLine() && + Scope == RHS->getScope() && BaseType == RHS->getBaseType() && + SizeInBits == RHS->getSizeInBits() && + AlignInBits == RHS->getAlignInBits() && + OffsetInBits == RHS->getOffsetInBits() && Flags == RHS->getFlags() && + ExtraData == RHS->getExtraData(); + } + unsigned getHashValue() const { + return hash_combine(Tag, Name, File, Line, Scope, BaseType, SizeInBits, + AlignInBits, OffsetInBits, Flags, ExtraData); + } +}; + +template <> struct MDNodeKeyImpl { + unsigned Tag; + StringRef Name; + Metadata *File; + unsigned Line; + Metadata *Scope; + Metadata *BaseType; + uint64_t SizeInBits; + uint64_t AlignInBits; + uint64_t OffsetInBits; + unsigned Flags; + Metadata *Elements; + unsigned RuntimeLang; + Metadata *VTableHolder; + Metadata *TemplateParams; + StringRef Identifier; + + MDNodeKeyImpl(unsigned Tag, StringRef Name, Metadata *File, unsigned Line, + Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits, + uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags, + Metadata *Elements, unsigned RuntimeLang, + Metadata *VTableHolder, Metadata *TemplateParams, + StringRef Identifier) + : Tag(Tag), Name(Name), File(File), Line(Line), Scope(Scope), + BaseType(BaseType), SizeInBits(SizeInBits), AlignInBits(AlignInBits), + OffsetInBits(OffsetInBits), Flags(Flags), Elements(Elements), + RuntimeLang(RuntimeLang), VTableHolder(VTableHolder), + TemplateParams(TemplateParams), Identifier(Identifier) {} + MDNodeKeyImpl(const MDCompositeType *N) + : Tag(N->getTag()), Name(N->getName()), File(N->getFile()), + Line(N->getLine()), Scope(N->getScope()), BaseType(N->getBaseType()), + SizeInBits(N->getSizeInBits()), AlignInBits(N->getAlignInBits()), + OffsetInBits(N->getOffsetInBits()), Flags(N->getFlags()), + Elements(N->getElements()), RuntimeLang(N->getRuntimeLang()), + VTableHolder(N->getVTableHolder()), + TemplateParams(N->getTemplateParams()), Identifier(N->getIdentifier()) { + } + + bool isKeyOf(const MDCompositeType *RHS) const { + return Tag == RHS->getTag() && Name == RHS->getName() && + File == RHS->getFile() && Line == RHS->getLine() && + Scope == RHS->getScope() && BaseType == RHS->getBaseType() && + SizeInBits == RHS->getSizeInBits() && + AlignInBits == RHS->getAlignInBits() && + OffsetInBits == RHS->getOffsetInBits() && Flags == RHS->getFlags() && + Elements == RHS->getElements() && + RuntimeLang == RHS->getRuntimeLang() && + VTableHolder == RHS->getVTableHolder() && + TemplateParams == RHS->getTemplateParams() && + Identifier == RHS->getIdentifier(); + } + unsigned getHashValue() const { + return hash_combine(Tag, Name, File, Line, Scope, BaseType, SizeInBits, + AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang, + VTableHolder, TemplateParams, Identifier); + } +}; + +template <> struct MDNodeKeyImpl { + unsigned Flags; + Metadata *TypeArray; + + MDNodeKeyImpl(int64_t Flags, Metadata *TypeArray) + : Flags(Flags), TypeArray(TypeArray) {} + MDNodeKeyImpl(const MDSubroutineType *N) + : Flags(N->getFlags()), TypeArray(N->getTypeArray()) {} + + bool isKeyOf(const MDSubroutineType *RHS) const { + return Flags == RHS->getFlags() && TypeArray == RHS->getTypeArray(); + } + unsigned getHashValue() const { return hash_combine(Flags, TypeArray); } +}; + +template <> struct MDNodeKeyImpl { + StringRef Filename; + StringRef Directory; + + MDNodeKeyImpl(StringRef Filename, StringRef Directory) + : Filename(Filename), Directory(Directory) {} + MDNodeKeyImpl(const MDFile *N) + : Filename(N->getFilename()), Directory(N->getDirectory()) {} + + bool isKeyOf(const MDFile *RHS) const { + return Filename == RHS->getFilename() && Directory == RHS->getDirectory(); + } + unsigned getHashValue() const { return hash_combine(Filename, Directory); } +}; + +template <> struct MDNodeKeyImpl { + unsigned SourceLanguage; + Metadata *File; + StringRef Producer; + bool IsOptimized; + StringRef Flags; + unsigned RuntimeVersion; + StringRef SplitDebugFilename; + unsigned EmissionKind; + Metadata *EnumTypes; + Metadata *RetainedTypes; + Metadata *Subprograms; + Metadata *GlobalVariables; + Metadata *ImportedEntities; + + MDNodeKeyImpl(unsigned SourceLanguage, Metadata *File, StringRef Producer, + bool IsOptimized, StringRef Flags, unsigned RuntimeVersion, + StringRef SplitDebugFilename, unsigned EmissionKind, + Metadata *EnumTypes, Metadata *RetainedTypes, + Metadata *Subprograms, Metadata *GlobalVariables, + Metadata *ImportedEntities) + : SourceLanguage(SourceLanguage), File(File), Producer(Producer), + IsOptimized(IsOptimized), Flags(Flags), RuntimeVersion(RuntimeVersion), + SplitDebugFilename(SplitDebugFilename), EmissionKind(EmissionKind), + EnumTypes(EnumTypes), RetainedTypes(RetainedTypes), + Subprograms(Subprograms), GlobalVariables(GlobalVariables), + ImportedEntities(ImportedEntities) {} + MDNodeKeyImpl(const MDCompileUnit *N) + : SourceLanguage(N->getSourceLanguage()), File(N->getFile()), + Producer(N->getProducer()), IsOptimized(N->isOptimized()), + Flags(N->getFlags()), RuntimeVersion(N->getRuntimeVersion()), + SplitDebugFilename(N->getSplitDebugFilename()), + EmissionKind(N->getEmissionKind()), EnumTypes(N->getEnumTypes()), + RetainedTypes(N->getRetainedTypes()), Subprograms(N->getSubprograms()), + GlobalVariables(N->getGlobalVariables()), + ImportedEntities(N->getImportedEntities()) {} + + bool isKeyOf(const MDCompileUnit *RHS) const { + return SourceLanguage == RHS->getSourceLanguage() && + File == RHS->getFile() && Producer == RHS->getProducer() && + IsOptimized == RHS->isOptimized() && Flags == RHS->getFlags() && + RuntimeVersion == RHS->getRuntimeVersion() && + SplitDebugFilename == RHS->getSplitDebugFilename() && + EmissionKind == RHS->getEmissionKind() && + EnumTypes == RHS->getEnumTypes() && + RetainedTypes == RHS->getRetainedTypes() && + Subprograms == RHS->getSubprograms() && + GlobalVariables == RHS->getGlobalVariables() && + ImportedEntities == RHS->getImportedEntities(); + } + unsigned getHashValue() const { + return hash_combine(SourceLanguage, File, Producer, IsOptimized, Flags, + RuntimeVersion, SplitDebugFilename, EmissionKind, + EnumTypes, RetainedTypes, Subprograms, GlobalVariables, + ImportedEntities); + } +}; + +template <> struct MDNodeKeyImpl { + Metadata *Scope; + StringRef Name; + StringRef LinkageName; + Metadata *File; + unsigned Line; + Metadata *Type; + bool IsLocalToUnit; + bool IsDefinition; + unsigned ScopeLine; + Metadata *ContainingType; + unsigned Virtuality; + unsigned VirtualIndex; + unsigned Flags; + bool IsOptimized; + Metadata *Function; + Metadata *TemplateParams; + Metadata *Declaration; + Metadata *Variables; + + MDNodeKeyImpl(Metadata *Scope, StringRef Name, StringRef LinkageName, + Metadata *File, unsigned Line, Metadata *Type, + bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine, + Metadata *ContainingType, unsigned Virtuality, + unsigned VirtualIndex, unsigned Flags, bool IsOptimized, + Metadata *Function, Metadata *TemplateParams, + Metadata *Declaration, Metadata *Variables) + : Scope(Scope), Name(Name), LinkageName(LinkageName), File(File), + Line(Line), Type(Type), IsLocalToUnit(IsLocalToUnit), + IsDefinition(IsDefinition), ScopeLine(ScopeLine), + ContainingType(ContainingType), Virtuality(Virtuality), + VirtualIndex(VirtualIndex), Flags(Flags), IsOptimized(IsOptimized), + Function(Function), TemplateParams(TemplateParams), + Declaration(Declaration), Variables(Variables) {} + MDNodeKeyImpl(const MDSubprogram *N) + : Scope(N->getScope()), Name(N->getName()), + LinkageName(N->getLinkageName()), File(N->getFile()), + Line(N->getLine()), Type(N->getType()), + IsLocalToUnit(N->isLocalToUnit()), IsDefinition(N->isDefinition()), + ScopeLine(N->getScopeLine()), ContainingType(N->getContainingType()), + Virtuality(N->getVirtuality()), VirtualIndex(N->getVirtualIndex()), + Flags(N->getFlags()), IsOptimized(N->isOptimized()), + Function(N->getFunction()), TemplateParams(N->getTemplateParams()), + Declaration(N->getDeclaration()), Variables(N->getVariables()) {} + + bool isKeyOf(const MDSubprogram *RHS) const { + return Scope == RHS->getScope() && Name == RHS->getName() && + LinkageName == RHS->getLinkageName() && File == RHS->getFile() && + Line == RHS->getLine() && Type == RHS->getType() && + IsLocalToUnit == RHS->isLocalToUnit() && + IsDefinition == RHS->isDefinition() && + ScopeLine == RHS->getScopeLine() && + ContainingType == RHS->getContainingType() && + Virtuality == RHS->getVirtuality() && + VirtualIndex == RHS->getVirtualIndex() && Flags == RHS->getFlags() && + IsOptimized == RHS->isOptimized() && + Function == RHS->getFunction() && + TemplateParams == RHS->getTemplateParams() && + Declaration == RHS->getDeclaration() && + Variables == RHS->getVariables(); + } + unsigned getHashValue() const { + return hash_combine(Scope, Name, LinkageName, File, Line, Type, + IsLocalToUnit, IsDefinition, ScopeLine, ContainingType, + Virtuality, VirtualIndex, Flags, IsOptimized, Function, + TemplateParams, Declaration, Variables); + } +}; + +template <> struct MDNodeKeyImpl { + Metadata *Scope; + Metadata *File; + unsigned Line; + unsigned Column; + + MDNodeKeyImpl(Metadata *Scope, Metadata *File, unsigned Line, unsigned Column) + : Scope(Scope), File(File), Line(Line), Column(Column) {} + MDNodeKeyImpl(const MDLexicalBlock *N) + : Scope(N->getScope()), File(N->getFile()), Line(N->getLine()), + Column(N->getColumn()) {} + + bool isKeyOf(const MDLexicalBlock *RHS) const { + return Scope == RHS->getScope() && File == RHS->getFile() && + Line == RHS->getLine() && Column == RHS->getColumn(); + } + unsigned getHashValue() const { + return hash_combine(Scope, File, Line, Column); + } +}; + +template <> struct MDNodeKeyImpl { + Metadata *Scope; + Metadata *File; + unsigned Discriminator; + + MDNodeKeyImpl(Metadata *Scope, Metadata *File, unsigned Discriminator) + : Scope(Scope), File(File), Discriminator(Discriminator) {} + MDNodeKeyImpl(const MDLexicalBlockFile *N) + : Scope(N->getScope()), File(N->getFile()), + Discriminator(N->getDiscriminator()) {} + + bool isKeyOf(const MDLexicalBlockFile *RHS) const { + return Scope == RHS->getScope() && File == RHS->getFile() && + Discriminator == RHS->getDiscriminator(); + } + unsigned getHashValue() const { + return hash_combine(Scope, File, Discriminator); + } +}; + +template <> struct MDNodeKeyImpl { + Metadata *Scope; + Metadata *File; + StringRef Name; + unsigned Line; + + MDNodeKeyImpl(Metadata *Scope, Metadata *File, StringRef Name, unsigned Line) + : Scope(Scope), File(File), Name(Name), Line(Line) {} + MDNodeKeyImpl(const MDNamespace *N) + : Scope(N->getScope()), File(N->getFile()), Name(N->getName()), + Line(N->getLine()) {} + + bool isKeyOf(const MDNamespace *RHS) const { + return Scope == RHS->getScope() && File == RHS->getFile() && + Name == RHS->getName() && Line == RHS->getLine(); + } + unsigned getHashValue() const { + return hash_combine(Scope, File, Name, Line); + } +}; + +template <> struct MDNodeKeyImpl { + StringRef Name; + Metadata *Type; + + MDNodeKeyImpl(StringRef Name, Metadata *Type) : Name(Name), Type(Type) {} + MDNodeKeyImpl(const MDTemplateTypeParameter *N) + : Name(N->getName()), Type(N->getType()) {} + + bool isKeyOf(const MDTemplateTypeParameter *RHS) const { + return Name == RHS->getName() && Type == RHS->getType(); + } + unsigned getHashValue() const { return hash_combine(Name, Type); } +}; + +template <> struct MDNodeKeyImpl { + unsigned Tag; + StringRef Name; + Metadata *Type; + Metadata *Value; + + MDNodeKeyImpl(unsigned Tag, StringRef Name, Metadata *Type, Metadata *Value) + : Tag(Tag), Name(Name), Type(Type), Value(Value) {} + MDNodeKeyImpl(const MDTemplateValueParameter *N) + : Tag(N->getTag()), Name(N->getName()), Type(N->getType()), + Value(N->getValue()) {} + + bool isKeyOf(const MDTemplateValueParameter *RHS) const { + return Tag == RHS->getTag() && Name == RHS->getName() && + Type == RHS->getType() && Value == RHS->getValue(); + } + unsigned getHashValue() const { return hash_combine(Tag, Name, Type, Value); } +}; + +template <> struct MDNodeKeyImpl { + Metadata *Scope; + StringRef Name; + StringRef LinkageName; + Metadata *File; + unsigned Line; + Metadata *Type; + bool IsLocalToUnit; + bool IsDefinition; + Metadata *Variable; + Metadata *StaticDataMemberDeclaration; + + MDNodeKeyImpl(Metadata *Scope, StringRef Name, StringRef LinkageName, + Metadata *File, unsigned Line, Metadata *Type, + bool IsLocalToUnit, bool IsDefinition, Metadata *Variable, + Metadata *StaticDataMemberDeclaration) + : Scope(Scope), Name(Name), LinkageName(LinkageName), File(File), + Line(Line), Type(Type), IsLocalToUnit(IsLocalToUnit), + IsDefinition(IsDefinition), Variable(Variable), + StaticDataMemberDeclaration(StaticDataMemberDeclaration) {} + MDNodeKeyImpl(const MDGlobalVariable *N) + : Scope(N->getScope()), Name(N->getName()), + LinkageName(N->getLinkageName()), File(N->getFile()), + Line(N->getLine()), Type(N->getType()), + IsLocalToUnit(N->isLocalToUnit()), IsDefinition(N->isDefinition()), + Variable(N->getVariable()), + StaticDataMemberDeclaration(N->getStaticDataMemberDeclaration()) {} + + bool isKeyOf(const MDGlobalVariable *RHS) const { + return Scope == RHS->getScope() && Name == RHS->getName() && + LinkageName == RHS->getLinkageName() && File == RHS->getFile() && + Line == RHS->getLine() && Type == RHS->getType() && + IsLocalToUnit == RHS->isLocalToUnit() && + IsDefinition == RHS->isDefinition() && + Variable == RHS->getVariable() && + StaticDataMemberDeclaration == RHS->getStaticDataMemberDeclaration(); + } + unsigned getHashValue() const { + return hash_combine(Scope, Name, LinkageName, File, Line, Type, + IsLocalToUnit, IsDefinition, Variable, + StaticDataMemberDeclaration); + } +}; + +template <> struct MDNodeKeyImpl { + unsigned Tag; + Metadata *Scope; + StringRef Name; + Metadata *File; + unsigned Line; + Metadata *Type; + unsigned Arg; + unsigned Flags; + Metadata *InlinedAt; + + MDNodeKeyImpl(unsigned Tag, Metadata *Scope, StringRef Name, Metadata *File, + unsigned Line, Metadata *Type, unsigned Arg, unsigned Flags, + Metadata *InlinedAt) + : Tag(Tag), Scope(Scope), Name(Name), File(File), Line(Line), Type(Type), + Arg(Arg), Flags(Flags), InlinedAt(InlinedAt) {} + MDNodeKeyImpl(const MDLocalVariable *N) + : Tag(N->getTag()), Scope(N->getScope()), Name(N->getName()), + File(N->getFile()), Line(N->getLine()), Type(N->getType()), + Arg(N->getArg()), Flags(N->getFlags()), InlinedAt(N->getInlinedAt()) {} + + bool isKeyOf(const MDLocalVariable *RHS) const { + return Tag == RHS->getTag() && Scope == RHS->getScope() && + Name == RHS->getName() && File == RHS->getFile() && + Line == RHS->getLine() && Type == RHS->getType() && + Arg == RHS->getArg() && Flags == RHS->getFlags() && + InlinedAt == RHS->getInlinedAt(); + } + unsigned getHashValue() const { + return hash_combine(Tag, Scope, Name, File, Line, Type, Arg, Flags, + InlinedAt); + } +}; + +template <> struct MDNodeKeyImpl { + ArrayRef Elements; + + MDNodeKeyImpl(ArrayRef Elements) : Elements(Elements) {} + MDNodeKeyImpl(const MDExpression *N) : Elements(N->getElements()) {} + + bool isKeyOf(const MDExpression *RHS) const { + return Elements == RHS->getElements(); + } + unsigned getHashValue() const { + return hash_combine_range(Elements.begin(), Elements.end()); + } +}; + +template <> struct MDNodeKeyImpl { + StringRef Name; + Metadata *File; + unsigned Line; + StringRef GetterName; + StringRef SetterName; + unsigned Attributes; + Metadata *Type; + + MDNodeKeyImpl(StringRef Name, Metadata *File, unsigned Line, + StringRef GetterName, StringRef SetterName, unsigned Attributes, + Metadata *Type) + : Name(Name), File(File), Line(Line), GetterName(GetterName), + SetterName(SetterName), Attributes(Attributes), Type(Type) {} + MDNodeKeyImpl(const MDObjCProperty *N) + : Name(N->getName()), File(N->getFile()), Line(N->getLine()), + GetterName(N->getGetterName()), SetterName(N->getSetterName()), + Attributes(N->getAttributes()), Type(N->getType()) {} + + bool isKeyOf(const MDObjCProperty *RHS) const { + return Name == RHS->getName() && File == RHS->getFile() && + Line == RHS->getLine() && GetterName == RHS->getGetterName() && + SetterName == RHS->getSetterName() && + Attributes == RHS->getAttributes() && Type == RHS->getType(); + } + unsigned getHashValue() const { + return hash_combine(Name, File, Line, GetterName, SetterName, Attributes, + Type); + } +}; + +template <> struct MDNodeKeyImpl { + unsigned Tag; + Metadata *Scope; + Metadata *Entity; + unsigned Line; + StringRef Name; + + MDNodeKeyImpl(unsigned Tag, Metadata *Scope, Metadata *Entity, unsigned Line, + StringRef Name) + : Tag(Tag), Scope(Scope), Entity(Entity), Line(Line), Name(Name) {} + MDNodeKeyImpl(const MDImportedEntity *N) + : Tag(N->getTag()), Scope(N->getScope()), Entity(N->getEntity()), + Line(N->getLine()), Name(N->getName()) {} + + bool isKeyOf(const MDImportedEntity *RHS) const { + return Tag == RHS->getTag() && Scope == RHS->getScope() && + Entity == RHS->getEntity() && Line == RHS->getLine() && + Name == RHS->getName(); + } + unsigned getHashValue() const { + return hash_combine(Tag, Scope, Entity, Line, Name); + } +}; + +/// \brief DenseMapInfo for MDNode subclasses. +template struct MDNodeInfo { + typedef MDNodeKeyImpl KeyTy; + static inline NodeTy *getEmptyKey() { + return DenseMapInfo::getEmptyKey(); + } + static inline NodeTy *getTombstoneKey() { + return DenseMapInfo::getTombstoneKey(); + } + static unsigned getHashValue(const KeyTy &Key) { return Key.getHashValue(); } + static unsigned getHashValue(const NodeTy *N) { + return KeyTy(N).getHashValue(); + } + static bool isEqual(const KeyTy &LHS, const NodeTy *RHS) { + if (RHS == getEmptyKey() || RHS == getTombstoneKey()) + return false; + return LHS.isKeyOf(RHS); + } + static bool isEqual(const NodeTy *LHS, const NodeTy *RHS) { + return LHS == RHS; + } +}; + +#define HANDLE_MDNODE_LEAF(CLASS) typedef MDNodeInfo CLASS##Info; +#include "llvm/IR/Metadata.def" + class LLVMContextImpl { public: /// OwnedModules - The set of modules instantiated in this context, and which @@ -279,12 +867,10 @@ public: LLVMContext::YieldCallbackTy YieldCallback; void *YieldOpaqueHandle; - typedef DenseMap IntMapTy; + typedef DenseMap IntMapTy; IntMapTy IntConstants; - - typedef DenseMap FPMapTy; + + typedef DenseMap FPMapTy; FPMapTy FPConstants; FoldingSet AttrsSet; @@ -292,14 +878,17 @@ public: FoldingSet AttrsSetNodes; StringMap MDStringCache; + DenseMap ValuesAsMetadata; + DenseMap MetadataAsValues; - DenseSet MDNodeSet; +#define HANDLE_MDNODE_LEAF(CLASS) DenseSet CLASS##s; +#include "llvm/IR/Metadata.def" // MDNodes may be uniqued or not uniqued. When they're not uniqued, they // aren't in the MDNodeSet, but they're still shared between objects, so no // one object can destroy them. This set allows us to at least destroy them // on Context destruction. - SmallPtrSet NonUniquedMDNodes; + SmallPtrSet DistinctMDNodes; DenseMap CAZConstants; @@ -326,9 +915,7 @@ public: ConstantInt *TheTrueVal; ConstantInt *TheFalseVal; - - LeakDetectorImpl LLVMObjects; - + // Basic type instances. Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy; Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy; @@ -340,11 +927,11 @@ public: BumpPtrAllocator TypeAllocator; DenseMap IntegerTypes; - - typedef DenseMap FunctionTypeMap; - FunctionTypeMap FunctionTypes; - typedef DenseMap StructTypeMap; - StructTypeMap AnonStructTypes; + + typedef DenseSet FunctionTypeSet; + FunctionTypeSet FunctionTypes; + typedef DenseSet StructTypeSet; + StructTypeSet AnonStructTypes; StringMap NamedStructTypes; unsigned NamedStructTypesUniqueID; @@ -362,32 +949,14 @@ public: /// CustomMDKindNames - Map to hold the metadata string to ID mapping. StringMap CustomMDKindNames; - - typedef std::pair > MDPairTy; + + typedef std::pair MDPairTy; typedef SmallVector MDMapTy; /// MetadataStore - Collection of per-instruction metadata used in this /// context. DenseMap MetadataStore; - /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope - /// entry with no "inlined at" element. - DenseMap ScopeRecordIdx; - - /// ScopeRecords - These are the actual mdnodes (in a value handle) for an - /// index. The ValueHandle ensures that ScopeRecordIdx stays up to date if - /// the MDNode is RAUW'd. - std::vector ScopeRecords; - - /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an - /// scope/inlined-at pair. - DenseMap, int> ScopeInlinedAtIdx; - - /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles) - /// for an index. The ValueHandle ensures that ScopeINlinedAtIdx stays up - /// to date. - std::vector > ScopeInlinedAtRecords; - /// DiscriminatorTable - This table maps file:line locations to an /// integer representing the next DWARF path discriminator to assign to /// instructions in different blocks at the same location. @@ -403,11 +972,20 @@ public: typedef DenseMap PrefixDataMapTy; PrefixDataMapTy PrefixDataMap; + /// \brief Mapping from a function to its prologue data, which is stored as + /// the operand of an unparented ReturnInst so that the prologue data has a + /// Use. + typedef DenseMap PrologueDataMapTy; + PrologueDataMapTy PrologueDataMap; + int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx); int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx); - + LLVMContextImpl(LLVMContext &C); ~LLVMContextImpl(); + + /// Destroy the ConstantArrays if they are not used. + void dropTriviallyDeadConstantArrays(); }; } diff --git a/lib/IR/LeakDetector.cpp b/lib/IR/LeakDetector.cpp deleted file mode 100644 index 6f71627..0000000 --- a/lib/IR/LeakDetector.cpp +++ /dev/null @@ -1,69 +0,0 @@ -//===-- LeakDetector.cpp - Implement LeakDetector interface ---------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file implements the LeakDetector class. -// -//===----------------------------------------------------------------------===// - -#include "llvm/IR/LeakDetector.h" -#include "LLVMContextImpl.h" -#include "llvm/ADT/SmallPtrSet.h" -#include "llvm/IR/Value.h" -#include "llvm/Support/Compiler.h" -#include "llvm/Support/ManagedStatic.h" -#include "llvm/Support/Mutex.h" -#include "llvm/Support/Threading.h" -using namespace llvm; - -static ManagedStatic > ObjectsLock; -static ManagedStatic > Objects; - -static void clearGarbage(LLVMContext &Context) { - Objects->clear(); - Context.pImpl->LLVMObjects.clear(); -} - -void LeakDetector::addGarbageObjectImpl(void *Object) { - sys::SmartScopedLock Lock(*ObjectsLock); - Objects->addGarbage(Object); -} - -void LeakDetector::addGarbageObjectImpl(const Value *Object) { - LLVMContextImpl *pImpl = Object->getContext().pImpl; - pImpl->LLVMObjects.addGarbage(Object); -} - -void LeakDetector::removeGarbageObjectImpl(void *Object) { - sys::SmartScopedLock Lock(*ObjectsLock); - Objects->removeGarbage(Object); -} - -void LeakDetector::removeGarbageObjectImpl(const Value *Object) { - LLVMContextImpl *pImpl = Object->getContext().pImpl; - pImpl->LLVMObjects.removeGarbage(Object); -} - -void LeakDetector::checkForGarbageImpl(LLVMContext &Context, - const std::string &Message) { - LLVMContextImpl *pImpl = Context.pImpl; - sys::SmartScopedLock Lock(*ObjectsLock); - - Objects->setName("GENERIC"); - pImpl->LLVMObjects.setName("LLVM"); - - // use non-short-circuit version so that both checks are performed - if (Objects->hasGarbage(Message) | - pImpl->LLVMObjects.hasGarbage(Message)) - errs() << "\nThis is probably because you removed an object, but didn't " - << "delete it. Please check your code for memory leaks.\n"; - - // Clear out results so we don't get duplicate warnings on - // next call... - clearGarbage(Context); -} diff --git a/lib/IR/LeaksContext.h b/lib/IR/LeaksContext.h deleted file mode 100644 index 3e485ab..0000000 --- a/lib/IR/LeaksContext.h +++ /dev/null @@ -1,98 +0,0 @@ -//===- LeaksContext.h - LeadDetector Implementation ------------*- C++ -*--===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file defines various helper methods and classes used by -// LLVMContextImpl for leaks detectors. -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_LIB_IR_LEAKSCONTEXT_H -#define LLVM_LIB_IR_LEAKSCONTEXT_H - -#include "llvm/ADT/SmallPtrSet.h" -#include "llvm/IR/Value.h" -#include "llvm/Support/raw_ostream.h" - -namespace llvm { - -template -struct PrinterTrait { - static void print(const T* P) { errs() << P; } -}; - -template<> -struct PrinterTrait { - static void print(const Value* P) { errs() << *P; } -}; - -template -struct LeakDetectorImpl { - explicit LeakDetectorImpl(const char* const name = "") : - Cache(nullptr), Name(name) { } - - void clear() { - Cache = nullptr; - Ts.clear(); - } - - void setName(const char* n) { - Name = n; - } - - // Because the most common usage pattern, by far, is to add a - // garbage object, then remove it immediately, we optimize this - // case. When an object is added, it is not added to the set - // immediately, it is added to the CachedValue Value. If it is - // immediately removed, no set search need be performed. - void addGarbage(const T* o) { - assert(Ts.count(o) == 0 && "Object already in set!"); - if (Cache) { - assert(Cache != o && "Object already in set!"); - Ts.insert(Cache); - } - Cache = o; - } - - void removeGarbage(const T* o) { - if (o == Cache) - Cache = nullptr; // Cache hit - else - Ts.erase(o); - } - - bool hasGarbage(const std::string& Message) { - addGarbage(nullptr); // Flush the Cache - - assert(!Cache && "No value should be cached anymore!"); - - if (!Ts.empty()) { - errs() << "Leaked " << Name << " objects found: " << Message << ":\n"; - for (typename SmallPtrSet::iterator I = Ts.begin(), - E = Ts.end(); I != E; ++I) { - errs() << '\t'; - PrinterTrait::print(*I); - errs() << '\n'; - } - errs() << '\n'; - - return true; - } - - return false; - } - -private: - SmallPtrSet Ts; - const T* Cache; - const char* Name; -}; - -} - -#endif diff --git a/lib/IR/LegacyPassManager.cpp b/lib/IR/LegacyPassManager.cpp index 28fa74c..fa8d50e 100644 --- a/lib/IR/LegacyPassManager.cpp +++ b/lib/IR/LegacyPassManager.cpp @@ -227,10 +227,7 @@ public: Pass(PT_PassManager, ID), PMDataManager(), PMTopLevelManager(new FPPassManager()), wasRun(false) {} - /// add - Add a pass to the queue of passes to run. This passes ownership of - /// the Pass to the PassManager. When the PassManager is destroyed, the pass - /// will be destroyed as well, so there is no need to delete the pass. This - /// implies that all passes MUST be allocated with 'new'. + /// \copydoc FunctionPassManager::add() void add(Pass *P) { schedulePass(P); } @@ -398,10 +395,7 @@ public: Pass(PT_PassManager, ID), PMDataManager(), PMTopLevelManager(new MPPassManager()) {} - /// add - Add a pass to the queue of passes to run. This passes ownership of - /// the Pass to the PassManager. When the PassManager is destroyed, the pass - /// will be destroyed as well, so there is no need to delete the pass. This - /// implies that all passes MUST be allocated with 'new'. + /// \copydoc PassManager::add() void add(Pass *P) { schedulePass(P); } @@ -606,8 +600,7 @@ void PMTopLevelManager::schedulePass(Pass *P) { // If P is an analysis pass and it is available then do not // generate the analysis again. Stale analysis info should not be // available at this point. - const PassInfo *PI = - PassRegistry::getPassRegistry()->getPassInfo(P->getPassID()); + const PassInfo *PI = findAnalysisPassInfo(P->getPassID()); if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) { delete P; return; @@ -625,7 +618,7 @@ void PMTopLevelManager::schedulePass(Pass *P) { Pass *AnalysisPass = findAnalysisPass(*I); if (!AnalysisPass) { - const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I); + const PassInfo *PI = findAnalysisPassInfo(*I); if (!PI) { // Pass P is not in the global PassRegistry @@ -722,8 +715,7 @@ Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) { return *I; // If Pass not found then check the interfaces implemented by Immutable Pass - const PassInfo *PassInf = - PassRegistry::getPassRegistry()->getPassInfo(PI); + const PassInfo *PassInf = findAnalysisPassInfo(PI); assert(PassInf && "Expected all immutable passes to be initialized"); const std::vector &ImmPI = PassInf->getInterfacesImplemented(); @@ -737,6 +729,17 @@ Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) { return nullptr; } +const PassInfo *PMTopLevelManager::findAnalysisPassInfo(AnalysisID AID) const { + const PassInfo *&PI = AnalysisPassInfos[AID]; + if (!PI) + PI = PassRegistry::getPassRegistry()->getPassInfo(AID); + else + assert(PI == PassRegistry::getPassRegistry()->getPassInfo(AID) && + "The pass info pointer changed for an analysis ID!"); + + return PI; +} + // Print passes managed by this top level manager. void PMTopLevelManager::dumpPasses() const { @@ -765,8 +768,7 @@ void PMTopLevelManager::dumpArguments() const { dbgs() << "Pass Arguments: "; for (SmallVectorImpl::const_iterator I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I) - if (const PassInfo *PI = - PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID())) { + if (const PassInfo *PI = findAnalysisPassInfo((*I)->getPassID())) { assert(PI && "Expected all immutable passes to be initialized"); if (!PI->isAnalysisGroup()) dbgs() << " -" << PI->getPassArgument(); @@ -830,7 +832,7 @@ void PMDataManager::recordAvailableAnalysis(Pass *P) { // This pass is the current implementation of all of the interfaces it // implements as well. - const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI); + const PassInfo *PInf = TPM->findAnalysisPassInfo(PI); if (!PInf) return; const std::vector &II = PInf->getInterfacesImplemented(); for (unsigned i = 0, e = II.size(); i != e; ++i) @@ -963,7 +965,7 @@ void PMDataManager::freePass(Pass *P, StringRef Msg, } AnalysisID PI = P->getPassID(); - if (const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI)) { + if (const PassInfo *PInf = TPM->findAnalysisPassInfo(PI)) { // Remove the pass itself (if it is not already removed). AvailableAnalysis.erase(PI); @@ -1043,7 +1045,7 @@ void PMDataManager::add(Pass *P, bool ProcessAnalysis) { for (SmallVectorImpl::iterator I = ReqAnalysisNotAvailable.begin(), E = ReqAnalysisNotAvailable.end() ;I != E; ++I) { - const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I); + const PassInfo *PI = TPM->findAnalysisPassInfo(*I); Pass *AnalysisPass = PI->createPass(); this->addLowerLevelRequiredPass(P, AnalysisPass); } @@ -1148,7 +1150,7 @@ void PMDataManager::dumpPassArguments() const { PMD->dumpPassArguments(); else if (const PassInfo *PI = - PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID())) + TPM->findAnalysisPassInfo((*I)->getPassID())) if (!PI->isAnalysisGroup()) dbgs() << " -" << PI->getPassArgument(); } @@ -1224,7 +1226,7 @@ void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P, dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:"; for (unsigned i = 0; i != Set.size(); ++i) { if (i) dbgs() << ','; - const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(Set[i]); + const PassInfo *PInf = TPM->findAnalysisPassInfo(Set[i]); if (!PInf) { // Some preserved passes, such as AliasAnalysis, may not be initialized by // all drivers. @@ -1389,11 +1391,6 @@ FunctionPassManager::~FunctionPassManager() { delete FPM; } -/// add - Add a pass to the queue of passes to run. This passes -/// ownership of the Pass to the PassManager. When the -/// PassManager_X is destroyed, the pass will be destroyed as well, so -/// there is no need to delete the pass. (TODO delete passes.) -/// This implies that all passes MUST be allocated with 'new'. void FunctionPassManager::add(Pass *P) { FPM->add(P); } @@ -1669,8 +1666,8 @@ void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) { OnTheFlyManagers[P] = FPP; } - const PassInfo * RequiredPassPI = - PassRegistry::getPassRegistry()->getPassInfo(RequiredPass->getPassID()); + const PassInfo *RequiredPassPI = + TPM->findAnalysisPassInfo(RequiredPass->getPassID()); Pass *FoundPass = nullptr; if (RequiredPassPI && RequiredPassPI->isAnalysis()) { @@ -1749,10 +1746,6 @@ PassManager::~PassManager() { delete PM; } -/// add - Add a pass to the queue of passes to run. This passes ownership of -/// the Pass to the PassManager. When the PassManager is destroyed, the pass -/// will be destroyed as well, so there is no need to delete the pass. This -/// implies that all passes MUST be allocated with 'new'. void PassManager::add(Pass *P) { PM->add(P); } diff --git a/lib/IR/MDBuilder.cpp b/lib/IR/MDBuilder.cpp index 3ec613c..a901011 100644 --- a/lib/IR/MDBuilder.cpp +++ b/lib/IR/MDBuilder.cpp @@ -21,11 +21,16 @@ MDString *MDBuilder::createString(StringRef Str) { return MDString::get(Context, Str); } +ConstantAsMetadata *MDBuilder::createConstant(Constant *C) { + return ConstantAsMetadata::get(C); +} + MDNode *MDBuilder::createFPMath(float Accuracy) { if (Accuracy == 0.0) return nullptr; assert(Accuracy > 0.0 && "Invalid fpmath accuracy!"); - Value *Op = ConstantFP::get(Type::getFloatTy(Context), Accuracy); + auto *Op = + createConstant(ConstantFP::get(Type::getFloatTy(Context), Accuracy)); return MDNode::get(Context, Op); } @@ -38,33 +43,38 @@ MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight, MDNode *MDBuilder::createBranchWeights(ArrayRef Weights) { assert(Weights.size() >= 2 && "Need at least two branch weights!"); - SmallVector Vals(Weights.size() + 1); + SmallVector Vals(Weights.size() + 1); Vals[0] = createString("branch_weights"); Type *Int32Ty = Type::getInt32Ty(Context); for (unsigned i = 0, e = Weights.size(); i != e; ++i) - Vals[i + 1] = ConstantInt::get(Int32Ty, Weights[i]); + Vals[i + 1] = createConstant(ConstantInt::get(Int32Ty, Weights[i])); return MDNode::get(Context, Vals); } MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) { assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!"); + + Type *Ty = IntegerType::get(Context, Lo.getBitWidth()); + return createRange(ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi)); +} + +MDNode *MDBuilder::createRange(Constant *Lo, Constant *Hi) { // If the range is everything then it is useless. if (Hi == Lo) return nullptr; // Return the range [Lo, Hi). - Type *Ty = IntegerType::get(Context, Lo.getBitWidth()); - Value *Range[2] = {ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi)}; + Metadata *Range[2] = {createConstant(Lo), createConstant(Hi)}; return MDNode::get(Context, Range); } MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) { // To ensure uniqueness the root node is self-referential. - MDNode *Dummy = MDNode::getTemporary(Context, None); + auto Dummy = MDNode::getTemporary(Context, None); - SmallVector Args(1, Dummy); + SmallVector Args(1, Dummy.get()); if (Extra) Args.push_back(Extra); if (!Name.empty()) @@ -76,7 +86,7 @@ MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) { // !1 = metadata !{metadata !0} <- root // Replace the dummy operand with the root node itself and delete the dummy. Root->replaceOperandWith(0, Root); - MDNode::deleteTemporary(Dummy); + // We now have // !1 = metadata !{metadata !1} <- self-referential root return Root; @@ -92,10 +102,10 @@ MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent, bool isConstant) { if (isConstant) { Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1); - Value *Ops[3] = {createString(Name), Parent, Flags}; + Metadata *Ops[3] = {createString(Name), Parent, createConstant(Flags)}; return MDNode::get(Context, Ops); } else { - Value *Ops[2] = {createString(Name), Parent}; + Metadata *Ops[2] = {createString(Name), Parent}; return MDNode::get(Context, Ops); } } @@ -105,18 +115,18 @@ MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) { } MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) { - Value *Ops[2] = { createString(Name), Domain }; + Metadata *Ops[2] = {createString(Name), Domain}; return MDNode::get(Context, Ops); } /// \brief Return metadata for a tbaa.struct node with the given /// struct field descriptions. MDNode *MDBuilder::createTBAAStructNode(ArrayRef Fields) { - SmallVector Vals(Fields.size() * 3); + SmallVector Vals(Fields.size() * 3); Type *Int64 = Type::getInt64Ty(Context); for (unsigned i = 0, e = Fields.size(); i != e; ++i) { - Vals[i * 3 + 0] = ConstantInt::get(Int64, Fields[i].Offset); - Vals[i * 3 + 1] = ConstantInt::get(Int64, Fields[i].Size); + Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset)); + Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size)); Vals[i * 3 + 2] = Fields[i].TBAA; } return MDNode::get(Context, Vals); @@ -126,12 +136,12 @@ MDNode *MDBuilder::createTBAAStructNode(ArrayRef Fields) { /// with the given name, a list of pairs (offset, field type in the type DAG). MDNode *MDBuilder::createTBAAStructTypeNode( StringRef Name, ArrayRef> Fields) { - SmallVector Ops(Fields.size() * 2 + 1); + SmallVector Ops(Fields.size() * 2 + 1); Type *Int64 = Type::getInt64Ty(Context); Ops[0] = createString(Name); for (unsigned i = 0, e = Fields.size(); i != e; ++i) { Ops[i * 2 + 1] = Fields[i].first; - Ops[i * 2 + 2] = ConstantInt::get(Int64, Fields[i].second); + Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second)); } return MDNode::get(Context, Ops); } @@ -141,7 +151,7 @@ MDNode *MDBuilder::createTBAAStructTypeNode( MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent, uint64_t Offset) { ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset); - Value *Ops[3] = {createString(Name), Parent, Off}; + Metadata *Ops[3] = {createString(Name), Parent, createConstant(Off)}; return MDNode::get(Context, Ops); } @@ -150,6 +160,7 @@ MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent, MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType, uint64_t Offset) { Type *Int64 = Type::getInt64Ty(Context); - Value *Ops[3] = {BaseType, AccessType, ConstantInt::get(Int64, Offset)}; + Metadata *Ops[3] = {BaseType, AccessType, + createConstant(ConstantInt::get(Int64, Offset))}; return MDNode::get(Context, Ops); } diff --git a/lib/IR/Metadata.cpp b/lib/IR/Metadata.cpp index 27ba9f7..0ad3c5c 100644 --- a/lib/IR/Metadata.cpp +++ b/lib/IR/Metadata.cpp @@ -1,4 +1,4 @@ -//===-- Metadata.cpp - Implement Metadata classes -------------------------===// +//===- Metadata.cpp - Implement Metadata classes --------------------------===// // // The LLVM Compiler Infrastructure // @@ -13,6 +13,7 @@ #include "llvm/IR/Metadata.h" #include "LLVMContextImpl.h" +#include "MetadataImpl.h" #include "SymbolTableListTraitsImpl.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/STLExtras.h" @@ -20,23 +21,342 @@ #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringMap.h" #include "llvm/IR/ConstantRange.h" +#include "llvm/IR/DebugInfoMetadata.h" #include "llvm/IR/Instruction.h" #include "llvm/IR/LLVMContext.h" -#include "llvm/IR/LeakDetector.h" #include "llvm/IR/Module.h" #include "llvm/IR/ValueHandle.h" using namespace llvm; -Metadata::Metadata(LLVMContext &Context, unsigned ID) - : Value(Type::getMetadataTy(Context), ID) {} +MetadataAsValue::MetadataAsValue(Type *Ty, Metadata *MD) + : Value(Ty, MetadataAsValueVal), MD(MD) { + track(); +} + +MetadataAsValue::~MetadataAsValue() { + getType()->getContext().pImpl->MetadataAsValues.erase(MD); + untrack(); +} + +/// \brief Canonicalize metadata arguments to intrinsics. +/// +/// To support bitcode upgrades (and assembly semantic sugar) for \a +/// MetadataAsValue, we need to canonicalize certain metadata. +/// +/// - nullptr is replaced by an empty MDNode. +/// - An MDNode with a single null operand is replaced by an empty MDNode. +/// - An MDNode whose only operand is a \a ConstantAsMetadata gets skipped. +/// +/// This maintains readability of bitcode from when metadata was a type of +/// value, and these bridges were unnecessary. +static Metadata *canonicalizeMetadataForValue(LLVMContext &Context, + Metadata *MD) { + if (!MD) + // !{} + return MDNode::get(Context, None); + + // Return early if this isn't a single-operand MDNode. + auto *N = dyn_cast(MD); + if (!N || N->getNumOperands() != 1) + return MD; + + if (!N->getOperand(0)) + // !{} + return MDNode::get(Context, None); + + if (auto *C = dyn_cast(N->getOperand(0))) + // Look through the MDNode. + return C; + + return MD; +} + +MetadataAsValue *MetadataAsValue::get(LLVMContext &Context, Metadata *MD) { + MD = canonicalizeMetadataForValue(Context, MD); + auto *&Entry = Context.pImpl->MetadataAsValues[MD]; + if (!Entry) + Entry = new MetadataAsValue(Type::getMetadataTy(Context), MD); + return Entry; +} + +MetadataAsValue *MetadataAsValue::getIfExists(LLVMContext &Context, + Metadata *MD) { + MD = canonicalizeMetadataForValue(Context, MD); + auto &Store = Context.pImpl->MetadataAsValues; + return Store.lookup(MD); +} + +void MetadataAsValue::handleChangedMetadata(Metadata *MD) { + LLVMContext &Context = getContext(); + MD = canonicalizeMetadataForValue(Context, MD); + auto &Store = Context.pImpl->MetadataAsValues; + + // Stop tracking the old metadata. + Store.erase(this->MD); + untrack(); + this->MD = nullptr; + + // Start tracking MD, or RAUW if necessary. + auto *&Entry = Store[MD]; + if (Entry) { + replaceAllUsesWith(Entry); + delete this; + return; + } + + this->MD = MD; + track(); + Entry = this; +} + +void MetadataAsValue::track() { + if (MD) + MetadataTracking::track(&MD, *MD, *this); +} + +void MetadataAsValue::untrack() { + if (MD) + MetadataTracking::untrack(MD); +} + +void ReplaceableMetadataImpl::addRef(void *Ref, OwnerTy Owner) { + bool WasInserted = + UseMap.insert(std::make_pair(Ref, std::make_pair(Owner, NextIndex))) + .second; + (void)WasInserted; + assert(WasInserted && "Expected to add a reference"); + + ++NextIndex; + assert(NextIndex != 0 && "Unexpected overflow"); +} + +void ReplaceableMetadataImpl::dropRef(void *Ref) { + bool WasErased = UseMap.erase(Ref); + (void)WasErased; + assert(WasErased && "Expected to drop a reference"); +} + +void ReplaceableMetadataImpl::moveRef(void *Ref, void *New, + const Metadata &MD) { + auto I = UseMap.find(Ref); + assert(I != UseMap.end() && "Expected to move a reference"); + auto OwnerAndIndex = I->second; + UseMap.erase(I); + bool WasInserted = UseMap.insert(std::make_pair(New, OwnerAndIndex)).second; + (void)WasInserted; + assert(WasInserted && "Expected to add a reference"); + + // Check that the references are direct if there's no owner. + (void)MD; + assert((OwnerAndIndex.first || *static_cast(Ref) == &MD) && + "Reference without owner must be direct"); + assert((OwnerAndIndex.first || *static_cast(New) == &MD) && + "Reference without owner must be direct"); +} + +void ReplaceableMetadataImpl::replaceAllUsesWith(Metadata *MD) { + assert(!(MD && isa(MD) && cast(MD)->isTemporary()) && + "Expected non-temp node"); + + if (UseMap.empty()) + return; + + // Copy out uses since UseMap will get touched below. + typedef std::pair> UseTy; + SmallVector Uses(UseMap.begin(), UseMap.end()); + std::sort(Uses.begin(), Uses.end(), [](const UseTy &L, const UseTy &R) { + return L.second.second < R.second.second; + }); + for (const auto &Pair : Uses) { + // Check that this Ref hasn't disappeared after RAUW (when updating a + // previous Ref). + if (!UseMap.count(Pair.first)) + continue; + + OwnerTy Owner = Pair.second.first; + if (!Owner) { + // Update unowned tracking references directly. + Metadata *&Ref = *static_cast(Pair.first); + Ref = MD; + if (MD) + MetadataTracking::track(Ref); + UseMap.erase(Pair.first); + continue; + } + + // Check for MetadataAsValue. + if (Owner.is()) { + Owner.get()->handleChangedMetadata(MD); + continue; + } + + // There's a Metadata owner -- dispatch. + Metadata *OwnerMD = Owner.get(); + switch (OwnerMD->getMetadataID()) { +#define HANDLE_METADATA_LEAF(CLASS) \ + case Metadata::CLASS##Kind: \ + cast(OwnerMD)->handleChangedOperand(Pair.first, MD); \ + continue; +#include "llvm/IR/Metadata.def" + default: + llvm_unreachable("Invalid metadata subclass"); + } + } + assert(UseMap.empty() && "Expected all uses to be replaced"); +} + +void ReplaceableMetadataImpl::resolveAllUses(bool ResolveUsers) { + if (UseMap.empty()) + return; + + if (!ResolveUsers) { + UseMap.clear(); + return; + } + + // Copy out uses since UseMap could get touched below. + typedef std::pair> UseTy; + SmallVector Uses(UseMap.begin(), UseMap.end()); + std::sort(Uses.begin(), Uses.end(), [](const UseTy &L, const UseTy &R) { + return L.second.second < R.second.second; + }); + UseMap.clear(); + for (const auto &Pair : Uses) { + auto Owner = Pair.second.first; + if (!Owner) + continue; + if (Owner.is()) + continue; + + // Resolve MDNodes that point at this. + auto *OwnerMD = dyn_cast(Owner.get()); + if (!OwnerMD) + continue; + if (OwnerMD->isResolved()) + continue; + OwnerMD->decrementUnresolvedOperandCount(); + } +} + +static Function *getLocalFunction(Value *V) { + assert(V && "Expected value"); + if (auto *A = dyn_cast(V)) + return A->getParent(); + if (BasicBlock *BB = cast(V)->getParent()) + return BB->getParent(); + return nullptr; +} + +ValueAsMetadata *ValueAsMetadata::get(Value *V) { + assert(V && "Unexpected null Value"); + + auto &Context = V->getContext(); + auto *&Entry = Context.pImpl->ValuesAsMetadata[V]; + if (!Entry) { + assert((isa(V) || isa(V) || isa(V)) && + "Expected constant or function-local value"); + assert(!V->NameAndIsUsedByMD.getInt() && + "Expected this to be the only metadata use"); + V->NameAndIsUsedByMD.setInt(true); + if (auto *C = dyn_cast(V)) + Entry = new ConstantAsMetadata(C); + else + Entry = new LocalAsMetadata(V); + } + + return Entry; +} + +ValueAsMetadata *ValueAsMetadata::getIfExists(Value *V) { + assert(V && "Unexpected null Value"); + return V->getContext().pImpl->ValuesAsMetadata.lookup(V); +} + +void ValueAsMetadata::handleDeletion(Value *V) { + assert(V && "Expected valid value"); + + auto &Store = V->getType()->getContext().pImpl->ValuesAsMetadata; + auto I = Store.find(V); + if (I == Store.end()) + return; + + // Remove old entry from the map. + ValueAsMetadata *MD = I->second; + assert(MD && "Expected valid metadata"); + assert(MD->getValue() == V && "Expected valid mapping"); + Store.erase(I); + + // Delete the metadata. + MD->replaceAllUsesWith(nullptr); + delete MD; +} + +void ValueAsMetadata::handleRAUW(Value *From, Value *To) { + assert(From && "Expected valid value"); + assert(To && "Expected valid value"); + assert(From != To && "Expected changed value"); + assert(From->getType() == To->getType() && "Unexpected type change"); + + LLVMContext &Context = From->getType()->getContext(); + auto &Store = Context.pImpl->ValuesAsMetadata; + auto I = Store.find(From); + if (I == Store.end()) { + assert(!From->NameAndIsUsedByMD.getInt() && + "Expected From not to be used by metadata"); + return; + } + + // Remove old entry from the map. + assert(From->NameAndIsUsedByMD.getInt() && + "Expected From to be used by metadata"); + From->NameAndIsUsedByMD.setInt(false); + ValueAsMetadata *MD = I->second; + assert(MD && "Expected valid metadata"); + assert(MD->getValue() == From && "Expected valid mapping"); + Store.erase(I); + + if (isa(MD)) { + if (auto *C = dyn_cast(To)) { + // Local became a constant. + MD->replaceAllUsesWith(ConstantAsMetadata::get(C)); + delete MD; + return; + } + if (getLocalFunction(From) && getLocalFunction(To) && + getLocalFunction(From) != getLocalFunction(To)) { + // Function changed. + MD->replaceAllUsesWith(nullptr); + delete MD; + return; + } + } else if (!isa(To)) { + // Changed to function-local value. + MD->replaceAllUsesWith(nullptr); + delete MD; + return; + } + + auto *&Entry = Store[To]; + if (Entry) { + // The target already exists. + MD->replaceAllUsesWith(Entry); + delete MD; + return; + } + + // Update MD in place (and update the map entry). + assert(!To->NameAndIsUsedByMD.getInt() && + "Expected this to be the only metadata use"); + To->NameAndIsUsedByMD.setInt(true); + MD->V = To; + Entry = MD; +} //===----------------------------------------------------------------------===// // MDString implementation. // -void MDString::anchor() { } - MDString *MDString::get(LLVMContext &Context, StringRef Str) { auto &Store = Context.pImpl->MDStringCache; auto I = Store.find(Str); @@ -44,353 +364,398 @@ MDString *MDString::get(LLVMContext &Context, StringRef Str) { return &I->second; auto *Entry = - StringMapEntry::Create(Str, Store.getAllocator(), Context); + StringMapEntry::Create(Str, Store.getAllocator(), MDString()); bool WasInserted = Store.insert(Entry); (void)WasInserted; assert(WasInserted && "Expected entry to be inserted"); + Entry->second.Entry = Entry; return &Entry->second; } StringRef MDString::getString() const { - return StringMapEntry::GetStringMapEntryFromValue(*this).first(); + assert(Entry && "Expected to find string map entry"); + return Entry->first(); } //===----------------------------------------------------------------------===// -// MDNodeOperand implementation. +// MDNode implementation. // -// Use CallbackVH to hold MDNode operands. -namespace llvm { -class MDNodeOperand : public CallbackVH { - MDNode *getParent() { - MDNodeOperand *Cur = this; +void *MDNode::operator new(size_t Size, unsigned NumOps) { + void *Ptr = ::operator new(Size + NumOps * sizeof(MDOperand)); + MDOperand *O = static_cast(Ptr); + for (MDOperand *E = O + NumOps; O != E; ++O) + (void)new (O) MDOperand; + return O; +} + +void MDNode::operator delete(void *Mem) { + MDNode *N = static_cast(Mem); + MDOperand *O = static_cast(Mem); + for (MDOperand *E = O - N->NumOperands; O != E; --O) + (O - 1)->~MDOperand(); + ::operator delete(O); +} - while (Cur->getValPtrInt() != 1) - ++Cur; +MDNode::MDNode(LLVMContext &Context, unsigned ID, StorageType Storage, + ArrayRef Ops1, ArrayRef Ops2) + : Metadata(ID, Storage), NumOperands(Ops1.size() + Ops2.size()), + NumUnresolved(0), Context(Context) { + unsigned Op = 0; + for (Metadata *MD : Ops1) + setOperand(Op++, MD); + for (Metadata *MD : Ops2) + setOperand(Op++, MD); + + if (isDistinct()) + return; - assert(Cur->getValPtrInt() == 1 && - "Couldn't find the end of the operand list!"); - return reinterpret_cast(Cur + 1); - } + if (isUniqued()) + // Check whether any operands are unresolved, requiring re-uniquing. If + // not, don't support RAUW. + if (!countUnresolvedOperands()) + return; -public: - MDNodeOperand() {} - virtual ~MDNodeOperand(); + this->Context.makeReplaceable(make_unique(Context)); +} - void set(Value *V) { - unsigned IsLast = this->getValPtrInt(); - this->setValPtr(V); - this->setAsLastOperand(IsLast); +TempMDNode MDNode::clone() const { + switch (getMetadataID()) { + default: + llvm_unreachable("Invalid MDNode subclass"); +#define HANDLE_MDNODE_LEAF(CLASS) \ + case CLASS##Kind: \ + return cast(this)->cloneImpl(); +#include "llvm/IR/Metadata.def" } +} - /// \brief Accessor method to mark the operand as the first in the list. - void setAsLastOperand(unsigned I) { this->setValPtrInt(I); } +static bool isOperandUnresolved(Metadata *Op) { + if (auto *N = dyn_cast_or_null(Op)) + return !N->isResolved(); + return false; +} - void deleted() override; - void allUsesReplacedWith(Value *NV) override; -}; -} // end namespace llvm. +unsigned MDNode::countUnresolvedOperands() { + assert(NumUnresolved == 0 && "Expected unresolved ops to be uncounted"); + NumUnresolved = std::count_if(op_begin(), op_end(), isOperandUnresolved); + return NumUnresolved; +} -// Provide out-of-line definition to prevent weak vtable. -MDNodeOperand::~MDNodeOperand() {} +void MDNode::makeUniqued() { + assert(isTemporary() && "Expected this to be temporary"); + assert(!isResolved() && "Expected this to be unresolved"); -void MDNodeOperand::deleted() { - getParent()->replaceOperand(this, nullptr); -} + // Make this 'uniqued'. + Storage = Uniqued; + if (!countUnresolvedOperands()) + resolve(); -void MDNodeOperand::allUsesReplacedWith(Value *NV) { - getParent()->replaceOperand(this, NV); + assert(isUniqued() && "Expected this to be uniqued"); } -//===----------------------------------------------------------------------===// -// MDNode implementation. -// +void MDNode::makeDistinct() { + assert(isTemporary() && "Expected this to be temporary"); + assert(!isResolved() && "Expected this to be unresolved"); + + // Pretend to be uniqued, resolve the node, and then store in distinct table. + Storage = Uniqued; + resolve(); + storeDistinctInContext(); -/// \brief Get the MDNodeOperand's coallocated on the end of the MDNode. -static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) { - // Use <= instead of < to permit a one-past-the-end address. - assert(Op <= N->getNumOperands() && "Invalid operand number"); - return reinterpret_cast(N) - N->getNumOperands() + Op; + assert(isDistinct() && "Expected this to be distinct"); + assert(isResolved() && "Expected this to be resolved"); } -void MDNode::replaceOperandWith(unsigned i, Value *Val) { - MDNodeOperand *Op = getOperandPtr(this, i); - replaceOperand(Op, Val); +void MDNode::resolve() { + assert(isUniqued() && "Expected this to be uniqued"); + assert(!isResolved() && "Expected this to be unresolved"); + + // Move the map, so that this immediately looks resolved. + auto Uses = Context.takeReplaceableUses(); + NumUnresolved = 0; + assert(isResolved() && "Expected this to be resolved"); + + // Drop RAUW support. + Uses->resolveAllUses(); } -void *MDNode::operator new(size_t Size, unsigned NumOps) { - void *Ptr = ::operator new(Size + NumOps * sizeof(MDNodeOperand)); - MDNodeOperand *Op = static_cast(Ptr); - if (NumOps) { - MDNodeOperand *Last = Op + NumOps; - for (; Op != Last; ++Op) - new (Op) MDNodeOperand(); - (Op - 1)->setAsLastOperand(1); - } - return Op; +void MDNode::resolveAfterOperandChange(Metadata *Old, Metadata *New) { + assert(NumUnresolved != 0 && "Expected unresolved operands"); + + // Check if an operand was resolved. + if (!isOperandUnresolved(Old)) { + if (isOperandUnresolved(New)) + // An operand was un-resolved! + ++NumUnresolved; + } else if (!isOperandUnresolved(New)) + decrementUnresolvedOperandCount(); } -void MDNode::operator delete(void *Mem) { - MDNode *N = static_cast(Mem); - MDNodeOperand *Op = static_cast(Mem); - for (unsigned I = 0, E = N->NumOperands; I != E; ++I) - (--Op)->~MDNodeOperand(); - ::operator delete(Op); +void MDNode::decrementUnresolvedOperandCount() { + if (!--NumUnresolved) + // Last unresolved operand has just been resolved. + resolve(); } -MDNode::MDNode(LLVMContext &C, unsigned ID, ArrayRef Vals, - bool isFunctionLocal) - : Metadata(C, ID) { - NumOperands = Vals.size(); +void MDNode::resolveCycles() { + if (isResolved()) + return; - if (isFunctionLocal) - setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit); + // Resolve this node immediately. + resolve(); - // Initialize the operand list. - unsigned i = 0; - for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op + NumOperands; - Op != E; ++Op, ++i) - Op->set(Vals[i]); -} + // Resolve all operands. + for (const auto &Op : operands()) { + auto *N = dyn_cast_or_null(Op); + if (!N) + continue; -GenericMDNode::~GenericMDNode() { - LLVMContextImpl *pImpl = getType()->getContext().pImpl; - if (isNotUniqued()) { - pImpl->NonUniquedMDNodes.erase(this); - } else { - pImpl->MDNodeSet.erase(this); + assert(!N->isTemporary() && + "Expected all forward declarations to be resolved"); + if (!N->isResolved()) + N->resolveCycles(); } } -void GenericMDNode::dropAllReferences() { - for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op + NumOperands; - Op != E; ++Op) - Op->set(nullptr); +static bool hasSelfReference(MDNode *N) { + for (Metadata *MD : N->operands()) + if (MD == N) + return true; + return false; +} + +MDNode *MDNode::replaceWithPermanentImpl() { + if (hasSelfReference(this)) + return replaceWithDistinctImpl(); + return replaceWithUniquedImpl(); } -static const Function *getFunctionForValue(Value *V) { - if (!V) return nullptr; - if (Instruction *I = dyn_cast(V)) { - BasicBlock *BB = I->getParent(); - return BB ? BB->getParent() : nullptr; +MDNode *MDNode::replaceWithUniquedImpl() { + // Try to uniquify in place. + MDNode *UniquedNode = uniquify(); + + if (UniquedNode == this) { + makeUniqued(); + return this; } - if (Argument *A = dyn_cast(V)) - return A->getParent(); - if (BasicBlock *BB = dyn_cast(V)) - return BB->getParent(); - if (MDNode *MD = dyn_cast(V)) - return MD->getFunction(); - return nullptr; + + // Collision, so RAUW instead. + replaceAllUsesWith(UniquedNode); + deleteAsSubclass(); + return UniquedNode; } -#ifndef NDEBUG -static const Function *assertLocalFunction(const MDNode *N) { - if (!N->isFunctionLocal()) return nullptr; +MDNode *MDNode::replaceWithDistinctImpl() { + makeDistinct(); + return this; +} - // FIXME: This does not handle cyclic function local metadata. - const Function *F = nullptr, *NewF = nullptr; - for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { - if (Value *V = N->getOperand(i)) { - if (MDNode *MD = dyn_cast(V)) - NewF = assertLocalFunction(MD); - else - NewF = getFunctionForValue(V); - } - if (!F) - F = NewF; - else - assert((NewF == nullptr || F == NewF) && - "inconsistent function-local metadata"); - } - return F; -} -#endif - -// getFunction - If this metadata is function-local and recursively has a -// function-local operand, return the first such operand's parent function. -// Otherwise, return null. getFunction() should not be used for performance- -// critical code because it recursively visits all the MDNode's operands. -const Function *MDNode::getFunction() const { -#ifndef NDEBUG - return assertLocalFunction(this); -#else - if (!isFunctionLocal()) return nullptr; - for (unsigned i = 0, e = getNumOperands(); i != e; ++i) - if (const Function *F = getFunctionForValue(getOperand(i))) - return F; - return nullptr; -#endif +void MDTuple::recalculateHash() { + setHash(MDTupleInfo::KeyTy::calculateHash(this)); } -/// \brief Check if the Value would require a function-local MDNode. -static bool isFunctionLocalValue(Value *V) { - return isa(V) || isa(V) || isa(V) || - (isa(V) && cast(V)->isFunctionLocal()); +void MDNode::dropAllReferences() { + for (unsigned I = 0, E = NumOperands; I != E; ++I) + setOperand(I, nullptr); + if (!isResolved()) { + Context.getReplaceableUses()->resolveAllUses(/* ResolveUsers */ false); + (void)Context.takeReplaceableUses(); + } } -MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef Vals, - FunctionLocalness FL, bool Insert) { - auto &Store = Context.pImpl->MDNodeSet; +void MDNode::handleChangedOperand(void *Ref, Metadata *New) { + unsigned Op = static_cast(Ref) - op_begin(); + assert(Op < getNumOperands() && "Expected valid operand"); - GenericMDNodeInfo::KeyTy Key(Vals); - auto I = Store.find_as(Key); - if (I != Store.end()) - return *I; - if (!Insert) - return nullptr; + if (!isUniqued()) { + // This node is not uniqued. Just set the operand and be done with it. + setOperand(Op, New); + return; + } - bool isFunctionLocal = false; - switch (FL) { - case FL_Unknown: - for (Value *V : Vals) { - if (!V) continue; - if (isFunctionLocalValue(V)) { - isFunctionLocal = true; - break; - } - } - break; - case FL_No: - isFunctionLocal = false; - break; - case FL_Yes: - isFunctionLocal = true; + // This node is uniqued. + eraseFromStore(); + + Metadata *Old = getOperand(Op); + setOperand(Op, New); + + // Drop uniquing for self-reference cycles. + if (New == this) { + if (!isResolved()) + resolve(); + storeDistinctInContext(); + return; + } + + // Re-unique the node. + auto *Uniqued = uniquify(); + if (Uniqued == this) { + if (!isResolved()) + resolveAfterOperandChange(Old, New); + return; + } + + // Collision. + if (!isResolved()) { + // Still unresolved, so RAUW. + // + // First, clear out all operands to prevent any recursion (similar to + // dropAllReferences(), but we still need the use-list). + for (unsigned O = 0, E = getNumOperands(); O != E; ++O) + setOperand(O, nullptr); + Context.getReplaceableUses()->replaceAllUsesWith(Uniqued); + deleteAsSubclass(); + return; + } + + // Store in non-uniqued form if RAUW isn't possible. + storeDistinctInContext(); +} + +void MDNode::deleteAsSubclass() { + switch (getMetadataID()) { + default: + llvm_unreachable("Invalid subclass of MDNode"); +#define HANDLE_MDNODE_LEAF(CLASS) \ + case CLASS##Kind: \ + delete cast(this); \ break; +#include "llvm/IR/Metadata.def" } +} - // Coallocate space for the node and Operands together, then placement new. - GenericMDNode *N = - new (Vals.size()) GenericMDNode(Context, Vals, isFunctionLocal); +template +static T *uniquifyImpl(T *N, DenseSet &Store) { + if (T *U = getUniqued(Store, N)) + return U; - N->Hash = Key.Hash; Store.insert(N); return N; } -MDNode *MDNode::get(LLVMContext &Context, ArrayRef Vals) { - return getMDNode(Context, Vals, FL_Unknown); -} +template struct MDNode::HasCachedHash { + typedef char Yes[1]; + typedef char No[2]; + template struct SFINAE {}; -MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context, - ArrayRef Vals, - bool isFunctionLocal) { - return getMDNode(Context, Vals, isFunctionLocal ? FL_Yes : FL_No); -} + template + static Yes &check(SFINAE *); + template static No &check(...); -MDNode *MDNode::getIfExists(LLVMContext &Context, ArrayRef Vals) { - return getMDNode(Context, Vals, FL_Unknown, false); -} + static const bool value = sizeof(check(nullptr)) == sizeof(Yes); +}; -MDNode *MDNode::getTemporary(LLVMContext &Context, ArrayRef Vals) { - MDNode *N = new (Vals.size()) MDNodeFwdDecl(Context, Vals, FL_No); - N->setValueSubclassData(N->getSubclassDataFromValue() | NotUniquedBit); - LeakDetector::addGarbageObject(N); - return N; +MDNode *MDNode::uniquify() { + assert(!hasSelfReference(this) && "Cannot uniquify a self-referencing node"); + + // Try to insert into uniquing store. + switch (getMetadataID()) { + default: + llvm_unreachable("Invalid subclass of MDNode"); +#define HANDLE_MDNODE_LEAF(CLASS) \ + case CLASS##Kind: { \ + CLASS *SubclassThis = cast(this); \ + std::integral_constant::value> \ + ShouldRecalculateHash; \ + dispatchRecalculateHash(SubclassThis, ShouldRecalculateHash); \ + return uniquifyImpl(SubclassThis, getContext().pImpl->CLASS##s); \ + } +#include "llvm/IR/Metadata.def" + } } -void MDNode::deleteTemporary(MDNode *N) { - assert(N->use_empty() && "Temporary MDNode has uses!"); - assert(isa(N) && "Expected forward declaration"); - assert((N->getSubclassDataFromValue() & NotUniquedBit) && - "Temporary MDNode does not have NotUniquedBit set!"); - LeakDetector::removeGarbageObject(N); - delete cast(N); -} - -/// \brief Return specified operand. -Value *MDNode::getOperand(unsigned i) const { - assert(i < getNumOperands() && "Invalid operand number"); - return *getOperandPtr(const_cast(this), i); -} - -void MDNode::setIsNotUniqued() { - setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit); - LLVMContextImpl *pImpl = getType()->getContext().pImpl; - auto *G = cast(this); - G->Hash = 0; - pImpl->NonUniquedMDNodes.insert(G); -} - -// Replace value from this node's operand list. -void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) { - Value *From = *Op; - - // If is possible that someone did GV->RAUW(inst), replacing a global variable - // with an instruction or some other function-local object. If this is a - // non-function-local MDNode, it can't point to a function-local object. - // Handle this case by implicitly dropping the MDNode reference to null. - // Likewise if the MDNode is function-local but for a different function. - if (To && isFunctionLocalValue(To)) { - if (!isFunctionLocal()) - To = nullptr; - else { - const Function *F = getFunction(); - const Function *FV = getFunctionForValue(To); - // Metadata can be function-local without having an associated function. - // So only consider functions to have changed if non-null. - if (F && FV && F != FV) - To = nullptr; - } +void MDNode::eraseFromStore() { + switch (getMetadataID()) { + default: + llvm_unreachable("Invalid subclass of MDNode"); +#define HANDLE_MDNODE_LEAF(CLASS) \ + case CLASS##Kind: \ + getContext().pImpl->CLASS##s.erase(cast(this)); \ + break; +#include "llvm/IR/Metadata.def" } - - if (From == To) - return; +} - // If this node is already not being uniqued (because one of the operands - // already went to null), then there is nothing else to do here. - if (isNotUniqued()) { - Op->set(To); - return; +MDTuple *MDTuple::getImpl(LLVMContext &Context, ArrayRef MDs, + StorageType Storage, bool ShouldCreate) { + unsigned Hash = 0; + if (Storage == Uniqued) { + MDTupleInfo::KeyTy Key(MDs); + if (auto *N = getUniqued(Context.pImpl->MDTuples, Key)) + return N; + if (!ShouldCreate) + return nullptr; + Hash = Key.getHash(); + } else { + assert(ShouldCreate && "Expected non-uniqued nodes to always be created"); } - auto &Store = getContext().pImpl->MDNodeSet; - auto *N = cast(this); + return storeImpl(new (MDs.size()) MDTuple(Context, Storage, Hash, MDs), + Storage, Context.pImpl->MDTuples); +} + +void MDNode::deleteTemporary(MDNode *N) { + assert(N->isTemporary() && "Expected temporary node"); + N->replaceAllUsesWith(nullptr); + N->deleteAsSubclass(); +} - // Remove "this" from the context map. - Store.erase(N); +void MDNode::storeDistinctInContext() { + assert(isResolved() && "Expected resolved nodes"); + Storage = Distinct; + + // Reset the hash. + switch (getMetadataID()) { + default: + llvm_unreachable("Invalid subclass of MDNode"); +#define HANDLE_MDNODE_LEAF(CLASS) \ + case CLASS##Kind: { \ + std::integral_constant::value> ShouldResetHash; \ + dispatchResetHash(cast(this), ShouldResetHash); \ + break; \ + } +#include "llvm/IR/Metadata.def" + } - // Update the operand. - Op->set(To); + getContext().pImpl->DistinctMDNodes.insert(this); +} - // If we are dropping an argument to null, we choose to not unique the MDNode - // anymore. This commonly occurs during destruction, and uniquing these - // brings little reuse. Also, this means we don't need to include - // isFunctionLocal bits in the hash for MDNodes. - if (!To) { - setIsNotUniqued(); +void MDNode::replaceOperandWith(unsigned I, Metadata *New) { + if (getOperand(I) == New) return; - } - // Now that the node is out of the table, get ready to reinsert it. First, - // check to see if another node with the same operands already exists in the - // set. If so, then this node is redundant. - SmallVector Vals; - GenericMDNodeInfo::KeyTy Key(N, Vals); - auto I = Store.find_as(Key); - if (I != Store.end()) { - N->replaceAllUsesWith(*I); - delete N; + if (!isUniqued()) { + setOperand(I, New); return; } - N->Hash = Key.Hash; - Store.insert(N); + handleChangedOperand(mutable_begin() + I, New); +} - // If this MDValue was previously function-local but no longer is, clear - // its function-local flag. - if (isFunctionLocal() && !isFunctionLocalValue(To)) { - bool isStillFunctionLocal = false; - for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { - Value *V = getOperand(i); - if (!V) continue; - if (isFunctionLocalValue(V)) { - isStillFunctionLocal = true; - break; +void MDNode::setOperand(unsigned I, Metadata *New) { + assert(I < NumOperands); + mutable_begin()[I].reset(New, isUniqued() ? this : nullptr); +} + +/// \brief Get a node, or a self-reference that looks like it. +/// +/// Special handling for finding self-references, for use by \a +/// MDNode::concatenate() and \a MDNode::intersect() to maintain behaviour from +/// when self-referencing nodes were still uniqued. If the first operand has +/// the same operands as \c Ops, return the first operand instead. +static MDNode *getOrSelfReference(LLVMContext &Context, + ArrayRef Ops) { + if (!Ops.empty()) + if (MDNode *N = dyn_cast_or_null(Ops[0])) + if (N->getNumOperands() == Ops.size() && N == N->getOperand(0)) { + for (unsigned I = 1, E = Ops.size(); I != E; ++I) + if (Ops[I] != N->getOperand(I)) + return MDNode::get(Context, Ops); + return N; } - } - if (!isStillFunctionLocal) - setValueSubclassData(getSubclassDataFromValue() & ~FunctionLocalBit); - } + + return MDNode::get(Context, Ops); } MDNode *MDNode::concatenate(MDNode *A, MDNode *B) { @@ -399,41 +764,50 @@ MDNode *MDNode::concatenate(MDNode *A, MDNode *B) { if (!B) return A; - SmallVector Vals(A->getNumOperands() + - B->getNumOperands()); - - unsigned j = 0; - for (unsigned i = 0, ie = A->getNumOperands(); i != ie; ++i) - Vals[j++] = A->getOperand(i); - for (unsigned i = 0, ie = B->getNumOperands(); i != ie; ++i) - Vals[j++] = B->getOperand(i); + SmallVector MDs; + MDs.reserve(A->getNumOperands() + B->getNumOperands()); + MDs.append(A->op_begin(), A->op_end()); + MDs.append(B->op_begin(), B->op_end()); - return MDNode::get(A->getContext(), Vals); + // FIXME: This preserves long-standing behaviour, but is it really the right + // behaviour? Or was that an unintended side-effect of node uniquing? + return getOrSelfReference(A->getContext(), MDs); } MDNode *MDNode::intersect(MDNode *A, MDNode *B) { if (!A || !B) return nullptr; - SmallVector Vals; - for (unsigned i = 0, ie = A->getNumOperands(); i != ie; ++i) { - Value *V = A->getOperand(i); - for (unsigned j = 0, je = B->getNumOperands(); j != je; ++j) - if (V == B->getOperand(j)) { - Vals.push_back(V); - break; - } - } + SmallVector MDs; + for (Metadata *MD : A->operands()) + if (std::find(B->op_begin(), B->op_end(), MD) != B->op_end()) + MDs.push_back(MD); + + // FIXME: This preserves long-standing behaviour, but is it really the right + // behaviour? Or was that an unintended side-effect of node uniquing? + return getOrSelfReference(A->getContext(), MDs); +} + +MDNode *MDNode::getMostGenericAliasScope(MDNode *A, MDNode *B) { + if (!A || !B) + return nullptr; + + SmallVector MDs(B->op_begin(), B->op_end()); + for (Metadata *MD : A->operands()) + if (std::find(B->op_begin(), B->op_end(), MD) == B->op_end()) + MDs.push_back(MD); - return MDNode::get(A->getContext(), Vals); + // FIXME: This preserves long-standing behaviour, but is it really the right + // behaviour? Or was that an unintended side-effect of node uniquing? + return getOrSelfReference(A->getContext(), MDs); } MDNode *MDNode::getMostGenericFPMath(MDNode *A, MDNode *B) { if (!A || !B) return nullptr; - APFloat AVal = cast(A->getOperand(0))->getValueAPF(); - APFloat BVal = cast(B->getOperand(0))->getValueAPF(); + APFloat AVal = mdconst::extract(A->getOperand(0))->getValueAPF(); + APFloat BVal = mdconst::extract(B->getOperand(0))->getValueAPF(); if (AVal.compare(BVal) == APFloat::cmpLessThan) return A; return B; @@ -447,25 +821,27 @@ static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) { return !A.intersectWith(B).isEmptySet() || isContiguous(A, B); } -static bool tryMergeRange(SmallVectorImpl &EndPoints, ConstantInt *Low, - ConstantInt *High) { +static bool tryMergeRange(SmallVectorImpl &EndPoints, + ConstantInt *Low, ConstantInt *High) { ConstantRange NewRange(Low->getValue(), High->getValue()); unsigned Size = EndPoints.size(); - APInt LB = cast(EndPoints[Size - 2])->getValue(); - APInt LE = cast(EndPoints[Size - 1])->getValue(); + APInt LB = EndPoints[Size - 2]->getValue(); + APInt LE = EndPoints[Size - 1]->getValue(); ConstantRange LastRange(LB, LE); if (canBeMerged(NewRange, LastRange)) { ConstantRange Union = LastRange.unionWith(NewRange); Type *Ty = High->getType(); - EndPoints[Size - 2] = ConstantInt::get(Ty, Union.getLower()); - EndPoints[Size - 1] = ConstantInt::get(Ty, Union.getUpper()); + EndPoints[Size - 2] = + cast(ConstantInt::get(Ty, Union.getLower())); + EndPoints[Size - 1] = + cast(ConstantInt::get(Ty, Union.getUpper())); return true; } return false; } -static void addRange(SmallVectorImpl &EndPoints, ConstantInt *Low, - ConstantInt *High) { +static void addRange(SmallVectorImpl &EndPoints, + ConstantInt *Low, ConstantInt *High) { if (!EndPoints.empty()) if (tryMergeRange(EndPoints, Low, High)) return; @@ -487,31 +863,33 @@ MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) { // First, walk both lists in older of the lower boundary of each interval. // At each step, try to merge the new interval to the last one we adedd. - SmallVector EndPoints; + SmallVector EndPoints; int AI = 0; int BI = 0; int AN = A->getNumOperands() / 2; int BN = B->getNumOperands() / 2; while (AI < AN && BI < BN) { - ConstantInt *ALow = cast(A->getOperand(2 * AI)); - ConstantInt *BLow = cast(B->getOperand(2 * BI)); + ConstantInt *ALow = mdconst::extract(A->getOperand(2 * AI)); + ConstantInt *BLow = mdconst::extract(B->getOperand(2 * BI)); if (ALow->getValue().slt(BLow->getValue())) { - addRange(EndPoints, ALow, cast(A->getOperand(2 * AI + 1))); + addRange(EndPoints, ALow, + mdconst::extract(A->getOperand(2 * AI + 1))); ++AI; } else { - addRange(EndPoints, BLow, cast(B->getOperand(2 * BI + 1))); + addRange(EndPoints, BLow, + mdconst::extract(B->getOperand(2 * BI + 1))); ++BI; } } while (AI < AN) { - addRange(EndPoints, cast(A->getOperand(2 * AI)), - cast(A->getOperand(2 * AI + 1))); + addRange(EndPoints, mdconst::extract(A->getOperand(2 * AI)), + mdconst::extract(A->getOperand(2 * AI + 1))); ++AI; } while (BI < BN) { - addRange(EndPoints, cast(B->getOperand(2 * BI)), - cast(B->getOperand(2 * BI + 1))); + addRange(EndPoints, mdconst::extract(B->getOperand(2 * BI)), + mdconst::extract(B->getOperand(2 * BI + 1))); ++BI; } @@ -519,8 +897,8 @@ MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) { // the last and first ones. unsigned Size = EndPoints.size(); if (Size > 4) { - ConstantInt *FB = cast(EndPoints[0]); - ConstantInt *FE = cast(EndPoints[1]); + ConstantInt *FB = EndPoints[0]; + ConstantInt *FE = EndPoints[1]; if (tryMergeRange(EndPoints, FB, FE)) { for (unsigned i = 0; i < Size - 2; ++i) { EndPoints[i] = EndPoints[i + 2]; @@ -532,26 +910,29 @@ MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) { // If in the end we have a single range, it is possible that it is now the // full range. Just drop the metadata in that case. if (EndPoints.size() == 2) { - ConstantRange Range(cast(EndPoints[0])->getValue(), - cast(EndPoints[1])->getValue()); + ConstantRange Range(EndPoints[0]->getValue(), EndPoints[1]->getValue()); if (Range.isFullSet()) return nullptr; } - return MDNode::get(A->getContext(), EndPoints); + SmallVector MDs; + MDs.reserve(EndPoints.size()); + for (auto *I : EndPoints) + MDs.push_back(ConstantAsMetadata::get(I)); + return MDNode::get(A->getContext(), MDs); } //===----------------------------------------------------------------------===// // NamedMDNode implementation. // -static SmallVector, 4> &getNMDOps(void *Operands) { - return *(SmallVector, 4> *)Operands; +static SmallVector &getNMDOps(void *Operands) { + return *(SmallVector *)Operands; } NamedMDNode::NamedMDNode(const Twine &N) : Name(N.str()), Parent(nullptr), - Operands(new SmallVector, 4>()) {} + Operands(new SmallVector()) {} NamedMDNode::~NamedMDNode() { dropAllReferences(); @@ -564,13 +945,15 @@ unsigned NamedMDNode::getNumOperands() const { MDNode *NamedMDNode::getOperand(unsigned i) const { assert(i < getNumOperands() && "Invalid Operand number!"); - return &*getNMDOps(Operands)[i]; + auto *N = getNMDOps(Operands)[i].get(); + return cast_or_null(N); } -void NamedMDNode::addOperand(MDNode *M) { - assert(!M->isFunctionLocal() && - "NamedMDNode operands must not be function-local!"); - getNMDOps(Operands).push_back(TrackingVH(M)); +void NamedMDNode::addOperand(MDNode *M) { getNMDOps(Operands).emplace_back(M); } + +void NamedMDNode::setOperand(unsigned I, MDNode *New) { + assert(I < getNumOperands() && "Invalid operand number"); + getNMDOps(Operands)[I].reset(New); } void NamedMDNode::eraseFromParent() { @@ -630,7 +1013,7 @@ void Instruction::dropUnknownMetadata(ArrayRef KnownIDs) { continue; } - Info[I] = Info.back(); + Info[I] = std::move(Info.back()); Info.pop_back(); --E; } @@ -667,13 +1050,14 @@ void Instruction::setMetadata(unsigned KindID, MDNode *Node) { // Handle replacement of an existing value. for (auto &P : Info) if (P.first == KindID) { - P.second = Node; + P.second.reset(Node); return; } } // No replacement, just add it to the list. - Info.push_back(std::make_pair(KindID, Node)); + Info.emplace_back(std::piecewise_construct, std::make_tuple(KindID), + std::make_tuple(Node)); return; } @@ -695,7 +1079,7 @@ void Instruction::setMetadata(unsigned KindID, MDNode *Node) { // Handle removal of an existing value. for (unsigned i = 0, e = Info.size(); i != e; ++i) if (Info[i].first == KindID) { - Info[i] = Info.back(); + Info[i] = std::move(Info.back()); Info.pop_back(); assert(!Info.empty() && "Removing last entry should be handled above"); return; @@ -712,8 +1096,8 @@ void Instruction::setAAMetadata(const AAMDNodes &N) { MDNode *Instruction::getMetadataImpl(unsigned KindID) const { // Handle 'dbg' as a special case since it is not stored in the hash table. if (KindID == LLVMContext::MD_dbg) - return DbgLoc.getAsMDNode(getContext()); - + return DbgLoc.getAsMDNode(); + if (!hasMetadataHashEntry()) return nullptr; LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; @@ -731,8 +1115,8 @@ void Instruction::getAllMetadataImpl( // Handle 'dbg' as a special case since it is not stored in the hash table. if (!DbgLoc.isUnknown()) { - Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg, - DbgLoc.getAsMDNode(getContext()))); + Result.push_back( + std::make_pair((unsigned)LLVMContext::MD_dbg, DbgLoc.getAsMDNode())); if (!hasMetadataHashEntry()) return; } @@ -743,7 +1127,9 @@ void Instruction::getAllMetadataImpl( getContext().pImpl->MetadataStore.find(this)->second; assert(!Info.empty() && "Shouldn't have called this"); - Result.append(Info.begin(), Info.end()); + Result.reserve(Result.size() + Info.size()); + for (auto &I : Info) + Result.push_back(std::make_pair(I.first, cast(I.second.get()))); // Sort the resulting array so it is stable. if (Result.size() > 1) @@ -759,7 +1145,9 @@ void Instruction::getAllMetadataOtherThanDebugLocImpl( const LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore.find(this)->second; assert(!Info.empty() && "Shouldn't have called this"); - Result.append(Info.begin(), Info.end()); + Result.reserve(Result.size() + Info.size()); + for (auto &I : Info) + Result.push_back(std::make_pair(I.first, cast(I.second.get()))); // Sort the resulting array so it is stable. if (Result.size() > 1) @@ -773,4 +1161,3 @@ void Instruction::clearMetadataHashEntries() { getContext().pImpl->MetadataStore.erase(this); setHasMetadataHashEntry(false); } - diff --git a/lib/IR/MetadataImpl.h b/lib/IR/MetadataImpl.h new file mode 100644 index 0000000..662a50e --- /dev/null +++ b/lib/IR/MetadataImpl.h @@ -0,0 +1,46 @@ +//===- MetadataImpl.h - Helpers for implementing metadata -----------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file has private helpers for implementing metadata types. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_IR_METADATAIMPL_H +#define LLVM_IR_METADATAIMPL_H + +#include "llvm/ADT/DenseSet.h" +#include "llvm/IR/Metadata.h" + +namespace llvm { + +template +static T *getUniqued(DenseSet &Store, + const typename InfoT::KeyTy &Key) { + auto I = Store.find_as(Key); + return I == Store.end() ? nullptr : *I; +} + +template +T *MDNode::storeImpl(T *N, StorageType Storage, StoreT &Store) { + switch (Storage) { + case Uniqued: + Store.insert(N); + break; + case Distinct: + N->storeDistinctInContext(); + break; + case Temporary: + break; + } + return N; +} + +} // end namespace llvm + +#endif diff --git a/lib/IR/MetadataTracking.cpp b/lib/IR/MetadataTracking.cpp new file mode 100644 index 0000000..47f0b93 --- /dev/null +++ b/lib/IR/MetadataTracking.cpp @@ -0,0 +1,55 @@ +//===- MetadataTracking.cpp - Implement metadata tracking -----------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements Metadata tracking. +// +//===----------------------------------------------------------------------===// + +#include "llvm/IR/MetadataTracking.h" +#include "llvm/IR/Metadata.h" + +using namespace llvm; + +ReplaceableMetadataImpl *ReplaceableMetadataImpl::get(Metadata &MD) { + if (auto *N = dyn_cast(&MD)) + return N->Context.getReplaceableUses(); + return dyn_cast(&MD); +} + +bool MetadataTracking::track(void *Ref, Metadata &MD, OwnerTy Owner) { + assert(Ref && "Expected live reference"); + assert((Owner || *static_cast(Ref) == &MD) && + "Reference without owner must be direct"); + if (auto *R = ReplaceableMetadataImpl::get(MD)) { + R->addRef(Ref, Owner); + return true; + } + return false; +} + +void MetadataTracking::untrack(void *Ref, Metadata &MD) { + assert(Ref && "Expected live reference"); + if (auto *R = ReplaceableMetadataImpl::get(MD)) + R->dropRef(Ref); +} + +bool MetadataTracking::retrack(void *Ref, Metadata &MD, void *New) { + assert(Ref && "Expected live reference"); + assert(New && "Expected live reference"); + assert(Ref != New && "Expected change"); + if (auto *R = ReplaceableMetadataImpl::get(MD)) { + R->moveRef(Ref, New, MD); + return true; + } + return false; +} + +bool MetadataTracking::isReplaceable(const Metadata &MD) { + return ReplaceableMetadataImpl::get(const_cast(MD)); +} diff --git a/lib/IR/Module.cpp b/lib/IR/Module.cpp index 14e534b..b0abe8c 100644 --- a/lib/IR/Module.cpp +++ b/lib/IR/Module.cpp @@ -22,7 +22,7 @@ #include "llvm/IR/GVMaterializer.h" #include "llvm/IR/InstrTypes.h" #include "llvm/IR/LLVMContext.h" -#include "llvm/IR/LeakDetector.h" +#include "llvm/IR/TypeFinder.h" #include "llvm/Support/Dwarf.h" #include "llvm/Support/Path.h" #include "llvm/Support/RandomNumberGenerator.h" @@ -46,7 +46,7 @@ template class llvm::SymbolTableListTraits; // Module::Module(StringRef MID, LLVMContext &C) - : Context(C), Materializer(), ModuleID(MID), RNG(nullptr), DL("") { + : Context(C), Materializer(), ModuleID(MID), DL("") { ValSymTab = new ValueSymbolTable(); NamedMDSymTab = new StringMap(); Context.addModule(this); @@ -61,9 +61,27 @@ Module::~Module() { NamedMDList.clear(); delete ValSymTab; delete static_cast *>(NamedMDSymTab); - delete RNG; } +RandomNumberGenerator *Module::createRNG(const Pass* P) const { + SmallString<32> Salt(P->getPassName()); + + // This RNG is guaranteed to produce the same random stream only + // when the Module ID and thus the input filename is the same. This + // might be problematic if the input filename extension changes + // (e.g. from .c to .bc or .ll). + // + // We could store this salt in NamedMetadata, but this would make + // the parameter non-const. This would unfortunately make this + // interface unusable by any Machine passes, since they only have a + // const reference to their IR Module. Alternatively we can always + // store salt metadata from the Module constructor. + Salt += sys::path::filename(getModuleIdentifier()); + + return new RandomNumberGenerator(Salt); +} + + /// getNamedValue - Return the first global value in the module with /// the specified name, of arbitrary type. This method returns null /// if a global with the specified name is not found. @@ -259,8 +277,8 @@ void Module::eraseNamedMetadata(NamedMDNode *NMD) { NamedMDList.erase(NMD); } -bool Module::isValidModFlagBehavior(Value *V, ModFlagBehavior &MFB) { - if (ConstantInt *Behavior = dyn_cast(V)) { +bool Module::isValidModFlagBehavior(Metadata *MD, ModFlagBehavior &MFB) { + if (ConstantInt *Behavior = mdconst::dyn_extract_or_null(MD)) { uint64_t Val = Behavior->getLimitedValue(); if (Val >= ModFlagBehaviorFirstVal && Val <= ModFlagBehaviorLastVal) { MFB = static_cast(Val); @@ -280,11 +298,11 @@ getModuleFlagsMetadata(SmallVectorImpl &Flags) const { ModFlagBehavior MFB; if (Flag->getNumOperands() >= 3 && isValidModFlagBehavior(Flag->getOperand(0), MFB) && - isa(Flag->getOperand(1))) { + dyn_cast_or_null(Flag->getOperand(1))) { // Check the operands of the MDNode before accessing the operands. // The verifier will actually catch these failures. MDString *Key = cast(Flag->getOperand(1)); - Value *Val = Flag->getOperand(2); + Metadata *Val = Flag->getOperand(2); Flags.push_back(ModuleFlagEntry(MFB, Key, Val)); } } @@ -292,7 +310,7 @@ getModuleFlagsMetadata(SmallVectorImpl &Flags) const { /// Return the corresponding value if Key appears in module flags, otherwise /// return null. -Value *Module::getModuleFlag(StringRef Key) const { +Metadata *Module::getModuleFlag(StringRef Key) const { SmallVector ModuleFlags; getModuleFlagsMetadata(ModuleFlags); for (const ModuleFlagEntry &MFE : ModuleFlags) { @@ -320,14 +338,18 @@ NamedMDNode *Module::getOrInsertModuleFlagsMetadata() { /// metadata. It will create the module-level flags named metadata if it doesn't /// already exist. void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key, - Value *Val) { + Metadata *Val) { Type *Int32Ty = Type::getInt32Ty(Context); - Value *Ops[3] = { - ConstantInt::get(Int32Ty, Behavior), MDString::get(Context, Key), Val - }; + Metadata *Ops[3] = { + ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Behavior)), + MDString::get(Context, Key), Val}; getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops)); } void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key, + Constant *Val) { + addModuleFlag(Behavior, Key, ConstantAsMetadata::get(Val)); +} +void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key, uint32_t Val) { Type *Int32Ty = Type::getInt32Ty(Context); addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val)); @@ -335,7 +357,7 @@ void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key, void Module::addModuleFlag(MDNode *Node) { assert(Node->getNumOperands() == 3 && "Invalid number of operands for module flag!"); - assert(isa(Node->getOperand(0)) && + assert(mdconst::hasa(Node->getOperand(0)) && isa(Node->getOperand(1)) && "Invalid operand types for module flag!"); getOrInsertModuleFlagsMetadata()->addOperand(Node); @@ -369,16 +391,6 @@ const DataLayout *Module::getDataLayout() const { return &DL; } -// We want reproducible builds, but ModuleID may be a full path so we just use -// the filename to salt the RNG (although it is not guaranteed to be unique). -RandomNumberGenerator &Module::getRNG() const { - if (RNG == nullptr) { - StringRef Salt = sys::path::filename(ModuleID); - RNG = new RandomNumberGenerator(Salt); - } - return *RNG; -} - //===----------------------------------------------------------------------===// // Methods to control the materialization of GlobalValues in the Module. // @@ -425,6 +437,19 @@ std::error_code Module::materializeAllPermanently() { // Other module related stuff. // +std::vector Module::getIdentifiedStructTypes() const { + // If we have a materializer, it is possible that some unread function + // uses a type that is currently not visible to a TypeFinder, so ask + // the materializer which types it created. + if (Materializer) + return Materializer->getIdentifiedStructTypes(); + + std::vector Ret; + TypeFinder SrcStructTypes; + SrcStructTypes.run(*this, true); + Ret.assign(SrcStructTypes.begin(), SrcStructTypes.end()); + return Ret; +} // dropAllReferences() - This function causes all the subelements to "let go" // of all references that they are maintaining. This allows one to 'delete' a @@ -445,10 +470,10 @@ void Module::dropAllReferences() { } unsigned Module::getDwarfVersion() const { - Value *Val = getModuleFlag("Dwarf Version"); + auto *Val = cast_or_null(getModuleFlag("Dwarf Version")); if (!Val) return dwarf::DWARF_VERSION; - return cast(Val)->getZExtValue(); + return cast(Val->getValue())->getZExtValue(); } Comdat *Module::getOrInsertComdat(StringRef Name) { @@ -458,12 +483,13 @@ Comdat *Module::getOrInsertComdat(StringRef Name) { } PICLevel::Level Module::getPICLevel() const { - Value *Val = getModuleFlag("PIC Level"); + auto *Val = cast_or_null(getModuleFlag("PIC Level")); if (Val == NULL) return PICLevel::Default; - return static_cast(cast(Val)->getZExtValue()); + return static_cast( + cast(Val->getValue())->getZExtValue()); } void Module::setPICLevel(PICLevel::Level PL) { diff --git a/lib/IR/Pass.cpp b/lib/IR/Pass.cpp index 91d86ae..df45460 100644 --- a/lib/IR/Pass.cpp +++ b/lib/IR/Pass.cpp @@ -223,8 +223,8 @@ void PassRegistrationListener::enumeratePasses() { PassRegistry::getPassRegistry()->enumerateWith(this); } -PassNameParser::PassNameParser() - : Opt(nullptr) { +PassNameParser::PassNameParser(cl::Option &O) + : cl::parser(O) { PassRegistry::getPassRegistry()->addRegistrationListener(this); } diff --git a/lib/IR/PassManager.cpp b/lib/IR/PassManager.cpp index 2e2a7cb..a5f407c 100644 --- a/lib/IR/PassManager.cpp +++ b/lib/IR/PassManager.cpp @@ -10,174 +10,13 @@ #include "llvm/ADT/STLExtras.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/PassManager.h" -#include "llvm/Support/CommandLine.h" -#include "llvm/Support/Debug.h" using namespace llvm; -static cl::opt -DebugPM("debug-pass-manager", cl::Hidden, - cl::desc("Print pass management debugging information")); - -PreservedAnalyses ModulePassManager::run(Module *M, ModuleAnalysisManager *AM) { - PreservedAnalyses PA = PreservedAnalyses::all(); - - if (DebugPM) - dbgs() << "Starting module pass manager run.\n"; - - for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) { - if (DebugPM) - dbgs() << "Running module pass: " << Passes[Idx]->name() << "\n"; - - PreservedAnalyses PassPA = Passes[Idx]->run(M, AM); - if (AM) - AM->invalidate(M, PassPA); - PA.intersect(std::move(PassPA)); - - M->getContext().yield(); - } - - if (DebugPM) - dbgs() << "Finished module pass manager run.\n"; - - return PA; -} - -ModuleAnalysisManager::ResultConceptT & -ModuleAnalysisManager::getResultImpl(void *PassID, Module *M) { - ModuleAnalysisResultMapT::iterator RI; - bool Inserted; - std::tie(RI, Inserted) = ModuleAnalysisResults.insert(std::make_pair( - PassID, std::unique_ptr>())); - - // If we don't have a cached result for this module, look up the pass and run - // it to produce a result, which we then add to the cache. - if (Inserted) - RI->second = lookupPass(PassID).run(M, this); - - return *RI->second; -} - -ModuleAnalysisManager::ResultConceptT * -ModuleAnalysisManager::getCachedResultImpl(void *PassID, Module *M) const { - ModuleAnalysisResultMapT::const_iterator RI = - ModuleAnalysisResults.find(PassID); - return RI == ModuleAnalysisResults.end() ? nullptr : &*RI->second; -} - -void ModuleAnalysisManager::invalidateImpl(void *PassID, Module *M) { - ModuleAnalysisResults.erase(PassID); -} - -void ModuleAnalysisManager::invalidateImpl(Module *M, - const PreservedAnalyses &PA) { - // FIXME: This is a total hack based on the fact that erasure doesn't - // invalidate iteration for DenseMap. - for (ModuleAnalysisResultMapT::iterator I = ModuleAnalysisResults.begin(), - E = ModuleAnalysisResults.end(); - I != E; ++I) - if (I->second->invalidate(M, PA)) - ModuleAnalysisResults.erase(I); -} - -PreservedAnalyses FunctionPassManager::run(Function *F, - FunctionAnalysisManager *AM) { - PreservedAnalyses PA = PreservedAnalyses::all(); - - if (DebugPM) - dbgs() << "Starting function pass manager run.\n"; - - for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) { - if (DebugPM) - dbgs() << "Running function pass: " << Passes[Idx]->name() << "\n"; - - PreservedAnalyses PassPA = Passes[Idx]->run(F, AM); - if (AM) - AM->invalidate(F, PassPA); - PA.intersect(std::move(PassPA)); - - F->getContext().yield(); - } - - if (DebugPM) - dbgs() << "Finished function pass manager run.\n"; - - return PA; -} - -bool FunctionAnalysisManager::empty() const { - assert(FunctionAnalysisResults.empty() == - FunctionAnalysisResultLists.empty() && - "The storage and index of analysis results disagree on how many there " - "are!"); - return FunctionAnalysisResults.empty(); -} - -void FunctionAnalysisManager::clear() { - FunctionAnalysisResults.clear(); - FunctionAnalysisResultLists.clear(); -} - -FunctionAnalysisManager::ResultConceptT & -FunctionAnalysisManager::getResultImpl(void *PassID, Function *F) { - FunctionAnalysisResultMapT::iterator RI; - bool Inserted; - std::tie(RI, Inserted) = FunctionAnalysisResults.insert(std::make_pair( - std::make_pair(PassID, F), FunctionAnalysisResultListT::iterator())); - - // If we don't have a cached result for this function, look up the pass and - // run it to produce a result, which we then add to the cache. - if (Inserted) { - FunctionAnalysisResultListT &ResultList = FunctionAnalysisResultLists[F]; - ResultList.emplace_back(PassID, lookupPass(PassID).run(F, this)); - RI->second = std::prev(ResultList.end()); - } - - return *RI->second->second; -} - -FunctionAnalysisManager::ResultConceptT * -FunctionAnalysisManager::getCachedResultImpl(void *PassID, Function *F) const { - FunctionAnalysisResultMapT::const_iterator RI = - FunctionAnalysisResults.find(std::make_pair(PassID, F)); - return RI == FunctionAnalysisResults.end() ? nullptr : &*RI->second->second; -} - -void FunctionAnalysisManager::invalidateImpl(void *PassID, Function *F) { - FunctionAnalysisResultMapT::iterator RI = - FunctionAnalysisResults.find(std::make_pair(PassID, F)); - if (RI == FunctionAnalysisResults.end()) - return; - - FunctionAnalysisResultLists[F].erase(RI->second); -} - -void FunctionAnalysisManager::invalidateImpl(Function *F, - const PreservedAnalyses &PA) { - // Clear all the invalidated results associated specifically with this - // function. - SmallVector InvalidatedPassIDs; - FunctionAnalysisResultListT &ResultsList = FunctionAnalysisResultLists[F]; - for (FunctionAnalysisResultListT::iterator I = ResultsList.begin(), - E = ResultsList.end(); - I != E;) - if (I->second->invalidate(F, PA)) { - InvalidatedPassIDs.push_back(I->first); - I = ResultsList.erase(I); - } else { - ++I; - } - while (!InvalidatedPassIDs.empty()) - FunctionAnalysisResults.erase( - std::make_pair(InvalidatedPassIDs.pop_back_val(), F)); - if (ResultsList.empty()) - FunctionAnalysisResultLists.erase(F); -} - char FunctionAnalysisManagerModuleProxy::PassID; FunctionAnalysisManagerModuleProxy::Result -FunctionAnalysisManagerModuleProxy::run(Module *M) { +FunctionAnalysisManagerModuleProxy::run(Module &M) { assert(FAM->empty() && "Function analyses ran prior to the module proxy!"); return Result(*FAM); } @@ -189,7 +28,7 @@ FunctionAnalysisManagerModuleProxy::Result::~Result() { } bool FunctionAnalysisManagerModuleProxy::Result::invalidate( - Module *M, const PreservedAnalyses &PA) { + Module &M, const PreservedAnalyses &PA) { // If this proxy isn't marked as preserved, then we can't even invalidate // individual function analyses, there may be an invalid set of Function // objects in the cache making it impossible to incrementally preserve them. diff --git a/lib/IR/Statepoint.cpp b/lib/IR/Statepoint.cpp new file mode 100644 index 0000000..83ee611 --- /dev/null +++ b/lib/IR/Statepoint.cpp @@ -0,0 +1,77 @@ +//===-- IR/Statepoint.cpp -- gc.statepoint utilities --- -----------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// +//===----------------------------------------------------------------------===// + +#include "llvm/IR/Function.h" +#include "llvm/IR/Constant.h" +#include "llvm/IR/Constants.h" +#include "llvm/IR/Statepoint.h" +#include "llvm/Support/CommandLine.h" + +using namespace std; +using namespace llvm; + +bool llvm::isStatepoint(const ImmutableCallSite &CS) { + if (!CS.getInstruction()) { + // This is not a call site + return false; + } + + const Function *F = CS.getCalledFunction(); + return (F && F->getIntrinsicID() == Intrinsic::experimental_gc_statepoint); +} +bool llvm::isStatepoint(const Value *inst) { + if (isa(inst) || isa(inst)) { + ImmutableCallSite CS(inst); + return isStatepoint(CS); + } + return false; +} +bool llvm::isStatepoint(const Value &inst) { + return isStatepoint(&inst); +} + +bool llvm::isGCRelocate(const ImmutableCallSite &CS) { + if (!CS.getInstruction()) { + // This is not a call site + return false; + } + + return isGCRelocate(CS.getInstruction()); +} +bool llvm::isGCRelocate(const Value *inst) { + if (const CallInst *call = dyn_cast(inst)) { + if (const Function *F = call->getCalledFunction()) { + return F->getIntrinsicID() == Intrinsic::experimental_gc_relocate; + } + } + return false; +} + +bool llvm::isGCResult(const ImmutableCallSite &CS) { + if (!CS.getInstruction()) { + // This is not a call site + return false; + } + + return isGCResult(CS.getInstruction()); +} +bool llvm::isGCResult(const Value *inst) { + if (const CallInst *call = dyn_cast(inst)) { + if (Function *F = call->getCalledFunction()) { + return (F->getIntrinsicID() == Intrinsic::experimental_gc_result_int || + F->getIntrinsicID() == Intrinsic::experimental_gc_result_float || + F->getIntrinsicID() == Intrinsic::experimental_gc_result_ptr || + F->getIntrinsicID() == Intrinsic::experimental_gc_result); + } + } + return false; +} diff --git a/lib/IR/Type.cpp b/lib/IR/Type.cpp index 0458b5f..65060dc 100644 --- a/lib/IR/Type.cpp +++ b/lib/IR/Type.cpp @@ -360,8 +360,7 @@ FunctionType *FunctionType::get(Type *ReturnType, ArrayRef Params, bool isVarArg) { LLVMContextImpl *pImpl = ReturnType->getContext().pImpl; FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg); - LLVMContextImpl::FunctionTypeMap::iterator I = - pImpl->FunctionTypes.find_as(Key); + auto I = pImpl->FunctionTypes.find_as(Key); FunctionType *FT; if (I == pImpl->FunctionTypes.end()) { @@ -369,9 +368,9 @@ FunctionType *FunctionType::get(Type *ReturnType, Allocate(sizeof(FunctionType) + sizeof(Type*) * (Params.size() + 1), AlignOf::Alignment); new (FT) FunctionType(ReturnType, Params, isVarArg); - pImpl->FunctionTypes[FT] = true; + pImpl->FunctionTypes.insert(FT); } else { - FT = I->first; + FT = *I; } return FT; @@ -404,8 +403,7 @@ StructType *StructType::get(LLVMContext &Context, ArrayRef ETypes, bool isPacked) { LLVMContextImpl *pImpl = Context.pImpl; AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked); - LLVMContextImpl::StructTypeMap::iterator I = - pImpl->AnonStructTypes.find_as(Key); + auto I = pImpl->AnonStructTypes.find_as(Key); StructType *ST; if (I == pImpl->AnonStructTypes.end()) { @@ -413,9 +411,9 @@ StructType *StructType::get(LLVMContext &Context, ArrayRef ETypes, ST = new (Context.pImpl->TypeAllocator) StructType(Context); ST->setSubclassData(SCDB_IsLiteral); // Literal struct. ST->setBody(ETypes, isPacked); - Context.pImpl->AnonStructTypes[ST] = true; + Context.pImpl->AnonStructTypes.insert(ST); } else { - ST = I->first; + ST = *I; } return ST; @@ -710,9 +708,10 @@ VectorType::VectorType(Type *ElType, unsigned NumEl) VectorType *VectorType::get(Type *elementType, unsigned NumElements) { Type *ElementType = const_cast(elementType); assert(NumElements > 0 && "#Elements of a VectorType must be greater than 0"); - assert(isValidElementType(ElementType) && - "Elements of a VectorType must be a primitive type"); - + assert(isValidElementType(ElementType) && "Element type of a VectorType must " + "be an integer, floating point, or " + "pointer type."); + LLVMContextImpl *pImpl = ElementType->getContext().pImpl; VectorType *&Entry = ElementType->getContext().pImpl ->VectorTypes[std::make_pair(ElementType, NumElements)]; diff --git a/lib/IR/TypeFinder.cpp b/lib/IR/TypeFinder.cpp index 6796075..e2fb8f8 100644 --- a/lib/IR/TypeFinder.cpp +++ b/lib/IR/TypeFinder.cpp @@ -47,6 +47,9 @@ void TypeFinder::run(const Module &M, bool onlyNamed) { if (FI->hasPrefixData()) incorporateValue(FI->getPrefixData()); + if (FI->hasPrologueData()) + incorporateValue(FI->getPrologueData()); + // First incorporate the arguments. for (Function::const_arg_iterator AI = FI->arg_begin(), AE = FI->arg_end(); AI != AE; ++AI) @@ -122,8 +125,13 @@ void TypeFinder::incorporateType(Type *Ty) { /// other ways. GlobalValues, basic blocks, instructions, and inst operands are /// all explicitly enumerated. void TypeFinder::incorporateValue(const Value *V) { - if (const MDNode *M = dyn_cast(V)) - return incorporateMDNode(M); + if (const auto *M = dyn_cast(V)) { + if (const auto *N = dyn_cast(M->getMetadata())) + return incorporateMDNode(N); + if (const auto *MDV = dyn_cast(M->getMetadata())) + return incorporateValue(MDV->getValue()); + return; + } if (!isa(V) || isa(V)) return; @@ -149,11 +157,21 @@ void TypeFinder::incorporateValue(const Value *V) { /// find types hiding within. void TypeFinder::incorporateMDNode(const MDNode *V) { // Already visited? - if (!VisitedConstants.insert(V).second) + if (!VisitedMetadata.insert(V).second) return; // Look in operands for types. - for (unsigned i = 0, e = V->getNumOperands(); i != e; ++i) - if (Value *Op = V->getOperand(i)) - incorporateValue(Op); + for (unsigned i = 0, e = V->getNumOperands(); i != e; ++i) { + Metadata *Op = V->getOperand(i); + if (!Op) + continue; + if (auto *N = dyn_cast(Op)) { + incorporateMDNode(N); + continue; + } + if (auto *C = dyn_cast(Op)) { + incorporateValue(C->getValue()); + continue; + } + } } diff --git a/lib/IR/Value.cpp b/lib/IR/Value.cpp index 4e0c11f1..7d205f9 100644 --- a/lib/IR/Value.cpp +++ b/lib/IR/Value.cpp @@ -23,9 +23,10 @@ #include "llvm/IR/GetElementPtrTypeIterator.h" #include "llvm/IR/InstrTypes.h" #include "llvm/IR/Instructions.h" -#include "llvm/IR/LeakDetector.h" +#include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/Module.h" #include "llvm/IR/Operator.h" +#include "llvm/IR/Statepoint.h" #include "llvm/IR/ValueHandle.h" #include "llvm/IR/ValueSymbolTable.h" #include "llvm/Support/Debug.h" @@ -44,9 +45,8 @@ static inline Type *checkType(Type *Ty) { } Value::Value(Type *ty, unsigned scid) - : VTy(checkType(ty)), UseList(nullptr), Name(nullptr), SubclassID(scid), - HasValueHandle(0), SubclassOptionalData(0), SubclassData(0), - NumOperands(0) { + : VTy(checkType(ty)), UseList(nullptr), SubclassID(scid), HasValueHandle(0), + SubclassOptionalData(0), SubclassData(0), NumOperands(0) { // FIXME: Why isn't this in the subclass gunk?? // Note, we cannot call isa before the CallInst has been // constructed. @@ -63,6 +63,8 @@ Value::~Value() { // Notify all ValueHandles (if present) that this value is going away. if (HasValueHandle) ValueHandleBase::ValueIsDeleted(this); + if (isUsedByMetadata()) + ValueAsMetadata::handleDeletion(this); #ifndef NDEBUG // Only in -g mode... // Check to make sure that there are no uses of this value that are still @@ -82,11 +84,14 @@ Value::~Value() { // If this value is named, destroy the name. This should not be in a symtab // at this point. - if (Name && SubclassID != MDStringVal) - Name->Destroy(); + destroyValueName(); +} - // There should be no uses of this object anymore, remove it. - LeakDetector::removeGarbageObject(this); +void Value::destroyValueName() { + ValueName *Name = getValueName(); + if (Name) + Name->Destroy(); + setValueName(nullptr); } bool Value::hasNUses(unsigned N) const { @@ -146,9 +151,7 @@ static bool getSymTab(Value *V, ValueSymbolTable *&ST) { } else if (Argument *A = dyn_cast(V)) { if (Function *P = A->getParent()) ST = &P->getValueSymbolTable(); - } else if (isa(V)) - return true; - else { + } else { assert(isa(V) && "Unknown value type!"); return true; // no name is setable for this. } @@ -159,14 +162,12 @@ StringRef Value::getName() const { // Make sure the empty string is still a C string. For historical reasons, // some clients want to call .data() on the result and expect it to be null // terminated. - if (!Name) return StringRef("", 0); - return Name->getKey(); + if (!getValueName()) + return StringRef("", 0); + return getValueName()->getKey(); } void Value::setName(const Twine &NewName) { - assert(SubclassID != MDStringVal && - "Cannot set the name of MDString with this method!"); - // Fast path for common IRBuilder case of setName("") when there is no name. if (NewName.isTriviallyEmpty() && !hasName()) return; @@ -193,20 +194,17 @@ void Value::setName(const Twine &NewName) { if (!ST) { // No symbol table to update? Just do the change. if (NameRef.empty()) { // Free the name for this value. - Name->Destroy(); - Name = nullptr; + destroyValueName(); return; } - if (Name) - Name->Destroy(); - // NOTE: Could optimize for the case the name is shrinking to not deallocate // then reallocated. + destroyValueName(); // Create the new name. - Name = ValueName::Create(NameRef); - Name->setValue(this); + setValueName(ValueName::Create(NameRef)); + getValueName()->setValue(this); return; } @@ -214,21 +212,18 @@ void Value::setName(const Twine &NewName) { // then reallocated. if (hasName()) { // Remove old name. - ST->removeValueName(Name); - Name->Destroy(); - Name = nullptr; + ST->removeValueName(getValueName()); + destroyValueName(); if (NameRef.empty()) return; } // Name is changing to something new. - Name = ST->createValueName(NameRef, this); + setValueName(ST->createValueName(NameRef, this)); } void Value::takeName(Value *V) { - assert(SubclassID != MDStringVal && "Cannot take the name of an MDString!"); - ValueSymbolTable *ST = nullptr; // If this value has a name, drop it. if (hasName()) { @@ -242,9 +237,8 @@ void Value::takeName(Value *V) { // Remove old name. if (ST) - ST->removeValueName(Name); - Name->Destroy(); - Name = nullptr; + ST->removeValueName(getValueName()); + destroyValueName(); } // Now we know that this has no name. @@ -270,9 +264,9 @@ void Value::takeName(Value *V) { // This works even if both values have no symtab yet. if (ST == VST) { // Take the name! - Name = V->Name; - V->Name = nullptr; - Name->setValue(this); + setValueName(V->getValueName()); + V->setValueName(nullptr); + getValueName()->setValue(this); return; } @@ -280,10 +274,10 @@ void Value::takeName(Value *V) { // then reinsert it into ST. if (VST) - VST->removeValueName(V->Name); - Name = V->Name; - V->Name = nullptr; - Name->setValue(this); + VST->removeValueName(V->getValueName()); + setValueName(V->getValueName()); + V->setValueName(nullptr); + getValueName()->setValue(this); if (ST) ST->reinsertValue(this); @@ -334,6 +328,8 @@ void Value::replaceAllUsesWith(Value *New) { // Notify all ValueHandles (if present) that this value is going away. if (HasValueHandle) ValueHandleBase::ValueIsRAUWd(this, New); + if (isUsedByMetadata()) + ValueAsMetadata::handleRAUW(this, New); while (!use_empty()) { Use &U = *UseList; @@ -353,6 +349,28 @@ void Value::replaceAllUsesWith(Value *New) { BB->replaceSuccessorsPhiUsesWith(cast(New)); } +// Like replaceAllUsesWith except it does not handle constants or basic blocks. +// This routine leaves uses within BB. +void Value::replaceUsesOutsideBlock(Value *New, BasicBlock *BB) { + assert(New && "Value::replaceUsesOutsideBlock(, BB) is invalid!"); + assert(!contains(New, this) && + "this->replaceUsesOutsideBlock(expr(this), BB) is NOT valid!"); + assert(New->getType() == getType() && + "replaceUses of value with new value of different type!"); + assert(BB && "Basic block that may contain a use of 'New' must be defined\n"); + + use_iterator UI = use_begin(), E = use_end(); + for (; UI != E;) { + Use &U = *UI; + ++UI; + auto *Usr = dyn_cast(U.getUser()); + if (Usr && Usr->getParent() == BB) + continue; + U.set(New); + } + return; +} + namespace { // Various metrics for how much to strip off of pointers. enum PointerStripKind { @@ -480,7 +498,7 @@ static bool isDereferenceablePointer(const Value *V, const DataLayout *DL, // is at least as large as for the resulting pointer type, then // we can look through the bitcast. if (DL) - if (const BitCastInst* BC = dyn_cast(V)) { + if (const BitCastOperator *BC = dyn_cast(V)) { Type *STy = BC->getSrcTy()->getPointerElementType(), *DTy = BC->getDestTy()->getPointerElementType(); if (STy->isSized() && DTy->isSized() && @@ -554,6 +572,13 @@ static bool isDereferenceablePointer(const Value *V, const DataLayout *DL, return true; } + // For gc.relocate, look through relocations + if (const IntrinsicInst *I = dyn_cast(V)) + if (I->getIntrinsicID() == Intrinsic::experimental_gc_relocate) { + GCRelocateOperands RelocateInst(I); + return isDereferenceablePointer(RelocateInst.derivedPtr(), DL, Visited); + } + if (const AddrSpaceCastInst *ASC = dyn_cast(V)) return isDereferenceablePointer(ASC->getOperand(0), DL, Visited); @@ -629,7 +654,7 @@ void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) { setPrevPtr(List); if (Next) { Next->setPrevPtr(&Next); - assert(VP.getPointer() == Next->VP.getPointer() && "Added to wrong list?"); + assert(V == Next->V && "Added to wrong list?"); } } @@ -644,14 +669,14 @@ void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) { } void ValueHandleBase::AddToUseList() { - assert(VP.getPointer() && "Null pointer doesn't have a use list!"); + assert(V && "Null pointer doesn't have a use list!"); - LLVMContextImpl *pImpl = VP.getPointer()->getContext().pImpl; + LLVMContextImpl *pImpl = V->getContext().pImpl; - if (VP.getPointer()->HasValueHandle) { + if (V->HasValueHandle) { // If this value already has a ValueHandle, then it must be in the // ValueHandles map already. - ValueHandleBase *&Entry = pImpl->ValueHandles[VP.getPointer()]; + ValueHandleBase *&Entry = pImpl->ValueHandles[V]; assert(Entry && "Value doesn't have any handles?"); AddToExistingUseList(&Entry); return; @@ -665,10 +690,10 @@ void ValueHandleBase::AddToUseList() { DenseMap &Handles = pImpl->ValueHandles; const void *OldBucketPtr = Handles.getPointerIntoBucketsArray(); - ValueHandleBase *&Entry = Handles[VP.getPointer()]; + ValueHandleBase *&Entry = Handles[V]; assert(!Entry && "Value really did already have handles?"); AddToExistingUseList(&Entry); - VP.getPointer()->HasValueHandle = true; + V->HasValueHandle = true; // If reallocation didn't happen or if this was the first insertion, don't // walk the table. @@ -680,14 +705,14 @@ void ValueHandleBase::AddToUseList() { // Okay, reallocation did happen. Fix the Prev Pointers. for (DenseMap::iterator I = Handles.begin(), E = Handles.end(); I != E; ++I) { - assert(I->second && I->first == I->second->VP.getPointer() && + assert(I->second && I->first == I->second->V && "List invariant broken!"); I->second->setPrevPtr(&I->second); } } void ValueHandleBase::RemoveFromUseList() { - assert(VP.getPointer() && VP.getPointer()->HasValueHandle && + assert(V && V->HasValueHandle && "Pointer doesn't have a use list!"); // Unlink this from its use list. @@ -704,11 +729,11 @@ void ValueHandleBase::RemoveFromUseList() { // If the Next pointer was null, then it is possible that this was the last // ValueHandle watching VP. If so, delete its entry from the ValueHandles // map. - LLVMContextImpl *pImpl = VP.getPointer()->getContext().pImpl; + LLVMContextImpl *pImpl = V->getContext().pImpl; DenseMap &Handles = pImpl->ValueHandles; if (Handles.isPointerIntoBucketsArray(PrevPtr)) { - Handles.erase(VP.getPointer()); - VP.getPointer()->HasValueHandle = false; + Handles.erase(V); + V->HasValueHandle = false; } } diff --git a/lib/IR/ValueSymbolTable.cpp b/lib/IR/ValueSymbolTable.cpp index 2b23f6d..4f078f0 100644 --- a/lib/IR/ValueSymbolTable.cpp +++ b/lib/IR/ValueSymbolTable.cpp @@ -38,8 +38,8 @@ void ValueSymbolTable::reinsertValue(Value* V) { assert(V->hasName() && "Can't insert nameless Value into symbol table"); // Try inserting the name, assuming it won't conflict. - if (vmap.insert(V->Name)) { - //DEBUG(dbgs() << " Inserted value: " << V->Name << ": " << *V << "\n"); + if (vmap.insert(V->getValueName())) { + //DEBUG(dbgs() << " Inserted value: " << V->getValueName() << ": " << *V << "\n"); return; } @@ -47,8 +47,8 @@ void ValueSymbolTable::reinsertValue(Value* V) { SmallString<256> UniqueName(V->getName().begin(), V->getName().end()); // The name is too already used, just free it so we can allocate a new name. - V->Name->Destroy(); - + V->getValueName()->Destroy(); + unsigned BaseSize = UniqueName.size(); while (1) { // Trim any suffix off and append the next number. @@ -59,7 +59,7 @@ void ValueSymbolTable::reinsertValue(Value* V) { auto IterBool = vmap.insert(std::make_pair(UniqueName, V)); if (IterBool.second) { // Newly inserted name. Success! - V->Name = &*IterBool.first; + V->setValueName(&*IterBool.first); //DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V << "\n"); return; } diff --git a/lib/IR/Verifier.cpp b/lib/IR/Verifier.cpp index 9698dbd..d01e138 100644 --- a/lib/IR/Verifier.cpp +++ b/lib/IR/Verifier.cpp @@ -68,6 +68,7 @@ #include "llvm/IR/Metadata.h" #include "llvm/IR/Module.h" #include "llvm/IR/PassManager.h" +#include "llvm/IR/Statepoint.h" #include "llvm/Pass.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" @@ -101,6 +102,13 @@ struct VerifierSupport { } } + void WriteMetadata(const Metadata *MD) { + if (!MD) + return; + MD->printAsOperand(OS, true, M); + OS << '\n'; + } + void WriteType(Type *T) { if (!T) return; @@ -127,6 +135,24 @@ struct VerifierSupport { Broken = true; } + void CheckFailed(const Twine &Message, const Metadata *V1, const Metadata *V2, + const Metadata *V3 = nullptr, const Metadata *V4 = nullptr) { + OS << Message.str() << "\n"; + WriteMetadata(V1); + WriteMetadata(V2); + WriteMetadata(V3); + WriteMetadata(V4); + Broken = true; + } + + void CheckFailed(const Twine &Message, const Metadata *V1, + const Value *V2 = nullptr) { + OS << Message.str() << "\n"; + WriteMetadata(V1); + WriteValue(V2); + Broken = true; + } + void CheckFailed(const Twine &Message, const Value *V1, Type *T2, const Value *V3 = nullptr) { OS << Message.str() << "\n"; @@ -155,7 +181,6 @@ class Verifier : public InstVisitor, VerifierSupport { friend class InstVisitor; LLVMContext *Context; - const DataLayout *DL; DominatorTree DT; /// \brief When verifying a basic block, keep track of all of the @@ -166,17 +191,21 @@ class Verifier : public InstVisitor, VerifierSupport { SmallPtrSet InstsInThisBlock; /// \brief Keep track of the metadata nodes that have been checked already. - SmallPtrSet MDNodes; + SmallPtrSet MDNodes; /// \brief The personality function referenced by the LandingPadInsts. /// All LandingPadInsts within the same function must use the same /// personality function. const Value *PersonalityFn; + /// \brief Whether we've seen a call to @llvm.frameallocate in this function + /// already. + bool SawFrameAllocate; + public: explicit Verifier(raw_ostream &OS = dbgs()) - : VerifierSupport(OS), Context(nullptr), DL(nullptr), - PersonalityFn(nullptr) {} + : VerifierSupport(OS), Context(nullptr), PersonalityFn(nullptr), + SawFrameAllocate(false) {} bool verify(const Function &F) { M = F.getParent(); @@ -211,6 +240,7 @@ public: visit(const_cast(F)); InstsInThisBlock.clear(); PersonalityFn = nullptr; + SawFrameAllocate = false; return !Broken; } @@ -260,7 +290,9 @@ private: void visitAliaseeSubExpr(SmallPtrSetImpl &Visited, const GlobalAlias &A, const Constant &C); void visitNamedMDNode(const NamedMDNode &NMD); - void visitMDNode(MDNode &MD, Function *F); + void visitMDNode(const MDNode &MD); + void visitMetadataAsValue(const MetadataAsValue &MD, Function *F); + void visitValueAsMetadata(const ValueAsMetadata &MD, Function *F); void visitComdat(const Comdat &C); void visitModuleIdents(const Module &M); void visitModuleFlags(const Module &M); @@ -271,6 +303,8 @@ private: void visitBasicBlock(BasicBlock &BB); void visitRangeMetadata(Instruction& I, MDNode* Range, Type* Ty); +#define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) void visit##CLASS(const CLASS &N); +#include "llvm/IR/Metadata.def" // InstVisitor overrides... using InstVisitor::visit; @@ -337,8 +371,8 @@ private: void VerifyFunctionAttrs(FunctionType *FT, AttributeSet Attrs, const Value *V); - void VerifyBitcastType(const Value *V, Type *DestTy, Type *SrcTy); void VerifyConstantExprBitcastType(const ConstantExpr *CE); + void VerifyStatepoint(ImmutableCallSite CS); }; class DebugInfoVerifier : public VerifierSupport { public: @@ -484,8 +518,7 @@ void Verifier::visitGlobalVariable(const GlobalVariable &GV) { continue; if (const User *U = dyn_cast(V)) { - for (unsigned I = 0, N = U->getNumOperands(); I != N; ++I) - WorkStack.push_back(U->getOperand(I)); + WorkStack.append(U->op_begin(), U->op_end()); } if (const ConstantExpr *CE = dyn_cast(V)) { @@ -559,59 +592,210 @@ void Verifier::visitNamedMDNode(const NamedMDNode &NMD) { if (!MD) continue; - Assert1(!MD->isFunctionLocal(), - "Named metadata operand cannot be function local!", MD); - visitMDNode(*MD, nullptr); + visitMDNode(*MD); } } -void Verifier::visitMDNode(MDNode &MD, Function *F) { +void Verifier::visitMDNode(const MDNode &MD) { // Only visit each node once. Metadata can be mutually recursive, so this // avoids infinite recursion here, as well as being an optimization. if (!MDNodes.insert(&MD).second) return; + switch (MD.getMetadataID()) { + default: + llvm_unreachable("Invalid MDNode subclass"); + case Metadata::MDTupleKind: + break; +#define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \ + case Metadata::CLASS##Kind: \ + visit##CLASS(cast(MD)); \ + break; +#include "llvm/IR/Metadata.def" + } + for (unsigned i = 0, e = MD.getNumOperands(); i != e; ++i) { - Value *Op = MD.getOperand(i); + Metadata *Op = MD.getOperand(i); if (!Op) continue; - if (isa(Op) || isa(Op)) + Assert2(!isa(Op), "Invalid operand for global metadata!", + &MD, Op); + if (auto *N = dyn_cast(Op)) { + visitMDNode(*N); continue; - if (MDNode *N = dyn_cast(Op)) { - Assert2(MD.isFunctionLocal() || !N->isFunctionLocal(), - "Global metadata operand cannot be function local!", &MD, N); - visitMDNode(*N, F); + } + if (auto *V = dyn_cast(Op)) { + visitValueAsMetadata(*V, nullptr); continue; } - Assert2(MD.isFunctionLocal(), "Invalid operand for global metadata!", &MD, Op); - - // If this was an instruction, bb, or argument, verify that it is in the - // function that we expect. - Function *ActualF = nullptr; - if (Instruction *I = dyn_cast(Op)) - ActualF = I->getParent()->getParent(); - else if (BasicBlock *BB = dyn_cast(Op)) - ActualF = BB->getParent(); - else if (Argument *A = dyn_cast(Op)) - ActualF = A->getParent(); - assert(ActualF && "Unimplemented function local metadata case!"); - - Assert2(ActualF == F, "function-local metadata used in wrong function", - &MD, Op); } + + // Check these last, so we diagnose problems in operands first. + Assert1(!MD.isTemporary(), "Expected no forward declarations!", &MD); + Assert1(MD.isResolved(), "All nodes should be resolved!", &MD); +} + +void Verifier::visitValueAsMetadata(const ValueAsMetadata &MD, Function *F) { + Assert1(MD.getValue(), "Expected valid value", &MD); + Assert2(!MD.getValue()->getType()->isMetadataTy(), + "Unexpected metadata round-trip through values", &MD, MD.getValue()); + + auto *L = dyn_cast(&MD); + if (!L) + return; + + Assert1(F, "function-local metadata used outside a function", L); + + // If this was an instruction, bb, or argument, verify that it is in the + // function that we expect. + Function *ActualF = nullptr; + if (Instruction *I = dyn_cast(L->getValue())) { + Assert2(I->getParent(), "function-local metadata not in basic block", L, I); + ActualF = I->getParent()->getParent(); + } else if (BasicBlock *BB = dyn_cast(L->getValue())) + ActualF = BB->getParent(); + else if (Argument *A = dyn_cast(L->getValue())) + ActualF = A->getParent(); + assert(ActualF && "Unimplemented function local metadata case!"); + + Assert1(ActualF == F, "function-local metadata used in wrong function", L); +} + +void Verifier::visitMetadataAsValue(const MetadataAsValue &MDV, Function *F) { + Metadata *MD = MDV.getMetadata(); + if (auto *N = dyn_cast(MD)) { + visitMDNode(*N); + return; + } + + // Only visit each node once. Metadata can be mutually recursive, so this + // avoids infinite recursion here, as well as being an optimization. + if (!MDNodes.insert(MD).second) + return; + + if (auto *V = dyn_cast(MD)) + visitValueAsMetadata(*V, F); +} + +void Verifier::visitMDLocation(const MDLocation &N) { + Assert1(N.getScope(), "location requires a valid scope", &N); + if (auto *IA = N.getInlinedAt()) + Assert2(isa(IA), "inlined-at should be a location", &N, IA); +} + +void Verifier::visitGenericDebugNode(const GenericDebugNode &N) { + Assert1(N.getTag(), "invalid tag", &N); +} + +void Verifier::visitMDSubrange(const MDSubrange &N) { + Assert1(N.getTag() == dwarf::DW_TAG_subrange_type, "invalid tag", &N); +} + +void Verifier::visitMDEnumerator(const MDEnumerator &N) { + Assert1(N.getTag() == dwarf::DW_TAG_enumerator, "invalid tag", &N); +} + +void Verifier::visitMDBasicType(const MDBasicType &N) { + Assert1(N.getTag() == dwarf::DW_TAG_base_type || + N.getTag() == dwarf::DW_TAG_unspecified_type, + "invalid tag", &N); +} + +void Verifier::visitMDDerivedType(const MDDerivedType &N) { + Assert1(N.getTag() == dwarf::DW_TAG_typedef || + N.getTag() == dwarf::DW_TAG_pointer_type || + N.getTag() == dwarf::DW_TAG_ptr_to_member_type || + N.getTag() == dwarf::DW_TAG_reference_type || + N.getTag() == dwarf::DW_TAG_rvalue_reference_type || + N.getTag() == dwarf::DW_TAG_const_type || + N.getTag() == dwarf::DW_TAG_volatile_type || + N.getTag() == dwarf::DW_TAG_restrict_type || + N.getTag() == dwarf::DW_TAG_member || + N.getTag() == dwarf::DW_TAG_inheritance || + N.getTag() == dwarf::DW_TAG_friend, + "invalid tag", &N); +} + +void Verifier::visitMDCompositeType(const MDCompositeType &N) { + Assert1(N.getTag() == dwarf::DW_TAG_array_type || + N.getTag() == dwarf::DW_TAG_structure_type || + N.getTag() == dwarf::DW_TAG_union_type || + N.getTag() == dwarf::DW_TAG_enumeration_type || + N.getTag() == dwarf::DW_TAG_subroutine_type || + N.getTag() == dwarf::DW_TAG_class_type, + "invalid tag", &N); +} + +void Verifier::visitMDSubroutineType(const MDSubroutineType &N) { + Assert1(N.getTag() == dwarf::DW_TAG_subroutine_type, "invalid tag", &N); +} + +void Verifier::visitMDFile(const MDFile &N) { + Assert1(N.getTag() == dwarf::DW_TAG_file_type, "invalid tag", &N); +} + +void Verifier::visitMDCompileUnit(const MDCompileUnit &N) { + Assert1(N.getTag() == dwarf::DW_TAG_compile_unit, "invalid tag", &N); +} + +void Verifier::visitMDSubprogram(const MDSubprogram &N) { + Assert1(N.getTag() == dwarf::DW_TAG_subprogram, "invalid tag", &N); +} + +void Verifier::visitMDLexicalBlock(const MDLexicalBlock &N) { + Assert1(N.getTag() == dwarf::DW_TAG_lexical_block, "invalid tag", &N); +} + +void Verifier::visitMDLexicalBlockFile(const MDLexicalBlockFile &N) { + Assert1(N.getTag() == dwarf::DW_TAG_lexical_block, "invalid tag", &N); +} + +void Verifier::visitMDNamespace(const MDNamespace &N) { + Assert1(N.getTag() == dwarf::DW_TAG_namespace, "invalid tag", &N); +} + +void Verifier::visitMDTemplateTypeParameter(const MDTemplateTypeParameter &N) { + Assert1(N.getTag() == dwarf::DW_TAG_template_type_parameter, "invalid tag", + &N); +} + +void Verifier::visitMDTemplateValueParameter( + const MDTemplateValueParameter &N) { + Assert1(N.getTag() == dwarf::DW_TAG_template_value_parameter || + N.getTag() == dwarf::DW_TAG_GNU_template_template_param || + N.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack, + "invalid tag", &N); +} + +void Verifier::visitMDGlobalVariable(const MDGlobalVariable &N) { + Assert1(N.getTag() == dwarf::DW_TAG_variable, "invalid tag", &N); +} + +void Verifier::visitMDLocalVariable(const MDLocalVariable &N) { + Assert1(N.getTag() == dwarf::DW_TAG_auto_variable || + N.getTag() == dwarf::DW_TAG_arg_variable, + "invalid tag", &N); +} + +void Verifier::visitMDExpression(const MDExpression &N) { + Assert1(N.getTag() == dwarf::DW_TAG_expression, "invalid tag", &N); + Assert1(N.isValid(), "invalid expression", &N); +} + +void Verifier::visitMDObjCProperty(const MDObjCProperty &N) { + Assert1(N.getTag() == dwarf::DW_TAG_APPLE_property, "invalid tag", &N); +} + +void Verifier::visitMDImportedEntity(const MDImportedEntity &N) { + Assert1(N.getTag() == dwarf::DW_TAG_imported_module || + N.getTag() == dwarf::DW_TAG_imported_declaration, + "invalid tag", &N); } void Verifier::visitComdat(const Comdat &C) { - // All Comdat::SelectionKind values other than Comdat::Any require a - // GlobalValue with the same name as the Comdat. - const GlobalValue *GV = M->getNamedValue(C.getName()); - if (C.getSelectionKind() != Comdat::Any) - Assert1(GV, - "comdat selection kind requires a global value with the same name", - &C); // The Module is invalid if the GlobalValue has private linkage. Entities // with private linkage don't have entries in the symbol table. - if (GV) + if (const GlobalValue *GV = M->getNamedValue(C.getName())) Assert1(!GV->hasPrivateLinkage(), "comdat global value has private linkage", GV); } @@ -627,7 +811,7 @@ void Verifier::visitModuleIdents(const Module &M) { const MDNode *N = Idents->getOperand(i); Assert1(N->getNumOperands() == 1, "incorrect number of operands in llvm.ident metadata", N); - Assert1(isa(N->getOperand(0)), + Assert1(dyn_cast_or_null(N->getOperand(0)), ("invalid value for llvm.ident metadata entry operand" "(the operand should be a string)"), N->getOperand(0)); @@ -649,7 +833,7 @@ void Verifier::visitModuleFlags(const Module &M) { for (unsigned I = 0, E = Requirements.size(); I != E; ++I) { const MDNode *Requirement = Requirements[I]; const MDString *Flag = cast(Requirement->getOperand(0)); - const Value *ReqValue = Requirement->getOperand(1); + const Metadata *ReqValue = Requirement->getOperand(1); const MDNode *Op = SeenIDs.lookup(Flag); if (!Op) { @@ -678,14 +862,14 @@ Verifier::visitModuleFlag(const MDNode *Op, Module::ModFlagBehavior MFB; if (!Module::isValidModFlagBehavior(Op->getOperand(0), MFB)) { Assert1( - dyn_cast(Op->getOperand(0)), + mdconst::dyn_extract_or_null(Op->getOperand(0)), "invalid behavior operand in module flag (expected constant integer)", Op->getOperand(0)); Assert1(false, "invalid behavior operand in module flag (unexpected constant)", Op->getOperand(0)); } - MDString *ID = dyn_cast(Op->getOperand(1)); + MDString *ID = dyn_cast_or_null(Op->getOperand(1)); Assert1(ID, "invalid ID operand in module flag (expected metadata string)", Op->getOperand(1)); @@ -960,48 +1144,13 @@ void Verifier::VerifyFunctionAttrs(FunctionType *FT, AttributeSet Attrs, } } -void Verifier::VerifyBitcastType(const Value *V, Type *DestTy, Type *SrcTy) { - // Get the size of the types in bits, we'll need this later - unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); - unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); - - // BitCast implies a no-op cast of type only. No bits change. - // However, you can't cast pointers to anything but pointers. - Assert1(SrcTy->isPointerTy() == DestTy->isPointerTy(), - "Bitcast requires both operands to be pointer or neither", V); - Assert1(SrcBitSize == DestBitSize, - "Bitcast requires types of same width", V); - - // Disallow aggregates. - Assert1(!SrcTy->isAggregateType(), - "Bitcast operand must not be aggregate", V); - Assert1(!DestTy->isAggregateType(), - "Bitcast type must not be aggregate", V); - - // Without datalayout, assume all address spaces are the same size. - // Don't check if both types are not pointers. - // Skip casts between scalars and vectors. - if (!DL || - !SrcTy->isPtrOrPtrVectorTy() || - !DestTy->isPtrOrPtrVectorTy() || - SrcTy->isVectorTy() != DestTy->isVectorTy()) { +void Verifier::VerifyConstantExprBitcastType(const ConstantExpr *CE) { + if (CE->getOpcode() != Instruction::BitCast) return; - } - - unsigned SrcAS = SrcTy->getPointerAddressSpace(); - unsigned DstAS = DestTy->getPointerAddressSpace(); - - Assert1(SrcAS == DstAS, - "Bitcasts between pointers of different address spaces is not legal." - "Use AddrSpaceCast instead.", V); -} -void Verifier::VerifyConstantExprBitcastType(const ConstantExpr *CE) { - if (CE->getOpcode() == Instruction::BitCast) { - Type *SrcTy = CE->getOperand(0)->getType(); - Type *DstTy = CE->getType(); - VerifyBitcastType(CE, DstTy, SrcTy); - } + Assert1(CastInst::castIsValid(Instruction::BitCast, CE->getOperand(0), + CE->getType()), + "Invalid bitcast", CE); } bool Verifier::VerifyAttributeCount(AttributeSet Attrs, unsigned Params) { @@ -1018,6 +1167,105 @@ bool Verifier::VerifyAttributeCount(AttributeSet Attrs, unsigned Params) { return false; } +/// \brief Verify that statepoint intrinsic is well formed. +void Verifier::VerifyStatepoint(ImmutableCallSite CS) { + assert(CS.getCalledFunction() && + CS.getCalledFunction()->getIntrinsicID() == + Intrinsic::experimental_gc_statepoint); + + const Instruction &CI = *CS.getInstruction(); + + Assert1(!CS.doesNotAccessMemory() && + !CS.onlyReadsMemory(), + "gc.statepoint must read and write memory to preserve " + "reordering restrictions required by safepoint semantics", &CI); + + const Value *Target = CS.getArgument(0); + const PointerType *PT = dyn_cast(Target->getType()); + Assert2(PT && PT->getElementType()->isFunctionTy(), + "gc.statepoint callee must be of function pointer type", + &CI, Target); + FunctionType *TargetFuncType = cast(PT->getElementType()); + + const Value *NumCallArgsV = CS.getArgument(1); + Assert1(isa(NumCallArgsV), + "gc.statepoint number of arguments to underlying call " + "must be constant integer", &CI); + const int NumCallArgs = cast(NumCallArgsV)->getZExtValue(); + Assert1(NumCallArgs >= 0, + "gc.statepoint number of arguments to underlying call " + "must be positive", &CI); + const int NumParams = (int)TargetFuncType->getNumParams(); + if (TargetFuncType->isVarArg()) { + Assert1(NumCallArgs >= NumParams, + "gc.statepoint mismatch in number of vararg call args", &CI); + + // TODO: Remove this limitation + Assert1(TargetFuncType->getReturnType()->isVoidTy(), + "gc.statepoint doesn't support wrapping non-void " + "vararg functions yet", &CI); + } else + Assert1(NumCallArgs == NumParams, + "gc.statepoint mismatch in number of call args", &CI); + + const Value *Unused = CS.getArgument(2); + Assert1(isa(Unused) && + cast(Unused)->isNullValue(), + "gc.statepoint parameter #3 must be zero", &CI); + + // Verify that the types of the call parameter arguments match + // the type of the wrapped callee. + for (int i = 0; i < NumParams; i++) { + Type *ParamType = TargetFuncType->getParamType(i); + Type *ArgType = CS.getArgument(3+i)->getType(); + Assert1(ArgType == ParamType, + "gc.statepoint call argument does not match wrapped " + "function type", &CI); + } + const int EndCallArgsInx = 2+NumCallArgs; + const Value *NumDeoptArgsV = CS.getArgument(EndCallArgsInx+1); + Assert1(isa(NumDeoptArgsV), + "gc.statepoint number of deoptimization arguments " + "must be constant integer", &CI); + const int NumDeoptArgs = cast(NumDeoptArgsV)->getZExtValue(); + Assert1(NumDeoptArgs >= 0, + "gc.statepoint number of deoptimization arguments " + "must be positive", &CI); + + Assert1(4 + NumCallArgs + NumDeoptArgs <= (int)CS.arg_size(), + "gc.statepoint too few arguments according to length fields", &CI); + + // Check that the only uses of this gc.statepoint are gc.result or + // gc.relocate calls which are tied to this statepoint and thus part + // of the same statepoint sequence + for (const User *U : CI.users()) { + const CallInst *Call = dyn_cast(U); + Assert2(Call, "illegal use of statepoint token", &CI, U); + if (!Call) continue; + Assert2(isGCRelocate(Call) || isGCResult(Call), + "gc.result or gc.relocate are the only value uses" + "of a gc.statepoint", &CI, U); + if (isGCResult(Call)) { + Assert2(Call->getArgOperand(0) == &CI, + "gc.result connected to wrong gc.statepoint", + &CI, Call); + } else if (isGCRelocate(Call)) { + Assert2(Call->getArgOperand(0) == &CI, + "gc.relocate connected to wrong gc.statepoint", + &CI, Call); + } + } + + // Note: It is legal for a single derived pointer to be listed multiple + // times. It's non-optimal, but it is legal. It can also happen after + // insertion if we strip a bitcast away. + // Note: It is really tempting to check that each base is relocated and + // that a derived pointer is never reused as a base pointer. This turns + // out to be problematic since optimizations run after safepoint insertion + // can recognize equality properties that the insertion logic doesn't know + // about. See example statepoint.ll in the verifier subdirectory +} + // visitFunction - Verify that a function is ok. // void Verifier::visitFunction(const Function &F) { @@ -1101,7 +1349,7 @@ void Verifier::visitFunction(const Function &F) { // Check the entry node const BasicBlock *Entry = &F.getEntryBlock(); - Assert1(pred_begin(Entry) == pred_end(Entry), + Assert1(pred_empty(Entry), "Entry block to function must not have predecessors!", Entry); // The address of the entry block cannot be taken, unless it is dead. @@ -1482,9 +1730,9 @@ void Verifier::visitIntToPtrInst(IntToPtrInst &I) { } void Verifier::visitBitCastInst(BitCastInst &I) { - Type *SrcTy = I.getOperand(0)->getType(); - Type *DestTy = I.getType(); - VerifyBitcastType(&I, DestTy, SrcTy); + Assert1( + CastInst::castIsValid(Instruction::BitCast, I.getOperand(0), I.getType()), + "Invalid bitcast", &I); visitInstruction(I); } @@ -1732,6 +1980,13 @@ void Verifier::visitInvokeInst(InvokeInst &II) { Assert1(II.getUnwindDest()->isLandingPad(), "The unwind destination does not have a landingpad instruction!",&II); + if (Function *F = II.getCalledFunction()) + // TODO: Ideally we should use visitIntrinsicFunction here. But it uses + // CallInst as an input parameter. It not woth updating this whole + // function only to support statepoint verification. + if (F->getIntrinsicID() == Intrinsic::experimental_gc_statepoint) + VerifyStatepoint(ImmutableCallSite(&II)); + visitTerminatorInst(II); } @@ -1906,9 +2161,11 @@ void Verifier::visitRangeMetadata(Instruction& I, ConstantRange LastRange(1); // Dummy initial value for (unsigned i = 0; i < NumRanges; ++i) { - ConstantInt *Low = dyn_cast(Range->getOperand(2*i)); + ConstantInt *Low = + mdconst::dyn_extract(Range->getOperand(2 * i)); Assert1(Low, "The lower limit must be an integer!", Low); - ConstantInt *High = dyn_cast(Range->getOperand(2*i + 1)); + ConstantInt *High = + mdconst::dyn_extract(Range->getOperand(2 * i + 1)); Assert1(High, "The upper limit must be an integer!", High); Assert1(High->getType() == Low->getType() && High->getType() == Ty, "Range types must match instruction type!", @@ -1931,9 +2188,9 @@ void Verifier::visitRangeMetadata(Instruction& I, } if (NumRanges > 2) { APInt FirstLow = - dyn_cast(Range->getOperand(0))->getValue(); + mdconst::dyn_extract(Range->getOperand(0))->getValue(); APInt FirstHigh = - dyn_cast(Range->getOperand(1))->getValue(); + mdconst::dyn_extract(Range->getOperand(1))->getValue(); ConstantRange FirstRange(FirstLow, FirstHigh); Assert1(FirstRange.intersectWith(LastRange).isEmptySet(), "Intervals are overlapping", Range); @@ -2229,7 +2486,8 @@ void Verifier::visitInstruction(Instruction &I) { Assert1(!F->isIntrinsic() || isa(I) || F->getIntrinsicID() == Intrinsic::donothing || F->getIntrinsicID() == Intrinsic::experimental_patchpoint_void || - F->getIntrinsicID() == Intrinsic::experimental_patchpoint_i64, + F->getIntrinsicID() == Intrinsic::experimental_patchpoint_i64 || + F->getIntrinsicID() == Intrinsic::experimental_gc_statepoint, "Cannot invoke an intrinsinc other than" " donothing or patchpoint", &I); Assert1(F->getParent() == M, "Referencing function in another module!", @@ -2277,8 +2535,8 @@ void Verifier::visitInstruction(Instruction &I) { Assert1(I.getType()->isFPOrFPVectorTy(), "fpmath requires a floating point result!", &I); Assert1(MD->getNumOperands() == 1, "fpmath takes one operand!", &I); - Value *Op0 = MD->getOperand(0); - if (ConstantFP *CFP0 = dyn_cast_or_null(Op0)) { + if (ConstantFP *CFP0 = + mdconst::dyn_extract_or_null(MD->getOperand(0))) { APFloat Accuracy = CFP0->getValueAPF(); Assert1(Accuracy.isFiniteNonZero() && !Accuracy.isNegative(), "fpmath accuracy not a positive number!", &I); @@ -2362,6 +2620,7 @@ bool Verifier::VerifyIntrinsicType(Type *Ty, ArgTys.push_back(Ty); switch (D.getArgumentKind()) { + case IITDescriptor::AK_Any: return false; // Success case IITDescriptor::AK_AnyInteger: return !Ty->isIntOrIntVectorTy(); case IITDescriptor::AK_AnyFloat: return !Ty->isFPOrFPVectorTy(); case IITDescriptor::AK_AnyVector: return !isa(Ty); @@ -2405,6 +2664,43 @@ bool Verifier::VerifyIntrinsicType(Type *Ty, !isa(ArgTys[D.getArgumentNumber()]) || VectorType::getHalfElementsVectorType( cast(ArgTys[D.getArgumentNumber()])) != Ty; + case IITDescriptor::SameVecWidthArgument: { + if (D.getArgumentNumber() >= ArgTys.size()) + return true; + VectorType * ReferenceType = + dyn_cast(ArgTys[D.getArgumentNumber()]); + VectorType *ThisArgType = dyn_cast(Ty); + if (!ThisArgType || !ReferenceType || + (ReferenceType->getVectorNumElements() != + ThisArgType->getVectorNumElements())) + return true; + return VerifyIntrinsicType(ThisArgType->getVectorElementType(), + Infos, ArgTys); + } + case IITDescriptor::PtrToArgument: { + if (D.getArgumentNumber() >= ArgTys.size()) + return true; + Type * ReferenceType = ArgTys[D.getArgumentNumber()]; + PointerType *ThisArgType = dyn_cast(Ty); + return (!ThisArgType || ThisArgType->getElementType() != ReferenceType); + } + case IITDescriptor::VecOfPtrsToElt: { + if (D.getArgumentNumber() >= ArgTys.size()) + return true; + VectorType * ReferenceType = + dyn_cast (ArgTys[D.getArgumentNumber()]); + VectorType *ThisArgVecTy = dyn_cast(Ty); + if (!ThisArgVecTy || !ReferenceType || + (ReferenceType->getVectorNumElements() != + ThisArgVecTy->getVectorNumElements())) + return true; + PointerType *ThisArgEltTy = + dyn_cast(ThisArgVecTy->getVectorElementType()); + if (!ThisArgEltTy) + return true; + return (!(ThisArgEltTy->getElementType() == + ReferenceType->getVectorElementType())); + } } llvm_unreachable("unhandled"); } @@ -2482,8 +2778,8 @@ void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) { // If the intrinsic takes MDNode arguments, verify that they are either global // or are local to *this* function. for (unsigned i = 0, e = CI.getNumArgOperands(); i != e; ++i) - if (MDNode *MD = dyn_cast(CI.getArgOperand(i))) - visitMDNode(*MD, CI.getParent()->getParent()); + if (auto *MD = dyn_cast(CI.getArgOperand(i))) + visitMetadataAsValue(*MD, CI.getParent()->getParent()); switch (ID) { default: @@ -2495,11 +2791,8 @@ void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) { "constant int", &CI); break; case Intrinsic::dbg_declare: { // llvm.dbg.declare - Assert1(CI.getArgOperand(0) && isa(CI.getArgOperand(0)), - "invalid llvm.dbg.declare intrinsic call 1", &CI); - MDNode *MD = cast(CI.getArgOperand(0)); - Assert1(MD->getNumOperands() == 1, - "invalid llvm.dbg.declare intrinsic call 2", &CI); + Assert1(CI.getArgOperand(0) && isa(CI.getArgOperand(0)), + "invalid llvm.dbg.declare intrinsic call 1", &CI); } break; case Intrinsic::memcpy: case Intrinsic::memmove: @@ -2559,7 +2852,142 @@ void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) { Assert1(isa(CI.getArgOperand(1)), "llvm.invariant.end parameter #2 must be a constant integer", &CI); break; + + case Intrinsic::frameallocate: { + BasicBlock *BB = CI.getParent(); + Assert1(BB == &BB->getParent()->front(), + "llvm.frameallocate used outside of entry block", &CI); + Assert1(!SawFrameAllocate, + "multiple calls to llvm.frameallocate in one function", &CI); + SawFrameAllocate = true; + Assert1(isa(CI.getArgOperand(0)), + "llvm.frameallocate argument must be constant integer size", &CI); + break; + } + case Intrinsic::framerecover: { + Value *FnArg = CI.getArgOperand(0)->stripPointerCasts(); + Function *Fn = dyn_cast(FnArg); + Assert1(Fn && !Fn->isDeclaration(), "llvm.framerecover first " + "argument must be function defined in this module", &CI); + break; + } + + case Intrinsic::experimental_gc_statepoint: + Assert1(!CI.isInlineAsm(), + "gc.statepoint support for inline assembly unimplemented", &CI); + + VerifyStatepoint(ImmutableCallSite(&CI)); + break; + case Intrinsic::experimental_gc_result_int: + case Intrinsic::experimental_gc_result_float: + case Intrinsic::experimental_gc_result_ptr: + case Intrinsic::experimental_gc_result: { + // Are we tied to a statepoint properly? + CallSite StatepointCS(CI.getArgOperand(0)); + const Function *StatepointFn = + StatepointCS.getInstruction() ? StatepointCS.getCalledFunction() : nullptr; + Assert2(StatepointFn && StatepointFn->isDeclaration() && + StatepointFn->getIntrinsicID() == Intrinsic::experimental_gc_statepoint, + "gc.result operand #1 must be from a statepoint", + &CI, CI.getArgOperand(0)); + + // Assert that result type matches wrapped callee. + const Value *Target = StatepointCS.getArgument(0); + const PointerType *PT = cast(Target->getType()); + const FunctionType *TargetFuncType = + cast(PT->getElementType()); + Assert1(CI.getType() == TargetFuncType->getReturnType(), + "gc.result result type does not match wrapped callee", + &CI); + break; } + case Intrinsic::experimental_gc_relocate: { + Assert1(CI.getNumArgOperands() == 3, "wrong number of arguments", &CI); + + // Check that this relocate is correctly tied to the statepoint + + // This is case for relocate on the unwinding path of an invoke statepoint + if (ExtractValueInst *ExtractValue = + dyn_cast(CI.getArgOperand(0))) { + Assert1(isa(ExtractValue->getAggregateOperand()), + "gc relocate on unwind path incorrectly linked to the statepoint", + &CI); + + const BasicBlock *invokeBB = + ExtractValue->getParent()->getUniquePredecessor(); + + // Landingpad relocates should have only one predecessor with invoke + // statepoint terminator + Assert1(invokeBB, + "safepoints should have unique landingpads", + ExtractValue->getParent()); + Assert1(invokeBB->getTerminator(), + "safepoint block should be well formed", + invokeBB); + Assert1(isStatepoint(invokeBB->getTerminator()), + "gc relocate should be linked to a statepoint", + invokeBB); + } + else { + // In all other cases relocate should be tied to the statepoint directly. + // This covers relocates on a normal return path of invoke statepoint and + // relocates of a call statepoint + auto Token = CI.getArgOperand(0); + Assert2(isa(Token) && isStatepoint(cast(Token)), + "gc relocate is incorrectly tied to the statepoint", + &CI, Token); + } + + // Verify rest of the relocate arguments + + GCRelocateOperands ops(&CI); + ImmutableCallSite StatepointCS(ops.statepoint()); + + // Both the base and derived must be piped through the safepoint + Value* Base = CI.getArgOperand(1); + Assert1(isa(Base), + "gc.relocate operand #2 must be integer offset", &CI); + + Value* Derived = CI.getArgOperand(2); + Assert1(isa(Derived), + "gc.relocate operand #3 must be integer offset", &CI); + + const int BaseIndex = cast(Base)->getZExtValue(); + const int DerivedIndex = cast(Derived)->getZExtValue(); + // Check the bounds + Assert1(0 <= BaseIndex && + BaseIndex < (int)StatepointCS.arg_size(), + "gc.relocate: statepoint base index out of bounds", &CI); + Assert1(0 <= DerivedIndex && + DerivedIndex < (int)StatepointCS.arg_size(), + "gc.relocate: statepoint derived index out of bounds", &CI); + + // Check that BaseIndex and DerivedIndex fall within the 'gc parameters' + // section of the statepoint's argument + const int NumCallArgs = + cast(StatepointCS.getArgument(1))->getZExtValue(); + const int NumDeoptArgs = + cast(StatepointCS.getArgument(NumCallArgs + 3))->getZExtValue(); + const int GCParamArgsStart = NumCallArgs + NumDeoptArgs + 4; + const int GCParamArgsEnd = StatepointCS.arg_size(); + Assert1(GCParamArgsStart <= BaseIndex && + BaseIndex < GCParamArgsEnd, + "gc.relocate: statepoint base index doesn't fall within the " + "'gc parameters' section of the statepoint call", &CI); + Assert1(GCParamArgsStart <= DerivedIndex && + DerivedIndex < GCParamArgsEnd, + "gc.relocate: statepoint derived index doesn't fall within the " + "'gc parameters' section of the statepoint call", &CI); + + + // Assert that the result type matches the type of the relocated pointer + GCRelocateOperands Operands(&CI); + Assert1(Operands.derivedPtr()->getType() == CI.getType(), + "gc.relocate: relocating a pointer shouldn't change its type", + &CI); + break; + } + }; } void DebugInfoVerifier::verifyDebugInfo() { @@ -2605,12 +3033,20 @@ void DebugInfoVerifier::processCallInst(DebugInfoFinder &Finder, if (Function *F = CI.getCalledFunction()) if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) switch (ID) { - case Intrinsic::dbg_declare: - Finder.processDeclare(*M, cast(&CI)); + case Intrinsic::dbg_declare: { + auto *DDI = cast(&CI); + Finder.processDeclare(*M, DDI); + if (auto E = DDI->getExpression()) + Assert1(DIExpression(E).Verify(), "DIExpression does not Verify!", E); break; - case Intrinsic::dbg_value: - Finder.processValue(*M, cast(&CI)); + } + case Intrinsic::dbg_value: { + auto *DVI = cast(&CI); + Finder.processValue(*M, DVI); + if (auto E = DVI->getExpression()) + Assert1(DIExpression(E).Verify(), "DIExpression does not Verify!", E); break; + } default: break; } @@ -2722,15 +3158,15 @@ ModulePass *llvm::createDebugInfoVerifierPass(bool FatalErrors) { return new DebugInfoVerifierLegacyPass(FatalErrors); } -PreservedAnalyses VerifierPass::run(Module *M) { - if (verifyModule(*M, &dbgs()) && FatalErrors) +PreservedAnalyses VerifierPass::run(Module &M) { + if (verifyModule(M, &dbgs()) && FatalErrors) report_fatal_error("Broken module found, compilation aborted!"); return PreservedAnalyses::all(); } -PreservedAnalyses VerifierPass::run(Function *F) { - if (verifyFunction(*F, &dbgs()) && FatalErrors) +PreservedAnalyses VerifierPass::run(Function &F) { + if (verifyFunction(F, &dbgs()) && FatalErrors) report_fatal_error("Broken function found, compilation aborted!"); return PreservedAnalyses::all(); -- cgit v1.1