Chandler Carruth | 74b6a77 | 2013-01-07 15:35:46 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/IR/Metadata.cpp - Metadata unit tests ----------------===// |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 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 | |
Duncan P. N. Exon Smith | 845755c4 | 2015-01-13 00:46:34 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 11 | #include "llvm/IR/Constants.h" |
| 12 | #include "llvm/IR/Instructions.h" |
| 13 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 14 | #include "llvm/IR/Metadata.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 15 | #include "llvm/IR/Module.h" |
| 16 | #include "llvm/IR/Type.h" |
Chandler Carruth | 130cec2 | 2012-12-04 10:23:08 +0000 | [diff] [blame] | 17 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | 130cec2 | 2012-12-04 10:23:08 +0000 | [diff] [blame] | 18 | #include "gtest/gtest.h" |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
| 21 | namespace { |
| 22 | |
Duncan P. N. Exon Smith | 2711ca7 | 2015-01-19 19:02:06 +0000 | [diff] [blame] | 23 | TEST(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 | |
| 31 | TEST(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 | |
| 39 | TEST(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 | |
| 48 | TEST(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 Yasskin | bd8a759 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 60 | class MetadataTest : public testing::Test { |
| 61 | protected: |
| 62 | LLVMContext Context; |
Duncan P. N. Exon Smith | fee167f | 2014-12-16 07:09:37 +0000 | [diff] [blame] | 63 | 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 Yasskin | bd8a759 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 69 | }; |
| 70 | typedef MetadataTest MDStringTest; |
Owen Anderson | 2358732 | 2009-07-31 21:38:10 +0000 | [diff] [blame] | 71 | |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 72 | // 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 Yasskin | bd8a759 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 74 | TEST_F(MDStringTest, CreateDifferent) { |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 75 | char x[3] = { 'f', 0, 'A' }; |
Owen Anderson | 2358732 | 2009-07-31 21:38:10 +0000 | [diff] [blame] | 76 | MDString *s1 = MDString::get(Context, StringRef(&x[0], 3)); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 77 | x[2] = 'B'; |
Owen Anderson | 2358732 | 2009-07-31 21:38:10 +0000 | [diff] [blame] | 78 | MDString *s2 = MDString::get(Context, StringRef(&x[0], 3)); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 79 | 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 Yasskin | bd8a759 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 84 | TEST_F(MDStringTest, CreateSame) { |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 85 | char x[4] = { 'a', 'b', 'c', 'X' }; |
| 86 | char y[4] = { 'a', 'b', 'c', 'Y' }; |
| 87 | |
Owen Anderson | 2358732 | 2009-07-31 21:38:10 +0000 | [diff] [blame] | 88 | MDString *s1 = MDString::get(Context, StringRef(&x[0], 3)); |
| 89 | MDString *s2 = MDString::get(Context, StringRef(&y[0], 3)); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 90 | EXPECT_EQ(s1, s2); |
| 91 | } |
| 92 | |
| 93 | // Test that MDString prints out the string we fed it. |
Jeffrey Yasskin | bd8a759 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 94 | TEST_F(MDStringTest, PrintingSimple) { |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 95 | char *str = new char[13]; |
| 96 | strncpy(str, "testing 1 2 3", 13); |
Owen Anderson | 2358732 | 2009-07-31 21:38:10 +0000 | [diff] [blame] | 97 | MDString *s = MDString::get(Context, StringRef(str, 13)); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 98 | strncpy(str, "aaaaaaaaaaaaa", 13); |
| 99 | delete[] str; |
| 100 | |
Chris Lattner | be354a6 | 2009-08-23 04:47:35 +0000 | [diff] [blame] | 101 | std::string Str; |
| 102 | raw_string_ostream oss(Str); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 103 | s->print(oss); |
Duncan P. N. Exon Smith | bb7d2fb | 2014-12-16 07:40:31 +0000 | [diff] [blame] | 104 | EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str()); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // Test printing of MDString with non-printable characters. |
Jeffrey Yasskin | bd8a759 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 108 | TEST_F(MDStringTest, PrintingComplex) { |
Jeffrey Yasskin | 065c357 | 2011-08-30 20:53:29 +0000 | [diff] [blame] | 109 | char str[5] = {0, '\n', '"', '\\', (char)-1}; |
Owen Anderson | 2358732 | 2009-07-31 21:38:10 +0000 | [diff] [blame] | 110 | MDString *s = MDString::get(Context, StringRef(str+0, 5)); |
Chris Lattner | be354a6 | 2009-08-23 04:47:35 +0000 | [diff] [blame] | 111 | std::string Str; |
| 112 | raw_string_ostream oss(Str); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 113 | s->print(oss); |
Duncan P. N. Exon Smith | bb7d2fb | 2014-12-16 07:40:31 +0000 | [diff] [blame] | 114 | EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str()); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Jeffrey Yasskin | bd8a759 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 117 | typedef MetadataTest MDNodeTest; |
| 118 | |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 119 | // Test the two constructors, and containing other Constants. |
Jeffrey Yasskin | bd8a759 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 120 | TEST_F(MDNodeTest, Simple) { |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 121 | char x[3] = { 'a', 'b', 'c' }; |
| 122 | char y[3] = { '1', '2', '3' }; |
| 123 | |
Owen Anderson | 2358732 | 2009-07-31 21:38:10 +0000 | [diff] [blame] | 124 | MDString *s1 = MDString::get(Context, StringRef(&x[0], 3)); |
| 125 | MDString *s2 = MDString::get(Context, StringRef(&y[0], 3)); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 126 | ConstantAsMetadata *CI = ConstantAsMetadata::get( |
| 127 | ConstantInt::get(getGlobalContext(), APInt(8, 0))); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 128 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 129 | std::vector<Metadata *> V; |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 130 | V.push_back(s1); |
| 131 | V.push_back(CI); |
| 132 | V.push_back(s2); |
| 133 | |
Jay Foad | 5514afe | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 134 | MDNode *n1 = MDNode::get(Context, V); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 135 | Metadata *const c1 = n1; |
Jay Foad | 5514afe | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 136 | MDNode *n2 = MDNode::get(Context, c1); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 137 | Metadata *const c2 = n2; |
Jay Foad | 5514afe | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 138 | MDNode *n3 = MDNode::get(Context, V); |
Duncan Sands | 26a80f3 | 2012-03-31 08:20:11 +0000 | [diff] [blame] | 139 | MDNode *n4 = MDNode::getIfExists(Context, V); |
| 140 | MDNode *n5 = MDNode::getIfExists(Context, c1); |
| 141 | MDNode *n6 = MDNode::getIfExists(Context, c2); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 142 | EXPECT_NE(n1, n2); |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 143 | EXPECT_EQ(n1, n3); |
Duncan Sands | 26a80f3 | 2012-03-31 08:20:11 +0000 | [diff] [blame] | 144 | EXPECT_EQ(n4, n1); |
| 145 | EXPECT_EQ(n5, n2); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 146 | EXPECT_EQ(n6, (Metadata *)nullptr); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 147 | |
Chris Lattner | 9b49302 | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 148 | 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 Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 152 | |
Chris Lattner | 9b49302 | 2009-12-31 01:22:29 +0000 | [diff] [blame] | 153 | EXPECT_EQ(1u, n2->getNumOperands()); |
| 154 | EXPECT_EQ(n1, n2->getOperand(0)); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 155 | } |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 156 | |
Jeffrey Yasskin | bd8a759 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 157 | TEST_F(MDNodeTest, Delete) { |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 158 | Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1); |
| 159 | Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext())); |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 160 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 161 | Metadata *const V = LocalAsMetadata::get(I); |
Jay Foad | 5514afe | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 162 | MDNode *n = MDNode::get(Context, V); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 163 | TrackingMDRef wvh(n); |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 164 | |
| 165 | EXPECT_EQ(n, wvh); |
| 166 | |
| 167 | delete I; |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 168 | } |
Devang Patel | 0924b33 | 2009-07-30 00:03:41 +0000 | [diff] [blame] | 169 | |
Duncan P. N. Exon Smith | ac8ee28 | 2014-12-07 19:52:06 +0000 | [diff] [blame] | 170 | TEST_F(MDNodeTest, SelfReference) { |
Duncan P. N. Exon Smith | 8c66273 | 2014-12-16 07:45:05 +0000 | [diff] [blame] | 171 | // !0 = !{!0} |
| 172 | // !1 = !{!0} |
Duncan P. N. Exon Smith | ac8ee28 | 2014-12-07 19:52:06 +0000 | [diff] [blame] | 173 | { |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 174 | auto Temp = MDNode::getTemporary(Context, None); |
| 175 | Metadata *Args[] = {Temp.get()}; |
Duncan P. N. Exon Smith | ac8ee28 | 2014-12-07 19:52:06 +0000 | [diff] [blame] | 176 | MDNode *Self = MDNode::get(Context, Args); |
| 177 | Self->replaceOperandWith(0, Self); |
Duncan P. N. Exon Smith | ac8ee28 | 2014-12-07 19:52:06 +0000 | [diff] [blame] | 178 | ASSERT_EQ(Self, Self->getOperand(0)); |
| 179 | |
| 180 | // Self-references should be distinct, so MDNode::get() should grab a |
| 181 | // uniqued node that references Self, not Self. |
| 182 | Args[0] = Self; |
| 183 | MDNode *Ref1 = MDNode::get(Context, Args); |
| 184 | MDNode *Ref2 = MDNode::get(Context, Args); |
| 185 | EXPECT_NE(Self, Ref1); |
| 186 | EXPECT_EQ(Ref1, Ref2); |
| 187 | } |
| 188 | |
Duncan P. N. Exon Smith | 8c66273 | 2014-12-16 07:45:05 +0000 | [diff] [blame] | 189 | // !0 = !{!0, !{}} |
| 190 | // !1 = !{!0, !{}} |
Duncan P. N. Exon Smith | ac8ee28 | 2014-12-07 19:52:06 +0000 | [diff] [blame] | 191 | { |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 192 | auto Temp = MDNode::getTemporary(Context, None); |
| 193 | Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)}; |
Duncan P. N. Exon Smith | ac8ee28 | 2014-12-07 19:52:06 +0000 | [diff] [blame] | 194 | MDNode *Self = MDNode::get(Context, Args); |
| 195 | Self->replaceOperandWith(0, Self); |
Duncan P. N. Exon Smith | ac8ee28 | 2014-12-07 19:52:06 +0000 | [diff] [blame] | 196 | ASSERT_EQ(Self, Self->getOperand(0)); |
| 197 | |
| 198 | // Self-references should be distinct, so MDNode::get() should grab a |
| 199 | // uniqued node that references Self, not Self itself. |
| 200 | Args[0] = Self; |
| 201 | MDNode *Ref1 = MDNode::get(Context, Args); |
| 202 | MDNode *Ref2 = MDNode::get(Context, Args); |
| 203 | EXPECT_NE(Self, Ref1); |
| 204 | EXPECT_EQ(Ref1, Ref2); |
| 205 | } |
| 206 | } |
| 207 | |
Duncan P. N. Exon Smith | fee167f | 2014-12-16 07:09:37 +0000 | [diff] [blame] | 208 | TEST_F(MDNodeTest, Print) { |
| 209 | Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7); |
| 210 | MDString *S = MDString::get(Context, "foo"); |
| 211 | MDNode *N0 = getNode(); |
| 212 | MDNode *N1 = getNode(N0); |
| 213 | MDNode *N2 = getNode(N0, N1); |
| 214 | |
| 215 | Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2}; |
| 216 | MDNode *N = MDNode::get(Context, Args); |
| 217 | |
| 218 | std::string Expected; |
| 219 | { |
| 220 | raw_string_ostream OS(Expected); |
Duncan P. N. Exon Smith | bb7d2fb | 2014-12-16 07:40:31 +0000 | [diff] [blame] | 221 | OS << "!{"; |
Duncan P. N. Exon Smith | fee167f | 2014-12-16 07:09:37 +0000 | [diff] [blame] | 222 | C->printAsOperand(OS); |
| 223 | OS << ", "; |
Duncan P. N. Exon Smith | bb7d2fb | 2014-12-16 07:40:31 +0000 | [diff] [blame] | 224 | S->printAsOperand(OS); |
Duncan P. N. Exon Smith | fee167f | 2014-12-16 07:09:37 +0000 | [diff] [blame] | 225 | OS << ", null"; |
| 226 | MDNode *Nodes[] = {N0, N1, N2}; |
| 227 | for (auto *Node : Nodes) |
| 228 | OS << ", <" << (void *)Node << ">"; |
| 229 | OS << "}\n"; |
| 230 | } |
| 231 | |
| 232 | std::string Actual; |
| 233 | { |
| 234 | raw_string_ostream OS(Actual); |
| 235 | N->print(OS); |
| 236 | } |
| 237 | |
| 238 | EXPECT_EQ(Expected, Actual); |
| 239 | } |
| 240 | |
Duncan P. N. Exon Smith | bcd960a | 2015-01-05 23:31:54 +0000 | [diff] [blame] | 241 | TEST_F(MDNodeTest, NullOperand) { |
| 242 | // metadata !{} |
| 243 | MDNode *Empty = MDNode::get(Context, None); |
| 244 | |
| 245 | // metadata !{metadata !{}} |
| 246 | Metadata *Ops[] = {Empty}; |
| 247 | MDNode *N = MDNode::get(Context, Ops); |
| 248 | ASSERT_EQ(Empty, N->getOperand(0)); |
| 249 | |
| 250 | // metadata !{metadata !{}} => metadata !{null} |
| 251 | N->replaceOperandWith(0, nullptr); |
| 252 | ASSERT_EQ(nullptr, N->getOperand(0)); |
| 253 | |
| 254 | // metadata !{null} |
| 255 | Ops[0] = nullptr; |
| 256 | MDNode *NullOp = MDNode::get(Context, Ops); |
| 257 | ASSERT_EQ(nullptr, NullOp->getOperand(0)); |
| 258 | EXPECT_EQ(N, NullOp); |
| 259 | } |
| 260 | |
Duncan P. N. Exon Smith | 136ea3f | 2015-01-07 21:35:38 +0000 | [diff] [blame] | 261 | TEST_F(MDNodeTest, DistinctOnUniquingCollision) { |
| 262 | // !{} |
| 263 | MDNode *Empty = MDNode::get(Context, None); |
| 264 | ASSERT_TRUE(Empty->isResolved()); |
| 265 | EXPECT_FALSE(Empty->isDistinct()); |
| 266 | |
| 267 | // !{!{}} |
| 268 | Metadata *Wrapped1Ops[] = {Empty}; |
| 269 | MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops); |
| 270 | ASSERT_EQ(Empty, Wrapped1->getOperand(0)); |
| 271 | ASSERT_TRUE(Wrapped1->isResolved()); |
| 272 | EXPECT_FALSE(Wrapped1->isDistinct()); |
| 273 | |
| 274 | // !{!{!{}}} |
| 275 | Metadata *Wrapped2Ops[] = {Wrapped1}; |
| 276 | MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops); |
| 277 | ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0)); |
| 278 | ASSERT_TRUE(Wrapped2->isResolved()); |
| 279 | EXPECT_FALSE(Wrapped2->isDistinct()); |
| 280 | |
| 281 | // !{!{!{}}} => !{!{}} |
| 282 | Wrapped2->replaceOperandWith(0, Empty); |
| 283 | ASSERT_EQ(Empty, Wrapped2->getOperand(0)); |
| 284 | EXPECT_TRUE(Wrapped2->isDistinct()); |
| 285 | EXPECT_FALSE(Wrapped1->isDistinct()); |
| 286 | } |
| 287 | |
Duncan P. N. Exon Smith | 5e5b850 | 2015-01-07 22:24:46 +0000 | [diff] [blame] | 288 | TEST_F(MDNodeTest, getDistinct) { |
| 289 | // !{} |
| 290 | MDNode *Empty = MDNode::get(Context, None); |
| 291 | ASSERT_TRUE(Empty->isResolved()); |
| 292 | ASSERT_FALSE(Empty->isDistinct()); |
| 293 | ASSERT_EQ(Empty, MDNode::get(Context, None)); |
| 294 | |
| 295 | // distinct !{} |
| 296 | MDNode *Distinct1 = MDNode::getDistinct(Context, None); |
| 297 | MDNode *Distinct2 = MDNode::getDistinct(Context, None); |
| 298 | EXPECT_TRUE(Distinct1->isResolved()); |
| 299 | EXPECT_TRUE(Distinct2->isDistinct()); |
| 300 | EXPECT_NE(Empty, Distinct1); |
| 301 | EXPECT_NE(Empty, Distinct2); |
| 302 | EXPECT_NE(Distinct1, Distinct2); |
| 303 | |
| 304 | // !{} |
| 305 | ASSERT_EQ(Empty, MDNode::get(Context, None)); |
| 306 | } |
| 307 | |
Duncan P. N. Exon Smith | de03a8b | 2015-01-19 18:45:35 +0000 | [diff] [blame] | 308 | TEST_F(MDNodeTest, isUniqued) { |
| 309 | MDNode *U = MDTuple::get(Context, None); |
| 310 | MDNode *D = MDTuple::getDistinct(Context, None); |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 311 | auto T = MDTuple::getTemporary(Context, None); |
Duncan P. N. Exon Smith | de03a8b | 2015-01-19 18:45:35 +0000 | [diff] [blame] | 312 | EXPECT_TRUE(U->isUniqued()); |
| 313 | EXPECT_FALSE(D->isUniqued()); |
| 314 | EXPECT_FALSE(T->isUniqued()); |
Duncan P. N. Exon Smith | de03a8b | 2015-01-19 18:45:35 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | TEST_F(MDNodeTest, isDistinct) { |
| 318 | MDNode *U = MDTuple::get(Context, None); |
| 319 | MDNode *D = MDTuple::getDistinct(Context, None); |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 320 | auto T = MDTuple::getTemporary(Context, None); |
Duncan P. N. Exon Smith | de03a8b | 2015-01-19 18:45:35 +0000 | [diff] [blame] | 321 | EXPECT_FALSE(U->isDistinct()); |
| 322 | EXPECT_TRUE(D->isDistinct()); |
| 323 | EXPECT_FALSE(T->isDistinct()); |
Duncan P. N. Exon Smith | de03a8b | 2015-01-19 18:45:35 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | TEST_F(MDNodeTest, isTemporary) { |
| 327 | MDNode *U = MDTuple::get(Context, None); |
| 328 | MDNode *D = MDTuple::getDistinct(Context, None); |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 329 | auto T = MDTuple::getTemporary(Context, None); |
Duncan P. N. Exon Smith | de03a8b | 2015-01-19 18:45:35 +0000 | [diff] [blame] | 330 | EXPECT_FALSE(U->isTemporary()); |
| 331 | EXPECT_FALSE(D->isTemporary()); |
| 332 | EXPECT_TRUE(T->isTemporary()); |
Duncan P. N. Exon Smith | d1474ee | 2015-01-12 18:41:26 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Duncan P. N. Exon Smith | 5e5b850 | 2015-01-07 22:24:46 +0000 | [diff] [blame] | 335 | TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) { |
| 336 | // temporary !{} |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 337 | auto Temp = MDTuple::getTemporary(Context, None); |
Duncan P. N. Exon Smith | 5e5b850 | 2015-01-07 22:24:46 +0000 | [diff] [blame] | 338 | ASSERT_FALSE(Temp->isResolved()); |
| 339 | |
| 340 | // distinct !{temporary !{}} |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 341 | Metadata *Ops[] = {Temp.get()}; |
Duncan P. N. Exon Smith | 5e5b850 | 2015-01-07 22:24:46 +0000 | [diff] [blame] | 342 | MDNode *Distinct = MDNode::getDistinct(Context, Ops); |
| 343 | EXPECT_TRUE(Distinct->isResolved()); |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 344 | EXPECT_EQ(Temp.get(), Distinct->getOperand(0)); |
Duncan P. N. Exon Smith | 5e5b850 | 2015-01-07 22:24:46 +0000 | [diff] [blame] | 345 | |
| 346 | // temporary !{} => !{} |
| 347 | MDNode *Empty = MDNode::get(Context, None); |
| 348 | Temp->replaceAllUsesWith(Empty); |
Duncan P. N. Exon Smith | 5e5b850 | 2015-01-07 22:24:46 +0000 | [diff] [blame] | 349 | EXPECT_EQ(Empty, Distinct->getOperand(0)); |
| 350 | } |
| 351 | |
Duncan P. N. Exon Smith | 5f46189 | 2015-01-12 19:22:04 +0000 | [diff] [blame] | 352 | TEST_F(MDNodeTest, handleChangedOperandRecursion) { |
| 353 | // !0 = !{} |
| 354 | MDNode *N0 = MDNode::get(Context, None); |
| 355 | |
| 356 | // !1 = !{!3, null} |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 357 | auto Temp3 = MDTuple::getTemporary(Context, None); |
| 358 | Metadata *Ops1[] = {Temp3.get(), nullptr}; |
Duncan P. N. Exon Smith | 5f46189 | 2015-01-12 19:22:04 +0000 | [diff] [blame] | 359 | MDNode *N1 = MDNode::get(Context, Ops1); |
| 360 | |
| 361 | // !2 = !{!3, !0} |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 362 | Metadata *Ops2[] = {Temp3.get(), N0}; |
Duncan P. N. Exon Smith | 5f46189 | 2015-01-12 19:22:04 +0000 | [diff] [blame] | 363 | MDNode *N2 = MDNode::get(Context, Ops2); |
| 364 | |
| 365 | // !3 = !{!2} |
| 366 | Metadata *Ops3[] = {N2}; |
| 367 | MDNode *N3 = MDNode::get(Context, Ops3); |
| 368 | Temp3->replaceAllUsesWith(N3); |
| 369 | |
| 370 | // !4 = !{!1} |
| 371 | Metadata *Ops4[] = {N1}; |
| 372 | MDNode *N4 = MDNode::get(Context, Ops4); |
| 373 | |
| 374 | // Confirm that the cycle prevented RAUW from getting dropped. |
| 375 | EXPECT_TRUE(N0->isResolved()); |
| 376 | EXPECT_FALSE(N1->isResolved()); |
| 377 | EXPECT_FALSE(N2->isResolved()); |
| 378 | EXPECT_FALSE(N3->isResolved()); |
| 379 | EXPECT_FALSE(N4->isResolved()); |
| 380 | |
| 381 | // Create a couple of distinct nodes to observe what's going on. |
| 382 | // |
| 383 | // !5 = distinct !{!2} |
| 384 | // !6 = distinct !{!3} |
| 385 | Metadata *Ops5[] = {N2}; |
| 386 | MDNode *N5 = MDNode::getDistinct(Context, Ops5); |
| 387 | Metadata *Ops6[] = {N3}; |
| 388 | MDNode *N6 = MDNode::getDistinct(Context, Ops6); |
| 389 | |
| 390 | // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW). |
| 391 | // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2 |
| 392 | // references !3, this can cause a re-entry of handleChangedOperand() when !3 |
| 393 | // is not ready for it. |
| 394 | // |
| 395 | // !2->replaceOperandWith(1, nullptr) |
| 396 | // !2: !{!3, !0} => !{!3, null} |
| 397 | // !2->replaceAllUsesWith(!1) |
| 398 | // !3: !{!2] => !{!1} |
| 399 | // !3->replaceAllUsesWith(!4) |
| 400 | N2->replaceOperandWith(1, nullptr); |
| 401 | |
| 402 | // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from |
| 403 | // under us. Just check that the other nodes are sane. |
| 404 | // |
| 405 | // !1 = !{!4, null} |
| 406 | // !4 = !{!1} |
| 407 | // !5 = distinct !{!1} |
| 408 | // !6 = distinct !{!4} |
| 409 | EXPECT_EQ(N4, N1->getOperand(0)); |
| 410 | EXPECT_EQ(N1, N4->getOperand(0)); |
| 411 | EXPECT_EQ(N1, N5->getOperand(0)); |
| 412 | EXPECT_EQ(N4, N6->getOperand(0)); |
| 413 | } |
| 414 | |
Duncan P. N. Exon Smith | 845755c4 | 2015-01-13 00:46:34 +0000 | [diff] [blame] | 415 | TEST_F(MDNodeTest, replaceResolvedOperand) { |
| 416 | // Check code for replacing one resolved operand with another. If doing this |
| 417 | // directly (via replaceOperandWith()) becomes illegal, change the operand to |
| 418 | // a global value that gets RAUW'ed. |
| 419 | // |
| 420 | // Use a temporary node to keep N from being resolved. |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 421 | auto Temp = MDTuple::getTemporary(Context, None); |
| 422 | Metadata *Ops[] = {nullptr, Temp.get()}; |
Duncan P. N. Exon Smith | 845755c4 | 2015-01-13 00:46:34 +0000 | [diff] [blame] | 423 | |
NAKAMURA Takumi | 2f8f054 | 2015-01-13 08:13:46 +0000 | [diff] [blame] | 424 | MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>()); |
Duncan P. N. Exon Smith | 845755c4 | 2015-01-13 00:46:34 +0000 | [diff] [blame] | 425 | MDNode *N = MDTuple::get(Context, Ops); |
| 426 | EXPECT_EQ(nullptr, N->getOperand(0)); |
| 427 | ASSERT_FALSE(N->isResolved()); |
| 428 | |
| 429 | // Check code for replacing resolved nodes. |
| 430 | N->replaceOperandWith(0, Empty); |
| 431 | EXPECT_EQ(Empty, N->getOperand(0)); |
| 432 | |
| 433 | // Check code for adding another unresolved operand. |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 434 | N->replaceOperandWith(0, Temp.get()); |
| 435 | EXPECT_EQ(Temp.get(), N->getOperand(0)); |
Duncan P. N. Exon Smith | 845755c4 | 2015-01-13 00:46:34 +0000 | [diff] [blame] | 436 | |
| 437 | // Remove the references to Temp; required for teardown. |
| 438 | Temp->replaceAllUsesWith(nullptr); |
| 439 | } |
| 440 | |
Duncan P. N. Exon Smith | de03ff5 | 2015-01-13 20:44:56 +0000 | [diff] [blame] | 441 | typedef MetadataTest MDLocationTest; |
| 442 | |
| 443 | TEST_F(MDLocationTest, Overflow) { |
| 444 | MDNode *N = MDNode::get(Context, None); |
| 445 | { |
| 446 | MDLocation *L = MDLocation::get(Context, 2, 7, N); |
| 447 | EXPECT_EQ(2u, L->getLine()); |
| 448 | EXPECT_EQ(7u, L->getColumn()); |
| 449 | } |
| 450 | unsigned U24 = 1u << 24; |
Duncan P. N. Exon Smith | 2f5bb31 | 2015-01-16 17:33:08 +0000 | [diff] [blame] | 451 | unsigned U16 = 1u << 16; |
Duncan P. N. Exon Smith | de03ff5 | 2015-01-13 20:44:56 +0000 | [diff] [blame] | 452 | { |
Duncan P. N. Exon Smith | 2f5bb31 | 2015-01-16 17:33:08 +0000 | [diff] [blame] | 453 | MDLocation *L = MDLocation::get(Context, U24 - 1, U16 - 1, N); |
Duncan P. N. Exon Smith | de03ff5 | 2015-01-13 20:44:56 +0000 | [diff] [blame] | 454 | EXPECT_EQ(U24 - 1, L->getLine()); |
Duncan P. N. Exon Smith | 2f5bb31 | 2015-01-16 17:33:08 +0000 | [diff] [blame] | 455 | EXPECT_EQ(U16 - 1, L->getColumn()); |
Duncan P. N. Exon Smith | de03ff5 | 2015-01-13 20:44:56 +0000 | [diff] [blame] | 456 | } |
| 457 | { |
Duncan P. N. Exon Smith | 2f5bb31 | 2015-01-16 17:33:08 +0000 | [diff] [blame] | 458 | MDLocation *L = MDLocation::get(Context, U24, U16, N); |
Duncan P. N. Exon Smith | de03ff5 | 2015-01-13 20:44:56 +0000 | [diff] [blame] | 459 | EXPECT_EQ(0u, L->getLine()); |
| 460 | EXPECT_EQ(0u, L->getColumn()); |
| 461 | } |
| 462 | { |
Duncan P. N. Exon Smith | 2f5bb31 | 2015-01-16 17:33:08 +0000 | [diff] [blame] | 463 | MDLocation *L = MDLocation::get(Context, U24 + 1, U16 + 1, N); |
Duncan P. N. Exon Smith | de03ff5 | 2015-01-13 20:44:56 +0000 | [diff] [blame] | 464 | EXPECT_EQ(0u, L->getLine()); |
| 465 | EXPECT_EQ(0u, L->getColumn()); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | TEST_F(MDLocationTest, getDistinct) { |
| 470 | MDNode *N = MDNode::get(Context, None); |
| 471 | MDLocation *L0 = MDLocation::getDistinct(Context, 2, 7, N); |
| 472 | EXPECT_TRUE(L0->isDistinct()); |
| 473 | MDLocation *L1 = MDLocation::get(Context, 2, 7, N); |
| 474 | EXPECT_FALSE(L1->isDistinct()); |
| 475 | EXPECT_EQ(L1, MDLocation::get(Context, 2, 7, N)); |
| 476 | } |
| 477 | |
Duncan P. N. Exon Smith | 799e56a | 2015-01-19 20:37:44 +0000 | [diff] [blame] | 478 | TEST_F(MDLocationTest, getTemporary) { |
| 479 | MDNode *N = MDNode::get(Context, None); |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 480 | auto L = MDLocation::getTemporary(Context, 2, 7, N); |
Duncan P. N. Exon Smith | 799e56a | 2015-01-19 20:37:44 +0000 | [diff] [blame] | 481 | EXPECT_TRUE(L->isTemporary()); |
| 482 | EXPECT_FALSE(L->isResolved()); |
Duncan P. N. Exon Smith | 799e56a | 2015-01-19 20:37:44 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 485 | typedef MetadataTest MetadataAsValueTest; |
| 486 | |
| 487 | TEST_F(MetadataAsValueTest, MDNode) { |
| 488 | MDNode *N = MDNode::get(Context, None); |
| 489 | auto *V = MetadataAsValue::get(Context, N); |
| 490 | EXPECT_TRUE(V->getType()->isMetadataTy()); |
| 491 | EXPECT_EQ(N, V->getMetadata()); |
| 492 | |
| 493 | auto *V2 = MetadataAsValue::get(Context, N); |
| 494 | EXPECT_EQ(V, V2); |
| 495 | } |
| 496 | |
| 497 | TEST_F(MetadataAsValueTest, MDNodeMDNode) { |
| 498 | MDNode *N = MDNode::get(Context, None); |
| 499 | Metadata *Ops[] = {N}; |
| 500 | MDNode *N2 = MDNode::get(Context, Ops); |
| 501 | auto *V = MetadataAsValue::get(Context, N2); |
| 502 | EXPECT_TRUE(V->getType()->isMetadataTy()); |
| 503 | EXPECT_EQ(N2, V->getMetadata()); |
| 504 | |
| 505 | auto *V2 = MetadataAsValue::get(Context, N2); |
| 506 | EXPECT_EQ(V, V2); |
| 507 | |
| 508 | auto *V3 = MetadataAsValue::get(Context, N); |
| 509 | EXPECT_TRUE(V3->getType()->isMetadataTy()); |
| 510 | EXPECT_NE(V, V3); |
| 511 | EXPECT_EQ(N, V3->getMetadata()); |
| 512 | } |
| 513 | |
| 514 | TEST_F(MetadataAsValueTest, MDNodeConstant) { |
| 515 | auto *C = ConstantInt::getTrue(Context); |
| 516 | auto *MD = ConstantAsMetadata::get(C); |
| 517 | Metadata *Ops[] = {MD}; |
| 518 | auto *N = MDNode::get(Context, Ops); |
| 519 | |
| 520 | auto *V = MetadataAsValue::get(Context, MD); |
| 521 | EXPECT_TRUE(V->getType()->isMetadataTy()); |
| 522 | EXPECT_EQ(MD, V->getMetadata()); |
| 523 | |
| 524 | auto *V2 = MetadataAsValue::get(Context, N); |
| 525 | EXPECT_EQ(MD, V2->getMetadata()); |
| 526 | EXPECT_EQ(V, V2); |
| 527 | } |
| 528 | |
Duncan P. N. Exon Smith | 121eeff | 2014-12-12 19:24:33 +0000 | [diff] [blame] | 529 | typedef MetadataTest ValueAsMetadataTest; |
| 530 | |
| 531 | TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) { |
| 532 | Type *Ty = Type::getInt1PtrTy(Context); |
| 533 | std::unique_ptr<GlobalVariable> GV0( |
| 534 | new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage)); |
| 535 | auto *MD = ValueAsMetadata::get(GV0.get()); |
| 536 | EXPECT_TRUE(MD->getValue() == GV0.get()); |
| 537 | ASSERT_TRUE(GV0->use_empty()); |
| 538 | |
| 539 | std::unique_ptr<GlobalVariable> GV1( |
| 540 | new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage)); |
| 541 | GV0->replaceAllUsesWith(GV1.get()); |
| 542 | EXPECT_TRUE(MD->getValue() == GV1.get()); |
| 543 | } |
| 544 | |
Duncan P. N. Exon Smith | 4a4f785 | 2015-01-14 19:56:10 +0000 | [diff] [blame] | 545 | TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) { |
| 546 | // Create a constant. |
| 547 | ConstantAsMetadata *CI = ConstantAsMetadata::get( |
| 548 | ConstantInt::get(getGlobalContext(), APInt(8, 0))); |
| 549 | |
| 550 | // Create a temporary to prevent nodes from resolving. |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 551 | auto Temp = MDTuple::getTemporary(Context, None); |
Duncan P. N. Exon Smith | 4a4f785 | 2015-01-14 19:56:10 +0000 | [diff] [blame] | 552 | |
| 553 | // When the first operand of N1 gets reset to nullptr, it'll collide with N2. |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 554 | Metadata *Ops1[] = {CI, CI, Temp.get()}; |
| 555 | Metadata *Ops2[] = {nullptr, CI, Temp.get()}; |
Duncan P. N. Exon Smith | 4a4f785 | 2015-01-14 19:56:10 +0000 | [diff] [blame] | 556 | |
| 557 | auto *N1 = MDTuple::get(Context, Ops1); |
| 558 | auto *N2 = MDTuple::get(Context, Ops2); |
| 559 | ASSERT_NE(N1, N2); |
| 560 | |
| 561 | // Tell metadata that the constant is getting deleted. |
| 562 | // |
| 563 | // After this, N1 will be invalid, so don't touch it. |
| 564 | ValueAsMetadata::handleDeletion(CI->getValue()); |
| 565 | EXPECT_EQ(nullptr, N2->getOperand(0)); |
| 566 | EXPECT_EQ(nullptr, N2->getOperand(1)); |
Duncan P. N. Exon Smith | 7d82313 | 2015-01-19 21:30:18 +0000 | [diff] [blame] | 567 | EXPECT_EQ(Temp.get(), N2->getOperand(2)); |
Duncan P. N. Exon Smith | 4a4f785 | 2015-01-14 19:56:10 +0000 | [diff] [blame] | 568 | |
| 569 | // Clean up Temp for teardown. |
| 570 | Temp->replaceAllUsesWith(nullptr); |
| 571 | } |
| 572 | |
Duncan P. N. Exon Smith | 121eeff | 2014-12-12 19:24:33 +0000 | [diff] [blame] | 573 | typedef MetadataTest TrackingMDRefTest; |
| 574 | |
| 575 | TEST_F(TrackingMDRefTest, UpdatesOnRAUW) { |
| 576 | Type *Ty = Type::getInt1PtrTy(Context); |
| 577 | std::unique_ptr<GlobalVariable> GV0( |
| 578 | new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage)); |
| 579 | TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get())); |
| 580 | EXPECT_TRUE(MD->getValue() == GV0.get()); |
| 581 | ASSERT_TRUE(GV0->use_empty()); |
| 582 | |
| 583 | std::unique_ptr<GlobalVariable> GV1( |
| 584 | new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage)); |
| 585 | GV0->replaceAllUsesWith(GV1.get()); |
| 586 | EXPECT_TRUE(MD->getValue() == GV1.get()); |
| 587 | |
| 588 | // Reset it, so we don't inadvertently test deletion. |
| 589 | MD.reset(); |
| 590 | } |
| 591 | |
| 592 | TEST_F(TrackingMDRefTest, UpdatesOnDeletion) { |
| 593 | Type *Ty = Type::getInt1PtrTy(Context); |
| 594 | std::unique_ptr<GlobalVariable> GV( |
| 595 | new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage)); |
| 596 | TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get())); |
| 597 | EXPECT_TRUE(MD->getValue() == GV.get()); |
| 598 | ASSERT_TRUE(GV->use_empty()); |
| 599 | |
| 600 | GV.reset(); |
| 601 | EXPECT_TRUE(!MD); |
| 602 | } |
| 603 | |
Devang Patel | 0924b33 | 2009-07-30 00:03:41 +0000 | [diff] [blame] | 604 | TEST(NamedMDNodeTest, Search) { |
Jeffrey Yasskin | bd8a759 | 2010-03-04 23:24:19 +0000 | [diff] [blame] | 605 | LLVMContext Context; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 606 | ConstantAsMetadata *C = |
| 607 | ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1)); |
| 608 | ConstantAsMetadata *C2 = |
| 609 | ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2)); |
Devang Patel | 0924b33 | 2009-07-30 00:03:41 +0000 | [diff] [blame] | 610 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 611 | Metadata *const V = C; |
| 612 | Metadata *const V2 = C2; |
Jay Foad | 5514afe | 2011-04-21 19:59:31 +0000 | [diff] [blame] | 613 | MDNode *n = MDNode::get(Context, V); |
| 614 | MDNode *n2 = MDNode::get(Context, V2); |
Devang Patel | 0924b33 | 2009-07-30 00:03:41 +0000 | [diff] [blame] | 615 | |
Jeffrey Yasskin | 3d73d1a | 2010-03-13 01:39:20 +0000 | [diff] [blame] | 616 | Module M("MyModule", Context); |
Devang Patel | 0924b33 | 2009-07-30 00:03:41 +0000 | [diff] [blame] | 617 | const char *Name = "llvm.NMD1"; |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 618 | NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name); |
| 619 | NMD->addOperand(n); |
| 620 | NMD->addOperand(n2); |
| 621 | |
Chris Lattner | be354a6 | 2009-08-23 04:47:35 +0000 | [diff] [blame] | 622 | std::string Str; |
| 623 | raw_string_ostream oss(Str); |
Devang Patel | 0924b33 | 2009-07-30 00:03:41 +0000 | [diff] [blame] | 624 | NMD->print(oss); |
Chris Lattner | 1e6e367 | 2009-12-31 02:12:13 +0000 | [diff] [blame] | 625 | EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n", |
Devang Patel | 0924b33 | 2009-07-30 00:03:41 +0000 | [diff] [blame] | 626 | oss.str().c_str()); |
| 627 | } |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 628 | } |