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 typedef MetadataTest MetadataAsValueTest;
207 TEST_F(MetadataAsValueTest, MDNode) {
208 MDNode *N = MDNode::get(Context, None);
209 auto *V = MetadataAsValue::get(Context, N);
210 EXPECT_TRUE(V->getType()->isMetadataTy());
211 EXPECT_EQ(N, V->getMetadata());
213 auto *V2 = MetadataAsValue::get(Context, N);
217 TEST_F(MetadataAsValueTest, MDNodeMDNode) {
218 MDNode *N = MDNode::get(Context, None);
219 Metadata *Ops[] = {N};
220 MDNode *N2 = MDNode::get(Context, Ops);
221 auto *V = MetadataAsValue::get(Context, N2);
222 EXPECT_TRUE(V->getType()->isMetadataTy());
223 EXPECT_EQ(N2, V->getMetadata());
225 auto *V2 = MetadataAsValue::get(Context, N2);
228 auto *V3 = MetadataAsValue::get(Context, N);
229 EXPECT_TRUE(V3->getType()->isMetadataTy());
231 EXPECT_EQ(N, V3->getMetadata());
234 TEST_F(MetadataAsValueTest, MDNodeConstant) {
235 auto *C = ConstantInt::getTrue(Context);
236 auto *MD = ConstantAsMetadata::get(C);
237 Metadata *Ops[] = {MD};
238 auto *N = MDNode::get(Context, Ops);
240 auto *V = MetadataAsValue::get(Context, MD);
241 EXPECT_TRUE(V->getType()->isMetadataTy());
242 EXPECT_EQ(MD, V->getMetadata());
244 auto *V2 = MetadataAsValue::get(Context, N);
245 EXPECT_EQ(MD, V2->getMetadata());
249 typedef MetadataTest ValueAsMetadataTest;
251 TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
252 Type *Ty = Type::getInt1PtrTy(Context);
253 std::unique_ptr<GlobalVariable> GV0(
254 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
255 auto *MD = ValueAsMetadata::get(GV0.get());
256 EXPECT_TRUE(MD->getValue() == GV0.get());
257 ASSERT_TRUE(GV0->use_empty());
259 std::unique_ptr<GlobalVariable> GV1(
260 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
261 GV0->replaceAllUsesWith(GV1.get());
262 EXPECT_TRUE(MD->getValue() == GV1.get());
265 typedef MetadataTest TrackingMDRefTest;
267 TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
268 Type *Ty = Type::getInt1PtrTy(Context);
269 std::unique_ptr<GlobalVariable> GV0(
270 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
271 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
272 EXPECT_TRUE(MD->getValue() == GV0.get());
273 ASSERT_TRUE(GV0->use_empty());
275 std::unique_ptr<GlobalVariable> GV1(
276 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
277 GV0->replaceAllUsesWith(GV1.get());
278 EXPECT_TRUE(MD->getValue() == GV1.get());
280 // Reset it, so we don't inadvertently test deletion.
284 TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
285 Type *Ty = Type::getInt1PtrTy(Context);
286 std::unique_ptr<GlobalVariable> GV(
287 new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
288 TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
289 EXPECT_TRUE(MD->getValue() == GV.get());
290 ASSERT_TRUE(GV->use_empty());
296 TEST(NamedMDNodeTest, Search) {
298 ConstantAsMetadata *C =
299 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
300 ConstantAsMetadata *C2 =
301 ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
303 Metadata *const V = C;
304 Metadata *const V2 = C2;
305 MDNode *n = MDNode::get(Context, V);
306 MDNode *n2 = MDNode::get(Context, V2);
308 Module M("MyModule", Context);
309 const char *Name = "llvm.NMD1";
310 NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
315 raw_string_ostream oss(Str);
317 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",