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, DeleteMDNodeFwdDecl) {
133 delete MDNode::getTemporary(Context, None);
136 TEST_F(MDNodeTest, SelfReference) {
140 MDNode *Temp = MDNode::getTemporary(Context, None);
141 Metadata *Args[] = {Temp};
142 MDNode *Self = MDNode::get(Context, Args);
143 Self->replaceOperandWith(0, Self);
144 MDNode::deleteTemporary(Temp);
145 ASSERT_EQ(Self, Self->getOperand(0));
147 // Self-references should be distinct, so MDNode::get() should grab a
148 // uniqued node that references Self, not Self.
150 MDNode *Ref1 = MDNode::get(Context, Args);
151 MDNode *Ref2 = MDNode::get(Context, Args);
152 EXPECT_NE(Self, Ref1);
153 EXPECT_EQ(Ref1, Ref2);
159 MDNode *Temp = MDNode::getTemporary(Context, None);
160 Metadata *Args[] = {Temp, MDNode::get(Context, None)};
161 MDNode *Self = MDNode::get(Context, Args);
162 Self->replaceOperandWith(0, Self);
163 MDNode::deleteTemporary(Temp);
164 ASSERT_EQ(Self, Self->getOperand(0));
166 // Self-references should be distinct, so MDNode::get() should grab a
167 // uniqued node that references Self, not Self itself.
169 MDNode *Ref1 = MDNode::get(Context, Args);
170 MDNode *Ref2 = MDNode::get(Context, Args);
171 EXPECT_NE(Self, Ref1);
172 EXPECT_EQ(Ref1, Ref2);
176 TEST_F(MDNodeTest, Print) {
177 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
178 MDString *S = MDString::get(Context, "foo");
179 MDNode *N0 = getNode();
180 MDNode *N1 = getNode(N0);
181 MDNode *N2 = getNode(N0, N1);
183 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
184 MDNode *N = MDNode::get(Context, Args);
186 std::string Expected;
188 raw_string_ostream OS(Expected);
190 C->printAsOperand(OS);
192 S->printAsOperand(OS);
194 MDNode *Nodes[] = {N0, N1, N2};
195 for (auto *Node : Nodes)
196 OS << ", <" << (void *)Node << ">";
202 raw_string_ostream OS(Actual);
206 EXPECT_EQ(Expected, Actual);
209 TEST_F(MDNodeTest, NullOperand) {
211 MDNode *Empty = MDNode::get(Context, None);
213 // metadata !{metadata !{}}
214 Metadata *Ops[] = {Empty};
215 MDNode *N = MDNode::get(Context, Ops);
216 ASSERT_EQ(Empty, N->getOperand(0));
218 // metadata !{metadata !{}} => metadata !{null}
219 N->replaceOperandWith(0, nullptr);
220 ASSERT_EQ(nullptr, N->getOperand(0));
224 MDNode *NullOp = MDNode::get(Context, Ops);
225 ASSERT_EQ(nullptr, NullOp->getOperand(0));
226 EXPECT_EQ(N, NullOp);
229 TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
231 MDNode *Empty = MDNode::get(Context, None);
232 ASSERT_TRUE(Empty->isResolved());
233 EXPECT_FALSE(Empty->isDistinct());
236 Metadata *Wrapped1Ops[] = {Empty};
237 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
238 ASSERT_EQ(Empty, Wrapped1->getOperand(0));
239 ASSERT_TRUE(Wrapped1->isResolved());
240 EXPECT_FALSE(Wrapped1->isDistinct());
243 Metadata *Wrapped2Ops[] = {Wrapped1};
244 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
245 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
246 ASSERT_TRUE(Wrapped2->isResolved());
247 EXPECT_FALSE(Wrapped2->isDistinct());
249 // !{!{!{}}} => !{!{}}
250 Wrapped2->replaceOperandWith(0, Empty);
251 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
252 EXPECT_TRUE(Wrapped2->isDistinct());
253 EXPECT_FALSE(Wrapped1->isDistinct());
256 TEST_F(MDNodeTest, getDistinct) {
258 MDNode *Empty = MDNode::get(Context, None);
259 ASSERT_TRUE(Empty->isResolved());
260 ASSERT_FALSE(Empty->isDistinct());
261 ASSERT_EQ(Empty, MDNode::get(Context, None));
264 MDNode *Distinct1 = MDNode::getDistinct(Context, None);
265 MDNode *Distinct2 = MDNode::getDistinct(Context, None);
266 EXPECT_TRUE(Distinct1->isResolved());
267 EXPECT_TRUE(Distinct2->isDistinct());
268 EXPECT_NE(Empty, Distinct1);
269 EXPECT_NE(Empty, Distinct2);
270 EXPECT_NE(Distinct1, Distinct2);
273 ASSERT_EQ(Empty, MDNode::get(Context, None));
276 TEST_F(MDNodeTest, TempIsDistinct) {
277 MDNode *T = MDNode::getTemporary(Context, None);
278 EXPECT_TRUE(T->isDistinct());
279 MDNode::deleteTemporary(T);
282 TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
284 MDNodeFwdDecl *Temp = MDNode::getTemporary(Context, None);
285 ASSERT_FALSE(Temp->isResolved());
287 // distinct !{temporary !{}}
288 Metadata *Ops[] = {Temp};
289 MDNode *Distinct = MDNode::getDistinct(Context, Ops);
290 EXPECT_TRUE(Distinct->isResolved());
291 EXPECT_EQ(Temp, Distinct->getOperand(0));
293 // temporary !{} => !{}
294 MDNode *Empty = MDNode::get(Context, None);
295 Temp->replaceAllUsesWith(Empty);
296 MDNode::deleteTemporary(Temp);
297 EXPECT_EQ(Empty, Distinct->getOperand(0));
300 TEST_F(MDNodeTest, handleChangedOperandRecursion) {
302 MDNode *N0 = MDNode::get(Context, None);
305 MDNodeFwdDecl *Temp3 = MDNode::getTemporary(Context, None);
306 Metadata *Ops1[] = {Temp3, nullptr};
307 MDNode *N1 = MDNode::get(Context, Ops1);
310 Metadata *Ops2[] = {Temp3, N0};
311 MDNode *N2 = MDNode::get(Context, Ops2);
314 Metadata *Ops3[] = {N2};
315 MDNode *N3 = MDNode::get(Context, Ops3);
316 Temp3->replaceAllUsesWith(N3);
320 Metadata *Ops4[] = {N1};
321 MDNode *N4 = MDNode::get(Context, Ops4);
323 // Confirm that the cycle prevented RAUW from getting dropped.
324 EXPECT_TRUE(N0->isResolved());
325 EXPECT_FALSE(N1->isResolved());
326 EXPECT_FALSE(N2->isResolved());
327 EXPECT_FALSE(N3->isResolved());
328 EXPECT_FALSE(N4->isResolved());
330 // Create a couple of distinct nodes to observe what's going on.
332 // !5 = distinct !{!2}
333 // !6 = distinct !{!3}
334 Metadata *Ops5[] = {N2};
335 MDNode *N5 = MDNode::getDistinct(Context, Ops5);
336 Metadata *Ops6[] = {N3};
337 MDNode *N6 = MDNode::getDistinct(Context, Ops6);
339 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
340 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2
341 // references !3, this can cause a re-entry of handleChangedOperand() when !3
342 // is not ready for it.
344 // !2->replaceOperandWith(1, nullptr)
345 // !2: !{!3, !0} => !{!3, null}
346 // !2->replaceAllUsesWith(!1)
347 // !3: !{!2] => !{!1}
348 // !3->replaceAllUsesWith(!4)
349 N2->replaceOperandWith(1, nullptr);
351 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
352 // under us. Just check that the other nodes are sane.
356 // !5 = distinct !{!1}
357 // !6 = distinct !{!4}
358 EXPECT_EQ(N4, N1->getOperand(0));
359 EXPECT_EQ(N1, N4->getOperand(0));
360 EXPECT_EQ(N1, N5->getOperand(0));
361 EXPECT_EQ(N4, N6->getOperand(0));
364 typedef MetadataTest MetadataAsValueTest;
366 TEST_F(MetadataAsValueTest, MDNode) {
367 MDNode *N = MDNode::get(Context, None);
368 auto *V = MetadataAsValue::get(Context, N);
369 EXPECT_TRUE(V->getType()->isMetadataTy());
370 EXPECT_EQ(N, V->getMetadata());
372 auto *V2 = MetadataAsValue::get(Context, N);
376 TEST_F(MetadataAsValueTest, MDNodeMDNode) {
377 MDNode *N = MDNode::get(Context, None);
378 Metadata *Ops[] = {N};
379 MDNode *N2 = MDNode::get(Context, Ops);
380 auto *V = MetadataAsValue::get(Context, N2);
381 EXPECT_TRUE(V->getType()->isMetadataTy());
382 EXPECT_EQ(N2, V->getMetadata());
384 auto *V2 = MetadataAsValue::get(Context, N2);
387 auto *V3 = MetadataAsValue::get(Context, N);
388 EXPECT_TRUE(V3->getType()->isMetadataTy());
390 EXPECT_EQ(N, V3->getMetadata());
393 TEST_F(MetadataAsValueTest, MDNodeConstant) {
394 auto *C = ConstantInt::getTrue(Context);
395 auto *MD = ConstantAsMetadata::get(C);
396 Metadata *Ops[] = {MD};
397 auto *N = MDNode::get(Context, Ops);
399 auto *V = MetadataAsValue::get(Context, MD);
400 EXPECT_TRUE(V->getType()->isMetadataTy());
401 EXPECT_EQ(MD, V->getMetadata());
403 auto *V2 = MetadataAsValue::get(Context, N);
404 EXPECT_EQ(MD, V2->getMetadata());
408 typedef MetadataTest ValueAsMetadataTest;
410 TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
411 Type *Ty = Type::getInt1PtrTy(Context);
412 std::unique_ptr<GlobalVariable> GV0(
413 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
414 auto *MD = ValueAsMetadata::get(GV0.get());
415 EXPECT_TRUE(MD->getValue() == GV0.get());
416 ASSERT_TRUE(GV0->use_empty());
418 std::unique_ptr<GlobalVariable> GV1(
419 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
420 GV0->replaceAllUsesWith(GV1.get());
421 EXPECT_TRUE(MD->getValue() == GV1.get());
424 typedef MetadataTest TrackingMDRefTest;
426 TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
427 Type *Ty = Type::getInt1PtrTy(Context);
428 std::unique_ptr<GlobalVariable> GV0(
429 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
430 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
431 EXPECT_TRUE(MD->getValue() == GV0.get());
432 ASSERT_TRUE(GV0->use_empty());
434 std::unique_ptr<GlobalVariable> GV1(
435 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
436 GV0->replaceAllUsesWith(GV1.get());
437 EXPECT_TRUE(MD->getValue() == GV1.get());
439 // Reset it, so we don't inadvertently test deletion.
443 TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
444 Type *Ty = Type::getInt1PtrTy(Context);
445 std::unique_ptr<GlobalVariable> GV(
446 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
447 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
448 EXPECT_TRUE(MD->getValue() == GV.get());
449 ASSERT_TRUE(GV->use_empty());
455 TEST(NamedMDNodeTest, Search) {
457 ConstantAsMetadata *C =
458 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
459 ConstantAsMetadata *C2 =
460 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
462 Metadata *const V = C;
463 Metadata *const V2 = C2;
464 MDNode *n = MDNode::get(Context, V);
465 MDNode *n2 = MDNode::get(Context, V2);
467 Module M("MyModule", Context);
468 const char *Name = "llvm.NMD1";
469 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
474 raw_string_ostream oss(Str);
476 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",