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/Metadata.h"
12 #include "llvm/IR/Constants.h"
13 #include "llvm/IR/Instructions.h"
14 #include "llvm/IR/LLVMContext.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 class MetadataTest : public testing::Test {
26 MDNode *getNode() { return MDNode::get(Context, None); }
27 MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); }
28 MDNode *getNode(Metadata *MD1, Metadata *MD2) {
29 Metadata *MDs[] = {MD1, MD2};
30 return MDNode::get(Context, MDs);
33 typedef MetadataTest MDStringTest;
35 // Test that construction of MDString with different value produces different
36 // MDString objects, even with the same string pointer and nulls in the string.
37 TEST_F(MDStringTest, CreateDifferent) {
38 char x[3] = { 'f', 0, 'A' };
39 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
41 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
45 // Test that creation of MDStrings with the same string contents produces the
46 // same MDString object, even with different pointers.
47 TEST_F(MDStringTest, CreateSame) {
48 char x[4] = { 'a', 'b', 'c', 'X' };
49 char y[4] = { 'a', 'b', 'c', 'Y' };
51 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
52 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
56 // Test that MDString prints out the string we fed it.
57 TEST_F(MDStringTest, PrintingSimple) {
58 char *str = new char[13];
59 strncpy(str, "testing 1 2 3", 13);
60 MDString *s = MDString::get(Context, StringRef(str, 13));
61 strncpy(str, "aaaaaaaaaaaaa", 13);
65 raw_string_ostream oss(Str);
67 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
70 // Test printing of MDString with non-printable characters.
71 TEST_F(MDStringTest, PrintingComplex) {
72 char str[5] = {0, '\n', '"', '\\', (char)-1};
73 MDString *s = MDString::get(Context, StringRef(str+0, 5));
75 raw_string_ostream oss(Str);
77 EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
80 typedef MetadataTest MDNodeTest;
82 // Test the two constructors, and containing other Constants.
83 TEST_F(MDNodeTest, Simple) {
84 char x[3] = { 'a', 'b', 'c' };
85 char y[3] = { '1', '2', '3' };
87 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
88 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
89 ConstantAsMetadata *CI = ConstantAsMetadata::get(
90 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
92 std::vector<Metadata *> V;
97 MDNode *n1 = MDNode::get(Context, V);
98 Metadata *const c1 = n1;
99 MDNode *n2 = MDNode::get(Context, c1);
100 Metadata *const c2 = n2;
101 MDNode *n3 = MDNode::get(Context, V);
102 MDNode *n4 = MDNode::getIfExists(Context, V);
103 MDNode *n5 = MDNode::getIfExists(Context, c1);
104 MDNode *n6 = MDNode::getIfExists(Context, c2);
109 EXPECT_EQ(n6, (Metadata *)nullptr);
111 EXPECT_EQ(3u, n1->getNumOperands());
112 EXPECT_EQ(s1, n1->getOperand(0));
113 EXPECT_EQ(CI, n1->getOperand(1));
114 EXPECT_EQ(s2, n1->getOperand(2));
116 EXPECT_EQ(1u, n2->getNumOperands());
117 EXPECT_EQ(n1, n2->getOperand(0));
120 TEST_F(MDNodeTest, Delete) {
121 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
122 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
124 Metadata *const V = LocalAsMetadata::get(I);
125 MDNode *n = MDNode::get(Context, V);
126 TrackingMDRef wvh(n);
133 TEST_F(MDNodeTest, DeleteMDNodeFwdDecl) {
134 delete MDNode::getTemporary(Context, None);
137 TEST_F(MDNodeTest, SelfReference) {
141 MDNode *Temp = MDNode::getTemporary(Context, None);
142 Metadata *Args[] = {Temp};
143 MDNode *Self = MDNode::get(Context, Args);
144 Self->replaceOperandWith(0, Self);
145 MDNode::deleteTemporary(Temp);
146 ASSERT_EQ(Self, Self->getOperand(0));
148 // Self-references should be distinct, so MDNode::get() should grab a
149 // uniqued node that references Self, not Self.
151 MDNode *Ref1 = MDNode::get(Context, Args);
152 MDNode *Ref2 = MDNode::get(Context, Args);
153 EXPECT_NE(Self, Ref1);
154 EXPECT_EQ(Ref1, Ref2);
160 MDNode *Temp = MDNode::getTemporary(Context, None);
161 Metadata *Args[] = {Temp, MDNode::get(Context, None)};
162 MDNode *Self = MDNode::get(Context, Args);
163 Self->replaceOperandWith(0, Self);
164 MDNode::deleteTemporary(Temp);
165 ASSERT_EQ(Self, Self->getOperand(0));
167 // Self-references should be distinct, so MDNode::get() should grab a
168 // uniqued node that references Self, not Self itself.
170 MDNode *Ref1 = MDNode::get(Context, Args);
171 MDNode *Ref2 = MDNode::get(Context, Args);
172 EXPECT_NE(Self, Ref1);
173 EXPECT_EQ(Ref1, Ref2);
177 TEST_F(MDNodeTest, Print) {
178 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
179 MDString *S = MDString::get(Context, "foo");
180 MDNode *N0 = getNode();
181 MDNode *N1 = getNode(N0);
182 MDNode *N2 = getNode(N0, N1);
184 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
185 MDNode *N = MDNode::get(Context, Args);
187 std::string Expected;
189 raw_string_ostream OS(Expected);
191 C->printAsOperand(OS);
193 S->printAsOperand(OS);
195 MDNode *Nodes[] = {N0, N1, N2};
196 for (auto *Node : Nodes)
197 OS << ", <" << (void *)Node << ">";
203 raw_string_ostream OS(Actual);
207 EXPECT_EQ(Expected, Actual);
210 TEST_F(MDNodeTest, NullOperand) {
212 MDNode *Empty = MDNode::get(Context, None);
214 // metadata !{metadata !{}}
215 Metadata *Ops[] = {Empty};
216 MDNode *N = MDNode::get(Context, Ops);
217 ASSERT_EQ(Empty, N->getOperand(0));
219 // metadata !{metadata !{}} => metadata !{null}
220 N->replaceOperandWith(0, nullptr);
221 ASSERT_EQ(nullptr, N->getOperand(0));
225 MDNode *NullOp = MDNode::get(Context, Ops);
226 ASSERT_EQ(nullptr, NullOp->getOperand(0));
227 EXPECT_EQ(N, NullOp);
230 TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
232 MDNode *Empty = MDNode::get(Context, None);
233 ASSERT_TRUE(Empty->isResolved());
234 EXPECT_FALSE(Empty->isDistinct());
237 Metadata *Wrapped1Ops[] = {Empty};
238 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
239 ASSERT_EQ(Empty, Wrapped1->getOperand(0));
240 ASSERT_TRUE(Wrapped1->isResolved());
241 EXPECT_FALSE(Wrapped1->isDistinct());
244 Metadata *Wrapped2Ops[] = {Wrapped1};
245 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
246 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
247 ASSERT_TRUE(Wrapped2->isResolved());
248 EXPECT_FALSE(Wrapped2->isDistinct());
250 // !{!{!{}}} => !{!{}}
251 Wrapped2->replaceOperandWith(0, Empty);
252 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
253 EXPECT_TRUE(Wrapped2->isDistinct());
254 EXPECT_FALSE(Wrapped1->isDistinct());
257 TEST_F(MDNodeTest, getDistinct) {
259 MDNode *Empty = MDNode::get(Context, None);
260 ASSERT_TRUE(Empty->isResolved());
261 ASSERT_FALSE(Empty->isDistinct());
262 ASSERT_EQ(Empty, MDNode::get(Context, None));
265 MDNode *Distinct1 = MDNode::getDistinct(Context, None);
266 MDNode *Distinct2 = MDNode::getDistinct(Context, None);
267 EXPECT_TRUE(Distinct1->isResolved());
268 EXPECT_TRUE(Distinct2->isDistinct());
269 EXPECT_NE(Empty, Distinct1);
270 EXPECT_NE(Empty, Distinct2);
271 EXPECT_NE(Distinct1, Distinct2);
274 ASSERT_EQ(Empty, MDNode::get(Context, None));
277 TEST_F(MDNodeTest, TempIsDistinct) {
278 MDNode *T = MDNode::getTemporary(Context, None);
279 EXPECT_TRUE(T->isDistinct());
280 MDNode::deleteTemporary(T);
283 TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
285 MDNodeFwdDecl *Temp = MDNode::getTemporary(Context, None);
286 ASSERT_FALSE(Temp->isResolved());
288 // distinct !{temporary !{}}
289 Metadata *Ops[] = {Temp};
290 MDNode *Distinct = MDNode::getDistinct(Context, Ops);
291 EXPECT_TRUE(Distinct->isResolved());
292 EXPECT_EQ(Temp, Distinct->getOperand(0));
294 // temporary !{} => !{}
295 MDNode *Empty = MDNode::get(Context, None);
296 Temp->replaceAllUsesWith(Empty);
297 MDNode::deleteTemporary(Temp);
298 EXPECT_EQ(Empty, Distinct->getOperand(0));
301 TEST_F(MDNodeTest, handleChangedOperandRecursion) {
303 MDNode *N0 = MDNode::get(Context, None);
306 MDNodeFwdDecl *Temp3 = MDNode::getTemporary(Context, None);
307 Metadata *Ops1[] = {Temp3, nullptr};
308 MDNode *N1 = MDNode::get(Context, Ops1);
311 Metadata *Ops2[] = {Temp3, N0};
312 MDNode *N2 = MDNode::get(Context, Ops2);
315 Metadata *Ops3[] = {N2};
316 MDNode *N3 = MDNode::get(Context, Ops3);
317 Temp3->replaceAllUsesWith(N3);
321 Metadata *Ops4[] = {N1};
322 MDNode *N4 = MDNode::get(Context, Ops4);
324 // Confirm that the cycle prevented RAUW from getting dropped.
325 EXPECT_TRUE(N0->isResolved());
326 EXPECT_FALSE(N1->isResolved());
327 EXPECT_FALSE(N2->isResolved());
328 EXPECT_FALSE(N3->isResolved());
329 EXPECT_FALSE(N4->isResolved());
331 // Create a couple of distinct nodes to observe what's going on.
333 // !5 = distinct !{!2}
334 // !6 = distinct !{!3}
335 Metadata *Ops5[] = {N2};
336 MDNode *N5 = MDNode::getDistinct(Context, Ops5);
337 Metadata *Ops6[] = {N3};
338 MDNode *N6 = MDNode::getDistinct(Context, Ops6);
340 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
341 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2
342 // references !3, this can cause a re-entry of handleChangedOperand() when !3
343 // is not ready for it.
345 // !2->replaceOperandWith(1, nullptr)
346 // !2: !{!3, !0} => !{!3, null}
347 // !2->replaceAllUsesWith(!1)
348 // !3: !{!2] => !{!1}
349 // !3->replaceAllUsesWith(!4)
350 N2->replaceOperandWith(1, nullptr);
352 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
353 // under us. Just check that the other nodes are sane.
357 // !5 = distinct !{!1}
358 // !6 = distinct !{!4}
359 EXPECT_EQ(N4, N1->getOperand(0));
360 EXPECT_EQ(N1, N4->getOperand(0));
361 EXPECT_EQ(N1, N5->getOperand(0));
362 EXPECT_EQ(N4, N6->getOperand(0));
365 TEST_F(MDNodeTest, replaceResolvedOperand) {
366 // Check code for replacing one resolved operand with another. If doing this
367 // directly (via replaceOperandWith()) becomes illegal, change the operand to
368 // a global value that gets RAUW'ed.
370 // Use a temporary node to keep N from being resolved.
371 std::unique_ptr<MDNodeFwdDecl> Temp(MDNodeFwdDecl::get(Context, None));
372 Metadata *Ops[] = {nullptr, Temp.get()};
374 MDNode *Empty = MDTuple::get(Context, {});
375 MDNode *N = MDTuple::get(Context, Ops);
376 EXPECT_EQ(nullptr, N->getOperand(0));
377 ASSERT_FALSE(N->isResolved());
379 // Check code for replacing resolved nodes.
380 N->replaceOperandWith(0, Empty);
381 EXPECT_EQ(Empty, N->getOperand(0));
383 // Remove the reference to Temp; required for teardown.
384 N->replaceOperandWith(1, nullptr);
387 typedef MetadataTest MetadataAsValueTest;
389 TEST_F(MetadataAsValueTest, MDNode) {
390 MDNode *N = MDNode::get(Context, None);
391 auto *V = MetadataAsValue::get(Context, N);
392 EXPECT_TRUE(V->getType()->isMetadataTy());
393 EXPECT_EQ(N, V->getMetadata());
395 auto *V2 = MetadataAsValue::get(Context, N);
399 TEST_F(MetadataAsValueTest, MDNodeMDNode) {
400 MDNode *N = MDNode::get(Context, None);
401 Metadata *Ops[] = {N};
402 MDNode *N2 = MDNode::get(Context, Ops);
403 auto *V = MetadataAsValue::get(Context, N2);
404 EXPECT_TRUE(V->getType()->isMetadataTy());
405 EXPECT_EQ(N2, V->getMetadata());
407 auto *V2 = MetadataAsValue::get(Context, N2);
410 auto *V3 = MetadataAsValue::get(Context, N);
411 EXPECT_TRUE(V3->getType()->isMetadataTy());
413 EXPECT_EQ(N, V3->getMetadata());
416 TEST_F(MetadataAsValueTest, MDNodeConstant) {
417 auto *C = ConstantInt::getTrue(Context);
418 auto *MD = ConstantAsMetadata::get(C);
419 Metadata *Ops[] = {MD};
420 auto *N = MDNode::get(Context, Ops);
422 auto *V = MetadataAsValue::get(Context, MD);
423 EXPECT_TRUE(V->getType()->isMetadataTy());
424 EXPECT_EQ(MD, V->getMetadata());
426 auto *V2 = MetadataAsValue::get(Context, N);
427 EXPECT_EQ(MD, V2->getMetadata());
431 typedef MetadataTest ValueAsMetadataTest;
433 TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
434 Type *Ty = Type::getInt1PtrTy(Context);
435 std::unique_ptr<GlobalVariable> GV0(
436 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
437 auto *MD = ValueAsMetadata::get(GV0.get());
438 EXPECT_TRUE(MD->getValue() == GV0.get());
439 ASSERT_TRUE(GV0->use_empty());
441 std::unique_ptr<GlobalVariable> GV1(
442 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
443 GV0->replaceAllUsesWith(GV1.get());
444 EXPECT_TRUE(MD->getValue() == GV1.get());
447 typedef MetadataTest TrackingMDRefTest;
449 TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
450 Type *Ty = Type::getInt1PtrTy(Context);
451 std::unique_ptr<GlobalVariable> GV0(
452 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
453 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
454 EXPECT_TRUE(MD->getValue() == GV0.get());
455 ASSERT_TRUE(GV0->use_empty());
457 std::unique_ptr<GlobalVariable> GV1(
458 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
459 GV0->replaceAllUsesWith(GV1.get());
460 EXPECT_TRUE(MD->getValue() == GV1.get());
462 // Reset it, so we don't inadvertently test deletion.
466 TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
467 Type *Ty = Type::getInt1PtrTy(Context);
468 std::unique_ptr<GlobalVariable> GV(
469 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
470 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
471 EXPECT_TRUE(MD->getValue() == GV.get());
472 ASSERT_TRUE(GV->use_empty());
478 TEST(NamedMDNodeTest, Search) {
480 ConstantAsMetadata *C =
481 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
482 ConstantAsMetadata *C2 =
483 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
485 Metadata *const V = C;
486 Metadata *const V2 = C2;
487 MDNode *n = MDNode::get(Context, V);
488 MDNode *n2 = MDNode::get(Context, V2);
490 Module M("MyModule", Context);
491 const char *Name = "llvm.NMD1";
492 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
497 raw_string_ostream oss(Str);
499 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",