1 //===- unittests/IR/MetadataTest.cpp - Metadata unit tests ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
24 TEST(ContextAndReplaceableUsesTest, FromContext) {
26 ContextAndReplaceableUses CRU(Context);
27 EXPECT_EQ(&Context, &CRU.getContext());
28 EXPECT_FALSE(CRU.hasReplaceableUses());
29 EXPECT_FALSE(CRU.getReplaceableUses());
32 TEST(ContextAndReplaceableUsesTest, FromReplaceableUses) {
34 ContextAndReplaceableUses CRU(make_unique<ReplaceableMetadataImpl>(Context));
35 EXPECT_EQ(&Context, &CRU.getContext());
36 EXPECT_TRUE(CRU.hasReplaceableUses());
37 EXPECT_TRUE(CRU.getReplaceableUses());
40 TEST(ContextAndReplaceableUsesTest, makeReplaceable) {
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());
49 TEST(ContextAndReplaceableUsesTest, takeReplaceableUses) {
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());
61 class MetadataTest : public testing::Test {
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);
71 typedef MetadataTest MDStringTest;
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));
79 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
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' };
89 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
90 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
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);
103 raw_string_ostream oss(Str);
105 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
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));
113 raw_string_ostream oss(Str);
115 EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
118 typedef MetadataTest MDNodeTest;
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' };
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)));
130 std::vector<Metadata *> V;
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);
147 EXPECT_EQ(n6, (Metadata *)nullptr);
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));
154 EXPECT_EQ(1u, n2->getNumOperands());
155 EXPECT_EQ(n1, n2->getOperand(0));
158 TEST_F(MDNodeTest, Delete) {
159 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
160 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
162 Metadata *const V = LocalAsMetadata::get(I);
163 MDNode *n = MDNode::get(Context, V);
164 TrackingMDRef wvh(n);
171 TEST_F(MDNodeTest, SelfReference) {
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));
181 // Self-references should be distinct, so MDNode::get() should grab a
182 // uniqued node that references Self, not 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);
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));
199 // Self-references should be distinct, so MDNode::get() should grab a
200 // uniqued node that references Self, not Self itself.
202 MDNode *Ref1 = MDNode::get(Context, Args);
203 MDNode *Ref2 = MDNode::get(Context, Args);
204 EXPECT_NE(Self, Ref1);
205 EXPECT_EQ(Ref1, Ref2);
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);
216 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
217 MDNode *N = MDNode::get(Context, Args);
219 std::string Expected;
221 raw_string_ostream OS(Expected);
223 C->printAsOperand(OS);
225 S->printAsOperand(OS);
227 MDNode *Nodes[] = {N0, N1, N2};
228 for (auto *Node : Nodes)
229 OS << ", <" << (void *)Node << ">";
235 raw_string_ostream OS(Actual);
239 EXPECT_EQ(Expected, Actual);
242 TEST_F(MDNodeTest, NullOperand) {
244 MDNode *Empty = MDNode::get(Context, None);
246 // metadata !{metadata !{}}
247 Metadata *Ops[] = {Empty};
248 MDNode *N = MDNode::get(Context, Ops);
249 ASSERT_EQ(Empty, N->getOperand(0));
251 // metadata !{metadata !{}} => metadata !{null}
252 N->replaceOperandWith(0, nullptr);
253 ASSERT_EQ(nullptr, N->getOperand(0));
257 MDNode *NullOp = MDNode::get(Context, Ops);
258 ASSERT_EQ(nullptr, NullOp->getOperand(0));
259 EXPECT_EQ(N, NullOp);
262 TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
264 MDNode *Empty = MDNode::get(Context, None);
265 ASSERT_TRUE(Empty->isResolved());
266 EXPECT_FALSE(Empty->isDistinct());
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());
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());
282 // !{!{!{}}} => !{!{}}
283 Wrapped2->replaceOperandWith(0, Empty);
284 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
285 EXPECT_TRUE(Wrapped2->isDistinct());
286 EXPECT_FALSE(Wrapped1->isDistinct());
289 TEST_F(MDNodeTest, getDistinct) {
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));
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);
306 ASSERT_EQ(Empty, MDNode::get(Context, None));
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());
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());
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());
336 TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
338 auto Temp = MDTuple::getTemporary(Context, None);
339 ASSERT_FALSE(Temp->isResolved());
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));
347 // temporary !{} => !{}
348 MDNode *Empty = MDNode::get(Context, None);
349 Temp->replaceAllUsesWith(Empty);
350 EXPECT_EQ(Empty, Distinct->getOperand(0));
353 TEST_F(MDNodeTest, handleChangedOperandRecursion) {
355 MDNode *N0 = MDNode::get(Context, None);
358 auto Temp3 = MDTuple::getTemporary(Context, None);
359 Metadata *Ops1[] = {Temp3.get(), nullptr};
360 MDNode *N1 = MDNode::get(Context, Ops1);
363 Metadata *Ops2[] = {Temp3.get(), N0};
364 MDNode *N2 = MDNode::get(Context, Ops2);
367 Metadata *Ops3[] = {N2};
368 MDNode *N3 = MDNode::get(Context, Ops3);
369 Temp3->replaceAllUsesWith(N3);
372 Metadata *Ops4[] = {N1};
373 MDNode *N4 = MDNode::get(Context, Ops4);
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());
382 // Create a couple of distinct nodes to observe what's going on.
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);
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.
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);
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.
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));
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.
421 // Use a temporary node to keep N from being resolved.
422 auto Temp = MDTuple::getTemporary(Context, None);
423 Metadata *Ops[] = {nullptr, Temp.get()};
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());
430 // Check code for replacing resolved nodes.
431 N->replaceOperandWith(0, Empty);
432 EXPECT_EQ(Empty, N->getOperand(0));
434 // Check code for adding another unresolved operand.
435 N->replaceOperandWith(0, Temp.get());
436 EXPECT_EQ(Temp.get(), N->getOperand(0));
438 // Remove the references to Temp; required for teardown.
439 Temp->replaceAllUsesWith(nullptr);
442 TEST_F(MDNodeTest, replaceWithUniqued) {
443 auto *Empty = MDTuple::get(Context, None);
444 MDTuple *FirstUniqued;
446 Metadata *Ops[] = {Empty};
447 auto Temp = MDTuple::getTemporary(Context, Ops);
448 EXPECT_TRUE(Temp->isTemporary());
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);
458 Metadata *Ops[] = {Empty};
459 auto Temp = MDTuple::getTemporary(Context, Ops);
460 EXPECT_TRUE(Temp->isTemporary());
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);
469 auto Unresolved = MDTuple::getTemporary(Context, None);
470 Metadata *Ops[] = {Unresolved.get()};
471 auto Temp = MDTuple::getTemporary(Context, Ops);
472 EXPECT_TRUE(Temp->isTemporary());
474 // Shouldn't be resolved.
475 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
476 EXPECT_TRUE(Uniqued->isUniqued());
477 EXPECT_FALSE(Uniqued->isResolved());
479 // Should be a different node.
480 EXPECT_NE(FirstUniqued, Uniqued);
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());
489 TEST_F(MDNodeTest, replaceWithDistinct) {
491 auto *Empty = MDTuple::get(Context, None);
492 Metadata *Ops[] = {Empty};
493 auto Temp = MDTuple::getTemporary(Context, Ops);
494 EXPECT_TRUE(Temp->isTemporary());
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);
504 auto Unresolved = MDTuple::getTemporary(Context, None);
505 Metadata *Ops[] = {Unresolved.get()};
506 auto Temp = MDTuple::getTemporary(Context, Ops);
507 EXPECT_TRUE(Temp->isTemporary());
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);
516 // Cleanup; required for teardown.
517 Unresolved->replaceAllUsesWith(nullptr);
521 TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) {
523 EXPECT_EQ(nullptr, Ref.get());
525 auto Temp = MDTuple::getTemporary(Context, None);
526 Ref.reset(Temp.get());
527 EXPECT_EQ(Temp.get(), Ref.get());
529 EXPECT_EQ(nullptr, Ref.get());
532 typedef MetadataTest MDLocationTest;
534 TEST_F(MDLocationTest, Overflow) {
535 MDNode *N = MDNode::get(Context, None);
537 MDLocation *L = MDLocation::get(Context, 2, 7, N);
538 EXPECT_EQ(2u, L->getLine());
539 EXPECT_EQ(7u, L->getColumn());
541 unsigned U16 = 1u << 16;
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());
548 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16, N);
549 EXPECT_EQ(UINT32_MAX, L->getLine());
550 EXPECT_EQ(0u, L->getColumn());
553 MDLocation *L = MDLocation::get(Context, UINT32_MAX, U16 + 1, N);
554 EXPECT_EQ(UINT32_MAX, L->getLine());
555 EXPECT_EQ(0u, L->getColumn());
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));
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());
575 typedef MetadataTest GenericDebugNodeTest;
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());
591 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
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());
599 Metadata *Ops2[] = {nullptr};
600 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops2));
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));
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));
617 typedef MetadataTest MetadataAsValueTest;
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());
625 auto *V2 = MetadataAsValue::get(Context, N);
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());
637 auto *V2 = MetadataAsValue::get(Context, N2);
640 auto *V3 = MetadataAsValue::get(Context, N);
641 EXPECT_TRUE(V3->getType()->isMetadataTy());
643 EXPECT_EQ(N, V3->getMetadata());
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);
652 auto *V = MetadataAsValue::get(Context, MD);
653 EXPECT_TRUE(V->getType()->isMetadataTy());
654 EXPECT_EQ(MD, V->getMetadata());
656 auto *V2 = MetadataAsValue::get(Context, N);
657 EXPECT_EQ(MD, V2->getMetadata());
661 typedef MetadataTest ValueAsMetadataTest;
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());
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());
677 TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
678 // Create a constant.
679 ConstantAsMetadata *CI = ConstantAsMetadata::get(
680 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
682 // Create a temporary to prevent nodes from resolving.
683 auto Temp = MDTuple::getTemporary(Context, None);
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()};
689 auto *N1 = MDTuple::get(Context, Ops1);
690 auto *N2 = MDTuple::get(Context, Ops2);
693 // Tell metadata that the constant is getting deleted.
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));
701 // Clean up Temp for teardown.
702 Temp->replaceAllUsesWith(nullptr);
705 typedef MetadataTest TrackingMDRefTest;
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());
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());
720 // Reset it, so we don't inadvertently test deletion.
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());
736 TEST(NamedMDNodeTest, Search) {
738 ConstantAsMetadata *C =
739 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
740 ConstantAsMetadata *C2 =
741 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
743 Metadata *const V = C;
744 Metadata *const V2 = C2;
745 MDNode *n = MDNode::get(Context, V);
746 MDNode *n2 = MDNode::get(Context, V2);
748 Module M("MyModule", Context);
749 const char *Name = "llvm.NMD1";
750 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
755 raw_string_ostream oss(Str);
757 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",