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