1f4a2713aSLionel Sambuc //===- llvm/unittest/IR/Metadata.cpp - Metadata unit tests ----------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc 
10*0a6a1f1dSLionel Sambuc #include "llvm/ADT/STLExtras.h"
11f4a2713aSLionel Sambuc #include "llvm/IR/Constants.h"
12f4a2713aSLionel Sambuc #include "llvm/IR/Instructions.h"
13f4a2713aSLionel Sambuc #include "llvm/IR/LLVMContext.h"
14*0a6a1f1dSLionel Sambuc #include "llvm/IR/Metadata.h"
15f4a2713aSLionel Sambuc #include "llvm/IR/Module.h"
16f4a2713aSLionel Sambuc #include "llvm/IR/Type.h"
17f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
18f4a2713aSLionel Sambuc #include "gtest/gtest.h"
19f4a2713aSLionel Sambuc using namespace llvm;
20f4a2713aSLionel Sambuc 
21f4a2713aSLionel Sambuc namespace {
22f4a2713aSLionel Sambuc 
23f4a2713aSLionel Sambuc class MetadataTest : public testing::Test {
24f4a2713aSLionel Sambuc protected:
25f4a2713aSLionel Sambuc   LLVMContext Context;
getNode()26*0a6a1f1dSLionel Sambuc   MDNode *getNode() { return MDNode::get(Context, None); }
getNode(Metadata * MD)27*0a6a1f1dSLionel Sambuc   MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); }
getNode(Metadata * MD1,Metadata * MD2)28*0a6a1f1dSLionel Sambuc   MDNode *getNode(Metadata *MD1, Metadata *MD2) {
29*0a6a1f1dSLionel Sambuc     Metadata *MDs[] = {MD1, MD2};
30*0a6a1f1dSLionel Sambuc     return MDNode::get(Context, MDs);
31*0a6a1f1dSLionel Sambuc   }
32f4a2713aSLionel Sambuc };
33f4a2713aSLionel Sambuc typedef MetadataTest MDStringTest;
34f4a2713aSLionel Sambuc 
35f4a2713aSLionel Sambuc // Test that construction of MDString with different value produces different
36f4a2713aSLionel Sambuc // MDString objects, even with the same string pointer and nulls in the string.
TEST_F(MDStringTest,CreateDifferent)37f4a2713aSLionel Sambuc TEST_F(MDStringTest, CreateDifferent) {
38f4a2713aSLionel Sambuc   char x[3] = { 'f', 0, 'A' };
39f4a2713aSLionel Sambuc   MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
40f4a2713aSLionel Sambuc   x[2] = 'B';
41f4a2713aSLionel Sambuc   MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
42f4a2713aSLionel Sambuc   EXPECT_NE(s1, s2);
43f4a2713aSLionel Sambuc }
44f4a2713aSLionel Sambuc 
45f4a2713aSLionel Sambuc // Test that creation of MDStrings with the same string contents produces the
46f4a2713aSLionel Sambuc // same MDString object, even with different pointers.
TEST_F(MDStringTest,CreateSame)47f4a2713aSLionel Sambuc TEST_F(MDStringTest, CreateSame) {
48f4a2713aSLionel Sambuc   char x[4] = { 'a', 'b', 'c', 'X' };
49f4a2713aSLionel Sambuc   char y[4] = { 'a', 'b', 'c', 'Y' };
50f4a2713aSLionel Sambuc 
51f4a2713aSLionel Sambuc   MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
52f4a2713aSLionel Sambuc   MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
53f4a2713aSLionel Sambuc   EXPECT_EQ(s1, s2);
54f4a2713aSLionel Sambuc }
55f4a2713aSLionel Sambuc 
56f4a2713aSLionel Sambuc // Test that MDString prints out the string we fed it.
TEST_F(MDStringTest,PrintingSimple)57f4a2713aSLionel Sambuc TEST_F(MDStringTest, PrintingSimple) {
58f4a2713aSLionel Sambuc   char *str = new char[13];
59f4a2713aSLionel Sambuc   strncpy(str, "testing 1 2 3", 13);
60f4a2713aSLionel Sambuc   MDString *s = MDString::get(Context, StringRef(str, 13));
61f4a2713aSLionel Sambuc   strncpy(str, "aaaaaaaaaaaaa", 13);
62f4a2713aSLionel Sambuc   delete[] str;
63f4a2713aSLionel Sambuc 
64f4a2713aSLionel Sambuc   std::string Str;
65f4a2713aSLionel Sambuc   raw_string_ostream oss(Str);
66f4a2713aSLionel Sambuc   s->print(oss);
67*0a6a1f1dSLionel Sambuc   EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
68f4a2713aSLionel Sambuc }
69f4a2713aSLionel Sambuc 
70f4a2713aSLionel Sambuc // Test printing of MDString with non-printable characters.
TEST_F(MDStringTest,PrintingComplex)71f4a2713aSLionel Sambuc TEST_F(MDStringTest, PrintingComplex) {
72f4a2713aSLionel Sambuc   char str[5] = {0, '\n', '"', '\\', (char)-1};
73f4a2713aSLionel Sambuc   MDString *s = MDString::get(Context, StringRef(str+0, 5));
74f4a2713aSLionel Sambuc   std::string Str;
75f4a2713aSLionel Sambuc   raw_string_ostream oss(Str);
76f4a2713aSLionel Sambuc   s->print(oss);
77*0a6a1f1dSLionel Sambuc   EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
78f4a2713aSLionel Sambuc }
79f4a2713aSLionel Sambuc 
80f4a2713aSLionel Sambuc typedef MetadataTest MDNodeTest;
81f4a2713aSLionel Sambuc 
82f4a2713aSLionel Sambuc // Test the two constructors, and containing other Constants.
TEST_F(MDNodeTest,Simple)83f4a2713aSLionel Sambuc TEST_F(MDNodeTest, Simple) {
84f4a2713aSLionel Sambuc   char x[3] = { 'a', 'b', 'c' };
85f4a2713aSLionel Sambuc   char y[3] = { '1', '2', '3' };
86f4a2713aSLionel Sambuc 
87f4a2713aSLionel Sambuc   MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
88f4a2713aSLionel Sambuc   MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
89*0a6a1f1dSLionel Sambuc   ConstantAsMetadata *CI = ConstantAsMetadata::get(
90*0a6a1f1dSLionel Sambuc       ConstantInt::get(getGlobalContext(), APInt(8, 0)));
91f4a2713aSLionel Sambuc 
92*0a6a1f1dSLionel Sambuc   std::vector<Metadata *> V;
93f4a2713aSLionel Sambuc   V.push_back(s1);
94f4a2713aSLionel Sambuc   V.push_back(CI);
95f4a2713aSLionel Sambuc   V.push_back(s2);
96f4a2713aSLionel Sambuc 
97f4a2713aSLionel Sambuc   MDNode *n1 = MDNode::get(Context, V);
98*0a6a1f1dSLionel Sambuc   Metadata *const c1 = n1;
99f4a2713aSLionel Sambuc   MDNode *n2 = MDNode::get(Context, c1);
100*0a6a1f1dSLionel Sambuc   Metadata *const c2 = n2;
101f4a2713aSLionel Sambuc   MDNode *n3 = MDNode::get(Context, V);
102f4a2713aSLionel Sambuc   MDNode *n4 = MDNode::getIfExists(Context, V);
103f4a2713aSLionel Sambuc   MDNode *n5 = MDNode::getIfExists(Context, c1);
104f4a2713aSLionel Sambuc   MDNode *n6 = MDNode::getIfExists(Context, c2);
105f4a2713aSLionel Sambuc   EXPECT_NE(n1, n2);
106f4a2713aSLionel Sambuc   EXPECT_EQ(n1, n3);
107f4a2713aSLionel Sambuc   EXPECT_EQ(n4, n1);
108f4a2713aSLionel Sambuc   EXPECT_EQ(n5, n2);
109*0a6a1f1dSLionel Sambuc   EXPECT_EQ(n6, (Metadata *)nullptr);
110f4a2713aSLionel Sambuc 
111f4a2713aSLionel Sambuc   EXPECT_EQ(3u, n1->getNumOperands());
112f4a2713aSLionel Sambuc   EXPECT_EQ(s1, n1->getOperand(0));
113f4a2713aSLionel Sambuc   EXPECT_EQ(CI, n1->getOperand(1));
114f4a2713aSLionel Sambuc   EXPECT_EQ(s2, n1->getOperand(2));
115f4a2713aSLionel Sambuc 
116f4a2713aSLionel Sambuc   EXPECT_EQ(1u, n2->getNumOperands());
117f4a2713aSLionel Sambuc   EXPECT_EQ(n1, n2->getOperand(0));
118f4a2713aSLionel Sambuc }
119f4a2713aSLionel Sambuc 
TEST_F(MDNodeTest,Delete)120f4a2713aSLionel Sambuc TEST_F(MDNodeTest, Delete) {
121f4a2713aSLionel Sambuc   Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
122f4a2713aSLionel Sambuc   Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
123f4a2713aSLionel Sambuc 
124*0a6a1f1dSLionel Sambuc   Metadata *const V = LocalAsMetadata::get(I);
125f4a2713aSLionel Sambuc   MDNode *n = MDNode::get(Context, V);
126*0a6a1f1dSLionel Sambuc   TrackingMDRef wvh(n);
127f4a2713aSLionel Sambuc 
128f4a2713aSLionel Sambuc   EXPECT_EQ(n, wvh);
129f4a2713aSLionel Sambuc 
130f4a2713aSLionel Sambuc   delete I;
131f4a2713aSLionel Sambuc }
132f4a2713aSLionel Sambuc 
TEST_F(MDNodeTest,DeleteMDNodeFwdDecl)133*0a6a1f1dSLionel Sambuc TEST_F(MDNodeTest, DeleteMDNodeFwdDecl) {
134*0a6a1f1dSLionel Sambuc   delete MDNode::getTemporary(Context, None);
135*0a6a1f1dSLionel Sambuc }
136*0a6a1f1dSLionel Sambuc 
TEST_F(MDNodeTest,SelfReference)137*0a6a1f1dSLionel Sambuc TEST_F(MDNodeTest, SelfReference) {
138*0a6a1f1dSLionel Sambuc   // !0 = !{!0}
139*0a6a1f1dSLionel Sambuc   // !1 = !{!0}
140*0a6a1f1dSLionel Sambuc   {
141*0a6a1f1dSLionel Sambuc     MDNode *Temp = MDNode::getTemporary(Context, None);
142*0a6a1f1dSLionel Sambuc     Metadata *Args[] = {Temp};
143*0a6a1f1dSLionel Sambuc     MDNode *Self = MDNode::get(Context, Args);
144*0a6a1f1dSLionel Sambuc     Self->replaceOperandWith(0, Self);
145*0a6a1f1dSLionel Sambuc     MDNode::deleteTemporary(Temp);
146*0a6a1f1dSLionel Sambuc     ASSERT_EQ(Self, Self->getOperand(0));
147*0a6a1f1dSLionel Sambuc 
148*0a6a1f1dSLionel Sambuc     // Self-references should be distinct, so MDNode::get() should grab a
149*0a6a1f1dSLionel Sambuc     // uniqued node that references Self, not Self.
150*0a6a1f1dSLionel Sambuc     Args[0] = Self;
151*0a6a1f1dSLionel Sambuc     MDNode *Ref1 = MDNode::get(Context, Args);
152*0a6a1f1dSLionel Sambuc     MDNode *Ref2 = MDNode::get(Context, Args);
153*0a6a1f1dSLionel Sambuc     EXPECT_NE(Self, Ref1);
154*0a6a1f1dSLionel Sambuc     EXPECT_EQ(Ref1, Ref2);
155*0a6a1f1dSLionel Sambuc   }
156*0a6a1f1dSLionel Sambuc 
157*0a6a1f1dSLionel Sambuc   // !0 = !{!0, !{}}
158*0a6a1f1dSLionel Sambuc   // !1 = !{!0, !{}}
159*0a6a1f1dSLionel Sambuc   {
160*0a6a1f1dSLionel Sambuc     MDNode *Temp = MDNode::getTemporary(Context, None);
161*0a6a1f1dSLionel Sambuc     Metadata *Args[] = {Temp, MDNode::get(Context, None)};
162*0a6a1f1dSLionel Sambuc     MDNode *Self = MDNode::get(Context, Args);
163*0a6a1f1dSLionel Sambuc     Self->replaceOperandWith(0, Self);
164*0a6a1f1dSLionel Sambuc     MDNode::deleteTemporary(Temp);
165*0a6a1f1dSLionel Sambuc     ASSERT_EQ(Self, Self->getOperand(0));
166*0a6a1f1dSLionel Sambuc 
167*0a6a1f1dSLionel Sambuc     // Self-references should be distinct, so MDNode::get() should grab a
168*0a6a1f1dSLionel Sambuc     // uniqued node that references Self, not Self itself.
169*0a6a1f1dSLionel Sambuc     Args[0] = Self;
170*0a6a1f1dSLionel Sambuc     MDNode *Ref1 = MDNode::get(Context, Args);
171*0a6a1f1dSLionel Sambuc     MDNode *Ref2 = MDNode::get(Context, Args);
172*0a6a1f1dSLionel Sambuc     EXPECT_NE(Self, Ref1);
173*0a6a1f1dSLionel Sambuc     EXPECT_EQ(Ref1, Ref2);
174*0a6a1f1dSLionel Sambuc   }
175*0a6a1f1dSLionel Sambuc }
176*0a6a1f1dSLionel Sambuc 
TEST_F(MDNodeTest,Print)177*0a6a1f1dSLionel Sambuc TEST_F(MDNodeTest, Print) {
178*0a6a1f1dSLionel Sambuc   Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
179*0a6a1f1dSLionel Sambuc   MDString *S = MDString::get(Context, "foo");
180*0a6a1f1dSLionel Sambuc   MDNode *N0 = getNode();
181*0a6a1f1dSLionel Sambuc   MDNode *N1 = getNode(N0);
182*0a6a1f1dSLionel Sambuc   MDNode *N2 = getNode(N0, N1);
183*0a6a1f1dSLionel Sambuc 
184*0a6a1f1dSLionel Sambuc   Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
185*0a6a1f1dSLionel Sambuc   MDNode *N = MDNode::get(Context, Args);
186*0a6a1f1dSLionel Sambuc 
187*0a6a1f1dSLionel Sambuc   std::string Expected;
188*0a6a1f1dSLionel Sambuc   {
189*0a6a1f1dSLionel Sambuc     raw_string_ostream OS(Expected);
190*0a6a1f1dSLionel Sambuc     OS << "!{";
191*0a6a1f1dSLionel Sambuc     C->printAsOperand(OS);
192*0a6a1f1dSLionel Sambuc     OS << ", ";
193*0a6a1f1dSLionel Sambuc     S->printAsOperand(OS);
194*0a6a1f1dSLionel Sambuc     OS << ", null";
195*0a6a1f1dSLionel Sambuc     MDNode *Nodes[] = {N0, N1, N2};
196*0a6a1f1dSLionel Sambuc     for (auto *Node : Nodes)
197*0a6a1f1dSLionel Sambuc       OS << ", <" << (void *)Node << ">";
198*0a6a1f1dSLionel Sambuc     OS << "}\n";
199*0a6a1f1dSLionel Sambuc   }
200*0a6a1f1dSLionel Sambuc 
201*0a6a1f1dSLionel Sambuc   std::string Actual;
202*0a6a1f1dSLionel Sambuc   {
203*0a6a1f1dSLionel Sambuc     raw_string_ostream OS(Actual);
204*0a6a1f1dSLionel Sambuc     N->print(OS);
205*0a6a1f1dSLionel Sambuc   }
206*0a6a1f1dSLionel Sambuc 
207*0a6a1f1dSLionel Sambuc   EXPECT_EQ(Expected, Actual);
208*0a6a1f1dSLionel Sambuc }
209*0a6a1f1dSLionel Sambuc 
TEST_F(MDNodeTest,NullOperand)210*0a6a1f1dSLionel Sambuc TEST_F(MDNodeTest, NullOperand) {
211*0a6a1f1dSLionel Sambuc   // metadata !{}
212*0a6a1f1dSLionel Sambuc   MDNode *Empty = MDNode::get(Context, None);
213*0a6a1f1dSLionel Sambuc 
214*0a6a1f1dSLionel Sambuc   // metadata !{metadata !{}}
215*0a6a1f1dSLionel Sambuc   Metadata *Ops[] = {Empty};
216*0a6a1f1dSLionel Sambuc   MDNode *N = MDNode::get(Context, Ops);
217*0a6a1f1dSLionel Sambuc   ASSERT_EQ(Empty, N->getOperand(0));
218*0a6a1f1dSLionel Sambuc 
219*0a6a1f1dSLionel Sambuc   // metadata !{metadata !{}} => metadata !{null}
220*0a6a1f1dSLionel Sambuc   N->replaceOperandWith(0, nullptr);
221*0a6a1f1dSLionel Sambuc   ASSERT_EQ(nullptr, N->getOperand(0));
222*0a6a1f1dSLionel Sambuc 
223*0a6a1f1dSLionel Sambuc   // metadata !{null}
224*0a6a1f1dSLionel Sambuc   Ops[0] = nullptr;
225*0a6a1f1dSLionel Sambuc   MDNode *NullOp = MDNode::get(Context, Ops);
226*0a6a1f1dSLionel Sambuc   ASSERT_EQ(nullptr, NullOp->getOperand(0));
227*0a6a1f1dSLionel Sambuc   EXPECT_EQ(N, NullOp);
228*0a6a1f1dSLionel Sambuc }
229*0a6a1f1dSLionel Sambuc 
TEST_F(MDNodeTest,DistinctOnUniquingCollision)230*0a6a1f1dSLionel Sambuc TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
231*0a6a1f1dSLionel Sambuc   // !{}
232*0a6a1f1dSLionel Sambuc   MDNode *Empty = MDNode::get(Context, None);
233*0a6a1f1dSLionel Sambuc   ASSERT_TRUE(Empty->isResolved());
234*0a6a1f1dSLionel Sambuc   EXPECT_FALSE(Empty->isDistinct());
235*0a6a1f1dSLionel Sambuc 
236*0a6a1f1dSLionel Sambuc   // !{!{}}
237*0a6a1f1dSLionel Sambuc   Metadata *Wrapped1Ops[] = {Empty};
238*0a6a1f1dSLionel Sambuc   MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
239*0a6a1f1dSLionel Sambuc   ASSERT_EQ(Empty, Wrapped1->getOperand(0));
240*0a6a1f1dSLionel Sambuc   ASSERT_TRUE(Wrapped1->isResolved());
241*0a6a1f1dSLionel Sambuc   EXPECT_FALSE(Wrapped1->isDistinct());
242*0a6a1f1dSLionel Sambuc 
243*0a6a1f1dSLionel Sambuc   // !{!{!{}}}
244*0a6a1f1dSLionel Sambuc   Metadata *Wrapped2Ops[] = {Wrapped1};
245*0a6a1f1dSLionel Sambuc   MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
246*0a6a1f1dSLionel Sambuc   ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
247*0a6a1f1dSLionel Sambuc   ASSERT_TRUE(Wrapped2->isResolved());
248*0a6a1f1dSLionel Sambuc   EXPECT_FALSE(Wrapped2->isDistinct());
249*0a6a1f1dSLionel Sambuc 
250*0a6a1f1dSLionel Sambuc   // !{!{!{}}} => !{!{}}
251*0a6a1f1dSLionel Sambuc   Wrapped2->replaceOperandWith(0, Empty);
252*0a6a1f1dSLionel Sambuc   ASSERT_EQ(Empty, Wrapped2->getOperand(0));
253*0a6a1f1dSLionel Sambuc   EXPECT_TRUE(Wrapped2->isDistinct());
254*0a6a1f1dSLionel Sambuc   EXPECT_FALSE(Wrapped1->isDistinct());
255*0a6a1f1dSLionel Sambuc }
256*0a6a1f1dSLionel Sambuc 
TEST_F(MDNodeTest,getDistinct)257*0a6a1f1dSLionel Sambuc TEST_F(MDNodeTest, getDistinct) {
258*0a6a1f1dSLionel Sambuc   // !{}
259*0a6a1f1dSLionel Sambuc   MDNode *Empty = MDNode::get(Context, None);
260*0a6a1f1dSLionel Sambuc   ASSERT_TRUE(Empty->isResolved());
261*0a6a1f1dSLionel Sambuc   ASSERT_FALSE(Empty->isDistinct());
262*0a6a1f1dSLionel Sambuc   ASSERT_EQ(Empty, MDNode::get(Context, None));
263*0a6a1f1dSLionel Sambuc 
264*0a6a1f1dSLionel Sambuc   // distinct !{}
265*0a6a1f1dSLionel Sambuc   MDNode *Distinct1 = MDNode::getDistinct(Context, None);
266*0a6a1f1dSLionel Sambuc   MDNode *Distinct2 = MDNode::getDistinct(Context, None);
267*0a6a1f1dSLionel Sambuc   EXPECT_TRUE(Distinct1->isResolved());
268*0a6a1f1dSLionel Sambuc   EXPECT_TRUE(Distinct2->isDistinct());
269*0a6a1f1dSLionel Sambuc   EXPECT_NE(Empty, Distinct1);
270*0a6a1f1dSLionel Sambuc   EXPECT_NE(Empty, Distinct2);
271*0a6a1f1dSLionel Sambuc   EXPECT_NE(Distinct1, Distinct2);
272*0a6a1f1dSLionel Sambuc 
273*0a6a1f1dSLionel Sambuc   // !{}
274*0a6a1f1dSLionel Sambuc   ASSERT_EQ(Empty, MDNode::get(Context, None));
275*0a6a1f1dSLionel Sambuc }
276*0a6a1f1dSLionel Sambuc 
TEST_F(MDNodeTest,TempIsDistinct)277*0a6a1f1dSLionel Sambuc TEST_F(MDNodeTest, TempIsDistinct) {
278*0a6a1f1dSLionel Sambuc   MDNode *T = MDNode::getTemporary(Context, None);
279*0a6a1f1dSLionel Sambuc   EXPECT_TRUE(T->isDistinct());
280*0a6a1f1dSLionel Sambuc   MDNode::deleteTemporary(T);
281*0a6a1f1dSLionel Sambuc }
282*0a6a1f1dSLionel Sambuc 
TEST_F(MDNodeTest,getDistinctWithUnresolvedOperands)283*0a6a1f1dSLionel Sambuc TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
284*0a6a1f1dSLionel Sambuc   // temporary !{}
285*0a6a1f1dSLionel Sambuc   MDNodeFwdDecl *Temp = MDNode::getTemporary(Context, None);
286*0a6a1f1dSLionel Sambuc   ASSERT_FALSE(Temp->isResolved());
287*0a6a1f1dSLionel Sambuc 
288*0a6a1f1dSLionel Sambuc   // distinct !{temporary !{}}
289*0a6a1f1dSLionel Sambuc   Metadata *Ops[] = {Temp};
290*0a6a1f1dSLionel Sambuc   MDNode *Distinct = MDNode::getDistinct(Context, Ops);
291*0a6a1f1dSLionel Sambuc   EXPECT_TRUE(Distinct->isResolved());
292*0a6a1f1dSLionel Sambuc   EXPECT_EQ(Temp, Distinct->getOperand(0));
293*0a6a1f1dSLionel Sambuc 
294*0a6a1f1dSLionel Sambuc   // temporary !{} => !{}
295*0a6a1f1dSLionel Sambuc   MDNode *Empty = MDNode::get(Context, None);
296*0a6a1f1dSLionel Sambuc   Temp->replaceAllUsesWith(Empty);
297*0a6a1f1dSLionel Sambuc   MDNode::deleteTemporary(Temp);
298*0a6a1f1dSLionel Sambuc   EXPECT_EQ(Empty, Distinct->getOperand(0));
299*0a6a1f1dSLionel Sambuc }
300*0a6a1f1dSLionel Sambuc 
TEST_F(MDNodeTest,handleChangedOperandRecursion)301*0a6a1f1dSLionel Sambuc TEST_F(MDNodeTest, handleChangedOperandRecursion) {
302*0a6a1f1dSLionel Sambuc   // !0 = !{}
303*0a6a1f1dSLionel Sambuc   MDNode *N0 = MDNode::get(Context, None);
304*0a6a1f1dSLionel Sambuc 
305*0a6a1f1dSLionel Sambuc   // !1 = !{!3, null}
306*0a6a1f1dSLionel Sambuc   std::unique_ptr<MDNodeFwdDecl> Temp3(MDNode::getTemporary(Context, None));
307*0a6a1f1dSLionel Sambuc   Metadata *Ops1[] = {Temp3.get(), nullptr};
308*0a6a1f1dSLionel Sambuc   MDNode *N1 = MDNode::get(Context, Ops1);
309*0a6a1f1dSLionel Sambuc 
310*0a6a1f1dSLionel Sambuc   // !2 = !{!3, !0}
311*0a6a1f1dSLionel Sambuc   Metadata *Ops2[] = {Temp3.get(), N0};
312*0a6a1f1dSLionel Sambuc   MDNode *N2 = MDNode::get(Context, Ops2);
313*0a6a1f1dSLionel Sambuc 
314*0a6a1f1dSLionel Sambuc   // !3 = !{!2}
315*0a6a1f1dSLionel Sambuc   Metadata *Ops3[] = {N2};
316*0a6a1f1dSLionel Sambuc   MDNode *N3 = MDNode::get(Context, Ops3);
317*0a6a1f1dSLionel Sambuc   Temp3->replaceAllUsesWith(N3);
318*0a6a1f1dSLionel Sambuc 
319*0a6a1f1dSLionel Sambuc   // !4 = !{!1}
320*0a6a1f1dSLionel Sambuc   Metadata *Ops4[] = {N1};
321*0a6a1f1dSLionel Sambuc   MDNode *N4 = MDNode::get(Context, Ops4);
322*0a6a1f1dSLionel Sambuc 
323*0a6a1f1dSLionel Sambuc   // Confirm that the cycle prevented RAUW from getting dropped.
324*0a6a1f1dSLionel Sambuc   EXPECT_TRUE(N0->isResolved());
325*0a6a1f1dSLionel Sambuc   EXPECT_FALSE(N1->isResolved());
326*0a6a1f1dSLionel Sambuc   EXPECT_FALSE(N2->isResolved());
327*0a6a1f1dSLionel Sambuc   EXPECT_FALSE(N3->isResolved());
328*0a6a1f1dSLionel Sambuc   EXPECT_FALSE(N4->isResolved());
329*0a6a1f1dSLionel Sambuc 
330*0a6a1f1dSLionel Sambuc   // Create a couple of distinct nodes to observe what's going on.
331*0a6a1f1dSLionel Sambuc   //
332*0a6a1f1dSLionel Sambuc   // !5 = distinct !{!2}
333*0a6a1f1dSLionel Sambuc   // !6 = distinct !{!3}
334*0a6a1f1dSLionel Sambuc   Metadata *Ops5[] = {N2};
335*0a6a1f1dSLionel Sambuc   MDNode *N5 = MDNode::getDistinct(Context, Ops5);
336*0a6a1f1dSLionel Sambuc   Metadata *Ops6[] = {N3};
337*0a6a1f1dSLionel Sambuc   MDNode *N6 = MDNode::getDistinct(Context, Ops6);
338*0a6a1f1dSLionel Sambuc 
339*0a6a1f1dSLionel Sambuc   // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
340*0a6a1f1dSLionel Sambuc   // This will ripple up, with !3 colliding with !4, and RAUWing.  Since !2
341*0a6a1f1dSLionel Sambuc   // references !3, this can cause a re-entry of handleChangedOperand() when !3
342*0a6a1f1dSLionel Sambuc   // is not ready for it.
343*0a6a1f1dSLionel Sambuc   //
344*0a6a1f1dSLionel Sambuc   // !2->replaceOperandWith(1, nullptr)
345*0a6a1f1dSLionel Sambuc   // !2: !{!3, !0} => !{!3, null}
346*0a6a1f1dSLionel Sambuc   // !2->replaceAllUsesWith(!1)
347*0a6a1f1dSLionel Sambuc   // !3: !{!2] => !{!1}
348*0a6a1f1dSLionel Sambuc   // !3->replaceAllUsesWith(!4)
349*0a6a1f1dSLionel Sambuc   N2->replaceOperandWith(1, nullptr);
350*0a6a1f1dSLionel Sambuc 
351*0a6a1f1dSLionel Sambuc   // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
352*0a6a1f1dSLionel Sambuc   // under us.  Just check that the other nodes are sane.
353*0a6a1f1dSLionel Sambuc   //
354*0a6a1f1dSLionel Sambuc   // !1 = !{!4, null}
355*0a6a1f1dSLionel Sambuc   // !4 = !{!1}
356*0a6a1f1dSLionel Sambuc   // !5 = distinct !{!1}
357*0a6a1f1dSLionel Sambuc   // !6 = distinct !{!4}
358*0a6a1f1dSLionel Sambuc   EXPECT_EQ(N4, N1->getOperand(0));
359*0a6a1f1dSLionel Sambuc   EXPECT_EQ(N1, N4->getOperand(0));
360*0a6a1f1dSLionel Sambuc   EXPECT_EQ(N1, N5->getOperand(0));
361*0a6a1f1dSLionel Sambuc   EXPECT_EQ(N4, N6->getOperand(0));
362*0a6a1f1dSLionel Sambuc }
363*0a6a1f1dSLionel Sambuc 
TEST_F(MDNodeTest,replaceResolvedOperand)364*0a6a1f1dSLionel Sambuc TEST_F(MDNodeTest, replaceResolvedOperand) {
365*0a6a1f1dSLionel Sambuc   // Check code for replacing one resolved operand with another.  If doing this
366*0a6a1f1dSLionel Sambuc   // directly (via replaceOperandWith()) becomes illegal, change the operand to
367*0a6a1f1dSLionel Sambuc   // a global value that gets RAUW'ed.
368*0a6a1f1dSLionel Sambuc   //
369*0a6a1f1dSLionel Sambuc   // Use a temporary node to keep N from being resolved.
370*0a6a1f1dSLionel Sambuc   std::unique_ptr<MDNodeFwdDecl> Temp(MDNodeFwdDecl::get(Context, None));
371*0a6a1f1dSLionel Sambuc   Metadata *Ops[] = {nullptr, Temp.get()};
372*0a6a1f1dSLionel Sambuc 
373*0a6a1f1dSLionel Sambuc   MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>());
374*0a6a1f1dSLionel Sambuc   MDNode *N = MDTuple::get(Context, Ops);
375*0a6a1f1dSLionel Sambuc   EXPECT_EQ(nullptr, N->getOperand(0));
376*0a6a1f1dSLionel Sambuc   ASSERT_FALSE(N->isResolved());
377*0a6a1f1dSLionel Sambuc 
378*0a6a1f1dSLionel Sambuc   // Check code for replacing resolved nodes.
379*0a6a1f1dSLionel Sambuc   N->replaceOperandWith(0, Empty);
380*0a6a1f1dSLionel Sambuc   EXPECT_EQ(Empty, N->getOperand(0));
381*0a6a1f1dSLionel Sambuc 
382*0a6a1f1dSLionel Sambuc   // Check code for adding another unresolved operand.
383*0a6a1f1dSLionel Sambuc   N->replaceOperandWith(0, Temp.get());
384*0a6a1f1dSLionel Sambuc   EXPECT_EQ(Temp.get(), N->getOperand(0));
385*0a6a1f1dSLionel Sambuc 
386*0a6a1f1dSLionel Sambuc   // Remove the references to Temp; required for teardown.
387*0a6a1f1dSLionel Sambuc   Temp->replaceAllUsesWith(nullptr);
388*0a6a1f1dSLionel Sambuc }
389*0a6a1f1dSLionel Sambuc 
390*0a6a1f1dSLionel Sambuc typedef MetadataTest MDLocationTest;
391*0a6a1f1dSLionel Sambuc 
TEST_F(MDLocationTest,Overflow)392*0a6a1f1dSLionel Sambuc TEST_F(MDLocationTest, Overflow) {
393*0a6a1f1dSLionel Sambuc   MDNode *N = MDNode::get(Context, None);
394*0a6a1f1dSLionel Sambuc   {
395*0a6a1f1dSLionel Sambuc     MDLocation *L = MDLocation::get(Context, 2, 7, N);
396*0a6a1f1dSLionel Sambuc     EXPECT_EQ(2u, L->getLine());
397*0a6a1f1dSLionel Sambuc     EXPECT_EQ(7u, L->getColumn());
398*0a6a1f1dSLionel Sambuc   }
399*0a6a1f1dSLionel Sambuc   unsigned U24 = 1u << 24;
400*0a6a1f1dSLionel Sambuc   unsigned U8 = 1u << 8;
401*0a6a1f1dSLionel Sambuc   {
402*0a6a1f1dSLionel Sambuc     MDLocation *L = MDLocation::get(Context, U24 - 1, U8 - 1, N);
403*0a6a1f1dSLionel Sambuc     EXPECT_EQ(U24 - 1, L->getLine());
404*0a6a1f1dSLionel Sambuc     EXPECT_EQ(U8 - 1, L->getColumn());
405*0a6a1f1dSLionel Sambuc   }
406*0a6a1f1dSLionel Sambuc   {
407*0a6a1f1dSLionel Sambuc     MDLocation *L = MDLocation::get(Context, U24, U8, N);
408*0a6a1f1dSLionel Sambuc     EXPECT_EQ(0u, L->getLine());
409*0a6a1f1dSLionel Sambuc     EXPECT_EQ(0u, L->getColumn());
410*0a6a1f1dSLionel Sambuc   }
411*0a6a1f1dSLionel Sambuc   {
412*0a6a1f1dSLionel Sambuc     MDLocation *L = MDLocation::get(Context, U24 + 1, U8 + 1, N);
413*0a6a1f1dSLionel Sambuc     EXPECT_EQ(0u, L->getLine());
414*0a6a1f1dSLionel Sambuc     EXPECT_EQ(0u, L->getColumn());
415*0a6a1f1dSLionel Sambuc   }
416*0a6a1f1dSLionel Sambuc }
417*0a6a1f1dSLionel Sambuc 
TEST_F(MDLocationTest,getDistinct)418*0a6a1f1dSLionel Sambuc TEST_F(MDLocationTest, getDistinct) {
419*0a6a1f1dSLionel Sambuc   MDNode *N = MDNode::get(Context, None);
420*0a6a1f1dSLionel Sambuc   MDLocation *L0 = MDLocation::getDistinct(Context, 2, 7, N);
421*0a6a1f1dSLionel Sambuc   EXPECT_TRUE(L0->isDistinct());
422*0a6a1f1dSLionel Sambuc   MDLocation *L1 = MDLocation::get(Context, 2, 7, N);
423*0a6a1f1dSLionel Sambuc   EXPECT_FALSE(L1->isDistinct());
424*0a6a1f1dSLionel Sambuc   EXPECT_EQ(L1, MDLocation::get(Context, 2, 7, N));
425*0a6a1f1dSLionel Sambuc }
426*0a6a1f1dSLionel Sambuc 
427*0a6a1f1dSLionel Sambuc typedef MetadataTest MetadataAsValueTest;
428*0a6a1f1dSLionel Sambuc 
TEST_F(MetadataAsValueTest,MDNode)429*0a6a1f1dSLionel Sambuc TEST_F(MetadataAsValueTest, MDNode) {
430*0a6a1f1dSLionel Sambuc   MDNode *N = MDNode::get(Context, None);
431*0a6a1f1dSLionel Sambuc   auto *V = MetadataAsValue::get(Context, N);
432*0a6a1f1dSLionel Sambuc   EXPECT_TRUE(V->getType()->isMetadataTy());
433*0a6a1f1dSLionel Sambuc   EXPECT_EQ(N, V->getMetadata());
434*0a6a1f1dSLionel Sambuc 
435*0a6a1f1dSLionel Sambuc   auto *V2 = MetadataAsValue::get(Context, N);
436*0a6a1f1dSLionel Sambuc   EXPECT_EQ(V, V2);
437*0a6a1f1dSLionel Sambuc }
438*0a6a1f1dSLionel Sambuc 
TEST_F(MetadataAsValueTest,MDNodeMDNode)439*0a6a1f1dSLionel Sambuc TEST_F(MetadataAsValueTest, MDNodeMDNode) {
440*0a6a1f1dSLionel Sambuc   MDNode *N = MDNode::get(Context, None);
441*0a6a1f1dSLionel Sambuc   Metadata *Ops[] = {N};
442*0a6a1f1dSLionel Sambuc   MDNode *N2 = MDNode::get(Context, Ops);
443*0a6a1f1dSLionel Sambuc   auto *V = MetadataAsValue::get(Context, N2);
444*0a6a1f1dSLionel Sambuc   EXPECT_TRUE(V->getType()->isMetadataTy());
445*0a6a1f1dSLionel Sambuc   EXPECT_EQ(N2, V->getMetadata());
446*0a6a1f1dSLionel Sambuc 
447*0a6a1f1dSLionel Sambuc   auto *V2 = MetadataAsValue::get(Context, N2);
448*0a6a1f1dSLionel Sambuc   EXPECT_EQ(V, V2);
449*0a6a1f1dSLionel Sambuc 
450*0a6a1f1dSLionel Sambuc   auto *V3 = MetadataAsValue::get(Context, N);
451*0a6a1f1dSLionel Sambuc   EXPECT_TRUE(V3->getType()->isMetadataTy());
452*0a6a1f1dSLionel Sambuc   EXPECT_NE(V, V3);
453*0a6a1f1dSLionel Sambuc   EXPECT_EQ(N, V3->getMetadata());
454*0a6a1f1dSLionel Sambuc }
455*0a6a1f1dSLionel Sambuc 
TEST_F(MetadataAsValueTest,MDNodeConstant)456*0a6a1f1dSLionel Sambuc TEST_F(MetadataAsValueTest, MDNodeConstant) {
457*0a6a1f1dSLionel Sambuc   auto *C = ConstantInt::getTrue(Context);
458*0a6a1f1dSLionel Sambuc   auto *MD = ConstantAsMetadata::get(C);
459*0a6a1f1dSLionel Sambuc   Metadata *Ops[] = {MD};
460*0a6a1f1dSLionel Sambuc   auto *N = MDNode::get(Context, Ops);
461*0a6a1f1dSLionel Sambuc 
462*0a6a1f1dSLionel Sambuc   auto *V = MetadataAsValue::get(Context, MD);
463*0a6a1f1dSLionel Sambuc   EXPECT_TRUE(V->getType()->isMetadataTy());
464*0a6a1f1dSLionel Sambuc   EXPECT_EQ(MD, V->getMetadata());
465*0a6a1f1dSLionel Sambuc 
466*0a6a1f1dSLionel Sambuc   auto *V2 = MetadataAsValue::get(Context, N);
467*0a6a1f1dSLionel Sambuc   EXPECT_EQ(MD, V2->getMetadata());
468*0a6a1f1dSLionel Sambuc   EXPECT_EQ(V, V2);
469*0a6a1f1dSLionel Sambuc }
470*0a6a1f1dSLionel Sambuc 
471*0a6a1f1dSLionel Sambuc typedef MetadataTest ValueAsMetadataTest;
472*0a6a1f1dSLionel Sambuc 
TEST_F(ValueAsMetadataTest,UpdatesOnRAUW)473*0a6a1f1dSLionel Sambuc TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
474*0a6a1f1dSLionel Sambuc   Type *Ty = Type::getInt1PtrTy(Context);
475*0a6a1f1dSLionel Sambuc   std::unique_ptr<GlobalVariable> GV0(
476*0a6a1f1dSLionel Sambuc       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
477*0a6a1f1dSLionel Sambuc   auto *MD = ValueAsMetadata::get(GV0.get());
478*0a6a1f1dSLionel Sambuc   EXPECT_TRUE(MD->getValue() == GV0.get());
479*0a6a1f1dSLionel Sambuc   ASSERT_TRUE(GV0->use_empty());
480*0a6a1f1dSLionel Sambuc 
481*0a6a1f1dSLionel Sambuc   std::unique_ptr<GlobalVariable> GV1(
482*0a6a1f1dSLionel Sambuc       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
483*0a6a1f1dSLionel Sambuc   GV0->replaceAllUsesWith(GV1.get());
484*0a6a1f1dSLionel Sambuc   EXPECT_TRUE(MD->getValue() == GV1.get());
485*0a6a1f1dSLionel Sambuc }
486*0a6a1f1dSLionel Sambuc 
TEST_F(ValueAsMetadataTest,CollidingDoubleUpdates)487*0a6a1f1dSLionel Sambuc TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
488*0a6a1f1dSLionel Sambuc   // Create a constant.
489*0a6a1f1dSLionel Sambuc   ConstantAsMetadata *CI = ConstantAsMetadata::get(
490*0a6a1f1dSLionel Sambuc       ConstantInt::get(getGlobalContext(), APInt(8, 0)));
491*0a6a1f1dSLionel Sambuc 
492*0a6a1f1dSLionel Sambuc   // Create a temporary to prevent nodes from resolving.
493*0a6a1f1dSLionel Sambuc   std::unique_ptr<MDNodeFwdDecl> Temp(MDNode::getTemporary(Context, None));
494*0a6a1f1dSLionel Sambuc 
495*0a6a1f1dSLionel Sambuc   // When the first operand of N1 gets reset to nullptr, it'll collide with N2.
496*0a6a1f1dSLionel Sambuc   Metadata *Ops1[] = {CI, CI, Temp.get()};
497*0a6a1f1dSLionel Sambuc   Metadata *Ops2[] = {nullptr, CI, Temp.get()};
498*0a6a1f1dSLionel Sambuc 
499*0a6a1f1dSLionel Sambuc   auto *N1 = MDTuple::get(Context, Ops1);
500*0a6a1f1dSLionel Sambuc   auto *N2 = MDTuple::get(Context, Ops2);
501*0a6a1f1dSLionel Sambuc   ASSERT_NE(N1, N2);
502*0a6a1f1dSLionel Sambuc 
503*0a6a1f1dSLionel Sambuc   // Tell metadata that the constant is getting deleted.
504*0a6a1f1dSLionel Sambuc   //
505*0a6a1f1dSLionel Sambuc   // After this, N1 will be invalid, so don't touch it.
506*0a6a1f1dSLionel Sambuc   ValueAsMetadata::handleDeletion(CI->getValue());
507*0a6a1f1dSLionel Sambuc   EXPECT_EQ(nullptr, N2->getOperand(0));
508*0a6a1f1dSLionel Sambuc   EXPECT_EQ(nullptr, N2->getOperand(1));
509*0a6a1f1dSLionel Sambuc   EXPECT_EQ(Temp.get(), N2->getOperand(2));
510*0a6a1f1dSLionel Sambuc 
511*0a6a1f1dSLionel Sambuc   // Clean up Temp for teardown.
512*0a6a1f1dSLionel Sambuc   Temp->replaceAllUsesWith(nullptr);
513*0a6a1f1dSLionel Sambuc }
514*0a6a1f1dSLionel Sambuc 
515*0a6a1f1dSLionel Sambuc typedef MetadataTest TrackingMDRefTest;
516*0a6a1f1dSLionel Sambuc 
TEST_F(TrackingMDRefTest,UpdatesOnRAUW)517*0a6a1f1dSLionel Sambuc TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
518*0a6a1f1dSLionel Sambuc   Type *Ty = Type::getInt1PtrTy(Context);
519*0a6a1f1dSLionel Sambuc   std::unique_ptr<GlobalVariable> GV0(
520*0a6a1f1dSLionel Sambuc       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
521*0a6a1f1dSLionel Sambuc   TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
522*0a6a1f1dSLionel Sambuc   EXPECT_TRUE(MD->getValue() == GV0.get());
523*0a6a1f1dSLionel Sambuc   ASSERT_TRUE(GV0->use_empty());
524*0a6a1f1dSLionel Sambuc 
525*0a6a1f1dSLionel Sambuc   std::unique_ptr<GlobalVariable> GV1(
526*0a6a1f1dSLionel Sambuc       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
527*0a6a1f1dSLionel Sambuc   GV0->replaceAllUsesWith(GV1.get());
528*0a6a1f1dSLionel Sambuc   EXPECT_TRUE(MD->getValue() == GV1.get());
529*0a6a1f1dSLionel Sambuc 
530*0a6a1f1dSLionel Sambuc   // Reset it, so we don't inadvertently test deletion.
531*0a6a1f1dSLionel Sambuc   MD.reset();
532*0a6a1f1dSLionel Sambuc }
533*0a6a1f1dSLionel Sambuc 
TEST_F(TrackingMDRefTest,UpdatesOnDeletion)534*0a6a1f1dSLionel Sambuc TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
535*0a6a1f1dSLionel Sambuc   Type *Ty = Type::getInt1PtrTy(Context);
536*0a6a1f1dSLionel Sambuc   std::unique_ptr<GlobalVariable> GV(
537*0a6a1f1dSLionel Sambuc       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
538*0a6a1f1dSLionel Sambuc   TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
539*0a6a1f1dSLionel Sambuc   EXPECT_TRUE(MD->getValue() == GV.get());
540*0a6a1f1dSLionel Sambuc   ASSERT_TRUE(GV->use_empty());
541*0a6a1f1dSLionel Sambuc 
542*0a6a1f1dSLionel Sambuc   GV.reset();
543*0a6a1f1dSLionel Sambuc   EXPECT_TRUE(!MD);
544*0a6a1f1dSLionel Sambuc }
545*0a6a1f1dSLionel Sambuc 
TEST(NamedMDNodeTest,Search)546f4a2713aSLionel Sambuc TEST(NamedMDNodeTest, Search) {
547f4a2713aSLionel Sambuc   LLVMContext Context;
548*0a6a1f1dSLionel Sambuc   ConstantAsMetadata *C =
549*0a6a1f1dSLionel Sambuc       ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
550*0a6a1f1dSLionel Sambuc   ConstantAsMetadata *C2 =
551*0a6a1f1dSLionel Sambuc       ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
552f4a2713aSLionel Sambuc 
553*0a6a1f1dSLionel Sambuc   Metadata *const V = C;
554*0a6a1f1dSLionel Sambuc   Metadata *const V2 = C2;
555f4a2713aSLionel Sambuc   MDNode *n = MDNode::get(Context, V);
556f4a2713aSLionel Sambuc   MDNode *n2 = MDNode::get(Context, V2);
557f4a2713aSLionel Sambuc 
558f4a2713aSLionel Sambuc   Module M("MyModule", Context);
559f4a2713aSLionel Sambuc   const char *Name = "llvm.NMD1";
560f4a2713aSLionel Sambuc   NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
561f4a2713aSLionel Sambuc   NMD->addOperand(n);
562f4a2713aSLionel Sambuc   NMD->addOperand(n2);
563f4a2713aSLionel Sambuc 
564f4a2713aSLionel Sambuc   std::string Str;
565f4a2713aSLionel Sambuc   raw_string_ostream oss(Str);
566f4a2713aSLionel Sambuc   NMD->print(oss);
567f4a2713aSLionel Sambuc   EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
568f4a2713aSLionel Sambuc                oss.str().c_str());
569f4a2713aSLionel Sambuc }
570f4a2713aSLionel Sambuc }
571