diff options
Diffstat (limited to 'lib/VMCore/Metadata.cpp')
-rw-r--r-- | lib/VMCore/Metadata.cpp | 220 |
1 files changed, 73 insertions, 147 deletions
diff --git a/lib/VMCore/Metadata.cpp b/lib/VMCore/Metadata.cpp index 216d8a2..d5c1dba 100644 --- a/lib/VMCore/Metadata.cpp +++ b/lib/VMCore/Metadata.cpp @@ -243,131 +243,103 @@ void NamedMDNode::dropAllReferences() { //===----------------------------------------------------------------------===// -// MetadataContextImpl implementation. +// MetadataContext implementation. // -namespace llvm { -class MetadataContextImpl { -public: - typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy; - typedef SmallVector<MDPairTy, 2> MDMapTy; - typedef DenseMap<const Instruction *, MDMapTy> MDStoreTy; - friend class BitcodeReader; -private: - - /// MetadataStore - Collection of metadata used in this context. - MDStoreTy MetadataStore; - /// MDHandlerNames - Map to hold metadata handler names. - StringMap<unsigned> MDHandlerNames; - -public: - // Name <-> ID mapping methods. - unsigned getMDKindID(StringRef Name); - void getMDKindNames(SmallVectorImpl<StringRef> &) const; - - - // Instruction metadata methods. - MDNode *getMetadata(const Instruction *Inst, unsigned Kind); - void getAllMetadata(const Instruction *Inst, - SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs)const; +#ifndef NDEBUG +/// isValidName - Return true if Name is a valid custom metadata handler name. +static bool isValidName(StringRef MDName) { + if (MDName.empty()) + return false; - void setMetadata(Instruction *Inst, unsigned Kind, MDNode *Node); + if (!isalpha(MDName[0])) + return false; - /// removeAllMetadata - Remove all metadata attached to an instruction. - void removeAllMetadata(Instruction *Inst); -}; + for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E; + ++I) { + if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.') + return false; + } + return true; } +#endif /// getMDKindID - Return a unique non-zero ID for the specified metadata kind. -unsigned MetadataContextImpl::getMDKindID(StringRef Name) { - unsigned &Entry = MDHandlerNames[Name]; - +unsigned LLVMContext::getMDKindID(StringRef Name) const { + assert(isValidName(Name) && "Invalid MDNode name"); + + unsigned &Entry = pImpl->CustomMDKindNames[Name]; + // If this is new, assign it its ID. - if (Entry == 0) Entry = MDHandlerNames.size(); + if (Entry == 0) Entry = pImpl->CustomMDKindNames.size(); return Entry; } /// getHandlerNames - Populate client supplied smallvector using custome /// metadata name and ID. -void MetadataContextImpl:: -getMDKindNames(SmallVectorImpl<StringRef> &Names) const { - Names.resize(MDHandlerNames.size()+1); +void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const { + Names.resize(pImpl->CustomMDKindNames.size()+1); Names[0] = ""; - for (StringMap<unsigned>::const_iterator I = MDHandlerNames.begin(), - E = MDHandlerNames.end(); I != E; ++I) + for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(), + E = pImpl->CustomMDKindNames.end(); I != E; ++I) // MD Handlers are numbered from 1. Names[I->second] = I->first(); } +//===----------------------------------------------------------------------===// +// Instruction Metadata method implementations. +// -/// getMetadata - Get the metadata of given kind attached to an Instruction. -/// If the metadata is not found then return 0. -MDNode *MetadataContextImpl:: -getMetadata(const Instruction *Inst, unsigned MDKind) { - MDMapTy &Info = MetadataStore[Inst]; - assert(Inst->hasMetadata() && !Info.empty() && "Shouldn't have called this"); - - for (MDMapTy::iterator I = Info.begin(), E = Info.end(); I != E; ++I) - if (I->first == MDKind) - return I->second; - return 0; +void Instruction::setMetadata(const char *Kind, MDNode *Node) { + if (Node == 0 && !hasMetadata()) return; + setMetadata(getContext().getMDKindID(Kind), Node); } -/// getAllMetadata - Get all of the metadata attached to an Instruction. -void MetadataContextImpl:: -getAllMetadata(const Instruction *Inst, - SmallVectorImpl<std::pair<unsigned, MDNode*> > &Result) const { - assert(Inst->hasMetadata() && MetadataStore.find(Inst) != MetadataStore.end() - && "Shouldn't have called this"); - const MDMapTy &Info = MetadataStore.find(Inst)->second; - assert(!Info.empty() && "Shouldn't have called this"); - - Result.clear(); - Result.append(Info.begin(), Info.end()); - - // Sort the resulting array so it is stable. - if (Result.size() > 1) - array_pod_sort(Result.begin(), Result.end()); +MDNode *Instruction::getMetadataImpl(const char *Kind) const { + return getMetadataImpl(getContext().getMDKindID(Kind)); } - -void MetadataContextImpl::setMetadata(Instruction *Inst, unsigned Kind, - MDNode *Node) { +/// setMetadata - Set the metadata of of the specified kind to the specified +/// node. This updates/replaces metadata if already present, or removes it if +/// Node is null. +void Instruction::setMetadata(unsigned KindID, MDNode *Node) { + if (Node == 0 && !hasMetadata()) return; + // Handle the case when we're adding/updating metadata on an instruction. if (Node) { - MDMapTy &Info = MetadataStore[Inst]; - assert(!Info.empty() == Inst->hasMetadata() && "HasMetadata bit is wonked"); + LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; + assert(!Info.empty() == hasMetadata() && "HasMetadata bit is wonked"); if (Info.empty()) { - Inst->setHasMetadata(true); + setHasMetadata(true); } else { // Handle replacement of an existing value. for (unsigned i = 0, e = Info.size(); i != e; ++i) - if (Info[i].first == Kind) { + if (Info[i].first == KindID) { Info[i].second = Node; return; } } // No replacement, just add it to the list. - Info.push_back(std::make_pair(Kind, Node)); + Info.push_back(std::make_pair(KindID, Node)); return; } // Otherwise, we're removing metadata from an instruction. - assert(Inst->hasMetadata() && MetadataStore.count(Inst) && + assert(hasMetadata() && getContext().pImpl->MetadataStore.count(this) && "HasMetadata bit out of date!"); - MDMapTy &Info = MetadataStore[Inst]; - + LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; + // Common case is removing the only entry. - if (Info.size() == 1 && Info[0].first == Kind) { - MetadataStore.erase(Inst); - Inst->setHasMetadata(false); + if (Info.size() == 1 && Info[0].first == KindID) { + getContext().pImpl->MetadataStore.erase(this); + setHasMetadata(false); return; } // Handle replacement of an existing value. for (unsigned i = 0, e = Info.size(); i != e; ++i) - if (Info[i].first == Kind) { + if (Info[i].first == KindID) { Info[i] = Info.back(); Info.pop_back(); assert(!Info.empty() && "Removing last entry should be handled above"); @@ -376,83 +348,37 @@ void MetadataContextImpl::setMetadata(Instruction *Inst, unsigned Kind, // Otherwise, removing an entry that doesn't exist on the instruction. } -/// removeAllMetadata - Remove all metadata attached with an instruction. -void MetadataContextImpl::removeAllMetadata(Instruction *Inst) { - MetadataStore.erase(Inst); - Inst->setHasMetadata(false); -} - - -//===----------------------------------------------------------------------===// -// MetadataContext implementation. -// -MetadataContext::MetadataContext() : pImpl(new MetadataContextImpl()) { } -MetadataContext::~MetadataContext() { delete pImpl; } - -#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 (!isalpha(MDName[0])) - return false; - - for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E; - ++I) { - if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.') - return false; - } - return true; -} -#endif - -/// getMDKindID - Return a unique non-zero ID for the specified metadata kind. -unsigned MetadataContext::getMDKindID(StringRef Name) const { - assert(isValidName(Name) && "Invalid MDNode name"); - return pImpl->getMDKindID(Name); -} - -/// getHandlerNames - Populate client supplied smallvector using custome -/// metadata name and ID. -void MetadataContext::getMDKindNames(SmallVectorImpl<StringRef> &N) const { - pImpl->getMDKindNames(N); -} - -//===----------------------------------------------------------------------===// -// Instruction Metadata method implementations. -// - -void Instruction::setMetadata(const char *Kind, MDNode *Node) { - if (Node == 0 && !hasMetadata()) return; - setMetadata(getContext().getMetadata().getMDKindID(Kind), Node); -} - -MDNode *Instruction::getMetadataImpl(const char *Kind) const { - return getMetadataImpl(getContext().getMetadata().getMDKindID(Kind)); -} - -/// setMetadata - Set the metadata of of the specified kind to the specified -/// node. This updates/replaces metadata if already present, or removes it if -/// Node is null. -void Instruction::setMetadata(unsigned KindID, MDNode *Node) { - if (Node == 0 && !hasMetadata()) return; - - getContext().getMetadata().pImpl->setMetadata(this, KindID, Node); -} - MDNode *Instruction::getMetadataImpl(unsigned KindID) const { - return getContext().getMetadata().pImpl->getMetadata(this, KindID); + LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this]; + assert(hasMetadata() && !Info.empty() && "Shouldn't have called this"); + + for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end(); + I != E; ++I) + if (I->first == KindID) + return I->second; + return 0; } void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned, MDNode*> > &Result)const { - getContext().getMetadata().pImpl->getAllMetadata(this, Result); + assert(hasMetadata() && getContext().pImpl->MetadataStore.count(this) && + "Shouldn't have called this"); + const LLVMContextImpl::MDMapTy &Info = + getContext().pImpl->MetadataStore.find(this)->second; + assert(!Info.empty() && "Shouldn't have called this"); + + Result.clear(); + Result.append(Info.begin(), Info.end()); + + // Sort the resulting array so it is stable. + if (Result.size() > 1) + array_pod_sort(Result.begin(), Result.end()); } /// removeAllMetadata - Remove all metadata from this instruction. void Instruction::removeAllMetadata() { assert(hasMetadata() && "Caller should check"); - getContext().getMetadata().pImpl->removeAllMetadata(this); + getContext().pImpl->MetadataStore.erase(this); + setHasMetadata(false); } |