From 1b0064d0d2f47655d38d128cb1781bd998f240ba Mon Sep 17 00:00:00 2001 From: "Duncan P. N. Exon Smith" Date: Mon, 19 Jan 2015 20:14:15 +0000 Subject: [PATCH] IR: Reuse `getImpl()` for `getDistinct()`, NFC Merge `getDistinct()`'s implementation with those of `get()` and `getIfExists()` for both `MDTuple` and `MDLocation`. This will make it easier to scale to supporting temporaries. llvm-svn: 226497 --- llvm/include/llvm/IR/Metadata.h | 33 +++++++------ llvm/lib/IR/Metadata.cpp | 102 +++++++++++++++++++++------------------- 2 files changed, 72 insertions(+), 63 deletions(-) diff --git a/llvm/include/llvm/IR/Metadata.h b/llvm/include/llvm/IR/Metadata.h index 5371efb..8cbd1df 100644 --- a/llvm/include/llvm/IR/Metadata.h +++ b/llvm/include/llvm/IR/Metadata.h @@ -855,31 +855,36 @@ class MDTuple : public UniquableMDNode { friend class LLVMContextImpl; friend class UniquableMDNode; - MDTuple(LLVMContext &C, StorageType Storage, ArrayRef Vals) - : UniquableMDNode(C, MDTupleKind, Storage, Vals) {} + MDTuple(LLVMContext &C, StorageType Storage, unsigned Hash, + ArrayRef Vals) + : UniquableMDNode(C, MDTupleKind, Storage, Vals) { + setHash(Hash); + } ~MDTuple() { dropAllReferences(); } void setHash(unsigned Hash) { MDNodeSubclassData = Hash; } void recalculateHash(); static MDTuple *getImpl(LLVMContext &Context, ArrayRef MDs, - bool ShouldCreate); + StorageType Storage, bool ShouldCreate = true); public: /// \brief Get the hash, if any. unsigned getHash() const { return MDNodeSubclassData; } static MDTuple *get(LLVMContext &Context, ArrayRef MDs) { - return getImpl(Context, MDs, /* ShouldCreate */ true); + return getImpl(Context, MDs, Uniqued); } static MDTuple *getIfExists(LLVMContext &Context, ArrayRef MDs) { - return getImpl(Context, MDs, /* ShouldCreate */ false); + return getImpl(Context, MDs, Uniqued, /* ShouldCreate */ false); } /// \brief Return a distinct node. /// /// Return a distinct node -- i.e., a node that is not uniqued. - static MDTuple *getDistinct(LLVMContext &Context, ArrayRef MDs); + static MDTuple *getDistinct(LLVMContext &Context, ArrayRef MDs) { + return getImpl(Context, MDs, Distinct); + } static bool classof(const Metadata *MD) { return MD->getMetadataID() == MDTupleKind; @@ -911,13 +916,10 @@ class MDLocation : public UniquableMDNode { unsigned Column, ArrayRef MDs); ~MDLocation() { dropAllReferences(); } - static MDLocation *constructHelper(LLVMContext &Context, StorageType Storage, - unsigned Line, unsigned Column, - Metadata *Scope, Metadata *InlinedAt); - static MDLocation *getImpl(LLVMContext &Context, unsigned Line, unsigned Column, Metadata *Scope, - Metadata *InlinedAt, bool ShouldCreate); + Metadata *InlinedAt, StorageType Storage, + bool ShouldCreate = true); // Disallow replacing operands. void replaceOperandWith(unsigned I, Metadata *New) LLVM_DELETED_FUNCTION; @@ -925,18 +927,19 @@ class MDLocation : public UniquableMDNode { public: static MDLocation *get(LLVMContext &Context, unsigned Line, unsigned Column, Metadata *Scope, Metadata *InlinedAt = nullptr) { - return getImpl(Context, Line, Column, Scope, InlinedAt, - /* ShouldCreate */ true); + return getImpl(Context, Line, Column, Scope, InlinedAt, Uniqued); } static MDLocation *getIfExists(LLVMContext &Context, unsigned Line, unsigned Column, Metadata *Scope, Metadata *InlinedAt = nullptr) { - return getImpl(Context, Line, Column, Scope, InlinedAt, + return getImpl(Context, Line, Column, Scope, InlinedAt, Uniqued, /* ShouldCreate */ false); } static MDLocation *getDistinct(LLVMContext &Context, unsigned Line, unsigned Column, Metadata *Scope, - Metadata *InlinedAt = nullptr); + Metadata *InlinedAt = nullptr) { + return getImpl(Context, Line, Column, Scope, InlinedAt, Distinct); + } unsigned getLine() const { return MDNodeSubclassData; } unsigned getColumn() const { return SubclassData16; } diff --git a/llvm/lib/IR/Metadata.cpp b/llvm/lib/IR/Metadata.cpp index 6ccb55d..cfa53ef 100644 --- a/llvm/lib/IR/Metadata.cpp +++ b/llvm/lib/IR/Metadata.cpp @@ -605,26 +605,35 @@ void UniquableMDNode::eraseFromStore() { } MDTuple *MDTuple::getImpl(LLVMContext &Context, ArrayRef MDs, - bool ShouldCreate) { - MDTupleInfo::KeyTy Key(MDs); + StorageType Storage, bool ShouldCreate) { + unsigned Hash = 0; + if (Storage == Uniqued) { + MDTupleInfo::KeyTy Key(MDs); + Hash = Key.Hash; + + auto &Store = Context.pImpl->MDTuples; + auto I = Store.find_as(Key); + if (I != Store.end()) + return *I; + if (!ShouldCreate) + return nullptr; + } else { + assert(ShouldCreate && "Expected non-uniqued nodes to always be created"); + } - auto &Store = Context.pImpl->MDTuples; - auto I = Store.find_as(Key); - if (I != Store.end()) - return *I; - if (!ShouldCreate) - return nullptr; + auto *N = new (MDs.size()) MDTuple(Context, Storage, Hash, MDs); - // Coallocate space for the node and Operands together, then placement new. - auto *N = new (MDs.size()) MDTuple(Context, Uniqued, MDs); - N->setHash(Key.Hash); - Store.insert(N); - return N; -} + switch (Storage) { + case Uniqued: + Context.pImpl->MDTuples.insert(N); + break; + case Distinct: + N->storeDistinctInContext(); + break; + case Temporary: + llvm_unreachable("Unexpected temporary node"); + } -MDTuple *MDTuple::getDistinct(LLVMContext &Context, ArrayRef MDs) { - auto *N = new (MDs.size()) MDTuple(Context, Distinct, MDs); - N->storeDistinctInContext(); return N; } @@ -657,17 +666,6 @@ MDLocation::MDLocation(LLVMContext &C, StorageType Storage, unsigned Line, SubclassData16 = Column; } -MDLocation *MDLocation::constructHelper(LLVMContext &Context, - StorageType Storage, unsigned Line, - unsigned Column, Metadata *Scope, - Metadata *InlinedAt) { - SmallVector Ops; - Ops.push_back(Scope); - if (InlinedAt) - Ops.push_back(InlinedAt); - return new (Ops.size()) MDLocation(Context, Storage, Line, Column, Ops); -} - static void adjustLine(unsigned &Line) { // Set to unknown on overflow. Still use 24 bits for now. if (Line >= (1u << 24)) @@ -682,34 +680,42 @@ static void adjustColumn(unsigned &Column) { MDLocation *MDLocation::getImpl(LLVMContext &Context, unsigned Line, unsigned Column, Metadata *Scope, - Metadata *InlinedAt, bool ShouldCreate) { + Metadata *InlinedAt, StorageType Storage, + bool ShouldCreate) { // Fixup line/column. adjustLine(Line); adjustColumn(Column); - MDLocationInfo::KeyTy Key(Line, Column, Scope, InlinedAt); + if (Storage == Uniqued) { + MDLocationInfo::KeyTy Key(Line, Column, Scope, InlinedAt); - auto &Store = Context.pImpl->MDLocations; - auto I = Store.find_as(Key); - if (I != Store.end()) - return *I; - if (!ShouldCreate) - return nullptr; + auto &Store = Context.pImpl->MDLocations; + auto I = Store.find_as(Key); + if (I != Store.end()) + return *I; + if (!ShouldCreate) + return nullptr; + } else { + assert(ShouldCreate && "Expected non-uniqued nodes to always be created"); + } - auto *N = constructHelper(Context, Uniqued, Line, Column, Scope, InlinedAt); - Store.insert(N); - return N; -} + SmallVector Ops; + Ops.push_back(Scope); + if (InlinedAt) + Ops.push_back(InlinedAt); + auto *N = new (Ops.size()) MDLocation(Context, Storage, Line, Column, Ops); -MDLocation *MDLocation::getDistinct(LLVMContext &Context, unsigned Line, - unsigned Column, Metadata *Scope, - Metadata *InlinedAt) { - // Fixup line/column. - adjustLine(Line); - adjustColumn(Column); + switch (Storage) { + case Uniqued: + Context.pImpl->MDLocations.insert(N); + break; + case Distinct: + N->storeDistinctInContext(); + break; + case Temporary: + llvm_unreachable("Unexpected temporary node"); + } - auto *N = constructHelper(Context, Distinct, Line, Column, Scope, InlinedAt); - N->storeDistinctInContext(); return N; } -- 2.7.4