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