1 //===- llvm/unittest/IR/Metadata.cpp - Metadata unit tests ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/IR/Metadata.h"
11 #include "llvm/IR/Constants.h"
12 #include "llvm/IR/Instructions.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/IR/Type.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include "gtest/gtest.h"
22 class MetadataTest : public testing::Test {
25 MDNode *getNode() { return MDNode::get(Context, None); }
26 MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); }
27 MDNode *getNode(Metadata *MD1, Metadata *MD2) {
28 Metadata *MDs[] = {MD1, MD2};
29 return MDNode::get(Context, MDs);
32 typedef MetadataTest MDStringTest;
34 // Test that construction of MDString with different value produces different
35 // MDString objects, even with the same string pointer and nulls in the string.
36 TEST_F(MDStringTest, CreateDifferent) {
37 char x[3] = { 'f', 0, 'A' };
38 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
40 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
44 // Test that creation of MDStrings with the same string contents produces the
45 // same MDString object, even with different pointers.
46 TEST_F(MDStringTest, CreateSame) {
47 char x[4] = { 'a', 'b', 'c', 'X' };
48 char y[4] = { 'a', 'b', 'c', 'Y' };
50 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
51 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
55 // Test that MDString prints out the string we fed it.
56 TEST_F(MDStringTest, PrintingSimple) {
57 char *str = new char[13];
58 strncpy(str, "testing 1 2 3", 13);
59 MDString *s = MDString::get(Context, StringRef(str, 13));
60 strncpy(str, "aaaaaaaaaaaaa", 13);
64 raw_string_ostream oss(Str);
66 EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
69 // Test printing of MDString with non-printable characters.
70 TEST_F(MDStringTest, PrintingComplex) {
71 char str[5] = {0, '\n', '"', '\\', (char)-1};
72 MDString *s = MDString::get(Context, StringRef(str+0, 5));
74 raw_string_ostream oss(Str);
76 EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
79 typedef MetadataTest MDNodeTest;
81 // Test the two constructors, and containing other Constants.
82 TEST_F(MDNodeTest, Simple) {
83 char x[3] = { 'a', 'b', 'c' };
84 char y[3] = { '1', '2', '3' };
86 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
87 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
88 ConstantAsMetadata *CI = ConstantAsMetadata::get(
89 ConstantInt::get(getGlobalContext(), APInt(8, 0)));
91 std::vector<Metadata *> V;
96 MDNode *n1 = MDNode::get(Context, V);
97 Metadata *const c1 = n1;
98 MDNode *n2 = MDNode::get(Context, c1);
99 Metadata *const c2 = n2;
100 MDNode *n3 = MDNode::get(Context, V);
101 MDNode *n4 = MDNode::getIfExists(Context, V);
102 MDNode *n5 = MDNode::getIfExists(Context, c1);
103 MDNode *n6 = MDNode::getIfExists(Context, c2);
108 EXPECT_EQ(n6, (Metadata *)nullptr);
110 EXPECT_EQ(3u, n1->getNumOperands());
111 EXPECT_EQ(s1, n1->getOperand(0));
112 EXPECT_EQ(CI, n1->getOperand(1));
113 EXPECT_EQ(s2, n1->getOperand(2));
115 EXPECT_EQ(1u, n2->getNumOperands());
116 EXPECT_EQ(n1, n2->getOperand(0));
119 TEST_F(MDNodeTest, Delete) {
120 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
121 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
123 Metadata *const V = LocalAsMetadata::get(I);
124 MDNode *n = MDNode::get(Context, V);
125 TrackingMDRef wvh(n);
132 TEST_F(MDNodeTest, SelfReference) {
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));
143 // Self-references should be distinct, so MDNode::get() should grab a
144 // uniqued node that references Self, not 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);
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));
162 // Self-references should be distinct, so MDNode::get() should grab a
163 // uniqued node that references Self, not Self itself.
165 MDNode *Ref1 = MDNode::get(Context, Args);
166 MDNode *Ref2 = MDNode::get(Context, Args);
167 EXPECT_NE(Self, Ref1);
168 EXPECT_EQ(Ref1, Ref2);
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);
179 Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
180 MDNode *N = MDNode::get(Context, Args);
182 std::string Expected;
184 raw_string_ostream OS(Expected);
186 C->printAsOperand(OS);
188 S->printAsOperand(OS);
190 MDNode *Nodes[] = {N0, N1, N2};
191 for (auto *Node : Nodes)
192 OS << ", <" << (void *)Node << ">";
198 raw_string_ostream OS(Actual);
202 EXPECT_EQ(Expected, Actual);
205 TEST_F(MDNodeTest, NullOperand) {
207 MDNode *Empty = MDNode::get(Context, None);
209 // metadata !{metadata !{}}
210 Metadata *Ops[] = {Empty};
211 MDNode *N = MDNode::get(Context, Ops);
212 ASSERT_EQ(Empty, N->getOperand(0));
214 // metadata !{metadata !{}} => metadata !{null}
215 N->replaceOperandWith(0, nullptr);
216 ASSERT_EQ(nullptr, N->getOperand(0));
220 MDNode *NullOp = MDNode::get(Context, Ops);
221 ASSERT_EQ(nullptr, NullOp->getOperand(0));
222 EXPECT_EQ(N, NullOp);
225 TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
227 MDNode *Empty = MDNode::get(Context, None);
228 ASSERT_TRUE(Empty->isResolved());
229 EXPECT_FALSE(Empty->isDistinct());
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());
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());
245 // !{!{!{}}} => !{!{}}
246 Wrapped2->replaceOperandWith(0, Empty);
247 ASSERT_EQ(Empty, Wrapped2->getOperand(0));
248 EXPECT_TRUE(Wrapped2->isDistinct());
249 EXPECT_FALSE(Wrapped1->isDistinct());
252 typedef MetadataTest MetadataAsValueTest;
254 TEST_F(MetadataAsValueTest, MDNode) {
255 MDNode *N = MDNode::get(Context, None);
256 auto *V = MetadataAsValue::get(Context, N);
257 EXPECT_TRUE(V->getType()->isMetadataTy());
258 EXPECT_EQ(N, V->getMetadata());
260 auto *V2 = MetadataAsValue::get(Context, N);
264 TEST_F(MetadataAsValueTest, MDNodeMDNode) {
265 MDNode *N = MDNode::get(Context, None);
266 Metadata *Ops[] = {N};
267 MDNode *N2 = MDNode::get(Context, Ops);
268 auto *V = MetadataAsValue::get(Context, N2);
269 EXPECT_TRUE(V->getType()->isMetadataTy());
270 EXPECT_EQ(N2, V->getMetadata());
272 auto *V2 = MetadataAsValue::get(Context, N2);
275 auto *V3 = MetadataAsValue::get(Context, N);
276 EXPECT_TRUE(V3->getType()->isMetadataTy());
278 EXPECT_EQ(N, V3->getMetadata());
281 TEST_F(MetadataAsValueTest, MDNodeConstant) {
282 auto *C = ConstantInt::getTrue(Context);
283 auto *MD = ConstantAsMetadata::get(C);
284 Metadata *Ops[] = {MD};
285 auto *N = MDNode::get(Context, Ops);
287 auto *V = MetadataAsValue::get(Context, MD);
288 EXPECT_TRUE(V->getType()->isMetadataTy());
289 EXPECT_EQ(MD, V->getMetadata());
291 auto *V2 = MetadataAsValue::get(Context, N);
292 EXPECT_EQ(MD, V2->getMetadata());
296 typedef MetadataTest ValueAsMetadataTest;
298 TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
299 Type *Ty = Type::getInt1PtrTy(Context);
300 std::unique_ptr<GlobalVariable> GV0(
301 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
302 auto *MD = ValueAsMetadata::get(GV0.get());
303 EXPECT_TRUE(MD->getValue() == GV0.get());
304 ASSERT_TRUE(GV0->use_empty());
306 std::unique_ptr<GlobalVariable> GV1(
307 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
308 GV0->replaceAllUsesWith(GV1.get());
309 EXPECT_TRUE(MD->getValue() == GV1.get());
312 typedef MetadataTest TrackingMDRefTest;
314 TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
315 Type *Ty = Type::getInt1PtrTy(Context);
316 std::unique_ptr<GlobalVariable> GV0(
317 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
318 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
319 EXPECT_TRUE(MD->getValue() == GV0.get());
320 ASSERT_TRUE(GV0->use_empty());
322 std::unique_ptr<GlobalVariable> GV1(
323 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
324 GV0->replaceAllUsesWith(GV1.get());
325 EXPECT_TRUE(MD->getValue() == GV1.get());
327 // Reset it, so we don't inadvertently test deletion.
331 TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
332 Type *Ty = Type::getInt1PtrTy(Context);
333 std::unique_ptr<GlobalVariable> GV(
334 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
335 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
336 EXPECT_TRUE(MD->getValue() == GV.get());
337 ASSERT_TRUE(GV->use_empty());
343 TEST(NamedMDNodeTest, Search) {
345 ConstantAsMetadata *C =
346 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
347 ConstantAsMetadata *C2 =
348 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
350 Metadata *const V = C;
351 Metadata *const V2 = C2;
352 MDNode *n = MDNode::get(Context, V);
353 MDNode *n2 = MDNode::get(Context, V2);
355 Module M("MyModule", Context);
356 const char *Name = "llvm.NMD1";
357 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
362 raw_string_ostream oss(Str);
364 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",