IR: Allow 32-bits for lines in debug location
[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 U16 = 1u << 16;
542   {
543     MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 - 1, N);
544     EXPECT_EQ(UINT32_MAX, L->getLine());
545     EXPECT_EQ(U16 - 1, L->getColumn());
546   }
547   {
548     MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16, N);
549     EXPECT_EQ(UINT32_MAX, L->getLine());
550     EXPECT_EQ(0u, L->getColumn());
551   }
552   {
553     MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 + 1, N);
554     EXPECT_EQ(UINT32_MAX, L->getLine());
555     EXPECT_EQ(0u, L->getColumn());
556   }
557 }
558
559 TEST_F(MDLocationTest, getDistinct) {
560   MDNode *N = MDNode::get(Context, None);
561   MDLocation *L0 = MDLocation::getDistinct(Context, 2, 7, N);
562   EXPECT_TRUE(L0->isDistinct());
563   MDLocation *L1 = MDLocation::get(Context, 2, 7, N);
564   EXPECT_FALSE(L1->isDistinct());
565   EXPECT_EQ(L1, MDLocation::get(Context, 2, 7, N));
566 }
567
568 TEST_F(MDLocationTest, getTemporary) {
569   MDNode *N = MDNode::get(Context, None);
570   auto L = MDLocation::getTemporary(Context, 2, 7, N);
571   EXPECT_TRUE(L->isTemporary());
572   EXPECT_FALSE(L->isResolved());
573 }
574
575 typedef MetadataTest GenericDebugNodeTest;
576
577 TEST_F(GenericDebugNodeTest, get) {
578   StringRef Header = "header";
579   auto *Empty = MDNode::get(Context, None);
580   Metadata *Ops1[] = {Empty};
581   auto *N = GenericDebugNode::get(Context, 15, Header, Ops1);
582   EXPECT_EQ(15u, N->getTag());
583   EXPECT_EQ(2u, N->getNumOperands());
584   EXPECT_EQ(Header, N->getHeader());
585   EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0));
586   EXPECT_EQ(1u, N->getNumDwarfOperands());
587   EXPECT_EQ(Empty, N->getDwarfOperand(0));
588   EXPECT_EQ(Empty, N->getOperand(1));
589   ASSERT_TRUE(N->isUniqued());
590
591   EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
592
593   N->replaceOperandWith(1, nullptr);
594   EXPECT_EQ(15u, N->getTag());
595   EXPECT_EQ(Header, N->getHeader());
596   EXPECT_EQ(nullptr, N->getDwarfOperand(0));
597   ASSERT_TRUE(N->isUniqued());
598
599   Metadata *Ops2[] = {nullptr};
600   EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops2));
601
602   N->replaceDwarfOperandWith(0, Empty);
603   EXPECT_EQ(15u, N->getTag());
604   EXPECT_EQ(Header, N->getHeader());
605   EXPECT_EQ(Empty, N->getDwarfOperand(0));
606   ASSERT_TRUE(N->isUniqued());
607   EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
608 }
609
610 TEST_F(GenericDebugNodeTest, getEmptyHeader) {
611   // Canonicalize !"" to null.
612   auto *N = GenericDebugNode::get(Context, 15, StringRef(), None);
613   EXPECT_EQ(StringRef(), N->getHeader());
614   EXPECT_EQ(nullptr, N->getOperand(0));
615 }
616
617 typedef MetadataTest MetadataAsValueTest;
618
619 TEST_F(MetadataAsValueTest, MDNode) {
620   MDNode *N = MDNode::get(Context, None);
621   auto *V = MetadataAsValue::get(Context, N);
622   EXPECT_TRUE(V->getType()->isMetadataTy());
623   EXPECT_EQ(N, V->getMetadata());
624
625   auto *V2 = MetadataAsValue::get(Context, N);
626   EXPECT_EQ(V, V2);
627 }
628
629 TEST_F(MetadataAsValueTest, MDNodeMDNode) {
630   MDNode *N = MDNode::get(Context, None);
631   Metadata *Ops[] = {N};
632   MDNode *N2 = MDNode::get(Context, Ops);
633   auto *V = MetadataAsValue::get(Context, N2);
634   EXPECT_TRUE(V->getType()->isMetadataTy());
635   EXPECT_EQ(N2, V->getMetadata());
636
637   auto *V2 = MetadataAsValue::get(Context, N2);
638   EXPECT_EQ(V, V2);
639
640   auto *V3 = MetadataAsValue::get(Context, N);
641   EXPECT_TRUE(V3->getType()->isMetadataTy());
642   EXPECT_NE(V, V3);
643   EXPECT_EQ(N, V3->getMetadata());
644 }
645
646 TEST_F(MetadataAsValueTest, MDNodeConstant) {
647   auto *C = ConstantInt::getTrue(Context);
648   auto *MD = ConstantAsMetadata::get(C);
649   Metadata *Ops[] = {MD};
650   auto *N = MDNode::get(Context, Ops);
651
652   auto *V = MetadataAsValue::get(Context, MD);
653   EXPECT_TRUE(V->getType()->isMetadataTy());
654   EXPECT_EQ(MD, V->getMetadata());
655
656   auto *V2 = MetadataAsValue::get(Context, N);
657   EXPECT_EQ(MD, V2->getMetadata());
658   EXPECT_EQ(V, V2);
659 }
660
661 typedef MetadataTest ValueAsMetadataTest;
662
663 TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
664   Type *Ty = Type::getInt1PtrTy(Context);
665   std::unique_ptr<GlobalVariable> GV0(
666       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
667   auto *MD = ValueAsMetadata::get(GV0.get());
668   EXPECT_TRUE(MD->getValue() == GV0.get());
669   ASSERT_TRUE(GV0->use_empty());
670
671   std::unique_ptr<GlobalVariable> GV1(
672       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
673   GV0->replaceAllUsesWith(GV1.get());
674   EXPECT_TRUE(MD->getValue() == GV1.get());
675 }
676
677 TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
678   // Create a constant.
679   ConstantAsMetadata *CI = ConstantAsMetadata::get(
680       ConstantInt::get(getGlobalContext(), APInt(8, 0)));
681
682   // Create a temporary to prevent nodes from resolving.
683   auto Temp = MDTuple::getTemporary(Context, None);
684
685   // When the first operand of N1 gets reset to nullptr, it'll collide with N2.
686   Metadata *Ops1[] = {CI, CI, Temp.get()};
687   Metadata *Ops2[] = {nullptr, CI, Temp.get()};
688
689   auto *N1 = MDTuple::get(Context, Ops1);
690   auto *N2 = MDTuple::get(Context, Ops2);
691   ASSERT_NE(N1, N2);
692
693   // Tell metadata that the constant is getting deleted.
694   //
695   // After this, N1 will be invalid, so don't touch it.
696   ValueAsMetadata::handleDeletion(CI->getValue());
697   EXPECT_EQ(nullptr, N2->getOperand(0));
698   EXPECT_EQ(nullptr, N2->getOperand(1));
699   EXPECT_EQ(Temp.get(), N2->getOperand(2));
700
701   // Clean up Temp for teardown.
702   Temp->replaceAllUsesWith(nullptr);
703 }
704
705 typedef MetadataTest TrackingMDRefTest;
706
707 TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
708   Type *Ty = Type::getInt1PtrTy(Context);
709   std::unique_ptr<GlobalVariable> GV0(
710       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
711   TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
712   EXPECT_TRUE(MD->getValue() == GV0.get());
713   ASSERT_TRUE(GV0->use_empty());
714
715   std::unique_ptr<GlobalVariable> GV1(
716       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
717   GV0->replaceAllUsesWith(GV1.get());
718   EXPECT_TRUE(MD->getValue() == GV1.get());
719
720   // Reset it, so we don't inadvertently test deletion.
721   MD.reset();
722 }
723
724 TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
725   Type *Ty = Type::getInt1PtrTy(Context);
726   std::unique_ptr<GlobalVariable> GV(
727       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
728   TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
729   EXPECT_TRUE(MD->getValue() == GV.get());
730   ASSERT_TRUE(GV->use_empty());
731
732   GV.reset();
733   EXPECT_TRUE(!MD);
734 }
735
736 TEST(NamedMDNodeTest, Search) {
737   LLVMContext Context;
738   ConstantAsMetadata *C =
739       ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
740   ConstantAsMetadata *C2 =
741       ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
742
743   Metadata *const V = C;
744   Metadata *const V2 = C2;
745   MDNode *n = MDNode::get(Context, V);
746   MDNode *n2 = MDNode::get(Context, V2);
747
748   Module M("MyModule", Context);
749   const char *Name = "llvm.NMD1";
750   NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
751   NMD->addOperand(n);
752   NMD->addOperand(n2);
753
754   std::string Str;
755   raw_string_ostream oss(Str);
756   NMD->print(oss);
757   EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
758                oss.str().c_str());
759 }
760 }