IR: Make MDNodeFwdDecl destructor public
[oota-llvm.git] / unittests / IR / MetadataTest.cpp
1 //===- llvm/unittest/IR/Metadata.cpp - Metadata unit tests ----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
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"
18 using namespace llvm;
19
20 namespace {
21
22 class MetadataTest : public testing::Test {
23 protected:
24   LLVMContext Context;
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);
30   }
31 };
32 typedef MetadataTest MDStringTest;
33
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));
39   x[2] = 'B';
40   MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
41   EXPECT_NE(s1, s2);
42 }
43
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' };
49
50   MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
51   MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
52   EXPECT_EQ(s1, s2);
53 }
54
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);
61   delete[] str;
62
63   std::string Str;
64   raw_string_ostream oss(Str);
65   s->print(oss);
66   EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
67 }
68
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));
73   std::string Str;
74   raw_string_ostream oss(Str);
75   s->print(oss);
76   EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
77 }
78
79 typedef MetadataTest MDNodeTest;
80
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' };
85
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)));
90
91   std::vector<Metadata *> V;
92   V.push_back(s1);
93   V.push_back(CI);
94   V.push_back(s2);
95
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);
104   EXPECT_NE(n1, n2);
105   EXPECT_EQ(n1, n3);
106   EXPECT_EQ(n4, n1);
107   EXPECT_EQ(n5, n2);
108   EXPECT_EQ(n6, (Metadata *)nullptr);
109
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));
114
115   EXPECT_EQ(1u, n2->getNumOperands());
116   EXPECT_EQ(n1, n2->getOperand(0));
117 }
118
119 TEST_F(MDNodeTest, Delete) {
120   Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
121   Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
122
123   Metadata *const V = LocalAsMetadata::get(I);
124   MDNode *n = MDNode::get(Context, V);
125   TrackingMDRef wvh(n);
126
127   EXPECT_EQ(n, wvh);
128
129   delete I;
130 }
131
132 TEST_F(MDNodeTest, DeleteMDNodeFwdDecl) {
133   delete MDNode::getTemporary(Context, None);
134 }
135
136 TEST_F(MDNodeTest, SelfReference) {
137   // !0 = !{!0}
138   // !1 = !{!0}
139   {
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));
146
147     // Self-references should be distinct, so MDNode::get() should grab a
148     // uniqued node that references Self, not Self.
149     Args[0] = 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);
154   }
155
156   // !0 = !{!0, !{}}
157   // !1 = !{!0, !{}}
158   {
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));
165
166     // Self-references should be distinct, so MDNode::get() should grab a
167     // uniqued node that references Self, not Self itself.
168     Args[0] = Self;
169     MDNode *Ref1 = MDNode::get(Context, Args);
170     MDNode *Ref2 = MDNode::get(Context, Args);
171     EXPECT_NE(Self, Ref1);
172     EXPECT_EQ(Ref1, Ref2);
173   }
174 }
175
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);
182
183   Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
184   MDNode *N = MDNode::get(Context, Args);
185
186   std::string Expected;
187   {
188     raw_string_ostream OS(Expected);
189     OS << "!{";
190     C->printAsOperand(OS);
191     OS << ", ";
192     S->printAsOperand(OS);
193     OS << ", null";
194     MDNode *Nodes[] = {N0, N1, N2};
195     for (auto *Node : Nodes)
196       OS << ", <" << (void *)Node << ">";
197     OS << "}\n";
198   }
199
200   std::string Actual;
201   {
202     raw_string_ostream OS(Actual);
203     N->print(OS);
204   }
205
206   EXPECT_EQ(Expected, Actual);
207 }
208
209 TEST_F(MDNodeTest, NullOperand) {
210   // metadata !{}
211   MDNode *Empty = MDNode::get(Context, None);
212
213   // metadata !{metadata !{}}
214   Metadata *Ops[] = {Empty};
215   MDNode *N = MDNode::get(Context, Ops);
216   ASSERT_EQ(Empty, N->getOperand(0));
217
218   // metadata !{metadata !{}} => metadata !{null}
219   N->replaceOperandWith(0, nullptr);
220   ASSERT_EQ(nullptr, N->getOperand(0));
221
222   // metadata !{null}
223   Ops[0] = nullptr;
224   MDNode *NullOp = MDNode::get(Context, Ops);
225   ASSERT_EQ(nullptr, NullOp->getOperand(0));
226   EXPECT_EQ(N, NullOp);
227 }
228
229 TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
230   // !{}
231   MDNode *Empty = MDNode::get(Context, None);
232   ASSERT_TRUE(Empty->isResolved());
233   EXPECT_FALSE(Empty->isDistinct());
234
235   // !{!{}}
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());
241
242   // !{!{!{}}}
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());
248
249   // !{!{!{}}} => !{!{}}
250   Wrapped2->replaceOperandWith(0, Empty);
251   ASSERT_EQ(Empty, Wrapped2->getOperand(0));
252   EXPECT_TRUE(Wrapped2->isDistinct());
253   EXPECT_FALSE(Wrapped1->isDistinct());
254 }
255
256 TEST_F(MDNodeTest, getDistinct) {
257   // !{}
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));
262
263   // distinct !{}
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);
271
272   // !{}
273   ASSERT_EQ(Empty, MDNode::get(Context, None));
274 }
275
276 TEST_F(MDNodeTest, TempIsDistinct) {
277   MDNode *T = MDNode::getTemporary(Context, None);
278   EXPECT_TRUE(T->isDistinct());
279   MDNode::deleteTemporary(T);
280 }
281
282 TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
283   // temporary !{}
284   MDNodeFwdDecl *Temp = MDNode::getTemporary(Context, None);
285   ASSERT_FALSE(Temp->isResolved());
286
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));
292
293   // temporary !{} => !{}
294   MDNode *Empty = MDNode::get(Context, None);
295   Temp->replaceAllUsesWith(Empty);
296   MDNode::deleteTemporary(Temp);
297   EXPECT_EQ(Empty, Distinct->getOperand(0));
298 }
299
300 TEST_F(MDNodeTest, handleChangedOperandRecursion) {
301   // !0 = !{}
302   MDNode *N0 = MDNode::get(Context, None);
303
304   // !1 = !{!3, null}
305   MDNodeFwdDecl *Temp3 = MDNode::getTemporary(Context, None);
306   Metadata *Ops1[] = {Temp3, nullptr};
307   MDNode *N1 = MDNode::get(Context, Ops1);
308
309   // !2 = !{!3, !0}
310   Metadata *Ops2[] = {Temp3, N0};
311   MDNode *N2 = MDNode::get(Context, Ops2);
312
313   // !3 = !{!2}
314   Metadata *Ops3[] = {N2};
315   MDNode *N3 = MDNode::get(Context, Ops3);
316   Temp3->replaceAllUsesWith(N3);
317
318   // !4 = !{!1}
319   Metadata *Ops4[] = {N1};
320   MDNode *N4 = MDNode::get(Context, Ops4);
321
322   // Confirm that the cycle prevented RAUW from getting dropped.
323   EXPECT_TRUE(N0->isResolved());
324   EXPECT_FALSE(N1->isResolved());
325   EXPECT_FALSE(N2->isResolved());
326   EXPECT_FALSE(N3->isResolved());
327   EXPECT_FALSE(N4->isResolved());
328
329   // Create a couple of distinct nodes to observe what's going on.
330   //
331   // !5 = distinct !{!2}
332   // !6 = distinct !{!3}
333   Metadata *Ops5[] = {N2};
334   MDNode *N5 = MDNode::getDistinct(Context, Ops5);
335   Metadata *Ops6[] = {N3};
336   MDNode *N6 = MDNode::getDistinct(Context, Ops6);
337
338   // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
339   // This will ripple up, with !3 colliding with !4, and RAUWing.  Since !2
340   // references !3, this can cause a re-entry of handleChangedOperand() when !3
341   // is not ready for it.
342   //
343   // !2->replaceOperandWith(1, nullptr)
344   // !2: !{!3, !0} => !{!3, null}
345   // !2->replaceAllUsesWith(!1)
346   // !3: !{!2] => !{!1}
347   // !3->replaceAllUsesWith(!4)
348   N2->replaceOperandWith(1, nullptr);
349
350   // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
351   // under us.  Just check that the other nodes are sane.
352   //
353   // !1 = !{!4, null}
354   // !4 = !{!1}
355   // !5 = distinct !{!1}
356   // !6 = distinct !{!4}
357   EXPECT_EQ(N4, N1->getOperand(0));
358   EXPECT_EQ(N1, N4->getOperand(0));
359   EXPECT_EQ(N1, N5->getOperand(0));
360   EXPECT_EQ(N4, N6->getOperand(0));
361 }
362
363 typedef MetadataTest MetadataAsValueTest;
364
365 TEST_F(MetadataAsValueTest, MDNode) {
366   MDNode *N = MDNode::get(Context, None);
367   auto *V = MetadataAsValue::get(Context, N);
368   EXPECT_TRUE(V->getType()->isMetadataTy());
369   EXPECT_EQ(N, V->getMetadata());
370
371   auto *V2 = MetadataAsValue::get(Context, N);
372   EXPECT_EQ(V, V2);
373 }
374
375 TEST_F(MetadataAsValueTest, MDNodeMDNode) {
376   MDNode *N = MDNode::get(Context, None);
377   Metadata *Ops[] = {N};
378   MDNode *N2 = MDNode::get(Context, Ops);
379   auto *V = MetadataAsValue::get(Context, N2);
380   EXPECT_TRUE(V->getType()->isMetadataTy());
381   EXPECT_EQ(N2, V->getMetadata());
382
383   auto *V2 = MetadataAsValue::get(Context, N2);
384   EXPECT_EQ(V, V2);
385
386   auto *V3 = MetadataAsValue::get(Context, N);
387   EXPECT_TRUE(V3->getType()->isMetadataTy());
388   EXPECT_NE(V, V3);
389   EXPECT_EQ(N, V3->getMetadata());
390 }
391
392 TEST_F(MetadataAsValueTest, MDNodeConstant) {
393   auto *C = ConstantInt::getTrue(Context);
394   auto *MD = ConstantAsMetadata::get(C);
395   Metadata *Ops[] = {MD};
396   auto *N = MDNode::get(Context, Ops);
397
398   auto *V = MetadataAsValue::get(Context, MD);
399   EXPECT_TRUE(V->getType()->isMetadataTy());
400   EXPECT_EQ(MD, V->getMetadata());
401
402   auto *V2 = MetadataAsValue::get(Context, N);
403   EXPECT_EQ(MD, V2->getMetadata());
404   EXPECT_EQ(V, V2);
405 }
406
407 typedef MetadataTest ValueAsMetadataTest;
408
409 TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
410   Type *Ty = Type::getInt1PtrTy(Context);
411   std::unique_ptr<GlobalVariable> GV0(
412       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
413   auto *MD = ValueAsMetadata::get(GV0.get());
414   EXPECT_TRUE(MD->getValue() == GV0.get());
415   ASSERT_TRUE(GV0->use_empty());
416
417   std::unique_ptr<GlobalVariable> GV1(
418       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
419   GV0->replaceAllUsesWith(GV1.get());
420   EXPECT_TRUE(MD->getValue() == GV1.get());
421 }
422
423 typedef MetadataTest TrackingMDRefTest;
424
425 TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
426   Type *Ty = Type::getInt1PtrTy(Context);
427   std::unique_ptr<GlobalVariable> GV0(
428       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
429   TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
430   EXPECT_TRUE(MD->getValue() == GV0.get());
431   ASSERT_TRUE(GV0->use_empty());
432
433   std::unique_ptr<GlobalVariable> GV1(
434       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
435   GV0->replaceAllUsesWith(GV1.get());
436   EXPECT_TRUE(MD->getValue() == GV1.get());
437
438   // Reset it, so we don't inadvertently test deletion.
439   MD.reset();
440 }
441
442 TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
443   Type *Ty = Type::getInt1PtrTy(Context);
444   std::unique_ptr<GlobalVariable> GV(
445       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
446   TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
447   EXPECT_TRUE(MD->getValue() == GV.get());
448   ASSERT_TRUE(GV->use_empty());
449
450   GV.reset();
451   EXPECT_TRUE(!MD);
452 }
453
454 TEST(NamedMDNodeTest, Search) {
455   LLVMContext Context;
456   ConstantAsMetadata *C =
457       ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
458   ConstantAsMetadata *C2 =
459       ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
460
461   Metadata *const V = C;
462   Metadata *const V2 = C2;
463   MDNode *n = MDNode::get(Context, V);
464   MDNode *n2 = MDNode::get(Context, V2);
465
466   Module M("MyModule", Context);
467   const char *Name = "llvm.NMD1";
468   NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
469   NMD->addOperand(n);
470   NMD->addOperand(n2);
471
472   std::string Str;
473   raw_string_ostream oss(Str);
474   NMD->print(oss);
475   EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
476                oss.str().c_str());
477 }
478 }