IR: Reuse `getImpl()` for `getDistinct()`, NFC
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>
Mon, 19 Jan 2015 20:14:15 +0000 (20:14 +0000)
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>
Mon, 19 Jan 2015 20:14:15 +0000 (20:14 +0000)
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.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@226497 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/IR/Metadata.h
lib/IR/Metadata.cpp

index 5371efba122407e545f585e7d2f6f616e39f5993..8cbd1df9dcbf73e6b087497881122473e5f57d68 100644 (file)
@@ -855,31 +855,36 @@ class MDTuple : public UniquableMDNode {
   friend class LLVMContextImpl;
   friend class UniquableMDNode;
 
-  MDTuple(LLVMContext &C, StorageType Storage, ArrayRef<Metadata *> Vals)
-      : UniquableMDNode(C, MDTupleKind, Storage, Vals) {}
+  MDTuple(LLVMContext &C, StorageType Storage, unsigned Hash,
+          ArrayRef<Metadata *> Vals)
+      : UniquableMDNode(C, MDTupleKind, Storage, Vals) {
+    setHash(Hash);
+  }
   ~MDTuple() { dropAllReferences(); }
 
   void setHash(unsigned Hash) { MDNodeSubclassData = Hash; }
   void recalculateHash();
 
   static MDTuple *getImpl(LLVMContext &Context, ArrayRef<Metadata *> 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<Metadata *> MDs) {
-    return getImpl(Context, MDs, /* ShouldCreate */ true);
+    return getImpl(Context, MDs, Uniqued);
   }
   static MDTuple *getIfExists(LLVMContext &Context, ArrayRef<Metadata *> 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<Metadata *> MDs);
+  static MDTuple *getDistinct(LLVMContext &Context, ArrayRef<Metadata *> 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<Metadata *> 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; }
index 6ccb55d18b29b2b5e7548aeddd1176d8c0234771..cfa53ef6f33fbecff4877e7751676466ec974778 100644 (file)
@@ -605,26 +605,35 @@ void UniquableMDNode::eraseFromStore() {
 }
 
 MDTuple *MDTuple::getImpl(LLVMContext &Context, ArrayRef<Metadata *> 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<Metadata *> 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<Metadata *, 2> 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<Metadata *, 2> 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;
 }