blob: 498728e3cacc8a002a77b90fe4a4635a2c98a56a [file] [log] [blame]
Chandler Carruth74b6a772013-01-07 15:35:46 +00001//===- llvm/unittest/IR/Metadata.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"
12#include "llvm/IR/Instructions.h"
13#include "llvm/IR/LLVMContext.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000014#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Module.h"
16#include "llvm/IR/Type.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000017#include "llvm/Support/raw_ostream.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000018#include "gtest/gtest.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000019using namespace llvm;
20
21namespace {
22
Duncan P. N. Exon Smith2711ca72015-01-19 19:02:06 +000023TEST(ContextAndReplaceableUsesTest, FromContext) {
24 LLVMContext Context;
25 ContextAndReplaceableUses CRU(Context);
26 EXPECT_EQ(&Context, &CRU.getContext());
27 EXPECT_FALSE(CRU.hasReplaceableUses());
28 EXPECT_FALSE(CRU.getReplaceableUses());
29}
30
31TEST(ContextAndReplaceableUsesTest, FromReplaceableUses) {
32 LLVMContext Context;
33 ContextAndReplaceableUses CRU(make_unique<ReplaceableMetadataImpl>(Context));
34 EXPECT_EQ(&Context, &CRU.getContext());
35 EXPECT_TRUE(CRU.hasReplaceableUses());
36 EXPECT_TRUE(CRU.getReplaceableUses());
37}
38
39TEST(ContextAndReplaceableUsesTest, makeReplaceable) {
40 LLVMContext Context;
41 ContextAndReplaceableUses CRU(Context);
42 CRU.makeReplaceable(make_unique<ReplaceableMetadataImpl>(Context));
43 EXPECT_EQ(&Context, &CRU.getContext());
44 EXPECT_TRUE(CRU.hasReplaceableUses());
45 EXPECT_TRUE(CRU.getReplaceableUses());
46}
47
48TEST(ContextAndReplaceableUsesTest, takeReplaceableUses) {
49 LLVMContext Context;
50 auto ReplaceableUses = make_unique<ReplaceableMetadataImpl>(Context);
51 auto *Ptr = ReplaceableUses.get();
52 ContextAndReplaceableUses CRU(std::move(ReplaceableUses));
53 ReplaceableUses = CRU.takeReplaceableUses();
54 EXPECT_EQ(&Context, &CRU.getContext());
55 EXPECT_FALSE(CRU.hasReplaceableUses());
56 EXPECT_FALSE(CRU.getReplaceableUses());
57 EXPECT_EQ(Ptr, ReplaceableUses.get());
58}
59
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000060class MetadataTest : public testing::Test {
61protected:
62 LLVMContext Context;
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +000063 MDNode *getNode() { return MDNode::get(Context, None); }
64 MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); }
65 MDNode *getNode(Metadata *MD1, Metadata *MD2) {
66 Metadata *MDs[] = {MD1, MD2};
67 return MDNode::get(Context, MDs);
68 }
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000069};
70typedef MetadataTest MDStringTest;
Owen Anderson23587322009-07-31 21:38:10 +000071
Nick Lewycky49f89192009-04-04 07:22:01 +000072// Test that construction of MDString with different value produces different
73// MDString objects, even with the same string pointer and nulls in the string.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000074TEST_F(MDStringTest, CreateDifferent) {
Nick Lewycky49f89192009-04-04 07:22:01 +000075 char x[3] = { 'f', 0, 'A' };
Owen Anderson23587322009-07-31 21:38:10 +000076 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000077 x[2] = 'B';
Owen Anderson23587322009-07-31 21:38:10 +000078 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000079 EXPECT_NE(s1, s2);
80}
81
82// Test that creation of MDStrings with the same string contents produces the
83// same MDString object, even with different pointers.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000084TEST_F(MDStringTest, CreateSame) {
Nick Lewycky49f89192009-04-04 07:22:01 +000085 char x[4] = { 'a', 'b', 'c', 'X' };
86 char y[4] = { 'a', 'b', 'c', 'Y' };
87
Owen Anderson23587322009-07-31 21:38:10 +000088 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
89 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Nick Lewycky49f89192009-04-04 07:22:01 +000090 EXPECT_EQ(s1, s2);
91}
92
93// Test that MDString prints out the string we fed it.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +000094TEST_F(MDStringTest, PrintingSimple) {
Nick Lewycky49f89192009-04-04 07:22:01 +000095 char *str = new char[13];
96 strncpy(str, "testing 1 2 3", 13);
Owen Anderson23587322009-07-31 21:38:10 +000097 MDString *s = MDString::get(Context, StringRef(str, 13));
Nick Lewycky49f89192009-04-04 07:22:01 +000098 strncpy(str, "aaaaaaaaaaaaa", 13);
99 delete[] str;
100
Chris Lattnerbe354a62009-08-23 04:47:35 +0000101 std::string Str;
102 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000103 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000104 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000105}
106
107// Test printing of MDString with non-printable characters.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000108TEST_F(MDStringTest, PrintingComplex) {
Jeffrey Yasskin065c3572011-08-30 20:53:29 +0000109 char str[5] = {0, '\n', '"', '\\', (char)-1};
Owen Anderson23587322009-07-31 21:38:10 +0000110 MDString *s = MDString::get(Context, StringRef(str+0, 5));
Chris Lattnerbe354a62009-08-23 04:47:35 +0000111 std::string Str;
112 raw_string_ostream oss(Str);
Nick Lewycky49f89192009-04-04 07:22:01 +0000113 s->print(oss);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000114 EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +0000115}
116
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000117typedef MetadataTest MDNodeTest;
118
Nick Lewycky49f89192009-04-04 07:22:01 +0000119// Test the two constructors, and containing other Constants.
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000120TEST_F(MDNodeTest, Simple) {
Nick Lewycky49f89192009-04-04 07:22:01 +0000121 char x[3] = { 'a', 'b', 'c' };
122 char y[3] = { '1', '2', '3' };
123
Owen Anderson23587322009-07-31 21:38:10 +0000124 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
125 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000126 ConstantAsMetadata *CI = ConstantAsMetadata::get(
127 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
Nick Lewycky49f89192009-04-04 07:22:01 +0000128
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000129 std::vector<Metadata *> V;
Nick Lewycky49f89192009-04-04 07:22:01 +0000130 V.push_back(s1);
131 V.push_back(CI);
132 V.push_back(s2);
133
Jay Foad5514afe2011-04-21 19:59:31 +0000134 MDNode *n1 = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000135 Metadata *const c1 = n1;
Jay Foad5514afe2011-04-21 19:59:31 +0000136 MDNode *n2 = MDNode::get(Context, c1);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000137 Metadata *const c2 = n2;
Jay Foad5514afe2011-04-21 19:59:31 +0000138 MDNode *n3 = MDNode::get(Context, V);
Duncan Sands26a80f32012-03-31 08:20:11 +0000139 MDNode *n4 = MDNode::getIfExists(Context, V);
140 MDNode *n5 = MDNode::getIfExists(Context, c1);
141 MDNode *n6 = MDNode::getIfExists(Context, c2);
Nick Lewycky49f89192009-04-04 07:22:01 +0000142 EXPECT_NE(n1, n2);
Devang Patelf7188322009-09-03 01:39:20 +0000143 EXPECT_EQ(n1, n3);
Duncan Sands26a80f32012-03-31 08:20:11 +0000144 EXPECT_EQ(n4, n1);
145 EXPECT_EQ(n5, n2);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000146 EXPECT_EQ(n6, (Metadata *)nullptr);
Nick Lewycky49f89192009-04-04 07:22:01 +0000147
Chris Lattner9b493022009-12-31 01:22:29 +0000148 EXPECT_EQ(3u, n1->getNumOperands());
149 EXPECT_EQ(s1, n1->getOperand(0));
150 EXPECT_EQ(CI, n1->getOperand(1));
151 EXPECT_EQ(s2, n1->getOperand(2));
Nick Lewycky49f89192009-04-04 07:22:01 +0000152
Chris Lattner9b493022009-12-31 01:22:29 +0000153 EXPECT_EQ(1u, n2->getNumOperands());
154 EXPECT_EQ(n1, n2->getOperand(0));
Nick Lewycky49f89192009-04-04 07:22:01 +0000155}
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000156
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000157TEST_F(MDNodeTest, Delete) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000158 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
159 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000160
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000161 Metadata *const V = LocalAsMetadata::get(I);
Jay Foad5514afe2011-04-21 19:59:31 +0000162 MDNode *n = MDNode::get(Context, V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000163 TrackingMDRef wvh(n);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000164
165 EXPECT_EQ(n, wvh);
166
167 delete I;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000168}
Devang Patel0924b332009-07-30 00:03:41 +0000169
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000170TEST_F(MDNodeTest, SelfReference) {
Duncan P. N. Exon Smith8c662732014-12-16 07:45:05 +0000171 // !0 = !{!0}
172 // !1 = !{!0}
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000173 {
174 MDNode *Temp = MDNode::getTemporary(Context, None);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000175 Metadata *Args[] = {Temp};
Duncan P. N. Exon Smithac8ee282014-12-07 19:52:06 +0000176 MDNode *Self = MDNode::get(Context, Args);
177 Self->replaceOperandWith(0, Self);
178 MDNode::deleteTemporary(Temp);
179 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 {
193 MDNode *Temp = MDNode::getTemporary(Context, None);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000194 Metadata *Args[] = {Temp, 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);
197 MDNode::deleteTemporary(Temp);
198 ASSERT_EQ(Self, Self->getOperand(0));
199
200 // Self-references should be distinct, so MDNode::get() should grab a
201 // uniqued node that references Self, not Self itself.
202 Args[0] = Self;
203 MDNode *Ref1 = MDNode::get(Context, Args);
204 MDNode *Ref2 = MDNode::get(Context, Args);
205 EXPECT_NE(Self, Ref1);
206 EXPECT_EQ(Ref1, Ref2);
207 }
208}
209
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000210TEST_F(MDNodeTest, Print) {
211 Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
212 MDString *S = MDString::get(Context, "foo");
213 MDNode *N0 = getNode();
214 MDNode *N1 = getNode(N0);
215 MDNode *N2 = getNode(N0, N1);
216
217 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
218 MDNode *N = MDNode::get(Context, Args);
219
220 std::string Expected;
221 {
222 raw_string_ostream OS(Expected);
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000223 OS << "!{";
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000224 C->printAsOperand(OS);
225 OS << ", ";
Duncan P. N. Exon Smithbb7d2fb2014-12-16 07:40:31 +0000226 S->printAsOperand(OS);
Duncan P. N. Exon Smithfee167f2014-12-16 07:09:37 +0000227 OS << ", null";
228 MDNode *Nodes[] = {N0, N1, N2};
229 for (auto *Node : Nodes)
230 OS << ", <" << (void *)Node << ">";
231 OS << "}\n";
232 }
233
234 std::string Actual;
235 {
236 raw_string_ostream OS(Actual);
237 N->print(OS);
238 }
239
240 EXPECT_EQ(Expected, Actual);
241}
242
Duncan P. N. Exon Smithbcd960a2015-01-05 23:31:54 +0000243TEST_F(MDNodeTest, NullOperand) {
244 // metadata !{}
245 MDNode *Empty = MDNode::get(Context, None);
246
247 // metadata !{metadata !{}}
248 Metadata *Ops[] = {Empty};
249 MDNode *N = MDNode::get(Context, Ops);
250 ASSERT_EQ(Empty, N->getOperand(0));
251
252 // metadata !{metadata !{}} => metadata !{null}
253 N->replaceOperandWith(0, nullptr);
254 ASSERT_EQ(nullptr, N->getOperand(0));
255
256 // metadata !{null}
257 Ops[0] = nullptr;
258 MDNode *NullOp = MDNode::get(Context, Ops);
259 ASSERT_EQ(nullptr, NullOp->getOperand(0));
260 EXPECT_EQ(N, NullOp);
261}
262
Duncan P. N. Exon Smith136ea3f2015-01-07 21:35:38 +0000263TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
264 // !{}
265 MDNode *Empty = MDNode::get(Context, None);
266 ASSERT_TRUE(Empty->isResolved());
267 EXPECT_FALSE(Empty->isDistinct());
268
269 // !{!{}}
270 Metadata *Wrapped1Ops[] = {Empty};
271 MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
272 ASSERT_EQ(Empty, Wrapped1->getOperand(0));
273 ASSERT_TRUE(Wrapped1->isResolved());
274 EXPECT_FALSE(Wrapped1->isDistinct());
275
276 // !{!{!{}}}
277 Metadata *Wrapped2Ops[] = {Wrapped1};
278 MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
279 ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
280 ASSERT_TRUE(Wrapped2->isResolved());
281 EXPECT_FALSE(Wrapped2->isDistinct());
282
283 // !{!{!{}}} => !{!{}}
284 Wrapped2->replaceOperandWith(0, Empty);
285 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
286 EXPECT_TRUE(Wrapped2->isDistinct());
287 EXPECT_FALSE(Wrapped1->isDistinct());
288}
289
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000290TEST_F(MDNodeTest, getDistinct) {
291 // !{}
292 MDNode *Empty = MDNode::get(Context, None);
293 ASSERT_TRUE(Empty->isResolved());
294 ASSERT_FALSE(Empty->isDistinct());
295 ASSERT_EQ(Empty, MDNode::get(Context, None));
296
297 // distinct !{}
298 MDNode *Distinct1 = MDNode::getDistinct(Context, None);
299 MDNode *Distinct2 = MDNode::getDistinct(Context, None);
300 EXPECT_TRUE(Distinct1->isResolved());
301 EXPECT_TRUE(Distinct2->isDistinct());
302 EXPECT_NE(Empty, Distinct1);
303 EXPECT_NE(Empty, Distinct2);
304 EXPECT_NE(Distinct1, Distinct2);
305
306 // !{}
307 ASSERT_EQ(Empty, MDNode::get(Context, None));
308}
309
Duncan P. N. Exon Smithde03a8b2015-01-19 18:45:35 +0000310TEST_F(MDNodeTest, isUniqued) {
311 MDNode *U = MDTuple::get(Context, None);
312 MDNode *D = MDTuple::getDistinct(Context, None);
313 MDNode *T = MDTuple::getTemporary(Context, None);
314 EXPECT_TRUE(U->isUniqued());
315 EXPECT_FALSE(D->isUniqued());
316 EXPECT_FALSE(T->isUniqued());
317 MDNode::deleteTemporary(T);
318}
319
320TEST_F(MDNodeTest, isDistinct) {
321 MDNode *U = MDTuple::get(Context, None);
322 MDNode *D = MDTuple::getDistinct(Context, None);
323 MDNode *T = MDTuple::getTemporary(Context, None);
324 EXPECT_FALSE(U->isDistinct());
325 EXPECT_TRUE(D->isDistinct());
326 EXPECT_FALSE(T->isDistinct());
327 MDNode::deleteTemporary(T);
328}
329
330TEST_F(MDNodeTest, isTemporary) {
331 MDNode *U = MDTuple::get(Context, None);
332 MDNode *D = MDTuple::getDistinct(Context, None);
333 MDNode *T = MDTuple::getTemporary(Context, None);
334 EXPECT_FALSE(U->isTemporary());
335 EXPECT_FALSE(D->isTemporary());
336 EXPECT_TRUE(T->isTemporary());
Duncan P. N. Exon Smithd1474ee2015-01-12 18:41:26 +0000337 MDNode::deleteTemporary(T);
338}
339
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000340TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
341 // temporary !{}
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +0000342 MDTuple *Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith5e5b8502015-01-07 22:24:46 +0000343 ASSERT_FALSE(Temp->isResolved());
344
345 // distinct !{temporary !{}}
346 Metadata *Ops[] = {Temp};
347 MDNode *Distinct = MDNode::getDistinct(Context, Ops);
348 EXPECT_TRUE(Distinct->isResolved());
349 EXPECT_EQ(Temp, Distinct->getOperand(0));
350
351 // temporary !{} => !{}
352 MDNode *Empty = MDNode::get(Context, None);
353 Temp->replaceAllUsesWith(Empty);
354 MDNode::deleteTemporary(Temp);
355 EXPECT_EQ(Empty, Distinct->getOperand(0));
356}
357
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000358TEST_F(MDNodeTest, handleChangedOperandRecursion) {
359 // !0 = !{}
360 MDNode *N0 = MDNode::get(Context, None);
361
362 // !1 = !{!3, null}
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +0000363 MDTuple *Temp3 = MDTuple::getTemporary(Context, None);
364 Metadata *Ops1[] = {Temp3, nullptr};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000365 MDNode *N1 = MDNode::get(Context, Ops1);
366
367 // !2 = !{!3, !0}
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +0000368 Metadata *Ops2[] = {Temp3, N0};
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000369 MDNode *N2 = MDNode::get(Context, Ops2);
370
371 // !3 = !{!2}
372 Metadata *Ops3[] = {N2};
373 MDNode *N3 = MDNode::get(Context, Ops3);
374 Temp3->replaceAllUsesWith(N3);
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +0000375 MDNode::deleteTemporary(Temp3);
Duncan P. N. Exon Smith5f461892015-01-12 19:22:04 +0000376
377 // !4 = !{!1}
378 Metadata *Ops4[] = {N1};
379 MDNode *N4 = MDNode::get(Context, Ops4);
380
381 // Confirm that the cycle prevented RAUW from getting dropped.
382 EXPECT_TRUE(N0->isResolved());
383 EXPECT_FALSE(N1->isResolved());
384 EXPECT_FALSE(N2->isResolved());
385 EXPECT_FALSE(N3->isResolved());
386 EXPECT_FALSE(N4->isResolved());
387
388 // Create a couple of distinct nodes to observe what's going on.
389 //
390 // !5 = distinct !{!2}
391 // !6 = distinct !{!3}
392 Metadata *Ops5[] = {N2};
393 MDNode *N5 = MDNode::getDistinct(Context, Ops5);
394 Metadata *Ops6[] = {N3};
395 MDNode *N6 = MDNode::getDistinct(Context, Ops6);
396
397 // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
398 // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2
399 // references !3, this can cause a re-entry of handleChangedOperand() when !3
400 // is not ready for it.
401 //
402 // !2->replaceOperandWith(1, nullptr)
403 // !2: !{!3, !0} => !{!3, null}
404 // !2->replaceAllUsesWith(!1)
405 // !3: !{!2] => !{!1}
406 // !3->replaceAllUsesWith(!4)
407 N2->replaceOperandWith(1, nullptr);
408
409 // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
410 // under us. Just check that the other nodes are sane.
411 //
412 // !1 = !{!4, null}
413 // !4 = !{!1}
414 // !5 = distinct !{!1}
415 // !6 = distinct !{!4}
416 EXPECT_EQ(N4, N1->getOperand(0));
417 EXPECT_EQ(N1, N4->getOperand(0));
418 EXPECT_EQ(N1, N5->getOperand(0));
419 EXPECT_EQ(N4, N6->getOperand(0));
420}
421
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000422TEST_F(MDNodeTest, replaceResolvedOperand) {
423 // Check code for replacing one resolved operand with another. If doing this
424 // directly (via replaceOperandWith()) becomes illegal, change the operand to
425 // a global value that gets RAUW'ed.
426 //
427 // Use a temporary node to keep N from being resolved.
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +0000428 MDTuple *Temp = MDTuple::getTemporary(Context, None);
429 Metadata *Ops[] = {nullptr, Temp};
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000430
NAKAMURA Takumi2f8f0542015-01-13 08:13:46 +0000431 MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>());
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000432 MDNode *N = MDTuple::get(Context, Ops);
433 EXPECT_EQ(nullptr, N->getOperand(0));
434 ASSERT_FALSE(N->isResolved());
435
436 // Check code for replacing resolved nodes.
437 N->replaceOperandWith(0, Empty);
438 EXPECT_EQ(Empty, N->getOperand(0));
439
440 // Check code for adding another unresolved operand.
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +0000441 N->replaceOperandWith(0, Temp);
442 EXPECT_EQ(Temp, N->getOperand(0));
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000443
444 // Remove the references to Temp; required for teardown.
445 Temp->replaceAllUsesWith(nullptr);
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +0000446 MDNode::deleteTemporary(Temp);
Duncan P. N. Exon Smith845755c42015-01-13 00:46:34 +0000447}
448
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000449typedef MetadataTest MDLocationTest;
450
451TEST_F(MDLocationTest, Overflow) {
452 MDNode *N = MDNode::get(Context, None);
453 {
454 MDLocation *L = MDLocation::get(Context, 2, 7, N);
455 EXPECT_EQ(2u, L->getLine());
456 EXPECT_EQ(7u, L->getColumn());
457 }
458 unsigned U24 = 1u << 24;
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000459 unsigned U16 = 1u << 16;
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000460 {
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000461 MDLocation *L = MDLocation::get(Context, U24 - 1, U16 - 1, N);
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000462 EXPECT_EQ(U24 - 1, L->getLine());
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000463 EXPECT_EQ(U16 - 1, L->getColumn());
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000464 }
465 {
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000466 MDLocation *L = MDLocation::get(Context, U24, U16, N);
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000467 EXPECT_EQ(0u, L->getLine());
468 EXPECT_EQ(0u, L->getColumn());
469 }
470 {
Duncan P. N. Exon Smith2f5bb312015-01-16 17:33:08 +0000471 MDLocation *L = MDLocation::get(Context, U24 + 1, U16 + 1, N);
Duncan P. N. Exon Smithde03ff52015-01-13 20:44:56 +0000472 EXPECT_EQ(0u, L->getLine());
473 EXPECT_EQ(0u, L->getColumn());
474 }
475}
476
477TEST_F(MDLocationTest, getDistinct) {
478 MDNode *N = MDNode::get(Context, None);
479 MDLocation *L0 = MDLocation::getDistinct(Context, 2, 7, N);
480 EXPECT_TRUE(L0->isDistinct());
481 MDLocation *L1 = MDLocation::get(Context, 2, 7, N);
482 EXPECT_FALSE(L1->isDistinct());
483 EXPECT_EQ(L1, MDLocation::get(Context, 2, 7, N));
484}
485
Duncan P. N. Exon Smith799e56a2015-01-19 20:37:44 +0000486TEST_F(MDLocationTest, getTemporary) {
487 MDNode *N = MDNode::get(Context, None);
488 MDLocation *L = MDLocation::getTemporary(Context, 2, 7, N);
489 EXPECT_TRUE(L->isTemporary());
490 EXPECT_FALSE(L->isResolved());
491 MDNode::deleteTemporary(L);
492}
493
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000494typedef MetadataTest MetadataAsValueTest;
495
496TEST_F(MetadataAsValueTest, MDNode) {
497 MDNode *N = MDNode::get(Context, None);
498 auto *V = MetadataAsValue::get(Context, N);
499 EXPECT_TRUE(V->getType()->isMetadataTy());
500 EXPECT_EQ(N, V->getMetadata());
501
502 auto *V2 = MetadataAsValue::get(Context, N);
503 EXPECT_EQ(V, V2);
504}
505
506TEST_F(MetadataAsValueTest, MDNodeMDNode) {
507 MDNode *N = MDNode::get(Context, None);
508 Metadata *Ops[] = {N};
509 MDNode *N2 = MDNode::get(Context, Ops);
510 auto *V = MetadataAsValue::get(Context, N2);
511 EXPECT_TRUE(V->getType()->isMetadataTy());
512 EXPECT_EQ(N2, V->getMetadata());
513
514 auto *V2 = MetadataAsValue::get(Context, N2);
515 EXPECT_EQ(V, V2);
516
517 auto *V3 = MetadataAsValue::get(Context, N);
518 EXPECT_TRUE(V3->getType()->isMetadataTy());
519 EXPECT_NE(V, V3);
520 EXPECT_EQ(N, V3->getMetadata());
521}
522
523TEST_F(MetadataAsValueTest, MDNodeConstant) {
524 auto *C = ConstantInt::getTrue(Context);
525 auto *MD = ConstantAsMetadata::get(C);
526 Metadata *Ops[] = {MD};
527 auto *N = MDNode::get(Context, Ops);
528
529 auto *V = MetadataAsValue::get(Context, MD);
530 EXPECT_TRUE(V->getType()->isMetadataTy());
531 EXPECT_EQ(MD, V->getMetadata());
532
533 auto *V2 = MetadataAsValue::get(Context, N);
534 EXPECT_EQ(MD, V2->getMetadata());
535 EXPECT_EQ(V, V2);
536}
537
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +0000538typedef MetadataTest ValueAsMetadataTest;
539
540TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
541 Type *Ty = Type::getInt1PtrTy(Context);
542 std::unique_ptr<GlobalVariable> GV0(
543 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
544 auto *MD = ValueAsMetadata::get(GV0.get());
545 EXPECT_TRUE(MD->getValue() == GV0.get());
546 ASSERT_TRUE(GV0->use_empty());
547
548 std::unique_ptr<GlobalVariable> GV1(
549 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
550 GV0->replaceAllUsesWith(GV1.get());
551 EXPECT_TRUE(MD->getValue() == GV1.get());
552}
553
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +0000554TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
555 // Create a constant.
556 ConstantAsMetadata *CI = ConstantAsMetadata::get(
557 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
558
559 // Create a temporary to prevent nodes from resolving.
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +0000560 MDTuple *Temp = MDTuple::getTemporary(Context, None);
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +0000561
562 // When the first operand of N1 gets reset to nullptr, it'll collide with N2.
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +0000563 Metadata *Ops1[] = {CI, CI, Temp};
564 Metadata *Ops2[] = {nullptr, CI, Temp};
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +0000565
566 auto *N1 = MDTuple::get(Context, Ops1);
567 auto *N2 = MDTuple::get(Context, Ops2);
568 ASSERT_NE(N1, N2);
569
570 // Tell metadata that the constant is getting deleted.
571 //
572 // After this, N1 will be invalid, so don't touch it.
573 ValueAsMetadata::handleDeletion(CI->getValue());
574 EXPECT_EQ(nullptr, N2->getOperand(0));
575 EXPECT_EQ(nullptr, N2->getOperand(1));
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +0000576 EXPECT_EQ(Temp, N2->getOperand(2));
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +0000577
578 // Clean up Temp for teardown.
579 Temp->replaceAllUsesWith(nullptr);
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +0000580 MDNode::deleteTemporary(Temp);
Duncan P. N. Exon Smith4a4f7852015-01-14 19:56:10 +0000581}
582
Duncan P. N. Exon Smith121eeff2014-12-12 19:24:33 +0000583typedef MetadataTest TrackingMDRefTest;
584
585TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
586 Type *Ty = Type::getInt1PtrTy(Context);
587 std::unique_ptr<GlobalVariable> GV0(
588 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
589 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
590 EXPECT_TRUE(MD->getValue() == GV0.get());
591 ASSERT_TRUE(GV0->use_empty());
592
593 std::unique_ptr<GlobalVariable> GV1(
594 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
595 GV0->replaceAllUsesWith(GV1.get());
596 EXPECT_TRUE(MD->getValue() == GV1.get());
597
598 // Reset it, so we don't inadvertently test deletion.
599 MD.reset();
600}
601
602TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
603 Type *Ty = Type::getInt1PtrTy(Context);
604 std::unique_ptr<GlobalVariable> GV(
605 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
606 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
607 EXPECT_TRUE(MD->getValue() == GV.get());
608 ASSERT_TRUE(GV->use_empty());
609
610 GV.reset();
611 EXPECT_TRUE(!MD);
612}
613
Devang Patel0924b332009-07-30 00:03:41 +0000614TEST(NamedMDNodeTest, Search) {
Jeffrey Yasskinbd8a7592010-03-04 23:24:19 +0000615 LLVMContext Context;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000616 ConstantAsMetadata *C =
617 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
618 ConstantAsMetadata *C2 =
619 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
Devang Patel0924b332009-07-30 00:03:41 +0000620
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000621 Metadata *const V = C;
622 Metadata *const V2 = C2;
Jay Foad5514afe2011-04-21 19:59:31 +0000623 MDNode *n = MDNode::get(Context, V);
624 MDNode *n2 = MDNode::get(Context, V2);
Devang Patel0924b332009-07-30 00:03:41 +0000625
Jeffrey Yasskin3d73d1a2010-03-13 01:39:20 +0000626 Module M("MyModule", Context);
Devang Patel0924b332009-07-30 00:03:41 +0000627 const char *Name = "llvm.NMD1";
Dan Gohman2637cc12010-07-21 23:38:33 +0000628 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
629 NMD->addOperand(n);
630 NMD->addOperand(n2);
631
Chris Lattnerbe354a62009-08-23 04:47:35 +0000632 std::string Str;
633 raw_string_ostream oss(Str);
Devang Patel0924b332009-07-30 00:03:41 +0000634 NMD->print(oss);
Chris Lattner1e6e3672009-12-31 02:12:13 +0000635 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
Devang Patel0924b332009-07-30 00:03:41 +0000636 oss.str().c_str());
637}
Nick Lewycky49f89192009-04-04 07:22:01 +0000638}