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/IR/Metadata.h"
11 #include "llvm/IR/Constants.h"
12 #include "llvm/IR/Instructions.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/IR/Type.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include "gtest/gtest.h"
22 class MetadataTest : public testing::Test {
25 MDNode *getNode() { return MDNode::get(Context, None); }
26 MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); }
27 MDNode *getNode(Metadata *MD1, Metadata *MD2) {
28 Metadata *MDs[] = {MD1, MD2};
29 return MDNode::get(Context, MDs);
32 typedef MetadataTest MDStringTest;
34 // Test that construction of MDString with different value produces different
35 // MDString objects, even with the same string pointer and nulls in the string.
36 TEST_F(MDStringTest, CreateDifferent) {
37 char x[3] = { 'f', 0, 'A' };
38 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
40 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
44 // Test that creation of MDStrings with the same string contents produces the
45 // same MDString object, even with different pointers.
46 TEST_F(MDStringTest, CreateSame) {
47 char x[4] = { 'a', 'b', 'c', 'X' };
48 char y[4] = { 'a', 'b', 'c', 'Y' };
50 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
51 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
55 // Test that MDString prints out the string we fed it.
56 TEST_F(MDStringTest, PrintingSimple) {
57 char *str = new char[13];
58 strncpy(str, "testing 1 2 3", 13);
59 MDString *s = MDString::get(Context, StringRef(str, 13));
60 strncpy(str, "aaaaaaaaaaaaa", 13);
64 raw_string_ostream oss(Str);
66 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
69 // Test printing of MDString with non-printable characters.
70 TEST_F(MDStringTest, PrintingComplex) {
71 char str[5] = {0, '\n', '"', '\\', (char)-1};
72 MDString *s = MDString::get(Context, StringRef(str+0, 5));
74 raw_string_ostream oss(Str);
76 EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
79 typedef MetadataTest MDNodeTest;
81 // Test the two constructors, and containing other Constants.
82 TEST_F(MDNodeTest, Simple) {
83 char x[3] = { 'a', 'b', 'c' };
84 char y[3] = { '1', '2', '3' };
86 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
87 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
88 ConstantAsMetadata *CI = ConstantAsMetadata::get(
89 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
91 std::vector<Metadata *> V;
96 MDNode *n1 = MDNode::get(Context, V);
97 Metadata *const c1 = n1;
98 MDNode *n2 = MDNode::get(Context, c1);
99 Metadata *const c2 = n2;
100 MDNode *n3 = MDNode::get(Context, V);
101 MDNode *n4 = MDNode::getIfExists(Context, V);
102 MDNode *n5 = MDNode::getIfExists(Context, c1);
103 MDNode *n6 = MDNode::getIfExists(Context, c2);
108 EXPECT_EQ(n6, (Metadata *)nullptr);
110 EXPECT_EQ(3u, n1->getNumOperands());
111 EXPECT_EQ(s1, n1->getOperand(0));
112 EXPECT_EQ(CI, n1->getOperand(1));
113 EXPECT_EQ(s2, n1->getOperand(2));
115 EXPECT_EQ(1u, n2->getNumOperands());
116 EXPECT_EQ(n1, n2->getOperand(0));
119 TEST_F(MDNodeTest, Delete) {
120 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
121 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
123 Metadata *const V = LocalAsMetadata::get(I);
124 MDNode *n = MDNode::get(Context, V);
125 TrackingMDRef wvh(n);
132 TEST_F(MDNodeTest, SelfReference) {
136 MDNode *Temp = MDNode::getTemporary(Context, None);
137 Metadata *Args[] = {Temp};
138 MDNode *Self = MDNode::get(Context, Args);
139 Self->replaceOperandWith(0, Self);
140 MDNode::deleteTemporary(Temp);
141 ASSERT_EQ(Self, Self->getOperand(0));
143 // Self-references should be distinct, so MDNode::get() should grab a
144 // uniqued node that references Self, not Self.
146 MDNode *Ref1 = MDNode::get(Context, Args);
147 MDNode *Ref2 = MDNode::get(Context, Args);
148 EXPECT_NE(Self, Ref1);
149 EXPECT_EQ(Ref1, Ref2);
155 MDNode *Temp = MDNode::getTemporary(Context, None);
156 Metadata *Args[] = {Temp, MDNode::get(Context, None)};
157 MDNode *Self = MDNode::get(Context, Args);
158 Self->replaceOperandWith(0, Self);
159 MDNode::deleteTemporary(Temp);
160 ASSERT_EQ(Self, Self->getOperand(0));
162 // Self-references should be distinct, so MDNode::get() should grab a
163 // uniqued node that references Self, not Self itself.
165 MDNode *Ref1 = MDNode::get(Context, Args);
166 MDNode *Ref2 = MDNode::get(Context, Args);
167 EXPECT_NE(Self, Ref1);
168 EXPECT_EQ(Ref1, Ref2);
172 TEST_F(MDNodeTest, Print) {
173 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
174 MDString *S = MDString::get(Context, "foo");
175 MDNode *N0 = getNode();
176 MDNode *N1 = getNode(N0);
177 MDNode *N2 = getNode(N0, N1);
179 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
180 MDNode *N = MDNode::get(Context, Args);
182 std::string Expected;
184 raw_string_ostream OS(Expected);
186 C->printAsOperand(OS);
188 S->printAsOperand(OS);
190 MDNode *Nodes[] = {N0, N1, N2};
191 for (auto *Node : Nodes)
192 OS << ", <" << (void *)Node << ">";
198 raw_string_ostream OS(Actual);
202 EXPECT_EQ(Expected, Actual);
205 TEST_F(MDNodeTest, NullOperand) {
207 MDNode *Empty = MDNode::get(Context, None);
209 // metadata !{metadata !{}}
210 Metadata *Ops[] = {Empty};
211 MDNode *N = MDNode::get(Context, Ops);
212 ASSERT_EQ(Empty, N->getOperand(0));
214 // metadata !{metadata !{}} => metadata !{null}
215 N->replaceOperandWith(0, nullptr);
216 ASSERT_EQ(nullptr, N->getOperand(0));
220 MDNode *NullOp = MDNode::get(Context, Ops);
221 ASSERT_EQ(nullptr, NullOp->getOperand(0));
222 EXPECT_EQ(N, NullOp);
225 TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
227 MDNode *Empty = MDNode::get(Context, None);
228 ASSERT_TRUE(Empty->isResolved());
229 EXPECT_FALSE(Empty->isDistinct());
232 Metadata *Wrapped1Ops[] = {Empty};
233 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
234 ASSERT_EQ(Empty, Wrapped1->getOperand(0));
235 ASSERT_TRUE(Wrapped1->isResolved());
236 EXPECT_FALSE(Wrapped1->isDistinct());
239 Metadata *Wrapped2Ops[] = {Wrapped1};
240 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
241 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
242 ASSERT_TRUE(Wrapped2->isResolved());
243 EXPECT_FALSE(Wrapped2->isDistinct());
245 // !{!{!{}}} => !{!{}}
246 Wrapped2->replaceOperandWith(0, Empty);
247 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
248 EXPECT_TRUE(Wrapped2->isDistinct());
249 EXPECT_FALSE(Wrapped1->isDistinct());
252 TEST_F(MDNodeTest, getDistinct) {
254 MDNode *Empty = MDNode::get(Context, None);
255 ASSERT_TRUE(Empty->isResolved());
256 ASSERT_FALSE(Empty->isDistinct());
257 ASSERT_EQ(Empty, MDNode::get(Context, None));
260 MDNode *Distinct1 = MDNode::getDistinct(Context, None);
261 MDNode *Distinct2 = MDNode::getDistinct(Context, None);
262 EXPECT_TRUE(Distinct1->isResolved());
263 EXPECT_TRUE(Distinct2->isDistinct());
264 EXPECT_NE(Empty, Distinct1);
265 EXPECT_NE(Empty, Distinct2);
266 EXPECT_NE(Distinct1, Distinct2);
269 ASSERT_EQ(Empty, MDNode::get(Context, None));
272 TEST_F(MDNodeTest, TempIsDistinct) {
273 MDNode *T = MDNode::getTemporary(Context, None);
274 EXPECT_TRUE(T->isDistinct());
275 MDNode::deleteTemporary(T);
278 TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
280 MDNodeFwdDecl *Temp = MDNode::getTemporary(Context, None);
281 ASSERT_FALSE(Temp->isResolved());
283 // distinct !{temporary !{}}
284 Metadata *Ops[] = {Temp};
285 MDNode *Distinct = MDNode::getDistinct(Context, Ops);
286 EXPECT_TRUE(Distinct->isResolved());
287 EXPECT_EQ(Temp, Distinct->getOperand(0));
289 // temporary !{} => !{}
290 MDNode *Empty = MDNode::get(Context, None);
291 Temp->replaceAllUsesWith(Empty);
292 MDNode::deleteTemporary(Temp);
293 EXPECT_EQ(Empty, Distinct->getOperand(0));
296 TEST_F(MDNodeTest, handleChangedOperandRecursion) {
298 MDNode *N0 = MDNode::get(Context, None);
301 MDNodeFwdDecl *Temp3 = MDNode::getTemporary(Context, None);
302 Metadata *Ops1[] = {Temp3, nullptr};
303 MDNode *N1 = MDNode::get(Context, Ops1);
306 Metadata *Ops2[] = {Temp3, N0};
307 MDNode *N2 = MDNode::get(Context, Ops2);
310 Metadata *Ops3[] = {N2};
311 MDNode *N3 = MDNode::get(Context, Ops3);
312 Temp3->replaceAllUsesWith(N3);
315 Metadata *Ops4[] = {N1};
316 MDNode *N4 = MDNode::get(Context, Ops4);
318 // Confirm that the cycle prevented RAUW from getting dropped.
319 EXPECT_TRUE(N0->isResolved());
320 EXPECT_FALSE(N1->isResolved());
321 EXPECT_FALSE(N2->isResolved());
322 EXPECT_FALSE(N3->isResolved());
323 EXPECT_FALSE(N4->isResolved());
325 // Create a couple of distinct nodes to observe what's going on.
327 // !5 = distinct !{!2}
328 // !6 = distinct !{!3}
329 Metadata *Ops5[] = {N2};
330 MDNode *N5 = MDNode::getDistinct(Context, Ops5);
331 Metadata *Ops6[] = {N3};
332 MDNode *N6 = MDNode::getDistinct(Context, Ops6);
334 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
335 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2
336 // references !3, this can cause a re-entry of handleChangedOperand() when !3
337 // is not ready for it.
339 // !2->replaceOperandWith(1, nullptr)
340 // !2: !{!3, !0} => !{!3, null}
341 // !2->replaceAllUsesWith(!1)
342 // !3: !{!2] => !{!1}
343 // !3->replaceAllUsesWith(!4)
344 N2->replaceOperandWith(1, nullptr);
346 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
347 // under us. Just check that the other nodes are sane.
351 // !5 = distinct !{!1}
352 // !6 = distinct !{!4}
353 EXPECT_EQ(N4, N1->getOperand(0));
354 EXPECT_EQ(N1, N4->getOperand(0));
355 EXPECT_EQ(N1, N5->getOperand(0));
356 EXPECT_EQ(N4, N6->getOperand(0));
359 typedef MetadataTest MetadataAsValueTest;
361 TEST_F(MetadataAsValueTest, MDNode) {
362 MDNode *N = MDNode::get(Context, None);
363 auto *V = MetadataAsValue::get(Context, N);
364 EXPECT_TRUE(V->getType()->isMetadataTy());
365 EXPECT_EQ(N, V->getMetadata());
367 auto *V2 = MetadataAsValue::get(Context, N);
371 TEST_F(MetadataAsValueTest, MDNodeMDNode) {
372 MDNode *N = MDNode::get(Context, None);
373 Metadata *Ops[] = {N};
374 MDNode *N2 = MDNode::get(Context, Ops);
375 auto *V = MetadataAsValue::get(Context, N2);
376 EXPECT_TRUE(V->getType()->isMetadataTy());
377 EXPECT_EQ(N2, V->getMetadata());
379 auto *V2 = MetadataAsValue::get(Context, N2);
382 auto *V3 = MetadataAsValue::get(Context, N);
383 EXPECT_TRUE(V3->getType()->isMetadataTy());
385 EXPECT_EQ(N, V3->getMetadata());
388 TEST_F(MetadataAsValueTest, MDNodeConstant) {
389 auto *C = ConstantInt::getTrue(Context);
390 auto *MD = ConstantAsMetadata::get(C);
391 Metadata *Ops[] = {MD};
392 auto *N = MDNode::get(Context, Ops);
394 auto *V = MetadataAsValue::get(Context, MD);
395 EXPECT_TRUE(V->getType()->isMetadataTy());
396 EXPECT_EQ(MD, V->getMetadata());
398 auto *V2 = MetadataAsValue::get(Context, N);
399 EXPECT_EQ(MD, V2->getMetadata());
403 typedef MetadataTest ValueAsMetadataTest;
405 TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
406 Type *Ty = Type::getInt1PtrTy(Context);
407 std::unique_ptr<GlobalVariable> GV0(
408 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
409 auto *MD = ValueAsMetadata::get(GV0.get());
410 EXPECT_TRUE(MD->getValue() == GV0.get());
411 ASSERT_TRUE(GV0->use_empty());
413 std::unique_ptr<GlobalVariable> GV1(
414 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
415 GV0->replaceAllUsesWith(GV1.get());
416 EXPECT_TRUE(MD->getValue() == GV1.get());
419 typedef MetadataTest TrackingMDRefTest;
421 TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
422 Type *Ty = Type::getInt1PtrTy(Context);
423 std::unique_ptr<GlobalVariable> GV0(
424 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
425 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
426 EXPECT_TRUE(MD->getValue() == GV0.get());
427 ASSERT_TRUE(GV0->use_empty());
429 std::unique_ptr<GlobalVariable> GV1(
430 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
431 GV0->replaceAllUsesWith(GV1.get());
432 EXPECT_TRUE(MD->getValue() == GV1.get());
434 // Reset it, so we don't inadvertently test deletion.
438 TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
439 Type *Ty = Type::getInt1PtrTy(Context);
440 std::unique_ptr<GlobalVariable> GV(
441 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
442 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
443 EXPECT_TRUE(MD->getValue() == GV.get());
444 ASSERT_TRUE(GV->use_empty());
450 TEST(NamedMDNodeTest, Search) {
452 ConstantAsMetadata *C =
453 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
454 ConstantAsMetadata *C2 =
455 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
457 Metadata *const V = C;
458 Metadata *const V2 = C2;
459 MDNode *n = MDNode::get(Context, V);
460 MDNode *n2 = MDNode::get(Context, V2);
462 Module M("MyModule", Context);
463 const char *Name = "llvm.NMD1";
464 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
469 raw_string_ostream oss(Str);
471 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",