IR: Drop uniquing for self-referencing MDNodes
[oota-llvm.git] / unittests / IR / MetadataTest.cpp
1 //===- llvm/unittest/IR/Metadata.cpp - Metadata unit tests ----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/IR/Metadata.h"
11 #include "llvm/IR/Constants.h"
12 #include "llvm/IR/Instructions.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/IR/Type.h"
16 #include "llvm/IR/ValueHandle.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include "gtest/gtest.h"
19 using namespace llvm;
20
21 namespace {
22
23 class MetadataTest : public testing::Test {
24 protected:
25   LLVMContext Context;
26 };
27 typedef MetadataTest MDStringTest;
28
29 // Test that construction of MDString with different value produces different
30 // MDString objects, even with the same string pointer and nulls in the string.
31 TEST_F(MDStringTest, CreateDifferent) {
32   char x[3] = { 'f', 0, 'A' };
33   MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
34   x[2] = 'B';
35   MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
36   EXPECT_NE(s1, s2);
37 }
38
39 // Test that creation of MDStrings with the same string contents produces the
40 // same MDString object, even with different pointers.
41 TEST_F(MDStringTest, CreateSame) {
42   char x[4] = { 'a', 'b', 'c', 'X' };
43   char y[4] = { 'a', 'b', 'c', 'Y' };
44
45   MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
46   MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
47   EXPECT_EQ(s1, s2);
48 }
49
50 // Test that MDString prints out the string we fed it.
51 TEST_F(MDStringTest, PrintingSimple) {
52   char *str = new char[13];
53   strncpy(str, "testing 1 2 3", 13);
54   MDString *s = MDString::get(Context, StringRef(str, 13));
55   strncpy(str, "aaaaaaaaaaaaa", 13);
56   delete[] str;
57
58   std::string Str;
59   raw_string_ostream oss(Str);
60   s->print(oss);
61   EXPECT_STREQ("metadata !\"testing 1 2 3\"", oss.str().c_str());
62 }
63
64 // Test printing of MDString with non-printable characters.
65 TEST_F(MDStringTest, PrintingComplex) {
66   char str[5] = {0, '\n', '"', '\\', (char)-1};
67   MDString *s = MDString::get(Context, StringRef(str+0, 5));
68   std::string Str;
69   raw_string_ostream oss(Str);
70   s->print(oss);
71   EXPECT_STREQ("metadata !\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
72 }
73
74 typedef MetadataTest MDNodeTest;
75
76 // Test the two constructors, and containing other Constants.
77 TEST_F(MDNodeTest, Simple) {
78   char x[3] = { 'a', 'b', 'c' };
79   char y[3] = { '1', '2', '3' };
80
81   MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
82   MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
83   ConstantInt *CI = ConstantInt::get(getGlobalContext(), APInt(8, 0));
84
85   std::vector<Value *> V;
86   V.push_back(s1);
87   V.push_back(CI);
88   V.push_back(s2);
89
90   MDNode *n1 = MDNode::get(Context, V);
91   Value *const c1 = n1;
92   MDNode *n2 = MDNode::get(Context, c1);
93   Value *const c2 = n2;
94   MDNode *n3 = MDNode::get(Context, V);
95   MDNode *n4 = MDNode::getIfExists(Context, V);
96   MDNode *n5 = MDNode::getIfExists(Context, c1);
97   MDNode *n6 = MDNode::getIfExists(Context, c2);
98   EXPECT_NE(n1, n2);
99   EXPECT_EQ(n1, n3);
100   EXPECT_EQ(n4, n1);
101   EXPECT_EQ(n5, n2);
102   EXPECT_EQ(n6, (Value*)nullptr);
103
104   EXPECT_EQ(3u, n1->getNumOperands());
105   EXPECT_EQ(s1, n1->getOperand(0));
106   EXPECT_EQ(CI, n1->getOperand(1));
107   EXPECT_EQ(s2, n1->getOperand(2));
108
109   EXPECT_EQ(1u, n2->getNumOperands());
110   EXPECT_EQ(n1, n2->getOperand(0));
111 }
112
113 TEST_F(MDNodeTest, Delete) {
114   Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
115   Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
116
117   Value *const V = I;
118   MDNode *n = MDNode::get(Context, V);
119   WeakVH wvh = n;
120
121   EXPECT_EQ(n, wvh);
122
123   delete I;
124 }
125
126 TEST_F(MDNodeTest, SelfReference) {
127   // !0 = metadata !{metadata !0}
128   // !1 = metadata !{metadata !0}
129   {
130     MDNode *Temp = MDNode::getTemporary(Context, None);
131     Value *Args[] = {Temp};
132     MDNode *Self = MDNode::get(Context, Args);
133     Self->replaceOperandWith(0, Self);
134     MDNode::deleteTemporary(Temp);
135     ASSERT_EQ(Self, Self->getOperand(0));
136
137     // Self-references should be distinct, so MDNode::get() should grab a
138     // uniqued node that references Self, not Self.
139     Args[0] = Self;
140     MDNode *Ref1 = MDNode::get(Context, Args);
141     MDNode *Ref2 = MDNode::get(Context, Args);
142     EXPECT_NE(Self, Ref1);
143     EXPECT_EQ(Ref1, Ref2);
144   }
145
146   // !0 = metadata !{metadata !0, metadata !{}}
147   // !1 = metadata !{metadata !0, metadata !{}}
148   {
149     MDNode *Temp = MDNode::getTemporary(Context, None);
150     Value *Args[] = {Temp, MDNode::get(Context, None)};
151     MDNode *Self = MDNode::get(Context, Args);
152     Self->replaceOperandWith(0, Self);
153     MDNode::deleteTemporary(Temp);
154     ASSERT_EQ(Self, Self->getOperand(0));
155
156     // Self-references should be distinct, so MDNode::get() should grab a
157     // uniqued node that references Self, not Self itself.
158     Args[0] = Self;
159     MDNode *Ref1 = MDNode::get(Context, Args);
160     MDNode *Ref2 = MDNode::get(Context, Args);
161     EXPECT_NE(Self, Ref1);
162     EXPECT_EQ(Ref1, Ref2);
163   }
164 }
165
166 TEST(NamedMDNodeTest, Search) {
167   LLVMContext Context;
168   Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 1);
169   Constant *C2 = ConstantInt::get(Type::getInt32Ty(Context), 2);
170
171   Value *const V = C;
172   Value *const V2 = C2;
173   MDNode *n = MDNode::get(Context, V);
174   MDNode *n2 = MDNode::get(Context, V2);
175
176   Module M("MyModule", Context);
177   const char *Name = "llvm.NMD1";
178   NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
179   NMD->addOperand(n);
180   NMD->addOperand(n2);
181
182   std::string Str;
183   raw_string_ostream oss(Str);
184   NMD->print(oss);
185   EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
186                oss.str().c_str());
187 }
188 }