blob: 8282017ed0eb11b93f8aef81737647d015fbfad1 [file] [log] [blame]
Duncan P. N. Exon Smith71db6422015-02-02 18:20:15 +00001//===- unittests/IR/MetadataTest.cpp - Metadata unit tests ----------------===//
Nick Lewycky49f89192009-04-04 07:22:01 +00002//
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
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +000010#include "llvm/ADT/STLExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000011#include "llvm/IR/Constants.h"
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000012#include "llvm/IR/DebugInfoMetadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000013#include "llvm/IR/Instructions.h"
14#include "llvm/IR/LLVMContext.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000015#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Module.h"
17#include "llvm/IR/Type.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000018#include "llvm/Support/raw_ostream.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000019#include "gtest/gtest.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000020using namespace llvm;
21
22namespace {
23
Duncan P. N. Exon Smith2711ca72015-01-19 19:02:06 +000024TEST(ContextAndReplaceableUsesTest, FromContext) {
25 LLVMContext Context;
26 ContextAndReplaceableUses CRU(Context);
27 EXPECT_EQ(&Context, &CRU.getContext());
28 EXPECT_FALSE(CRU.hasReplaceableUses());
29 EXPECT_FALSE(CRU.getReplaceableUses());
30}
31
32TEST(ContextAndReplaceableUsesTest, FromReplaceableUses) {
33 LLVMContext Context;
34 ContextAndReplaceableUses CRU(make_unique<ReplaceableMetadataImpl>(Context));
35 EXPECT_EQ(&Context, &CRU.getContext());
36 EXPECT_TRUE(CRU.hasReplaceableUses());
37 EXPECT_TRUE(CRU.getReplaceableUses());
38}
39
40TEST(ContextAndReplaceableUsesTest, makeReplaceable) {
41 LLVMContext Context;
42 ContextAndReplaceableUses CRU(Context);
43 CRU.makeReplaceable(make_unique<ReplaceableMetadataImpl>(Context));
44 EXPECT_EQ(&Context, &CRU.getContext());
45 EXPECT_TRUE(CRU.hasReplaceableUses());
46 EXPECT_TRUE(CRU.getReplaceableUses());
47}
48
49TEST(ContextAndReplaceableUsesTest, takeReplaceableUses) {
50 LLVMContext Context;
51 auto ReplaceableUses = make_unique<ReplaceableMetadataImpl>(Context);
52 auto *Ptr = ReplaceableUses.get();
53 ContextAndReplaceableUses CRU(std::move(ReplaceableUses));
54 ReplaceableUses = CRU.takeReplaceableUses();
55 EXPECT_EQ(&Context, &CRU.getContext());
56 EXPECT_FALSE(CRU.hasReplaceableUses());
57 EXPECT_FALSE(CRU.getReplaceableUses());
58 EXPECT_EQ(Ptr, ReplaceableUses.get());
59}
60
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000061class MetadataTest : public testing::Test {
62protected:
63 LLVMContext Context;
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +000064 MDNode *getNode() { return MDNode::get(Context, None); }
65 MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); }
66 MDNode *getNode(Metadata *MD1, Metadata *MD2) {
67 Metadata *MDs[] = {MD1, MD2};
68 return MDNode::get(Context, MDs);
69 }
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000070};
71typedef MetadataTest MDStringTest;
Owen Anderson23587322009-07-31 21:38:10 +000072
Nick Lewycky49f89192009-04-04 07:22:01 +000073// Test that construction of MDString with different value produces different
74// MDString objects, even with the same string pointer and nulls in the string.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000075TEST_F(MDStringTest, CreateDifferent) {
Nick Lewycky49f89192009-04-04 07:22:01 +000076 char x[3] = { 'f', 0, 'A' };
Owen Anderson23587322009-07-31 21:38:10 +000077 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000078 x[2] = 'B';
Owen Anderson23587322009-07-31 21:38:10 +000079 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000080 EXPECT_NE(s1, s2);
81}
82
83// Test that creation of MDStrings with the same string contents produces the
84// same MDString object, even with different pointers.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000085TEST_F(MDStringTest, CreateSame) {
Nick Lewycky49f89192009-04-04 07:22:01 +000086 char x[4] = { 'a', 'b', 'c', 'X' };
87 char y[4] = { 'a', 'b', 'c', 'Y' };
88
Owen Anderson23587322009-07-31 21:38:10 +000089 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
90 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000091 EXPECT_EQ(s1, s2);
92}
93
94// Test that MDString prints out the string we fed it.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000095TEST_F(MDStringTest, PrintingSimple) {
Nick Lewycky49f89192009-04-04 07:22:01 +000096 char *str = new char[13];
97 strncpy(str, "testing 1 2 3", 13);
Owen Anderson23587322009-07-31 21:38:10 +000098 MDString *s = MDString::get(Context, StringRef(str, 13));
Nick Lewycky49f89192009-04-04 07:22:01 +000099 strncpy(str, "aaaaaaaaaaaaa", 13);
100 delete[] str;
101
Chris Lattnerbe354a62009-08-23 04:47:35 +0000102 std::string Str;
103 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000104 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000105 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000106}
107
108// Test printing of MDString with non-printable characters.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000109TEST_F(MDStringTest, PrintingComplex) {
Jeffrey Yasskin065c3572011-08-30 20:53:29 +0000110 char str[5] = {0, '\n', '"', '\\', (char)-1};
Owen Anderson23587322009-07-31 21:38:10 +0000111 MDString *s = MDString::get(Context, StringRef(str+0, 5));
Chris Lattnerbe354a62009-08-23 04:47:35 +0000112 std::string Str;
113 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000114 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000115 EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000116}
117
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000118typedef MetadataTest MDNodeTest;
119
Nick Lewycky49f89192009-04-04 07:22:01 +0000120// Test the two constructors, and containing other Constants.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000121TEST_F(MDNodeTest, Simple) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000122 char x[3] = { 'a', 'b', 'c' };
123 char y[3] = { '1', '2', '3' };
124
Owen Anderson23587322009-07-31 21:38:10 +0000125 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
126 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000127 ConstantAsMetadata *CI = ConstantAsMetadata::get(
128 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
Nick Lewycky49f89192009-04-04 07:22:01 +0000129
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000130 std::vector<Metadata *> V;
Nick Lewycky49f89192009-04-04 07:22:01 +0000131 V.push_back(s1);
132 V.push_back(CI);
133 V.push_back(s2);
134
Jay Foad5514afe2011-04-21 19:59:31 +0000135 MDNode *n1 = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000136 Metadata *const c1 = n1;
Jay Foad5514afe2011-04-21 19:59:31 +0000137 MDNode *n2 = MDNode::get(Context, c1);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000138 Metadata *const c2 = n2;
Jay Foad5514afe2011-04-21 19:59:31 +0000139 MDNode *n3 = MDNode::get(Context, V);
Duncan Sands26a80f32012-03-31 08:20:11 +0000140 MDNode *n4 = MDNode::getIfExists(Context, V);
141 MDNode *n5 = MDNode::getIfExists(Context, c1);
142 MDNode *n6 = MDNode::getIfExists(Context, c2);
Nick Lewycky49f89192009-04-04 07:22:01 +0000143 EXPECT_NE(n1, n2);
Devang Patelf7188322009-09-03 01:39:20 +0000144 EXPECT_EQ(n1, n3);
Duncan Sands26a80f32012-03-31 08:20:11 +0000145 EXPECT_EQ(n4, n1);
146 EXPECT_EQ(n5, n2);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000147 EXPECT_EQ(n6, (Metadata *)nullptr);
Nick Lewycky49f89192009-04-04 07:22:01 +0000148
Chris Lattner9b493022009-12-31 01:22:29 +0000149 EXPECT_EQ(3u, n1->getNumOperands());
150 EXPECT_EQ(s1, n1->getOperand(0));
151 EXPECT_EQ(CI, n1->getOperand(1));
152 EXPECT_EQ(s2, n1->getOperand(2));
Nick Lewycky49f89192009-04-04 07:22:01 +0000153
Chris Lattner9b493022009-12-31 01:22:29 +0000154 EXPECT_EQ(1u, n2->getNumOperands());
155 EXPECT_EQ(n1, n2->getOperand(0));
Nick Lewycky49f89192009-04-04 07:22:01 +0000156}
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000157
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000158TEST_F(MDNodeTest, Delete) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000159 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
160 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000161
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000162 Metadata *const V = LocalAsMetadata::get(I);
Jay Foad5514afe2011-04-21 19:59:31 +0000163 MDNode *n = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000164 TrackingMDRef wvh(n);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000165
166 EXPECT_EQ(n, wvh);
167
168 delete I;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000169}
Devang Patel0924b332009-07-30 00:03:41 +0000170
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000171TEST_F(MDNodeTest, SelfReference) {
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000172 // !0 = !{!0}
173 // !1 = !{!0}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000174 {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000175 auto Temp = MDNode::getTemporary(Context, None);
176 Metadata *Args[] = {Temp.get()};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000177 MDNode *Self = MDNode::get(Context, Args);
178 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000179 ASSERT_EQ(Self, Self->getOperand(0));
180
181 // Self-references should be distinct, so MDNode::get() should grab a
182 // uniqued node that references Self, not Self.
183 Args[0] = Self;
184 MDNode *Ref1 = MDNode::get(Context, Args);
185 MDNode *Ref2 = MDNode::get(Context, Args);
186 EXPECT_NE(Self, Ref1);
187 EXPECT_EQ(Ref1, Ref2);
188 }
189
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000190 // !0 = !{!0, !{}}
191 // !1 = !{!0, !{}}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000192 {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000193 auto Temp = MDNode::getTemporary(Context, None);
194 Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000195 MDNode *Self = MDNode::get(Context, Args);
196 Self->replaceOperandWith(0, Self);
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000197 ASSERT_EQ(Self, Self->getOperand(0));
198
199 // Self-references should be distinct, so MDNode::get() should grab a
200 // uniqued node that references Self, not Self itself.
201 Args[0] = Self;
202 MDNode *Ref1 = MDNode::get(Context, Args);
203 MDNode *Ref2 = MDNode::get(Context, Args);
204 EXPECT_NE(Self, Ref1);
205 EXPECT_EQ(Ref1, Ref2);
206 }
207}
208
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000209TEST_F(MDNodeTest, Print) {
210 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
211 MDString *S = MDString::get(Context, "foo");
212 MDNode *N0 = getNode();
213 MDNode *N1 = getNode(N0);
214 MDNode *N2 = getNode(N0, N1);
215
216 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
217 MDNode *N = MDNode::get(Context, Args);
218
219 std::string Expected;
220 {
221 raw_string_ostream OS(Expected);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000222 OS << "!{";
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000223 C->printAsOperand(OS);
224 OS << ", ";
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000225 S->printAsOperand(OS);
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000226 OS << ", null";
227 MDNode *Nodes[] = {N0, N1, N2};
228 for (auto *Node : Nodes)
229 OS << ", <" << (void *)Node << ">";
230 OS << "}\n";
231 }
232
233 std::string Actual;
234 {
235 raw_string_ostream OS(Actual);
236 N->print(OS);
237 }
238
239 EXPECT_EQ(Expected, Actual);
240}
241
Duncan P. N. Exon Smithbcd960a2015-01-05 23:31:54 +0000242TEST_F(MDNodeTest, NullOperand) {
243 // metadata !{}
244 MDNode *Empty = MDNode::get(Context, None);
245
246 // metadata !{metadata !{}}
247 Metadata *Ops[] = {Empty};
248 MDNode *N = MDNode::get(Context, Ops);
249 ASSERT_EQ(Empty, N->getOperand(0));
250
251 // metadata !{metadata !{}} => metadata !{null}
252 N->replaceOperandWith(0, nullptr);
253 ASSERT_EQ(nullptr, N->getOperand(0));
254
255 // metadata !{null}
256 Ops[0] = nullptr;
257 MDNode *NullOp = MDNode::get(Context, Ops);
258 ASSERT_EQ(nullptr, NullOp->getOperand(0));
259 EXPECT_EQ(N, NullOp);
260}
261
Duncan P. N. Exon Smith136ea3f2015-01-07 21:35:38 +0000262TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
263 // !{}
264 MDNode *Empty = MDNode::get(Context, None);
265 ASSERT_TRUE(Empty->isResolved());
266 EXPECT_FALSE(Empty->isDistinct());
267
268 // !{!{}}
269 Metadata *Wrapped1Ops[] = {Empty};
270 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
271 ASSERT_EQ(Empty, Wrapped1->getOperand(0));
272 ASSERT_TRUE(Wrapped1->isResolved());
273 EXPECT_FALSE(Wrapped1->isDistinct());
274
275 // !{!{!{}}}
276 Metadata *Wrapped2Ops[] = {Wrapped1};
277 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
278 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
279 ASSERT_TRUE(Wrapped2->isResolved());
280 EXPECT_FALSE(Wrapped2->isDistinct());
281
282 // !{!{!{}}} => !{!{}}
283 Wrapped2->replaceOperandWith(0, Empty);
284 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
285 EXPECT_TRUE(Wrapped2->isDistinct());
286 EXPECT_FALSE(Wrapped1->isDistinct());
287}
288
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000289TEST_F(MDNodeTest, getDistinct) {
290 // !{}
291 MDNode *Empty = MDNode::get(Context, None);
292 ASSERT_TRUE(Empty->isResolved());
293 ASSERT_FALSE(Empty->isDistinct());
294 ASSERT_EQ(Empty, MDNode::get(Context, None));
295
296 // distinct !{}
297 MDNode *Distinct1 = MDNode::getDistinct(Context, None);
298 MDNode *Distinct2 = MDNode::getDistinct(Context, None);
299 EXPECT_TRUE(Distinct1->isResolved());
300 EXPECT_TRUE(Distinct2->isDistinct());
301 EXPECT_NE(Empty, Distinct1);
302 EXPECT_NE(Empty, Distinct2);
303 EXPECT_NE(Distinct1, Distinct2);
304
305 // !{}
306 ASSERT_EQ(Empty, MDNode::get(Context, None));
307}
308
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000309TEST_F(MDNodeTest, isUniqued) {
310 MDNode *U = MDTuple::get(Context, None);
311 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000312 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000313 EXPECT_TRUE(U->isUniqued());
314 EXPECT_FALSE(D->isUniqued());
315 EXPECT_FALSE(T->isUniqued());
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000316}
317
318TEST_F(MDNodeTest, isDistinct) {
319 MDNode *U = MDTuple::get(Context, None);
320 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000321 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000322 EXPECT_FALSE(U->isDistinct());
323 EXPECT_TRUE(D->isDistinct());
324 EXPECT_FALSE(T->isDistinct());
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000325}
326
327TEST_F(MDNodeTest, isTemporary) {
328 MDNode *U = MDTuple::get(Context, None);
329 MDNode *D = MDTuple::getDistinct(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000330 auto T = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000331 EXPECT_FALSE(U->isTemporary());
332 EXPECT_FALSE(D->isTemporary());
333 EXPECT_TRUE(T->isTemporary());
Duncan P. N. Exon Smithd1474ee2015-01-12 18:41:26 +0000334}
335
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000336TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
337 // temporary !{}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000338 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000339 ASSERT_FALSE(Temp->isResolved());
340
341 // distinct !{temporary !{}}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000342 Metadata *Ops[] = {Temp.get()};
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000343 MDNode *Distinct = MDNode::getDistinct(Context, Ops);
344 EXPECT_TRUE(Distinct->isResolved());
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000345 EXPECT_EQ(Temp.get(), Distinct->getOperand(0));
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000346
347 // temporary !{} => !{}
348 MDNode *Empty = MDNode::get(Context, None);
349 Temp->replaceAllUsesWith(Empty);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000350 EXPECT_EQ(Empty, Distinct->getOperand(0));
351}
352
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000353TEST_F(MDNodeTest, handleChangedOperandRecursion) {
354 // !0 = !{}
355 MDNode *N0 = MDNode::get(Context, None);
356
357 // !1 = !{!3, null}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000358 auto Temp3 = MDTuple::getTemporary(Context, None);
359 Metadata *Ops1[] = {Temp3.get(), nullptr};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000360 MDNode *N1 = MDNode::get(Context, Ops1);
361
362 // !2 = !{!3, !0}
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000363 Metadata *Ops2[] = {Temp3.get(), N0};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000364 MDNode *N2 = MDNode::get(Context, Ops2);
365
366 // !3 = !{!2}
367 Metadata *Ops3[] = {N2};
368 MDNode *N3 = MDNode::get(Context, Ops3);
369 Temp3->replaceAllUsesWith(N3);
370
371 // !4 = !{!1}
372 Metadata *Ops4[] = {N1};
373 MDNode *N4 = MDNode::get(Context, Ops4);
374
375 // Confirm that the cycle prevented RAUW from getting dropped.
376 EXPECT_TRUE(N0->isResolved());
377 EXPECT_FALSE(N1->isResolved());
378 EXPECT_FALSE(N2->isResolved());
379 EXPECT_FALSE(N3->isResolved());
380 EXPECT_FALSE(N4->isResolved());
381
382 // Create a couple of distinct nodes to observe what's going on.
383 //
384 // !5 = distinct !{!2}
385 // !6 = distinct !{!3}
386 Metadata *Ops5[] = {N2};
387 MDNode *N5 = MDNode::getDistinct(Context, Ops5);
388 Metadata *Ops6[] = {N3};
389 MDNode *N6 = MDNode::getDistinct(Context, Ops6);
390
391 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
392 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2
393 // references !3, this can cause a re-entry of handleChangedOperand() when !3
394 // is not ready for it.
395 //
396 // !2->replaceOperandWith(1, nullptr)
397 // !2: !{!3, !0} => !{!3, null}
398 // !2->replaceAllUsesWith(!1)
399 // !3: !{!2] => !{!1}
400 // !3->replaceAllUsesWith(!4)
401 N2->replaceOperandWith(1, nullptr);
402
403 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
404 // under us. Just check that the other nodes are sane.
405 //
406 // !1 = !{!4, null}
407 // !4 = !{!1}
408 // !5 = distinct !{!1}
409 // !6 = distinct !{!4}
410 EXPECT_EQ(N4, N1->getOperand(0));
411 EXPECT_EQ(N1, N4->getOperand(0));
412 EXPECT_EQ(N1, N5->getOperand(0));
413 EXPECT_EQ(N4, N6->getOperand(0));
414}
415
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000416TEST_F(MDNodeTest, replaceResolvedOperand) {
417 // Check code for replacing one resolved operand with another. If doing this
418 // directly (via replaceOperandWith()) becomes illegal, change the operand to
419 // a global value that gets RAUW'ed.
420 //
421 // Use a temporary node to keep N from being resolved.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000422 auto Temp = MDTuple::getTemporary(Context, None);
423 Metadata *Ops[] = {nullptr, Temp.get()};
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000424
NAKAMURA Takumi2f8f0542015-01-13 08:13:46 +0000425 MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>());
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000426 MDNode *N = MDTuple::get(Context, Ops);
427 EXPECT_EQ(nullptr, N->getOperand(0));
428 ASSERT_FALSE(N->isResolved());
429
430 // Check code for replacing resolved nodes.
431 N->replaceOperandWith(0, Empty);
432 EXPECT_EQ(Empty, N->getOperand(0));
433
434 // Check code for adding another unresolved operand.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000435 N->replaceOperandWith(0, Temp.get());
436 EXPECT_EQ(Temp.get(), N->getOperand(0));
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000437
438 // Remove the references to Temp; required for teardown.
439 Temp->replaceAllUsesWith(nullptr);
440}
441
Duncan P. N. Exon Smithe3353092015-01-19 22:24:52 +0000442TEST_F(MDNodeTest, replaceWithUniqued) {
443 auto *Empty = MDTuple::get(Context, None);
444 MDTuple *FirstUniqued;
445 {
446 Metadata *Ops[] = {Empty};
447 auto Temp = MDTuple::getTemporary(Context, Ops);
448 EXPECT_TRUE(Temp->isTemporary());
449
450 // Don't expect a collision.
451 auto *Current = Temp.get();
452 FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp));
453 EXPECT_TRUE(FirstUniqued->isUniqued());
454 EXPECT_TRUE(FirstUniqued->isResolved());
455 EXPECT_EQ(Current, FirstUniqued);
456 }
457 {
458 Metadata *Ops[] = {Empty};
459 auto Temp = MDTuple::getTemporary(Context, Ops);
460 EXPECT_TRUE(Temp->isTemporary());
461
462 // Should collide with Uniqued above this time.
463 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
464 EXPECT_TRUE(Uniqued->isUniqued());
465 EXPECT_TRUE(Uniqued->isResolved());
466 EXPECT_EQ(FirstUniqued, Uniqued);
467 }
468 {
469 auto Unresolved = MDTuple::getTemporary(Context, None);
470 Metadata *Ops[] = {Unresolved.get()};
471 auto Temp = MDTuple::getTemporary(Context, Ops);
472 EXPECT_TRUE(Temp->isTemporary());
473
474 // Shouldn't be resolved.
475 auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
476 EXPECT_TRUE(Uniqued->isUniqued());
477 EXPECT_FALSE(Uniqued->isResolved());
478
479 // Should be a different node.
480 EXPECT_NE(FirstUniqued, Uniqued);
481
482 // Should resolve when we update its node (note: be careful to avoid a
483 // collision with any other nodes above).
484 Uniqued->replaceOperandWith(0, nullptr);
485 EXPECT_TRUE(Uniqued->isResolved());
486 }
487}
488
489TEST_F(MDNodeTest, replaceWithDistinct) {
490 {
491 auto *Empty = MDTuple::get(Context, None);
492 Metadata *Ops[] = {Empty};
493 auto Temp = MDTuple::getTemporary(Context, Ops);
494 EXPECT_TRUE(Temp->isTemporary());
495
496 // Don't expect a collision.
497 auto *Current = Temp.get();
498 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
499 EXPECT_TRUE(Distinct->isDistinct());
500 EXPECT_TRUE(Distinct->isResolved());
501 EXPECT_EQ(Current, Distinct);
502 }
503 {
504 auto Unresolved = MDTuple::getTemporary(Context, None);
505 Metadata *Ops[] = {Unresolved.get()};
506 auto Temp = MDTuple::getTemporary(Context, Ops);
507 EXPECT_TRUE(Temp->isTemporary());
508
509 // Don't expect a collision.
510 auto *Current = Temp.get();
511 auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
512 EXPECT_TRUE(Distinct->isDistinct());
513 EXPECT_TRUE(Distinct->isResolved());
514 EXPECT_EQ(Current, Distinct);
515
516 // Cleanup; required for teardown.
517 Unresolved->replaceAllUsesWith(nullptr);
518 }
519}
520
Duncan P. N. Exon Smith8d536972015-01-22 21:36:45 +0000521TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) {
522 TrackingMDRef Ref;
523 EXPECT_EQ(nullptr, Ref.get());
524 {
525 auto Temp = MDTuple::getTemporary(Context, None);
526 Ref.reset(Temp.get());
527 EXPECT_EQ(Temp.get(), Ref.get());
528 }
529 EXPECT_EQ(nullptr, Ref.get());
530}
531
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000532typedef MetadataTest MDLocationTest;
533
534TEST_F(MDLocationTest, Overflow) {
535 MDNode *N = MDNode::get(Context, None);
536 {
537 MDLocation *L = MDLocation::get(Context, 2, 7, N);
538 EXPECT_EQ(2u, L->getLine());
539 EXPECT_EQ(7u, L->getColumn());
540 }
541 unsigned U24 = 1u << 24;
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000542 unsigned U16 = 1u << 16;
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000543 {
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000544 MDLocation *L = MDLocation::get(Context, U24 - 1, U16 - 1, N);
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000545 EXPECT_EQ(U24 - 1, L->getLine());
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000546 EXPECT_EQ(U16 - 1, L->getColumn());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000547 }
548 {
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000549 MDLocation *L = MDLocation::get(Context, U24, U16, N);
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000550 EXPECT_EQ(0u, L->getLine());
551 EXPECT_EQ(0u, L->getColumn());
552 }
553 {
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000554 MDLocation *L = MDLocation::get(Context, U24 + 1, U16 + 1, N);
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000555 EXPECT_EQ(0u, L->getLine());
556 EXPECT_EQ(0u, L->getColumn());
557 }
558}
559
560TEST_F(MDLocationTest, getDistinct) {
561 MDNode *N = MDNode::get(Context, None);
562 MDLocation *L0 = MDLocation::getDistinct(Context, 2, 7, N);
563 EXPECT_TRUE(L0->isDistinct());
564 MDLocation *L1 = MDLocation::get(Context, 2, 7, N);
565 EXPECT_FALSE(L1->isDistinct());
566 EXPECT_EQ(L1, MDLocation::get(Context, 2, 7, N));
567}
568
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000569TEST_F(MDLocationTest, getTemporary) {
570 MDNode *N = MDNode::get(Context, None);
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000571 auto L = MDLocation::getTemporary(Context, 2, 7, N);
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000572 EXPECT_TRUE(L->isTemporary());
573 EXPECT_FALSE(L->isResolved());
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000574}
575
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000576typedef MetadataTest GenericDebugNodeTest;
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000577
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000578TEST_F(GenericDebugNodeTest, get) {
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000579 StringRef Header = "header";
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000580 auto *Empty = MDNode::get(Context, None);
581 Metadata *Ops1[] = {Empty};
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000582 auto *N = GenericDebugNode::get(Context, 15, Header, Ops1);
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000583 EXPECT_EQ(15u, N->getTag());
584 EXPECT_EQ(2u, N->getNumOperands());
585 EXPECT_EQ(Header, N->getHeader());
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000586 EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000587 EXPECT_EQ(1u, N->getNumDwarfOperands());
588 EXPECT_EQ(Empty, N->getDwarfOperand(0));
589 EXPECT_EQ(Empty, N->getOperand(1));
590 ASSERT_TRUE(N->isUniqued());
591
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000592 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000593
594 N->replaceOperandWith(1, nullptr);
595 EXPECT_EQ(15u, N->getTag());
596 EXPECT_EQ(Header, N->getHeader());
597 EXPECT_EQ(nullptr, N->getDwarfOperand(0));
598 ASSERT_TRUE(N->isUniqued());
599
600 Metadata *Ops2[] = {nullptr};
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000601 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops2));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000602
603 N->replaceDwarfOperandWith(0, Empty);
604 EXPECT_EQ(15u, N->getTag());
605 EXPECT_EQ(Header, N->getHeader());
606 EXPECT_EQ(Empty, N->getDwarfOperand(0));
607 ASSERT_TRUE(N->isUniqued());
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000608 EXPECT_EQ(N, GenericDebugNode::get(Context, 15, Header, Ops1));
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000609}
610
Duncan P. N. Exon Smithe8b5e492015-01-22 22:47:44 +0000611TEST_F(GenericDebugNodeTest, getEmptyHeader) {
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +0000612 // Canonicalize !"" to null.
Duncan P. N. Exon Smith68ab0232015-01-22 23:10:55 +0000613 auto *N = GenericDebugNode::get(Context, 15, StringRef(), None);
614 EXPECT_EQ(StringRef(), N->getHeader());
615 EXPECT_EQ(nullptr, N->getOperand(0));
Duncan P. N. Exon Smith2da09e42015-01-20 00:58:46 +0000616}
617
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000618typedef MetadataTest MetadataAsValueTest;
619
620TEST_F(MetadataAsValueTest, MDNode) {
621 MDNode *N = MDNode::get(Context, None);
622 auto *V = MetadataAsValue::get(Context, N);
623 EXPECT_TRUE(V->getType()->isMetadataTy());
624 EXPECT_EQ(N, V->getMetadata());
625
626 auto *V2 = MetadataAsValue::get(Context, N);
627 EXPECT_EQ(V, V2);
628}
629
630TEST_F(MetadataAsValueTest, MDNodeMDNode) {
631 MDNode *N = MDNode::get(Context, None);
632 Metadata *Ops[] = {N};
633 MDNode *N2 = MDNode::get(Context, Ops);
634 auto *V = MetadataAsValue::get(Context, N2);
635 EXPECT_TRUE(V->getType()->isMetadataTy());
636 EXPECT_EQ(N2, V->getMetadata());
637
638 auto *V2 = MetadataAsValue::get(Context, N2);
639 EXPECT_EQ(V, V2);
640
641 auto *V3 = MetadataAsValue::get(Context, N);
642 EXPECT_TRUE(V3->getType()->isMetadataTy());
643 EXPECT_NE(V, V3);
644 EXPECT_EQ(N, V3->getMetadata());
645}
646
647TEST_F(MetadataAsValueTest, MDNodeConstant) {
648 auto *C = ConstantInt::getTrue(Context);
649 auto *MD = ConstantAsMetadata::get(C);
650 Metadata *Ops[] = {MD};
651 auto *N = MDNode::get(Context, Ops);
652
653 auto *V = MetadataAsValue::get(Context, MD);
654 EXPECT_TRUE(V->getType()->isMetadataTy());
655 EXPECT_EQ(MD, V->getMetadata());
656
657 auto *V2 = MetadataAsValue::get(Context, N);
658 EXPECT_EQ(MD, V2->getMetadata());
659 EXPECT_EQ(V, V2);
660}
661
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +0000662typedef MetadataTest ValueAsMetadataTest;
663
664TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
665 Type *Ty = Type::getInt1PtrTy(Context);
666 std::unique_ptr<GlobalVariable> GV0(
667 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
668 auto *MD = ValueAsMetadata::get(GV0.get());
669 EXPECT_TRUE(MD->getValue() == GV0.get());
670 ASSERT_TRUE(GV0->use_empty());
671
672 std::unique_ptr<GlobalVariable> GV1(
673 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
674 GV0->replaceAllUsesWith(GV1.get());
675 EXPECT_TRUE(MD->getValue() == GV1.get());
676}
677
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +0000678TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
679 // Create a constant.
680 ConstantAsMetadata *CI = ConstantAsMetadata::get(
681 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
682
683 // Create a temporary to prevent nodes from resolving.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000684 auto Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +0000685
686 // When the first operand of N1 gets reset to nullptr, it'll collide with N2.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000687 Metadata *Ops1[] = {CI, CI, Temp.get()};
688 Metadata *Ops2[] = {nullptr, CI, Temp.get()};
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +0000689
690 auto *N1 = MDTuple::get(Context, Ops1);
691 auto *N2 = MDTuple::get(Context, Ops2);
692 ASSERT_NE(N1, N2);
693
694 // Tell metadata that the constant is getting deleted.
695 //
696 // After this, N1 will be invalid, so don't touch it.
697 ValueAsMetadata::handleDeletion(CI->getValue());
698 EXPECT_EQ(nullptr, N2->getOperand(0));
699 EXPECT_EQ(nullptr, N2->getOperand(1));
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000700 EXPECT_EQ(Temp.get(), N2->getOperand(2));
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +0000701
702 // Clean up Temp for teardown.
703 Temp->replaceAllUsesWith(nullptr);
704}
705
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +0000706typedef MetadataTest TrackingMDRefTest;
707
708TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
709 Type *Ty = Type::getInt1PtrTy(Context);
710 std::unique_ptr<GlobalVariable> GV0(
711 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
712 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
713 EXPECT_TRUE(MD->getValue() == GV0.get());
714 ASSERT_TRUE(GV0->use_empty());
715
716 std::unique_ptr<GlobalVariable> GV1(
717 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
718 GV0->replaceAllUsesWith(GV1.get());
719 EXPECT_TRUE(MD->getValue() == GV1.get());
720
721 // Reset it, so we don't inadvertently test deletion.
722 MD.reset();
723}
724
725TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
726 Type *Ty = Type::getInt1PtrTy(Context);
727 std::unique_ptr<GlobalVariable> GV(
728 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
729 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
730 EXPECT_TRUE(MD->getValue() == GV.get());
731 ASSERT_TRUE(GV->use_empty());
732
733 GV.reset();
734 EXPECT_TRUE(!MD);
735}
736
Devang Patel0924b332009-07-30 00:03:41 +0000737TEST(NamedMDNodeTest, Search) {
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000738 LLVMContext Context;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000739 ConstantAsMetadata *C =
740 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
741 ConstantAsMetadata *C2 =
742 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
Devang Patel0924b332009-07-30 00:03:41 +0000743
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000744 Metadata *const V = C;
745 Metadata *const V2 = C2;
Jay Foad5514afe2011-04-21 19:59:31 +0000746 MDNode *n = MDNode::get(Context, V);
747 MDNode *n2 = MDNode::get(Context, V2);
Devang Patel0924b332009-07-30 00:03:41 +0000748
Jeffrey Yasskin3d73d1a2010-03-13 01:39:20 +0000749 Module M("MyModule", Context);
Devang Patel0924b332009-07-30 00:03:41 +0000750 const char *Name = "llvm.NMD1";
Dan Gohman2637cc12010-07-21 23:38:33 +0000751 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
752 NMD->addOperand(n);
753 NMD->addOperand(n2);
754
Chris Lattnerbe354a62009-08-23 04:47:35 +0000755 std::string Str;
756 raw_string_ostream oss(Str);
Devang Patel0924b332009-07-30 00:03:41 +0000757 NMD->print(oss);
Chris Lattner1e6e3672009-12-31 02:12:13 +0000758 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
Devang Patel0924b332009-07-30 00:03:41 +0000759 oss.str().c_str());
760}
Nick Lewycky49f89192009-04-04 07:22:01 +0000761}