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