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