diff options
author | Duncan P. N. Exon Smith <dexonsmith@apple.com> | 2015-01-12 20:09:34 +0000 |
---|---|---|
committer | Duncan P. N. Exon Smith <dexonsmith@apple.com> | 2015-01-12 20:09:34 +0000 |
commit | 118632dbf6851afc878098903a5baf7c8851d3c2 (patch) | |
tree | 2ac4f36d54bae0ee66fd8be3a0978b37840dfc8c /llvm/lib/IR/Metadata.cpp | |
parent | 30045e6148d7ad1a209160f1880a9d5a4d5ba26a (diff) | |
download | bcm5719-llvm-118632dbf6851afc878098903a5baf7c8851d3c2.tar.gz bcm5719-llvm-118632dbf6851afc878098903a5baf7c8851d3c2.zip |
IR: Split GenericMDNode into MDTuple and UniquableMDNode
Split `GenericMDNode` into two classes (with more descriptive names).
- `UniquableMDNode` will be a common subclass for `MDNode`s that are
sometimes uniqued like constants, and sometimes 'distinct'.
This class gets the (short-lived) RAUW support and related API.
- `MDTuple` is the basic tuple that has always been returned by
`MDNode::get()`. This is as opposed to more specific nodes to be
added soon, which have additional fields, custom assembly syntax,
and extra semantics.
This class gets the hash-related logic, since other sublcasses of
`UniquableMDNode` may need to hash based on other fields.
To keep this diff from getting too big, I've added casts to `MDTuple`
that won't really scale as new subclasses of `UniquableMDNode` are
added, but I'll clean those up incrementally.
(No functionality change intended.)
llvm-svn: 225682
Diffstat (limited to 'llvm/lib/IR/Metadata.cpp')
-rw-r--r-- | llvm/lib/IR/Metadata.cpp | 75 |
1 files changed, 39 insertions, 36 deletions
diff --git a/llvm/lib/IR/Metadata.cpp b/llvm/lib/IR/Metadata.cpp index 859512db001..282ea49aa41 100644 --- a/llvm/lib/IR/Metadata.cpp +++ b/llvm/lib/IR/Metadata.cpp @@ -222,8 +222,8 @@ void ReplaceableMetadataImpl::resolveAllUses(bool ResolveUsers) { if (Owner.is<MetadataAsValue *>()) continue; - // Resolve GenericMDNodes that point at this. - auto *OwnerMD = dyn_cast<GenericMDNode>(Owner.get<Metadata *>()); + // Resolve UniquableMDNodes that point at this. + auto *OwnerMD = dyn_cast<UniquableMDNode>(Owner.get<Metadata *>()); if (!OwnerMD) continue; if (OwnerMD->isResolved()) @@ -400,7 +400,7 @@ MDNode::MDNode(LLVMContext &Context, unsigned ID, ArrayRef<Metadata *> MDs) bool MDNode::isResolved() const { if (isa<MDNodeFwdDecl>(this)) return false; - return cast<GenericMDNode>(this)->isResolved(); + return cast<UniquableMDNode>(this)->isResolved(); } static bool isOperandUnresolved(Metadata *Op) { @@ -409,9 +409,9 @@ static bool isOperandUnresolved(Metadata *Op) { return false; } -GenericMDNode::GenericMDNode(LLVMContext &C, ArrayRef<Metadata *> Vals, - bool AllowRAUW) - : MDNode(C, GenericMDNodeKind, Vals) { +UniquableMDNode::UniquableMDNode(LLVMContext &C, unsigned ID, + ArrayRef<Metadata *> Vals, bool AllowRAUW) + : MDNode(C, ID, Vals) { if (!AllowRAUW) return; @@ -427,16 +427,14 @@ GenericMDNode::GenericMDNode(LLVMContext &C, ArrayRef<Metadata *> Vals, SubclassData32 = NumUnresolved; } -GenericMDNode::~GenericMDNode() { - LLVMContextImpl *pImpl = getContext().pImpl; +UniquableMDNode::~UniquableMDNode() { if (isStoredDistinctInContext()) - pImpl->NonUniquedMDNodes.erase(this); - else - pImpl->MDNodeSet.erase(this); + getContext().pImpl->DistinctMDNodes.erase(this); + dropAllReferences(); } -void GenericMDNode::resolve() { +void UniquableMDNode::resolve() { assert(!isResolved() && "Expected this to be unresolved"); // Move the map, so that this immediately looks resolved. @@ -448,7 +446,7 @@ void GenericMDNode::resolve() { Uses->resolveAllUses(); } -void GenericMDNode::resolveAfterOperandChange(Metadata *Old, Metadata *New) { +void UniquableMDNode::resolveAfterOperandChange(Metadata *Old, Metadata *New) { assert(SubclassData32 != 0 && "Expected unresolved operands"); // Check if an operand was resolved. @@ -458,13 +456,13 @@ void GenericMDNode::resolveAfterOperandChange(Metadata *Old, Metadata *New) { decrementUnresolvedOperandCount(); } -void GenericMDNode::decrementUnresolvedOperandCount() { +void UniquableMDNode::decrementUnresolvedOperandCount() { if (!--SubclassData32) // Last unresolved operand has just been resolved. resolve(); } -void GenericMDNode::resolveCycles() { +void UniquableMDNode::resolveCycles() { if (isResolved()) return; @@ -477,13 +475,18 @@ void GenericMDNode::resolveCycles() { continue; assert(!isa<MDNodeFwdDecl>(Op) && "Expected all forward declarations to be resolved"); - if (auto *N = dyn_cast<GenericMDNode>(Op)) + if (auto *N = dyn_cast<UniquableMDNode>(Op)) if (!N->isResolved()) N->resolveCycles(); } } -void GenericMDNode::recalculateHash() { +MDTuple::~MDTuple() { + if (!isStoredDistinctInContext()) + getContext().pImpl->MDTuples.erase(this); +} + +void MDTuple::recalculateHash() { setHash(hash_combine_range(op_begin(), op_end())); #ifndef NDEBUG { @@ -498,10 +501,10 @@ void GenericMDNode::recalculateHash() { void MDNode::dropAllReferences() { for (unsigned I = 0, E = NumOperands; I != E; ++I) setOperand(I, nullptr); - if (auto *G = dyn_cast<GenericMDNode>(this)) - if (!G->isResolved()) { - G->ReplaceableUses->resolveAllUses(/* ResolveUsers */ false); - G->ReplaceableUses.reset(); + if (auto *N = dyn_cast<UniquableMDNode>(this)) + if (!N->isResolved()) { + N->ReplaceableUses->resolveAllUses(/* ResolveUsers */ false); + N->ReplaceableUses.reset(); } } @@ -522,7 +525,7 @@ namespace llvm { static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); } } -void GenericMDNode::handleChangedOperand(void *Ref, Metadata *New) { +void UniquableMDNode::handleChangedOperand(void *Ref, Metadata *New) { unsigned Op = static_cast<MDOperand *>(Ref) - op_begin(); assert(Op < getNumOperands() && "Expected valid operand"); @@ -534,8 +537,8 @@ void GenericMDNode::handleChangedOperand(void *Ref, Metadata *New) { return; } - auto &Store = getContext().pImpl->MDNodeSet; - Store.erase(this); + auto &Store = getContext().pImpl->MDTuples; + Store.erase(cast<MDTuple>(this)); Metadata *Old = getOperand(Op); setOperand(Op, New); @@ -549,11 +552,11 @@ void GenericMDNode::handleChangedOperand(void *Ref, Metadata *New) { } // Re-unique the node. - recalculateHash(); - GenericMDNodeInfo::KeyTy Key(this); + cast<MDTuple>(this)->recalculateHash(); + MDTupleInfo::KeyTy Key(cast<MDTuple>(this)); auto I = Store.find_as(Key); if (I == Store.end()) { - Store.insert(this); + Store.insert(cast<MDTuple>(this)); if (!isResolved()) resolveAfterOperandChange(Old, New); @@ -570,7 +573,7 @@ void GenericMDNode::handleChangedOperand(void *Ref, Metadata *New) { for (unsigned O = 0, E = getNumOperands(); O != E; ++O) setOperand(O, nullptr); ReplaceableUses->replaceAllUsesWith(*I); - delete this; + delete cast<MDTuple>(this); return; } @@ -580,9 +583,9 @@ void GenericMDNode::handleChangedOperand(void *Ref, Metadata *New) { MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef<Metadata *> MDs, bool Insert) { - auto &Store = Context.pImpl->MDNodeSet; + auto &Store = Context.pImpl->MDTuples; - GenericMDNodeInfo::KeyTy Key(MDs); + MDTupleInfo::KeyTy Key(MDs); auto I = Store.find_as(Key); if (I != Store.end()) return *I; @@ -590,14 +593,14 @@ MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef<Metadata *> MDs, return nullptr; // Coallocate space for the node and Operands together, then placement new. - auto *N = new (MDs.size()) GenericMDNode(Context, MDs, /* AllowRAUW */ true); + auto *N = new (MDs.size()) MDTuple(Context, MDs, /* AllowRAUW */ true); N->setHash(Key.Hash); Store.insert(N); return N; } MDNode *MDNode::getDistinct(LLVMContext &Context, ArrayRef<Metadata *> MDs) { - auto *N = new (MDs.size()) GenericMDNode(Context, MDs, /* AllowRAUW */ false); + auto *N = new (MDs.size()) MDTuple(Context, MDs, /* AllowRAUW */ false); N->storeDistinctInContext(); return N; } @@ -616,9 +619,9 @@ void MDNode::deleteTemporary(MDNode *N) { void MDNode::storeDistinctInContext() { assert(!IsDistinctInContext && "Expected newly distinct metadata"); IsDistinctInContext = true; - auto *G = cast<GenericMDNode>(this); - G->setHash(0); - getContext().pImpl->NonUniquedMDNodes.insert(G); + auto *T = cast<MDTuple>(this); + T->setHash(0); + getContext().pImpl->DistinctMDNodes.insert(T); } void MDNode::replaceOperandWith(unsigned I, Metadata *New) { @@ -630,7 +633,7 @@ void MDNode::replaceOperandWith(unsigned I, Metadata *New) { return; } - cast<GenericMDNode>(this)->handleChangedOperand(mutable_begin() + I, New); + cast<UniquableMDNode>(this)->handleChangedOperand(mutable_begin() + I, New); } void MDNode::setOperand(unsigned I, Metadata *New) { |