[llvm] r226497 - IR: Reuse `getImpl()` for `getDistinct()`, NFC

Duncan P. N. Exon Smith dexonsmith at apple.com
Mon Jan 19 12:14:15 PST 2015


Author: dexonsmith
Date: Mon Jan 19 14:14:15 2015
New Revision: 226497

URL: http://llvm.org/viewvc/llvm-project?rev=226497&view=rev
Log:
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.

Modified:
    llvm/trunk/include/llvm/IR/Metadata.h
    llvm/trunk/lib/IR/Metadata.cpp

Modified: llvm/trunk/include/llvm/IR/Metadata.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/Metadata.h?rev=226497&r1=226496&r2=226497&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/Metadata.h (original)
+++ llvm/trunk/include/llvm/IR/Metadata.h Mon Jan 19 14:14:15 2015
@@ -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 UniquableMDNod
              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 UniquableMDNod
 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; }

Modified: llvm/trunk/lib/IR/Metadata.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Metadata.cpp?rev=226497&r1=226496&r2=226497&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Metadata.cpp (original)
+++ llvm/trunk/lib/IR/Metadata.cpp Mon Jan 19 14:14:15 2015
@@ -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;
-
-  // 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;
-}
+  auto *N = new (MDs.size()) MDTuple(Context, Storage, Hash, MDs);
+
+  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, S
   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 &Colum
 
 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;
 }
 





More information about the llvm-commits mailing list