IR: Fix an inverted assertion when replacing resolved operands
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>
Tue, 13 Jan 2015 00:10:38 +0000 (00:10 +0000)
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>
Tue, 13 Jan 2015 00:10:38 +0000 (00:10 +0000)
Add a unit test, since this bug was only exposed by clang tests.  Thanks
to Rafael for tracking this down!

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

lib/IR/Metadata.cpp
unittests/IR/MetadataTest.cpp

index 13844313925aaf5a78f24b5e5ad9a3ccc01c82fa..0132bab82c675edc61f12ff307890e2a32d9660c 100644 (file)
@@ -444,7 +444,7 @@ void UniquableMDNode::resolveAfterOperandChange(Metadata *Old, Metadata *New) {
 
   // Check if an operand was resolved.
   if (!isOperandUnresolved(Old))
 
   // Check if an operand was resolved.
   if (!isOperandUnresolved(Old))
-    assert(isOperandUnresolved(New) && "Operand just became unresolved");
+    assert(!isOperandUnresolved(New) && "Operand just became unresolved");
   else if (!isOperandUnresolved(New))
     decrementUnresolvedOperandCount();
 }
   else if (!isOperandUnresolved(New))
     decrementUnresolvedOperandCount();
 }
index af110742dd7118291ac3ba726e2fdf1f8a5e0448..c364aeacba114154b06f7eb84c9a3ca38dac4fd2 100644 (file)
@@ -7,6 +7,7 @@
 //
 //===----------------------------------------------------------------------===//
 
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/IR/Metadata.h"
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/Metadata.h"
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/Instructions.h"
@@ -361,6 +362,28 @@ TEST_F(MDNodeTest, handleChangedOperandRecursion) {
   EXPECT_EQ(N4, N6->getOperand(0));
 }
 
   EXPECT_EQ(N4, N6->getOperand(0));
 }
 
+TEST_F(MDNodeTest, replaceResolvedOperand) {
+  // Check code for replacing one resolved operand with another.  If doing this
+  // directly (via replaceOperandWith()) becomes illegal, change the operand to
+  // a global value that gets RAUW'ed.
+  //
+  // Use a temporary node to keep N from being resolved.
+  std::unique_ptr<MDNodeFwdDecl> Temp(MDNodeFwdDecl::get(Context, None));
+  Metadata *Ops[] = {nullptr, Temp.get()};
+
+  MDNode *Empty = MDTuple::get(Context, {});
+  MDNode *N = MDTuple::get(Context, Ops);
+  EXPECT_EQ(nullptr, N->getOperand(0));
+  ASSERT_FALSE(N->isResolved());
+
+  // Check code for replacing resolved nodes.
+  N->replaceOperandWith(0, Empty);
+  EXPECT_EQ(Empty, N->getOperand(0));
+
+  // Remove the reference to Temp; required for teardown.
+  N->replaceOperandWith(1, nullptr);
+}
+
 typedef MetadataTest MetadataAsValueTest;
 
 TEST_F(MetadataAsValueTest, MDNode) {
 typedef MetadataTest MetadataAsValueTest;
 
 TEST_F(MetadataAsValueTest, MDNode) {