1 //===- llvm/unittest/IR/Metadata.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/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"
23 TEST(ContextAndReplaceableUsesTest, FromContext) {
25 ContextAndReplaceableUses CRU(Context);
26 EXPECT_EQ(&Context, &CRU.getContext());
27 EXPECT_FALSE(CRU.hasReplaceableUses());
28 EXPECT_FALSE(CRU.getReplaceableUses());
31 TEST(ContextAndReplaceableUsesTest, FromReplaceableUses) {
33 ContextAndReplaceableUses CRU(make_unique<ReplaceableMetadataImpl>(Context));
34 EXPECT_EQ(&Context, &CRU.getContext());
35 EXPECT_TRUE(CRU.hasReplaceableUses());
36 EXPECT_TRUE(CRU.getReplaceableUses());
39 TEST(ContextAndReplaceableUsesTest, makeReplaceable) {
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());
48 TEST(ContextAndReplaceableUsesTest, takeReplaceableUses) {
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());
60 class MetadataTest : public testing::Test {
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);
70 typedef MetadataTest MDStringTest;
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));
78 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
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' };
88 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
89 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
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);
102 raw_string_ostream oss(Str);
104 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
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));
112 raw_string_ostream oss(Str);
114 EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
117 typedef MetadataTest MDNodeTest;
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' };
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)));
129 std::vector<Metadata *> V;
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);
146 EXPECT_EQ(n6, (Metadata *)nullptr);
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));
153 EXPECT_EQ(1u, n2->getNumOperands());
154 EXPECT_EQ(n1, n2->getOperand(0));
157 TEST_F(MDNodeTest, Delete) {
158 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
159 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
161 Metadata *const V = LocalAsMetadata::get(I);
162 MDNode *n = MDNode::get(Context, V);
163 TrackingMDRef wvh(n);
170 TEST_F(MDNodeTest, SelfReference) {
174 auto Temp = MDNode::getTemporary(Context, None);
175 Metadata *Args[] = {Temp.get()};
176 MDNode *Self = MDNode::get(Context, Args);
177 Self->replaceOperandWith(0, Self);
178 ASSERT_EQ(Self, Self->getOperand(0));
180 // Self-references should be distinct, so MDNode::get() should grab a
181 // uniqued node that references Self, not Self.
183 MDNode *Ref1 = MDNode::get(Context, Args);
184 MDNode *Ref2 = MDNode::get(Context, Args);
185 EXPECT_NE(Self, Ref1);
186 EXPECT_EQ(Ref1, Ref2);
192 auto Temp = MDNode::getTemporary(Context, None);
193 Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)};
194 MDNode *Self = MDNode::get(Context, Args);
195 Self->replaceOperandWith(0, Self);
196 ASSERT_EQ(Self, Self->getOperand(0));
198 // Self-references should be distinct, so MDNode::get() should grab a
199 // uniqued node that references Self, not Self itself.
201 MDNode *Ref1 = MDNode::get(Context, Args);
202 MDNode *Ref2 = MDNode::get(Context, Args);
203 EXPECT_NE(Self, Ref1);
204 EXPECT_EQ(Ref1, Ref2);
208 TEST_F(MDNodeTest, Print) {
209 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
210 MDString *S = MDString::get(Context, "foo");
211 MDNode *N0 = getNode();
212 MDNode *N1 = getNode(N0);
213 MDNode *N2 = getNode(N0, N1);
215 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
216 MDNode *N = MDNode::get(Context, Args);
218 std::string Expected;
220 raw_string_ostream OS(Expected);
222 C->printAsOperand(OS);
224 S->printAsOperand(OS);
226 MDNode *Nodes[] = {N0, N1, N2};
227 for (auto *Node : Nodes)
228 OS << ", <" << (void *)Node << ">";
234 raw_string_ostream OS(Actual);
238 EXPECT_EQ(Expected, Actual);
241 TEST_F(MDNodeTest, NullOperand) {
243 MDNode *Empty = MDNode::get(Context, None);
245 // metadata !{metadata !{}}
246 Metadata *Ops[] = {Empty};
247 MDNode *N = MDNode::get(Context, Ops);
248 ASSERT_EQ(Empty, N->getOperand(0));
250 // metadata !{metadata !{}} => metadata !{null}
251 N->replaceOperandWith(0, nullptr);
252 ASSERT_EQ(nullptr, N->getOperand(0));
256 MDNode *NullOp = MDNode::get(Context, Ops);
257 ASSERT_EQ(nullptr, NullOp->getOperand(0));
258 EXPECT_EQ(N, NullOp);
261 TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
263 MDNode *Empty = MDNode::get(Context, None);
264 ASSERT_TRUE(Empty->isResolved());
265 EXPECT_FALSE(Empty->isDistinct());
268 Metadata *Wrapped1Ops[] = {Empty};
269 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
270 ASSERT_EQ(Empty, Wrapped1->getOperand(0));
271 ASSERT_TRUE(Wrapped1->isResolved());
272 EXPECT_FALSE(Wrapped1->isDistinct());
275 Metadata *Wrapped2Ops[] = {Wrapped1};
276 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
277 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
278 ASSERT_TRUE(Wrapped2->isResolved());
279 EXPECT_FALSE(Wrapped2->isDistinct());
281 // !{!{!{}}} => !{!{}}
282 Wrapped2->replaceOperandWith(0, Empty);
283 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
284 EXPECT_TRUE(Wrapped2->isDistinct());
285 EXPECT_FALSE(Wrapped1->isDistinct());
288 TEST_F(MDNodeTest, getDistinct) {
290 MDNode *Empty = MDNode::get(Context, None);
291 ASSERT_TRUE(Empty->isResolved());
292 ASSERT_FALSE(Empty->isDistinct());
293 ASSERT_EQ(Empty, MDNode::get(Context, None));
296 MDNode *Distinct1 = MDNode::getDistinct(Context, None);
297 MDNode *Distinct2 = MDNode::getDistinct(Context, None);
298 EXPECT_TRUE(Distinct1->isResolved());
299 EXPECT_TRUE(Distinct2->isDistinct());
300 EXPECT_NE(Empty, Distinct1);
301 EXPECT_NE(Empty, Distinct2);
302 EXPECT_NE(Distinct1, Distinct2);
305 ASSERT_EQ(Empty, MDNode::get(Context, None));
308 TEST_F(MDNodeTest, isUniqued) {
309 MDNode *U = MDTuple::get(Context, None);
310 MDNode *D = MDTuple::getDistinct(Context, None);
311 auto T = MDTuple::getTemporary(Context, None);
312 EXPECT_TRUE(U->isUniqued());
313 EXPECT_FALSE(D->isUniqued());
314 EXPECT_FALSE(T->isUniqued());
317 TEST_F(MDNodeTest, isDistinct) {
318 MDNode *U = MDTuple::get(Context, None);
319 MDNode *D = MDTuple::getDistinct(Context, None);
320 auto T = MDTuple::getTemporary(Context, None);
321 EXPECT_FALSE(U->isDistinct());
322 EXPECT_TRUE(D->isDistinct());
323 EXPECT_FALSE(T->isDistinct());
326 TEST_F(MDNodeTest, isTemporary) {
327 MDNode *U = MDTuple::get(Context, None);
328 MDNode *D = MDTuple::getDistinct(Context, None);
329 auto T = MDTuple::getTemporary(Context, None);
330 EXPECT_FALSE(U->isTemporary());
331 EXPECT_FALSE(D->isTemporary());
332 EXPECT_TRUE(T->isTemporary());
335 TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
337 auto Temp = MDTuple::getTemporary(Context, None);
338 ASSERT_FALSE(Temp->isResolved());
340 // distinct !{temporary !{}}
341 Metadata *Ops[] = {Temp.get()};
342 MDNode *Distinct = MDNode::getDistinct(Context, Ops);
343 EXPECT_TRUE(Distinct->isResolved());
344 EXPECT_EQ(Temp.get(), Distinct->getOperand(0));
346 // temporary !{} => !{}
347 MDNode *Empty = MDNode::get(Context, None);
348 Temp->replaceAllUsesWith(Empty);
349 EXPECT_EQ(Empty, Distinct->getOperand(0));
352 TEST_F(MDNodeTest, handleChangedOperandRecursion) {
354 MDNode *N0 = MDNode::get(Context, None);
357 auto Temp3 = MDTuple::getTemporary(Context, None);
358 Metadata *Ops1[] = {Temp3.get(), nullptr};
359 MDNode *N1 = MDNode::get(Context, Ops1);
362 Metadata *Ops2[] = {Temp3.get(), N0};
363 MDNode *N2 = MDNode::get(Context, Ops2);
366 Metadata *Ops3[] = {N2};
367 MDNode *N3 = MDNode::get(Context, Ops3);
368 Temp3->replaceAllUsesWith(N3);
371 Metadata *Ops4[] = {N1};
372 MDNode *N4 = MDNode::get(Context, Ops4);
374 // Confirm that the cycle prevented RAUW from getting dropped.
375 EXPECT_TRUE(N0->isResolved());
376 EXPECT_FALSE(N1->isResolved());
377 EXPECT_FALSE(N2->isResolved());
378 EXPECT_FALSE(N3->isResolved());
379 EXPECT_FALSE(N4->isResolved());
381 // Create a couple of distinct nodes to observe what's going on.
383 // !5 = distinct !{!2}
384 // !6 = distinct !{!3}
385 Metadata *Ops5[] = {N2};
386 MDNode *N5 = MDNode::getDistinct(Context, Ops5);
387 Metadata *Ops6[] = {N3};
388 MDNode *N6 = MDNode::getDistinct(Context, Ops6);
390 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
391 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2
392 // references !3, this can cause a re-entry of handleChangedOperand() when !3
393 // is not ready for it.
395 // !2->replaceOperandWith(1, nullptr)
396 // !2: !{!3, !0} => !{!3, null}
397 // !2->replaceAllUsesWith(!1)
398 // !3: !{!2] => !{!1}
399 // !3->replaceAllUsesWith(!4)
400 N2->replaceOperandWith(1, nullptr);
402 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
403 // under us. Just check that the other nodes are sane.
407 // !5 = distinct !{!1}
408 // !6 = distinct !{!4}
409 EXPECT_EQ(N4, N1->getOperand(0));
410 EXPECT_EQ(N1, N4->getOperand(0));
411 EXPECT_EQ(N1, N5->getOperand(0));
412 EXPECT_EQ(N4, N6->getOperand(0));
415 TEST_F(MDNodeTest, replaceResolvedOperand) {
416 // Check code for replacing one resolved operand with another. If doing this
417 // directly (via replaceOperandWith()) becomes illegal, change the operand to
418 // a global value that gets RAUW'ed.
420 // Use a temporary node to keep N from being resolved.
421 auto Temp = MDTuple::getTemporary(Context, None);
422 Metadata *Ops[] = {nullptr, Temp.get()};
424 MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>());
425 MDNode *N = MDTuple::get(Context, Ops);
426 EXPECT_EQ(nullptr, N->getOperand(0));
427 ASSERT_FALSE(N->isResolved());
429 // Check code for replacing resolved nodes.
430 N->replaceOperandWith(0, Empty);
431 EXPECT_EQ(Empty, N->getOperand(0));
433 // Check code for adding another unresolved operand.
434 N->replaceOperandWith(0, Temp.get());
435 EXPECT_EQ(Temp.get(), N->getOperand(0));
437 // Remove the references to Temp; required for teardown.
438 Temp->replaceAllUsesWith(nullptr);
441 TEST_F(MDNodeTest, replaceWithUniqued) {
442 auto *Empty = MDTuple::get(Context, None);
443 MDTuple *FirstUniqued;
445 Metadata *Ops[] = {Empty};
446 auto Temp = MDTuple::getTemporary(Context, Ops);
447 EXPECT_TRUE(Temp->isTemporary());
449 // Don't expect a collision.
450 auto *Current = Temp.get();
451 FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp));
452 EXPECT_TRUE(FirstUniqued->isUniqued());
453 EXPECT_TRUE(FirstUniqued->isResolved());
454 EXPECT_EQ(Current, FirstUniqued);
457 Metadata *Ops[] = {Empty};
458 auto Temp = MDTuple::getTemporary(Context, Ops);
459 EXPECT_TRUE(Temp->isTemporary());
461 // Should collide with Uniqued above this time.
462 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
463 EXPECT_TRUE(Uniqued->isUniqued());
464 EXPECT_TRUE(Uniqued->isResolved());
465 EXPECT_EQ(FirstUniqued, Uniqued);
468 auto Unresolved = MDTuple::getTemporary(Context, None);
469 Metadata *Ops[] = {Unresolved.get()};
470 auto Temp = MDTuple::getTemporary(Context, Ops);
471 EXPECT_TRUE(Temp->isTemporary());
473 // Shouldn't be resolved.
474 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
475 EXPECT_TRUE(Uniqued->isUniqued());
476 EXPECT_FALSE(Uniqued->isResolved());
478 // Should be a different node.
479 EXPECT_NE(FirstUniqued, Uniqued);
481 // Should resolve when we update its node (note: be careful to avoid a
482 // collision with any other nodes above).
483 Uniqued->replaceOperandWith(0, nullptr);
484 EXPECT_TRUE(Uniqued->isResolved());
488 TEST_F(MDNodeTest, replaceWithDistinct) {
490 auto *Empty = MDTuple::get(Context, None);
491 Metadata *Ops[] = {Empty};
492 auto Temp = MDTuple::getTemporary(Context, Ops);
493 EXPECT_TRUE(Temp->isTemporary());
495 // Don't expect a collision.
496 auto *Current = Temp.get();
497 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
498 EXPECT_TRUE(Distinct->isDistinct());
499 EXPECT_TRUE(Distinct->isResolved());
500 EXPECT_EQ(Current, Distinct);
503 auto Unresolved = MDTuple::getTemporary(Context, None);
504 Metadata *Ops[] = {Unresolved.get()};
505 auto Temp = MDTuple::getTemporary(Context, Ops);
506 EXPECT_TRUE(Temp->isTemporary());
508 // Don't expect a collision.
509 auto *Current = Temp.get();
510 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
511 EXPECT_TRUE(Distinct->isDistinct());
512 EXPECT_TRUE(Distinct->isResolved());
513 EXPECT_EQ(Current, Distinct);
515 // Cleanup; required for teardown.
516 Unresolved->replaceAllUsesWith(nullptr);
520 typedef MetadataTest MDLocationTest;
522 TEST_F(MDLocationTest, Overflow) {
523 MDNode *N = MDNode::get(Context, None);
525 MDLocation *L = MDLocation::get(Context, 2, 7, N);
526 EXPECT_EQ(2u, L->getLine());
527 EXPECT_EQ(7u, L->getColumn());
529 unsigned U24 = 1u << 24;
530 unsigned U16 = 1u << 16;
532 MDLocation *L = MDLocation::get(Context, U24 - 1, U16 - 1, N);
533 EXPECT_EQ(U24 - 1, L->getLine());
534 EXPECT_EQ(U16 - 1, L->getColumn());
537 MDLocation *L = MDLocation::get(Context, U24, U16, N);
538 EXPECT_EQ(0u, L->getLine());
539 EXPECT_EQ(0u, L->getColumn());
542 MDLocation *L = MDLocation::get(Context, U24 + 1, U16 + 1, N);
543 EXPECT_EQ(0u, L->getLine());
544 EXPECT_EQ(0u, L->getColumn());
548 TEST_F(MDLocationTest, getDistinct) {
549 MDNode *N = MDNode::get(Context, None);
550 MDLocation *L0 = MDLocation::getDistinct(Context, 2, 7, N);
551 EXPECT_TRUE(L0->isDistinct());
552 MDLocation *L1 = MDLocation::get(Context, 2, 7, N);
553 EXPECT_FALSE(L1->isDistinct());
554 EXPECT_EQ(L1, MDLocation::get(Context, 2, 7, N));
557 TEST_F(MDLocationTest, getTemporary) {
558 MDNode *N = MDNode::get(Context, None);
559 auto L = MDLocation::getTemporary(Context, 2, 7, N);
560 EXPECT_TRUE(L->isTemporary());
561 EXPECT_FALSE(L->isResolved());
564 typedef MetadataTest MetadataAsValueTest;
566 TEST_F(MetadataAsValueTest, MDNode) {
567 MDNode *N = MDNode::get(Context, None);
568 auto *V = MetadataAsValue::get(Context, N);
569 EXPECT_TRUE(V->getType()->isMetadataTy());
570 EXPECT_EQ(N, V->getMetadata());
572 auto *V2 = MetadataAsValue::get(Context, N);
576 TEST_F(MetadataAsValueTest, MDNodeMDNode) {
577 MDNode *N = MDNode::get(Context, None);
578 Metadata *Ops[] = {N};
579 MDNode *N2 = MDNode::get(Context, Ops);
580 auto *V = MetadataAsValue::get(Context, N2);
581 EXPECT_TRUE(V->getType()->isMetadataTy());
582 EXPECT_EQ(N2, V->getMetadata());
584 auto *V2 = MetadataAsValue::get(Context, N2);
587 auto *V3 = MetadataAsValue::get(Context, N);
588 EXPECT_TRUE(V3->getType()->isMetadataTy());
590 EXPECT_EQ(N, V3->getMetadata());
593 TEST_F(MetadataAsValueTest, MDNodeConstant) {
594 auto *C = ConstantInt::getTrue(Context);
595 auto *MD = ConstantAsMetadata::get(C);
596 Metadata *Ops[] = {MD};
597 auto *N = MDNode::get(Context, Ops);
599 auto *V = MetadataAsValue::get(Context, MD);
600 EXPECT_TRUE(V->getType()->isMetadataTy());
601 EXPECT_EQ(MD, V->getMetadata());
603 auto *V2 = MetadataAsValue::get(Context, N);
604 EXPECT_EQ(MD, V2->getMetadata());
608 typedef MetadataTest ValueAsMetadataTest;
610 TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
611 Type *Ty = Type::getInt1PtrTy(Context);
612 std::unique_ptr<GlobalVariable> GV0(
613 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
614 auto *MD = ValueAsMetadata::get(GV0.get());
615 EXPECT_TRUE(MD->getValue() == GV0.get());
616 ASSERT_TRUE(GV0->use_empty());
618 std::unique_ptr<GlobalVariable> GV1(
619 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
620 GV0->replaceAllUsesWith(GV1.get());
621 EXPECT_TRUE(MD->getValue() == GV1.get());
624 TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
625 // Create a constant.
626 ConstantAsMetadata *CI = ConstantAsMetadata::get(
627 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
629 // Create a temporary to prevent nodes from resolving.
630 auto Temp = MDTuple::getTemporary(Context, None);
632 // When the first operand of N1 gets reset to nullptr, it'll collide with N2.
633 Metadata *Ops1[] = {CI, CI, Temp.get()};
634 Metadata *Ops2[] = {nullptr, CI, Temp.get()};
636 auto *N1 = MDTuple::get(Context, Ops1);
637 auto *N2 = MDTuple::get(Context, Ops2);
640 // Tell metadata that the constant is getting deleted.
642 // After this, N1 will be invalid, so don't touch it.
643 ValueAsMetadata::handleDeletion(CI->getValue());
644 EXPECT_EQ(nullptr, N2->getOperand(0));
645 EXPECT_EQ(nullptr, N2->getOperand(1));
646 EXPECT_EQ(Temp.get(), N2->getOperand(2));
648 // Clean up Temp for teardown.
649 Temp->replaceAllUsesWith(nullptr);
652 typedef MetadataTest TrackingMDRefTest;
654 TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
655 Type *Ty = Type::getInt1PtrTy(Context);
656 std::unique_ptr<GlobalVariable> GV0(
657 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
658 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
659 EXPECT_TRUE(MD->getValue() == GV0.get());
660 ASSERT_TRUE(GV0->use_empty());
662 std::unique_ptr<GlobalVariable> GV1(
663 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
664 GV0->replaceAllUsesWith(GV1.get());
665 EXPECT_TRUE(MD->getValue() == GV1.get());
667 // Reset it, so we don't inadvertently test deletion.
671 TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
672 Type *Ty = Type::getInt1PtrTy(Context);
673 std::unique_ptr<GlobalVariable> GV(
674 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
675 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
676 EXPECT_TRUE(MD->getValue() == GV.get());
677 ASSERT_TRUE(GV->use_empty());
683 TEST(NamedMDNodeTest, Search) {
685 ConstantAsMetadata *C =
686 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
687 ConstantAsMetadata *C2 =
688 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
690 Metadata *const V = C;
691 Metadata *const V2 = C2;
692 MDNode *n = MDNode::get(Context, V);
693 MDNode *n2 = MDNode::get(Context, V2);
695 Module M("MyModule", Context);
696 const char *Name = "llvm.NMD1";
697 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
702 raw_string_ostream oss(Str);
704 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",