ecda5e8820e7eed57d65bb4a7862acbae953159f
[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, SelfReference) {
133   // !0 = !{!0}
134   // !1 = !{!0}
135   {
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));
142
143     // Self-references should be distinct, so MDNode::get() should grab a
144     // uniqued node that references Self, not Self.
145     Args[0] = 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);
150   }
151
152   // !0 = !{!0, !{}}
153   // !1 = !{!0, !{}}
154   {
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));
161
162     // Self-references should be distinct, so MDNode::get() should grab a
163     // uniqued node that references Self, not Self itself.
164     Args[0] = Self;
165     MDNode *Ref1 = MDNode::get(Context, Args);
166     MDNode *Ref2 = MDNode::get(Context, Args);
167     EXPECT_NE(Self, Ref1);
168     EXPECT_EQ(Ref1, Ref2);
169   }
170 }
171
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);
178
179   Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
180   MDNode *N = MDNode::get(Context, Args);
181
182   std::string Expected;
183   {
184     raw_string_ostream OS(Expected);
185     OS << "!{";
186     C->printAsOperand(OS);
187     OS << ", ";
188     S->printAsOperand(OS);
189     OS << ", null";
190     MDNode *Nodes[] = {N0, N1, N2};
191     for (auto *Node : Nodes)
192       OS << ", <" << (void *)Node << ">";
193     OS << "}\n";
194   }
195
196   std::string Actual;
197   {
198     raw_string_ostream OS(Actual);
199     N->print(OS);
200   }
201
202   EXPECT_EQ(Expected, Actual);
203 }
204
205 TEST_F(MDNodeTest, NullOperand) {
206   // metadata !{}
207   MDNode *Empty = MDNode::get(Context, None);
208
209   // metadata !{metadata !{}}
210   Metadata *Ops[] = {Empty};
211   MDNode *N = MDNode::get(Context, Ops);
212   ASSERT_EQ(Empty, N->getOperand(0));
213
214   // metadata !{metadata !{}} => metadata !{null}
215   N->replaceOperandWith(0, nullptr);
216   ASSERT_EQ(nullptr, N->getOperand(0));
217
218   // metadata !{null}
219   Ops[0] = nullptr;
220   MDNode *NullOp = MDNode::get(Context, Ops);
221   ASSERT_EQ(nullptr, NullOp->getOperand(0));
222   EXPECT_EQ(N, NullOp);
223 }
224
225 TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
226   // !{}
227   MDNode *Empty = MDNode::get(Context, None);
228   ASSERT_TRUE(Empty->isResolved());
229   EXPECT_FALSE(Empty->isDistinct());
230
231   // !{!{}}
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());
237
238   // !{!{!{}}}
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());
244
245   // !{!{!{}}} => !{!{}}
246   Wrapped2->replaceOperandWith(0, Empty);
247   ASSERT_EQ(Empty, Wrapped2->getOperand(0));
248   EXPECT_TRUE(Wrapped2->isDistinct());
249   EXPECT_FALSE(Wrapped1->isDistinct());
250 }
251
252 TEST_F(MDNodeTest, getDistinct) {
253   // !{}
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));
258
259   // distinct !{}
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);
267
268   // !{}
269   ASSERT_EQ(Empty, MDNode::get(Context, None));
270 }
271
272 TEST_F(MDNodeTest, TempIsDistinct) {
273   MDNode *T = MDNode::getTemporary(Context, None);
274   EXPECT_TRUE(T->isDistinct());
275   MDNode::deleteTemporary(T);
276 }
277
278 TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
279   // temporary !{}
280   MDNodeFwdDecl *Temp = MDNode::getTemporary(Context, None);
281   ASSERT_FALSE(Temp->isResolved());
282
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));
288
289   // temporary !{} => !{}
290   MDNode *Empty = MDNode::get(Context, None);
291   Temp->replaceAllUsesWith(Empty);
292   MDNode::deleteTemporary(Temp);
293   EXPECT_EQ(Empty, Distinct->getOperand(0));
294 }
295
296 TEST_F(MDNodeTest, handleChangedOperandRecursion) {
297   // !0 = !{}
298   MDNode *N0 = MDNode::get(Context, None);
299
300   // !1 = !{!3, null}
301   MDNodeFwdDecl *Temp3 = MDNode::getTemporary(Context, None);
302   Metadata *Ops1[] = {Temp3, nullptr};
303   MDNode *N1 = MDNode::get(Context, Ops1);
304
305   // !2 = !{!3, !0}
306   Metadata *Ops2[] = {Temp3, N0};
307   MDNode *N2 = MDNode::get(Context, Ops2);
308
309   // !3 = !{!2}
310   Metadata *Ops3[] = {N2};
311   MDNode *N3 = MDNode::get(Context, Ops3);
312   Temp3->replaceAllUsesWith(N3);
313
314   // !4 = !{!1}
315   Metadata *Ops4[] = {N1};
316   MDNode *N4 = MDNode::get(Context, Ops4);
317
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());
324
325   // Create a couple of distinct nodes to observe what's going on.
326   //
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);
333
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.
338   //
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);
345
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.
348   //
349   // !1 = !{!4, null}
350   // !4 = !{!1}
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));
357 }
358
359 typedef MetadataTest MetadataAsValueTest;
360
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());
366
367   auto *V2 = MetadataAsValue::get(Context, N);
368   EXPECT_EQ(V, V2);
369 }
370
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());
378
379   auto *V2 = MetadataAsValue::get(Context, N2);
380   EXPECT_EQ(V, V2);
381
382   auto *V3 = MetadataAsValue::get(Context, N);
383   EXPECT_TRUE(V3->getType()->isMetadataTy());
384   EXPECT_NE(V, V3);
385   EXPECT_EQ(N, V3->getMetadata());
386 }
387
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);
393
394   auto *V = MetadataAsValue::get(Context, MD);
395   EXPECT_TRUE(V->getType()->isMetadataTy());
396   EXPECT_EQ(MD, V->getMetadata());
397
398   auto *V2 = MetadataAsValue::get(Context, N);
399   EXPECT_EQ(MD, V2->getMetadata());
400   EXPECT_EQ(V, V2);
401 }
402
403 typedef MetadataTest ValueAsMetadataTest;
404
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());
412
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());
417 }
418
419 typedef MetadataTest TrackingMDRefTest;
420
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());
428
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());
433
434   // Reset it, so we don't inadvertently test deletion.
435   MD.reset();
436 }
437
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());
445
446   GV.reset();
447   EXPECT_TRUE(!MD);
448 }
449
450 TEST(NamedMDNodeTest, Search) {
451   LLVMContext Context;
452   ConstantAsMetadata *C =
453       ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
454   ConstantAsMetadata *C2 =
455       ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
456
457   Metadata *const V = C;
458   Metadata *const V2 = C2;
459   MDNode *n = MDNode::get(Context, V);
460   MDNode *n2 = MDNode::get(Context, V2);
461
462   Module M("MyModule", Context);
463   const char *Name = "llvm.NMD1";
464   NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
465   NMD->addOperand(n);
466   NMD->addOperand(n2);
467
468   std::string Str;
469   raw_string_ostream oss(Str);
470   NMD->print(oss);
471   EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
472                oss.str().c_str());
473 }
474 }