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