IR: Add MDNode::isDistinct()
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>
Wed, 7 Jan 2015 21:35:38 +0000 (21:35 +0000)
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>
Wed, 7 Jan 2015 21:35:38 +0000 (21:35 +0000)
Add API to indicate whether an `MDNode` is distinct.  A distinct node is
not stored in the MDNode uniquing tables, and will never be returned by
`MDNode::get()`.

Although distinct nodes are only currently created by uniquing
collisions (when operands change), PR22111 will allow these nodes to be
explicitly created.

llvm-svn: 225401

llvm/include/llvm/IR/Metadata.h
llvm/unittests/IR/MetadataTest.cpp

index a010341..4ae2913 100644 (file)
@@ -642,6 +642,12 @@ public:
   /// \brief Check if node is fully resolved.
   bool isResolved() const;
 
+  /// \brief Check if node is distinct.
+  ///
+  /// Distinct nodes are not uniqued, and will not be returned by \a
+  /// MDNode::get().
+  bool isDistinct() const { return IsDistinctInContext; }
+
 protected:
   /// \brief Set an operand.
   ///
index ef071b3..9c4403c 100644 (file)
@@ -222,6 +222,33 @@ TEST_F(MDNodeTest, NullOperand) {
   EXPECT_EQ(N, NullOp);
 }
 
+TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
+  // !{}
+  MDNode *Empty = MDNode::get(Context, None);
+  ASSERT_TRUE(Empty->isResolved());
+  EXPECT_FALSE(Empty->isDistinct());
+
+  // !{!{}}
+  Metadata *Wrapped1Ops[] = {Empty};
+  MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
+  ASSERT_EQ(Empty, Wrapped1->getOperand(0));
+  ASSERT_TRUE(Wrapped1->isResolved());
+  EXPECT_FALSE(Wrapped1->isDistinct());
+
+  // !{!{!{}}}
+  Metadata *Wrapped2Ops[] = {Wrapped1};
+  MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
+  ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
+  ASSERT_TRUE(Wrapped2->isResolved());
+  EXPECT_FALSE(Wrapped2->isDistinct());
+
+  // !{!{!{}}} => !{!{}}
+  Wrapped2->replaceOperandWith(0, Empty);
+  ASSERT_EQ(Empty, Wrapped2->getOperand(0));
+  EXPECT_TRUE(Wrapped2->isDistinct());
+  EXPECT_FALSE(Wrapped1->isDistinct());
+}
+
 typedef MetadataTest MetadataAsValueTest;
 
 TEST_F(MetadataAsValueTest, MDNode) {