OSDN Git Service

Revert "DebugInfo: use strongly typed enum for debug info flags"
[android-x86/external-llvm.git] / unittests / IR / MetadataTest.cpp
1 //===- unittests/IR/MetadataTest.cpp - Metadata unit tests ----------------===//
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
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/IR/Constants.h"
12 #include "llvm/IR/DebugInfo.h"
13 #include "llvm/IR/DebugInfoMetadata.h"
14 #include "llvm/IR/Function.h"
15 #include "llvm/IR/Instructions.h"
16 #include "llvm/IR/LLVMContext.h"
17 #include "llvm/IR/Metadata.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/IR/ModuleSlotTracker.h"
20 #include "llvm/IR/Type.h"
21 #include "llvm/IR/Verifier.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "gtest/gtest.h"
24 using namespace llvm;
25
26 namespace {
27
28 TEST(ContextAndReplaceableUsesTest, FromContext) {
29   LLVMContext Context;
30   ContextAndReplaceableUses CRU(Context);
31   EXPECT_EQ(&Context, &CRU.getContext());
32   EXPECT_FALSE(CRU.hasReplaceableUses());
33   EXPECT_FALSE(CRU.getReplaceableUses());
34 }
35
36 TEST(ContextAndReplaceableUsesTest, FromReplaceableUses) {
37   LLVMContext Context;
38   ContextAndReplaceableUses CRU(make_unique<ReplaceableMetadataImpl>(Context));
39   EXPECT_EQ(&Context, &CRU.getContext());
40   EXPECT_TRUE(CRU.hasReplaceableUses());
41   EXPECT_TRUE(CRU.getReplaceableUses());
42 }
43
44 TEST(ContextAndReplaceableUsesTest, makeReplaceable) {
45   LLVMContext Context;
46   ContextAndReplaceableUses CRU(Context);
47   CRU.makeReplaceable(make_unique<ReplaceableMetadataImpl>(Context));
48   EXPECT_EQ(&Context, &CRU.getContext());
49   EXPECT_TRUE(CRU.hasReplaceableUses());
50   EXPECT_TRUE(CRU.getReplaceableUses());
51 }
52
53 TEST(ContextAndReplaceableUsesTest, takeReplaceableUses) {
54   LLVMContext Context;
55   auto ReplaceableUses = make_unique<ReplaceableMetadataImpl>(Context);
56   auto *Ptr = ReplaceableUses.get();
57   ContextAndReplaceableUses CRU(std::move(ReplaceableUses));
58   ReplaceableUses = CRU.takeReplaceableUses();
59   EXPECT_EQ(&Context, &CRU.getContext());
60   EXPECT_FALSE(CRU.hasReplaceableUses());
61   EXPECT_FALSE(CRU.getReplaceableUses());
62   EXPECT_EQ(Ptr, ReplaceableUses.get());
63 }
64
65 class MetadataTest : public testing::Test {
66 public:
67   MetadataTest() : M("test", Context), Counter(0) {}
68
69 protected:
70   LLVMContext Context;
71   Module M;
72   int Counter;
73
74   MDNode *getNode() { return MDNode::get(Context, None); }
75   MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); }
76   MDNode *getNode(Metadata *MD1, Metadata *MD2) {
77     Metadata *MDs[] = {MD1, MD2};
78     return MDNode::get(Context, MDs);
79   }
80
81   MDTuple *getTuple() { return MDTuple::getDistinct(Context, None); }
82   DISubroutineType *getSubroutineType() {
83     return DISubroutineType::getDistinct(Context, 0, 0, getNode(nullptr));
84   }
85   DISubprogram *getSubprogram() {
86     return DISubprogram::getDistinct(Context, nullptr, "", "", nullptr, 0,
87                                      nullptr, false, false, 0, nullptr,
88                                      0, 0, 0, 0, false, nullptr);
89   }
90   DIFile *getFile() {
91     return DIFile::getDistinct(Context, "file.c", "/path/to/dir");
92   }
93   DICompileUnit *getUnit() {
94     return DICompileUnit::getDistinct(Context, 1, getFile(), "clang", false,
95                                       "-g", 2, "", DICompileUnit::FullDebug,
96                                       getTuple(), getTuple(), getTuple(),
97                                       getTuple(), getTuple(), 0, true);
98   }
99   DIType *getBasicType(StringRef Name) {
100     return DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, Name);
101   }
102   DIType *getDerivedType() {
103     return DIDerivedType::getDistinct(Context, dwarf::DW_TAG_pointer_type, "",
104                                       nullptr, 0, nullptr,
105                                       getBasicType("basictype"), 1, 2, 0, 0);
106   }
107   Constant *getConstant() {
108     return ConstantInt::get(Type::getInt32Ty(Context), Counter++);
109   }
110   ConstantAsMetadata *getConstantAsMetadata() {
111     return ConstantAsMetadata::get(getConstant());
112   }
113   DIType *getCompositeType() {
114     return DICompositeType::getDistinct(
115         Context, dwarf::DW_TAG_structure_type, "", nullptr, 0, nullptr, nullptr,
116         32, 32, 0, 0, nullptr, 0, nullptr, nullptr, "");
117   }
118   Function *getFunction(StringRef Name) {
119     return cast<Function>(M.getOrInsertFunction(
120         Name, FunctionType::get(Type::getVoidTy(Context), None, false)));
121   }
122 };
123 typedef MetadataTest MDStringTest;
124
125 // Test that construction of MDString with different value produces different
126 // MDString objects, even with the same string pointer and nulls in the string.
127 TEST_F(MDStringTest, CreateDifferent) {
128   char x[3] = { 'f', 0, 'A' };
129   MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
130   x[2] = 'B';
131   MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
132   EXPECT_NE(s1, s2);
133 }
134
135 // Test that creation of MDStrings with the same string contents produces the
136 // same MDString object, even with different pointers.
137 TEST_F(MDStringTest, CreateSame) {
138   char x[4] = { 'a', 'b', 'c', 'X' };
139   char y[4] = { 'a', 'b', 'c', 'Y' };
140
141   MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
142   MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
143   EXPECT_EQ(s1, s2);
144 }
145
146 // Test that MDString prints out the string we fed it.
147 TEST_F(MDStringTest, PrintingSimple) {
148   char *str = new char[13];
149   strncpy(str, "testing 1 2 3", 13);
150   MDString *s = MDString::get(Context, StringRef(str, 13));
151   strncpy(str, "aaaaaaaaaaaaa", 13);
152   delete[] str;
153
154   std::string Str;
155   raw_string_ostream oss(Str);
156   s->print(oss);
157   EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
158 }
159
160 // Test printing of MDString with non-printable characters.
161 TEST_F(MDStringTest, PrintingComplex) {
162   char str[5] = {0, '\n', '"', '\\', (char)-1};
163   MDString *s = MDString::get(Context, StringRef(str+0, 5));
164   std::string Str;
165   raw_string_ostream oss(Str);
166   s->print(oss);
167   EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
168 }
169
170 typedef MetadataTest MDNodeTest;
171
172 // Test the two constructors, and containing other Constants.
173 TEST_F(MDNodeTest, Simple) {
174   char x[3] = { 'a', 'b', 'c' };
175   char y[3] = { '1', '2', '3' };
176
177   MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
178   MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
179   ConstantAsMetadata *CI =
180       ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0)));
181
182   std::vector<Metadata *> V;
183   V.push_back(s1);
184   V.push_back(CI);
185   V.push_back(s2);
186
187   MDNode *n1 = MDNode::get(Context, V);
188   Metadata *const c1 = n1;
189   MDNode *n2 = MDNode::get(Context, c1);
190   Metadata *const c2 = n2;
191   MDNode *n3 = MDNode::get(Context, V);
192   MDNode *n4 = MDNode::getIfExists(Context, V);
193   MDNode *n5 = MDNode::getIfExists(Context, c1);
194   MDNode *n6 = MDNode::getIfExists(Context, c2);
195   EXPECT_NE(n1, n2);
196   EXPECT_EQ(n1, n3);
197   EXPECT_EQ(n4, n1);
198   EXPECT_EQ(n5, n2);
199   EXPECT_EQ(n6, (Metadata *)nullptr);
200
201   EXPECT_EQ(3u, n1->getNumOperands());
202   EXPECT_EQ(s1, n1->getOperand(0));
203   EXPECT_EQ(CI, n1->getOperand(1));
204   EXPECT_EQ(s2, n1->getOperand(2));
205
206   EXPECT_EQ(1u, n2->getNumOperands());
207   EXPECT_EQ(n1, n2->getOperand(0));
208 }
209
210 TEST_F(MDNodeTest, Delete) {
211   Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 1);
212   Instruction *I = new BitCastInst(C, Type::getInt32Ty(Context));
213
214   Metadata *const V = LocalAsMetadata::get(I);
215   MDNode *n = MDNode::get(Context, V);
216   TrackingMDRef wvh(n);
217
218   EXPECT_EQ(n, wvh);
219
220   delete I;
221 }
222
223 TEST_F(MDNodeTest, SelfReference) {
224   // !0 = !{!0}
225   // !1 = !{!0}
226   {
227     auto Temp = MDNode::getTemporary(Context, None);
228     Metadata *Args[] = {Temp.get()};
229     MDNode *Self = MDNode::get(Context, Args);
230     Self->replaceOperandWith(0, Self);
231     ASSERT_EQ(Self, Self->getOperand(0));
232
233     // Self-references should be distinct, so MDNode::get() should grab a
234     // uniqued node that references Self, not Self.
235     Args[0] = Self;
236     MDNode *Ref1 = MDNode::get(Context, Args);
237     MDNode *Ref2 = MDNode::get(Context, Args);
238     EXPECT_NE(Self, Ref1);
239     EXPECT_EQ(Ref1, Ref2);
240   }
241
242   // !0 = !{!0, !{}}
243   // !1 = !{!0, !{}}
244   {
245     auto Temp = MDNode::getTemporary(Context, None);
246     Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)};
247     MDNode *Self = MDNode::get(Context, Args);
248     Self->replaceOperandWith(0, Self);
249     ASSERT_EQ(Self, Self->getOperand(0));
250
251     // Self-references should be distinct, so MDNode::get() should grab a
252     // uniqued node that references Self, not Self itself.
253     Args[0] = Self;
254     MDNode *Ref1 = MDNode::get(Context, Args);
255     MDNode *Ref2 = MDNode::get(Context, Args);
256     EXPECT_NE(Self, Ref1);
257     EXPECT_EQ(Ref1, Ref2);
258   }
259 }
260
261 TEST_F(MDNodeTest, Print) {
262   Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
263   MDString *S = MDString::get(Context, "foo");
264   MDNode *N0 = getNode();
265   MDNode *N1 = getNode(N0);
266   MDNode *N2 = getNode(N0, N1);
267
268   Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
269   MDNode *N = MDNode::get(Context, Args);
270
271   std::string Expected;
272   {
273     raw_string_ostream OS(Expected);
274     OS << "<" << (void *)N << "> = !{";
275     C->printAsOperand(OS);
276     OS << ", ";
277     S->printAsOperand(OS);
278     OS << ", null";
279     MDNode *Nodes[] = {N0, N1, N2};
280     for (auto *Node : Nodes)
281       OS << ", <" << (void *)Node << ">";
282     OS << "}";
283   }
284
285   std::string Actual;
286   {
287     raw_string_ostream OS(Actual);
288     N->print(OS);
289   }
290
291   EXPECT_EQ(Expected, Actual);
292 }
293
294 #define EXPECT_PRINTER_EQ(EXPECTED, PRINT)                                     \
295   do {                                                                         \
296     std::string Actual_;                                                       \
297     raw_string_ostream OS(Actual_);                                            \
298     PRINT;                                                                     \
299     OS.flush();                                                                \
300     std::string Expected_(EXPECTED);                                           \
301     EXPECT_EQ(Expected_, Actual_);                                             \
302   } while (false)
303
304 TEST_F(MDNodeTest, PrintTemporary) {
305   MDNode *Arg = getNode();
306   TempMDNode Temp = MDNode::getTemporary(Context, Arg);
307   MDNode *N = getNode(Temp.get());
308   Module M("test", Context);
309   NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
310   NMD->addOperand(N);
311
312   EXPECT_PRINTER_EQ("!0 = !{!1}", N->print(OS, &M));
313   EXPECT_PRINTER_EQ("!1 = <temporary!> !{!2}", Temp->print(OS, &M));
314   EXPECT_PRINTER_EQ("!2 = !{}", Arg->print(OS, &M));
315
316   // Cleanup.
317   Temp->replaceAllUsesWith(Arg);
318 }
319
320 TEST_F(MDNodeTest, PrintFromModule) {
321   Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
322   MDString *S = MDString::get(Context, "foo");
323   MDNode *N0 = getNode();
324   MDNode *N1 = getNode(N0);
325   MDNode *N2 = getNode(N0, N1);
326
327   Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
328   MDNode *N = MDNode::get(Context, Args);
329   Module M("test", Context);
330   NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
331   NMD->addOperand(N);
332
333   std::string Expected;
334   {
335     raw_string_ostream OS(Expected);
336     OS << "!0 = !{";
337     C->printAsOperand(OS);
338     OS << ", ";
339     S->printAsOperand(OS);
340     OS << ", null, !1, !2, !3}";
341   }
342
343   EXPECT_PRINTER_EQ(Expected, N->print(OS, &M));
344 }
345
346 TEST_F(MDNodeTest, PrintFromFunction) {
347   Module M("test", Context);
348   auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
349   auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
350   auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
351   auto *BB0 = BasicBlock::Create(Context, "entry", F0);
352   auto *BB1 = BasicBlock::Create(Context, "entry", F1);
353   auto *R0 = ReturnInst::Create(Context, BB0);
354   auto *R1 = ReturnInst::Create(Context, BB1);
355   auto *N0 = MDNode::getDistinct(Context, None);
356   auto *N1 = MDNode::getDistinct(Context, None);
357   R0->setMetadata("md", N0);
358   R1->setMetadata("md", N1);
359
360   EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, &M));
361   EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, &M));
362
363   ModuleSlotTracker MST(&M);
364   EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, MST));
365   EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, MST));
366 }
367
368 TEST_F(MDNodeTest, PrintFromMetadataAsValue) {
369   Module M("test", Context);
370
371   auto *Intrinsic =
372       Function::Create(FunctionType::get(Type::getVoidTy(Context),
373                                          Type::getMetadataTy(Context), false),
374                        GlobalValue::ExternalLinkage, "llvm.intrinsic", &M);
375
376   auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
377   auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
378   auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
379   auto *BB0 = BasicBlock::Create(Context, "entry", F0);
380   auto *BB1 = BasicBlock::Create(Context, "entry", F1);
381   auto *N0 = MDNode::getDistinct(Context, None);
382   auto *N1 = MDNode::getDistinct(Context, None);
383   auto *MAV0 = MetadataAsValue::get(Context, N0);
384   auto *MAV1 = MetadataAsValue::get(Context, N1);
385   CallInst::Create(Intrinsic, MAV0, "", BB0);
386   CallInst::Create(Intrinsic, MAV1, "", BB1);
387
388   EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS));
389   EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS));
390   EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false));
391   EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false));
392   EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true));
393   EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true));
394
395   ModuleSlotTracker MST(&M);
396   EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS, MST));
397   EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS, MST));
398   EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false, MST));
399   EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false, MST));
400   EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true, MST));
401   EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true, MST));
402 }
403 #undef EXPECT_PRINTER_EQ
404
405 TEST_F(MDNodeTest, NullOperand) {
406   // metadata !{}
407   MDNode *Empty = MDNode::get(Context, None);
408
409   // metadata !{metadata !{}}
410   Metadata *Ops[] = {Empty};
411   MDNode *N = MDNode::get(Context, Ops);
412   ASSERT_EQ(Empty, N->getOperand(0));
413
414   // metadata !{metadata !{}} => metadata !{null}
415   N->replaceOperandWith(0, nullptr);
416   ASSERT_EQ(nullptr, N->getOperand(0));
417
418   // metadata !{null}
419   Ops[0] = nullptr;
420   MDNode *NullOp = MDNode::get(Context, Ops);
421   ASSERT_EQ(nullptr, NullOp->getOperand(0));
422   EXPECT_EQ(N, NullOp);
423 }
424
425 TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
426   // !{}
427   MDNode *Empty = MDNode::get(Context, None);
428   ASSERT_TRUE(Empty->isResolved());
429   EXPECT_FALSE(Empty->isDistinct());
430
431   // !{!{}}
432   Metadata *Wrapped1Ops[] = {Empty};
433   MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
434   ASSERT_EQ(Empty, Wrapped1->getOperand(0));
435   ASSERT_TRUE(Wrapped1->isResolved());
436   EXPECT_FALSE(Wrapped1->isDistinct());
437
438   // !{!{!{}}}
439   Metadata *Wrapped2Ops[] = {Wrapped1};
440   MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
441   ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
442   ASSERT_TRUE(Wrapped2->isResolved());
443   EXPECT_FALSE(Wrapped2->isDistinct());
444
445   // !{!{!{}}} => !{!{}}
446   Wrapped2->replaceOperandWith(0, Empty);
447   ASSERT_EQ(Empty, Wrapped2->getOperand(0));
448   EXPECT_TRUE(Wrapped2->isDistinct());
449   EXPECT_FALSE(Wrapped1->isDistinct());
450 }
451
452 TEST_F(MDNodeTest, UniquedOnDeletedOperand) {
453   // temp !{}
454   TempMDTuple T = MDTuple::getTemporary(Context, None);
455
456   // !{temp !{}}
457   Metadata *Ops[] = {T.get()};
458   MDTuple *N = MDTuple::get(Context, Ops);
459
460   // !{temp !{}} => !{null}
461   T.reset();
462   ASSERT_TRUE(N->isUniqued());
463   Metadata *NullOps[] = {nullptr};
464   ASSERT_EQ(N, MDTuple::get(Context, NullOps));
465 }
466
467 TEST_F(MDNodeTest, DistinctOnDeletedValueOperand) {
468   // i1* @GV
469   Type *Ty = Type::getInt1PtrTy(Context);
470   std::unique_ptr<GlobalVariable> GV(
471       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
472   ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get());
473
474   // !{i1* @GV}
475   Metadata *Ops[] = {Op};
476   MDTuple *N = MDTuple::get(Context, Ops);
477
478   // !{i1* @GV} => !{null}
479   GV.reset();
480   ASSERT_TRUE(N->isDistinct());
481   ASSERT_EQ(nullptr, N->getOperand(0));
482   Metadata *NullOps[] = {nullptr};
483   ASSERT_NE(N, MDTuple::get(Context, NullOps));
484 }
485
486 TEST_F(MDNodeTest, getDistinct) {
487   // !{}
488   MDNode *Empty = MDNode::get(Context, None);
489   ASSERT_TRUE(Empty->isResolved());
490   ASSERT_FALSE(Empty->isDistinct());
491   ASSERT_EQ(Empty, MDNode::get(Context, None));
492
493   // distinct !{}
494   MDNode *Distinct1 = MDNode::getDistinct(Context, None);
495   MDNode *Distinct2 = MDNode::getDistinct(Context, None);
496   EXPECT_TRUE(Distinct1->isResolved());
497   EXPECT_TRUE(Distinct2->isDistinct());
498   EXPECT_NE(Empty, Distinct1);
499   EXPECT_NE(Empty, Distinct2);
500   EXPECT_NE(Distinct1, Distinct2);
501
502   // !{}
503   ASSERT_EQ(Empty, MDNode::get(Context, None));
504 }
505
506 TEST_F(MDNodeTest, isUniqued) {
507   MDNode *U = MDTuple::get(Context, None);
508   MDNode *D = MDTuple::getDistinct(Context, None);
509   auto T = MDTuple::getTemporary(Context, None);
510   EXPECT_TRUE(U->isUniqued());
511   EXPECT_FALSE(D->isUniqued());
512   EXPECT_FALSE(T->isUniqued());
513 }
514
515 TEST_F(MDNodeTest, isDistinct) {
516   MDNode *U = MDTuple::get(Context, None);
517   MDNode *D = MDTuple::getDistinct(Context, None);
518   auto T = MDTuple::getTemporary(Context, None);
519   EXPECT_FALSE(U->isDistinct());
520   EXPECT_TRUE(D->isDistinct());
521   EXPECT_FALSE(T->isDistinct());
522 }
523
524 TEST_F(MDNodeTest, isTemporary) {
525   MDNode *U = MDTuple::get(Context, None);
526   MDNode *D = MDTuple::getDistinct(Context, None);
527   auto T = MDTuple::getTemporary(Context, None);
528   EXPECT_FALSE(U->isTemporary());
529   EXPECT_FALSE(D->isTemporary());
530   EXPECT_TRUE(T->isTemporary());
531 }
532
533 TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
534   // temporary !{}
535   auto Temp = MDTuple::getTemporary(Context, None);
536   ASSERT_FALSE(Temp->isResolved());
537
538   // distinct !{temporary !{}}
539   Metadata *Ops[] = {Temp.get()};
540   MDNode *Distinct = MDNode::getDistinct(Context, Ops);
541   EXPECT_TRUE(Distinct->isResolved());
542   EXPECT_EQ(Temp.get(), Distinct->getOperand(0));
543
544   // temporary !{} => !{}
545   MDNode *Empty = MDNode::get(Context, None);
546   Temp->replaceAllUsesWith(Empty);
547   EXPECT_EQ(Empty, Distinct->getOperand(0));
548 }
549
550 TEST_F(MDNodeTest, handleChangedOperandRecursion) {
551   // !0 = !{}
552   MDNode *N0 = MDNode::get(Context, None);
553
554   // !1 = !{!3, null}
555   auto Temp3 = MDTuple::getTemporary(Context, None);
556   Metadata *Ops1[] = {Temp3.get(), nullptr};
557   MDNode *N1 = MDNode::get(Context, Ops1);
558
559   // !2 = !{!3, !0}
560   Metadata *Ops2[] = {Temp3.get(), N0};
561   MDNode *N2 = MDNode::get(Context, Ops2);
562
563   // !3 = !{!2}
564   Metadata *Ops3[] = {N2};
565   MDNode *N3 = MDNode::get(Context, Ops3);
566   Temp3->replaceAllUsesWith(N3);
567
568   // !4 = !{!1}
569   Metadata *Ops4[] = {N1};
570   MDNode *N4 = MDNode::get(Context, Ops4);
571
572   // Confirm that the cycle prevented RAUW from getting dropped.
573   EXPECT_TRUE(N0->isResolved());
574   EXPECT_FALSE(N1->isResolved());
575   EXPECT_FALSE(N2->isResolved());
576   EXPECT_FALSE(N3->isResolved());
577   EXPECT_FALSE(N4->isResolved());
578
579   // Create a couple of distinct nodes to observe what's going on.
580   //
581   // !5 = distinct !{!2}
582   // !6 = distinct !{!3}
583   Metadata *Ops5[] = {N2};
584   MDNode *N5 = MDNode::getDistinct(Context, Ops5);
585   Metadata *Ops6[] = {N3};
586   MDNode *N6 = MDNode::getDistinct(Context, Ops6);
587
588   // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
589   // This will ripple up, with !3 colliding with !4, and RAUWing.  Since !2
590   // references !3, this can cause a re-entry of handleChangedOperand() when !3
591   // is not ready for it.
592   //
593   // !2->replaceOperandWith(1, nullptr)
594   // !2: !{!3, !0} => !{!3, null}
595   // !2->replaceAllUsesWith(!1)
596   // !3: !{!2] => !{!1}
597   // !3->replaceAllUsesWith(!4)
598   N2->replaceOperandWith(1, nullptr);
599
600   // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
601   // under us.  Just check that the other nodes are sane.
602   //
603   // !1 = !{!4, null}
604   // !4 = !{!1}
605   // !5 = distinct !{!1}
606   // !6 = distinct !{!4}
607   EXPECT_EQ(N4, N1->getOperand(0));
608   EXPECT_EQ(N1, N4->getOperand(0));
609   EXPECT_EQ(N1, N5->getOperand(0));
610   EXPECT_EQ(N4, N6->getOperand(0));
611 }
612
613 TEST_F(MDNodeTest, replaceResolvedOperand) {
614   // Check code for replacing one resolved operand with another.  If doing this
615   // directly (via replaceOperandWith()) becomes illegal, change the operand to
616   // a global value that gets RAUW'ed.
617   //
618   // Use a temporary node to keep N from being resolved.
619   auto Temp = MDTuple::getTemporary(Context, None);
620   Metadata *Ops[] = {nullptr, Temp.get()};
621
622   MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>());
623   MDNode *N = MDTuple::get(Context, Ops);
624   EXPECT_EQ(nullptr, N->getOperand(0));
625   ASSERT_FALSE(N->isResolved());
626
627   // Check code for replacing resolved nodes.
628   N->replaceOperandWith(0, Empty);
629   EXPECT_EQ(Empty, N->getOperand(0));
630
631   // Check code for adding another unresolved operand.
632   N->replaceOperandWith(0, Temp.get());
633   EXPECT_EQ(Temp.get(), N->getOperand(0));
634
635   // Remove the references to Temp; required for teardown.
636   Temp->replaceAllUsesWith(nullptr);
637 }
638
639 TEST_F(MDNodeTest, replaceWithUniqued) {
640   auto *Empty = MDTuple::get(Context, None);
641   MDTuple *FirstUniqued;
642   {
643     Metadata *Ops[] = {Empty};
644     auto Temp = MDTuple::getTemporary(Context, Ops);
645     EXPECT_TRUE(Temp->isTemporary());
646
647     // Don't expect a collision.
648     auto *Current = Temp.get();
649     FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp));
650     EXPECT_TRUE(FirstUniqued->isUniqued());
651     EXPECT_TRUE(FirstUniqued->isResolved());
652     EXPECT_EQ(Current, FirstUniqued);
653   }
654   {
655     Metadata *Ops[] = {Empty};
656     auto Temp = MDTuple::getTemporary(Context, Ops);
657     EXPECT_TRUE(Temp->isTemporary());
658
659     // Should collide with Uniqued above this time.
660     auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
661     EXPECT_TRUE(Uniqued->isUniqued());
662     EXPECT_TRUE(Uniqued->isResolved());
663     EXPECT_EQ(FirstUniqued, Uniqued);
664   }
665   {
666     auto Unresolved = MDTuple::getTemporary(Context, None);
667     Metadata *Ops[] = {Unresolved.get()};
668     auto Temp = MDTuple::getTemporary(Context, Ops);
669     EXPECT_TRUE(Temp->isTemporary());
670
671     // Shouldn't be resolved.
672     auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
673     EXPECT_TRUE(Uniqued->isUniqued());
674     EXPECT_FALSE(Uniqued->isResolved());
675
676     // Should be a different node.
677     EXPECT_NE(FirstUniqued, Uniqued);
678
679     // Should resolve when we update its node (note: be careful to avoid a
680     // collision with any other nodes above).
681     Uniqued->replaceOperandWith(0, nullptr);
682     EXPECT_TRUE(Uniqued->isResolved());
683   }
684 }
685
686 TEST_F(MDNodeTest, replaceWithUniquedResolvingOperand) {
687   // temp !{}
688   MDTuple *Op = MDTuple::getTemporary(Context, None).release();
689   EXPECT_FALSE(Op->isResolved());
690
691   // temp !{temp !{}}
692   Metadata *Ops[] = {Op};
693   MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
694   EXPECT_FALSE(N->isResolved());
695
696   // temp !{temp !{}} => !{temp !{}}
697   ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
698   EXPECT_FALSE(N->isResolved());
699
700   // !{temp !{}} => !{!{}}
701   ASSERT_EQ(Op, MDNode::replaceWithUniqued(TempMDTuple(Op)));
702   EXPECT_TRUE(Op->isResolved());
703   EXPECT_TRUE(N->isResolved());
704 }
705
706 TEST_F(MDNodeTest, replaceWithUniquedDeletedOperand) {
707   // i1* @GV
708   Type *Ty = Type::getInt1PtrTy(Context);
709   std::unique_ptr<GlobalVariable> GV(
710       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
711   ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get());
712
713   // temp !{i1* @GV}
714   Metadata *Ops[] = {Op};
715   MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
716
717   // temp !{i1* @GV} => !{i1* @GV}
718   ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
719   ASSERT_TRUE(N->isUniqued());
720
721   // !{i1* @GV} => !{null}
722   GV.reset();
723   ASSERT_TRUE(N->isDistinct());
724   ASSERT_EQ(nullptr, N->getOperand(0));
725   Metadata *NullOps[] = {nullptr};
726   ASSERT_NE(N, MDTuple::get(Context, NullOps));
727 }
728
729 TEST_F(MDNodeTest, replaceWithUniquedChangedOperand) {
730   // i1* @GV
731   Type *Ty = Type::getInt1PtrTy(Context);
732   std::unique_ptr<GlobalVariable> GV(
733       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
734   ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get());
735
736   // temp !{i1* @GV}
737   Metadata *Ops[] = {Op};
738   MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
739
740   // temp !{i1* @GV} => !{i1* @GV}
741   ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
742   ASSERT_TRUE(N->isUniqued());
743
744   // !{i1* @GV} => !{i1* @GV2}
745   std::unique_ptr<GlobalVariable> GV2(
746       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
747   GV->replaceAllUsesWith(GV2.get());
748   ASSERT_TRUE(N->isUniqued());
749   Metadata *NullOps[] = {ConstantAsMetadata::get(GV2.get())};
750   ASSERT_EQ(N, MDTuple::get(Context, NullOps));
751 }
752
753 TEST_F(MDNodeTest, replaceWithDistinct) {
754   {
755     auto *Empty = MDTuple::get(Context, None);
756     Metadata *Ops[] = {Empty};
757     auto Temp = MDTuple::getTemporary(Context, Ops);
758     EXPECT_TRUE(Temp->isTemporary());
759
760     // Don't expect a collision.
761     auto *Current = Temp.get();
762     auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
763     EXPECT_TRUE(Distinct->isDistinct());
764     EXPECT_TRUE(Distinct->isResolved());
765     EXPECT_EQ(Current, Distinct);
766   }
767   {
768     auto Unresolved = MDTuple::getTemporary(Context, None);
769     Metadata *Ops[] = {Unresolved.get()};
770     auto Temp = MDTuple::getTemporary(Context, Ops);
771     EXPECT_TRUE(Temp->isTemporary());
772
773     // Don't expect a collision.
774     auto *Current = Temp.get();
775     auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
776     EXPECT_TRUE(Distinct->isDistinct());
777     EXPECT_TRUE(Distinct->isResolved());
778     EXPECT_EQ(Current, Distinct);
779
780     // Cleanup; required for teardown.
781     Unresolved->replaceAllUsesWith(nullptr);
782   }
783 }
784
785 TEST_F(MDNodeTest, replaceWithPermanent) {
786   Metadata *Ops[] = {nullptr};
787   auto Temp = MDTuple::getTemporary(Context, Ops);
788   auto *T = Temp.get();
789
790   // U is a normal, uniqued node that references T.
791   auto *U = MDTuple::get(Context, T);
792   EXPECT_TRUE(U->isUniqued());
793
794   // Make Temp self-referencing.
795   Temp->replaceOperandWith(0, T);
796
797   // Try to uniquify Temp.  This should, despite the name in the API, give a
798   // 'distinct' node, since self-references aren't allowed to be uniqued.
799   //
800   // Since it's distinct, N should have the same address as when it was a
801   // temporary (i.e., be equal to T not U).
802   auto *N = MDNode::replaceWithPermanent(std::move(Temp));
803   EXPECT_EQ(N, T);
804   EXPECT_TRUE(N->isDistinct());
805
806   // U should be the canonical unique node with N as the argument.
807   EXPECT_EQ(U, MDTuple::get(Context, N));
808   EXPECT_TRUE(U->isUniqued());
809
810   // This temporary should collide with U when replaced, but it should still be
811   // uniqued.
812   EXPECT_EQ(U, MDNode::replaceWithPermanent(MDTuple::getTemporary(Context, N)));
813   EXPECT_TRUE(U->isUniqued());
814
815   // This temporary should become a new uniqued node.
816   auto Temp2 = MDTuple::getTemporary(Context, U);
817   auto *V = Temp2.get();
818   EXPECT_EQ(V, MDNode::replaceWithPermanent(std::move(Temp2)));
819   EXPECT_TRUE(V->isUniqued());
820   EXPECT_EQ(U, V->getOperand(0));
821 }
822
823 TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) {
824   TrackingMDRef Ref;
825   EXPECT_EQ(nullptr, Ref.get());
826   {
827     auto Temp = MDTuple::getTemporary(Context, None);
828     Ref.reset(Temp.get());
829     EXPECT_EQ(Temp.get(), Ref.get());
830   }
831   EXPECT_EQ(nullptr, Ref.get());
832 }
833
834 typedef MetadataTest DILocationTest;
835
836 TEST_F(DILocationTest, Overflow) {
837   DISubprogram *N = getSubprogram();
838   {
839     DILocation *L = DILocation::get(Context, 2, 7, N);
840     EXPECT_EQ(2u, L->getLine());
841     EXPECT_EQ(7u, L->getColumn());
842   }
843   unsigned U16 = 1u << 16;
844   {
845     DILocation *L = DILocation::get(Context, UINT32_MAX, U16 - 1, N);
846     EXPECT_EQ(UINT32_MAX, L->getLine());
847     EXPECT_EQ(U16 - 1, L->getColumn());
848   }
849   {
850     DILocation *L = DILocation::get(Context, UINT32_MAX, U16, N);
851     EXPECT_EQ(UINT32_MAX, L->getLine());
852     EXPECT_EQ(0u, L->getColumn());
853   }
854   {
855     DILocation *L = DILocation::get(Context, UINT32_MAX, U16 + 1, N);
856     EXPECT_EQ(UINT32_MAX, L->getLine());
857     EXPECT_EQ(0u, L->getColumn());
858   }
859 }
860
861 TEST_F(DILocationTest, getDistinct) {
862   MDNode *N = getSubprogram();
863   DILocation *L0 = DILocation::getDistinct(Context, 2, 7, N);
864   EXPECT_TRUE(L0->isDistinct());
865   DILocation *L1 = DILocation::get(Context, 2, 7, N);
866   EXPECT_FALSE(L1->isDistinct());
867   EXPECT_EQ(L1, DILocation::get(Context, 2, 7, N));
868 }
869
870 TEST_F(DILocationTest, getTemporary) {
871   MDNode *N = MDNode::get(Context, None);
872   auto L = DILocation::getTemporary(Context, 2, 7, N);
873   EXPECT_TRUE(L->isTemporary());
874   EXPECT_FALSE(L->isResolved());
875 }
876
877 TEST_F(DILocationTest, cloneTemporary) {
878   MDNode *N = MDNode::get(Context, None);
879   auto L = DILocation::getTemporary(Context, 2, 7, N);
880   EXPECT_TRUE(L->isTemporary());
881   auto L2 = L->clone();
882   EXPECT_TRUE(L2->isTemporary());
883 }
884
885 typedef MetadataTest GenericDINodeTest;
886
887 TEST_F(GenericDINodeTest, get) {
888   StringRef Header = "header";
889   auto *Empty = MDNode::get(Context, None);
890   Metadata *Ops1[] = {Empty};
891   auto *N = GenericDINode::get(Context, 15, Header, Ops1);
892   EXPECT_EQ(15u, N->getTag());
893   EXPECT_EQ(2u, N->getNumOperands());
894   EXPECT_EQ(Header, N->getHeader());
895   EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0));
896   EXPECT_EQ(1u, N->getNumDwarfOperands());
897   EXPECT_EQ(Empty, N->getDwarfOperand(0));
898   EXPECT_EQ(Empty, N->getOperand(1));
899   ASSERT_TRUE(N->isUniqued());
900
901   EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops1));
902
903   N->replaceOperandWith(1, nullptr);
904   EXPECT_EQ(15u, N->getTag());
905   EXPECT_EQ(Header, N->getHeader());
906   EXPECT_EQ(nullptr, N->getDwarfOperand(0));
907   ASSERT_TRUE(N->isUniqued());
908
909   Metadata *Ops2[] = {nullptr};
910   EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops2));
911
912   N->replaceDwarfOperandWith(0, Empty);
913   EXPECT_EQ(15u, N->getTag());
914   EXPECT_EQ(Header, N->getHeader());
915   EXPECT_EQ(Empty, N->getDwarfOperand(0));
916   ASSERT_TRUE(N->isUniqued());
917   EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops1));
918
919   TempGenericDINode Temp = N->clone();
920   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
921 }
922
923 TEST_F(GenericDINodeTest, getEmptyHeader) {
924   // Canonicalize !"" to null.
925   auto *N = GenericDINode::get(Context, 15, StringRef(), None);
926   EXPECT_EQ(StringRef(), N->getHeader());
927   EXPECT_EQ(nullptr, N->getOperand(0));
928 }
929
930 typedef MetadataTest DISubrangeTest;
931
932 TEST_F(DISubrangeTest, get) {
933   auto *N = DISubrange::get(Context, 5, 7);
934   EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
935   EXPECT_EQ(5, N->getCount());
936   EXPECT_EQ(7, N->getLowerBound());
937   EXPECT_EQ(N, DISubrange::get(Context, 5, 7));
938   EXPECT_EQ(DISubrange::get(Context, 5, 0), DISubrange::get(Context, 5));
939
940   TempDISubrange Temp = N->clone();
941   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
942 }
943
944 TEST_F(DISubrangeTest, getEmptyArray) {
945   auto *N = DISubrange::get(Context, -1, 0);
946   EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
947   EXPECT_EQ(-1, N->getCount());
948   EXPECT_EQ(0, N->getLowerBound());
949   EXPECT_EQ(N, DISubrange::get(Context, -1, 0));
950 }
951
952 typedef MetadataTest DIEnumeratorTest;
953
954 TEST_F(DIEnumeratorTest, get) {
955   auto *N = DIEnumerator::get(Context, 7, "name");
956   EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag());
957   EXPECT_EQ(7, N->getValue());
958   EXPECT_EQ("name", N->getName());
959   EXPECT_EQ(N, DIEnumerator::get(Context, 7, "name"));
960
961   EXPECT_NE(N, DIEnumerator::get(Context, 8, "name"));
962   EXPECT_NE(N, DIEnumerator::get(Context, 7, "nam"));
963
964   TempDIEnumerator Temp = N->clone();
965   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
966 }
967
968 typedef MetadataTest DIBasicTypeTest;
969
970 TEST_F(DIBasicTypeTest, get) {
971   auto *N =
972       DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7);
973   EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
974   EXPECT_EQ("special", N->getName());
975   EXPECT_EQ(33u, N->getSizeInBits());
976   EXPECT_EQ(26u, N->getAlignInBits());
977   EXPECT_EQ(7u, N->getEncoding());
978   EXPECT_EQ(0u, N->getLine());
979   EXPECT_EQ(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
980                                 26, 7));
981
982   EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
983                                 "special", 33, 26, 7));
984   EXPECT_NE(N,
985             DIBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7));
986   EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
987                                 26, 7));
988   EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
989                                 25, 7));
990   EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
991                                 26, 6));
992
993   TempDIBasicType Temp = N->clone();
994   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
995 }
996
997 TEST_F(DIBasicTypeTest, getWithLargeValues) {
998   auto *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special",
999                              UINT64_MAX, UINT64_MAX - 1, 7);
1000   EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
1001   EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
1002 }
1003
1004 TEST_F(DIBasicTypeTest, getUnspecified) {
1005   auto *N =
1006       DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, "unspecified");
1007   EXPECT_EQ(dwarf::DW_TAG_unspecified_type, N->getTag());
1008   EXPECT_EQ("unspecified", N->getName());
1009   EXPECT_EQ(0u, N->getSizeInBits());
1010   EXPECT_EQ(0u, N->getAlignInBits());
1011   EXPECT_EQ(0u, N->getEncoding());
1012   EXPECT_EQ(0u, N->getLine());
1013 }
1014
1015 typedef MetadataTest DITypeTest;
1016
1017 TEST_F(DITypeTest, clone) {
1018   // Check that DIType has a specialized clone that returns TempDIType.
1019   DIType *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "int", 32, 32,
1020                                dwarf::DW_ATE_signed);
1021
1022   TempDIType Temp = N->clone();
1023   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1024 }
1025
1026 TEST_F(DITypeTest, setFlags) {
1027   // void (void)
1028   Metadata *TypesOps[] = {nullptr};
1029   Metadata *Types = MDTuple::get(Context, TypesOps);
1030
1031   DIType *D = DISubroutineType::getDistinct(Context, 0u, 0, Types);
1032   EXPECT_EQ(0u, D->getFlags());
1033   D->setFlags(DINode::FlagRValueReference);
1034   EXPECT_EQ(DINode::FlagRValueReference, D->getFlags());
1035   D->setFlags(0u);
1036   EXPECT_EQ(0u, D->getFlags());
1037
1038   TempDIType T = DISubroutineType::getTemporary(Context, 0u, 0, Types);
1039   EXPECT_EQ(0u, T->getFlags());
1040   T->setFlags(DINode::FlagRValueReference);
1041   EXPECT_EQ(DINode::FlagRValueReference, T->getFlags());
1042   T->setFlags(0u);
1043   EXPECT_EQ(0u, T->getFlags());
1044 }
1045
1046 typedef MetadataTest DIDerivedTypeTest;
1047
1048 TEST_F(DIDerivedTypeTest, get) {
1049   DIFile *File = getFile();
1050   DIScope *Scope = getSubprogram();
1051   DIType *BaseType = getBasicType("basic");
1052   MDTuple *ExtraData = getTuple();
1053
1054   auto *N = DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
1055                                File, 1, Scope, BaseType, 2, 3, 4, 5, ExtraData);
1056   EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag());
1057   EXPECT_EQ("something", N->getName());
1058   EXPECT_EQ(File, N->getFile());
1059   EXPECT_EQ(1u, N->getLine());
1060   EXPECT_EQ(Scope, N->getScope());
1061   EXPECT_EQ(BaseType, N->getBaseType());
1062   EXPECT_EQ(2u, N->getSizeInBits());
1063   EXPECT_EQ(3u, N->getAlignInBits());
1064   EXPECT_EQ(4u, N->getOffsetInBits());
1065   EXPECT_EQ(5u, N->getFlags());
1066   EXPECT_EQ(ExtraData, N->getExtraData());
1067   EXPECT_EQ(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1068                                   "something", File, 1, Scope, BaseType, 2, 3,
1069                                   4, 5, ExtraData));
1070
1071   EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_reference_type,
1072                                   "something", File, 1, Scope, BaseType, 2, 3,
1073                                   4, 5, ExtraData));
1074   EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
1075                                   File, 1, Scope, BaseType, 2, 3, 4, 5,
1076                                   ExtraData));
1077   EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1078                                   "something", getFile(), 1, Scope, BaseType, 2,
1079                                   3, 4, 5, ExtraData));
1080   EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1081                                   "something", File, 2, Scope, BaseType, 2, 3,
1082                                   4, 5, ExtraData));
1083   EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1084                                   "something", File, 1, getSubprogram(),
1085                                   BaseType, 2, 3, 4, 5, ExtraData));
1086   EXPECT_NE(N, DIDerivedType::get(
1087                    Context, dwarf::DW_TAG_pointer_type, "something", File, 1,
1088                    Scope, getBasicType("basic2"), 2, 3, 4, 5, ExtraData));
1089   EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1090                                   "something", File, 1, Scope, BaseType, 3, 3,
1091                                   4, 5, ExtraData));
1092   EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1093                                   "something", File, 1, Scope, BaseType, 2, 2,
1094                                   4, 5, ExtraData));
1095   EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1096                                   "something", File, 1, Scope, BaseType, 2, 3,
1097                                   5, 5, ExtraData));
1098   EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1099                                   "something", File, 1, Scope, BaseType, 2, 3,
1100                                   4, 4, ExtraData));
1101   EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
1102                                   "something", File, 1, Scope, BaseType, 2, 3,
1103                                   4, 5, getTuple()));
1104
1105   TempDIDerivedType Temp = N->clone();
1106   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1107 }
1108
1109 TEST_F(DIDerivedTypeTest, getWithLargeValues) {
1110   DIFile *File = getFile();
1111   DIScope *Scope = getSubprogram();
1112   DIType *BaseType = getBasicType("basic");
1113   MDTuple *ExtraData = getTuple();
1114
1115   auto *N = DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
1116                                File, 1, Scope, BaseType, UINT64_MAX,
1117                                UINT64_MAX - 1, UINT64_MAX - 2, 5, ExtraData);
1118   EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
1119   EXPECT_EQ(UINT64_MAX - 1, N->getAlignInBits());
1120   EXPECT_EQ(UINT64_MAX - 2, N->getOffsetInBits());
1121 }
1122
1123 typedef MetadataTest DICompositeTypeTest;
1124
1125 TEST_F(DICompositeTypeTest, get) {
1126   unsigned Tag = dwarf::DW_TAG_structure_type;
1127   StringRef Name = "some name";
1128   DIFile *File = getFile();
1129   unsigned Line = 1;
1130   DIScope *Scope = getSubprogram();
1131   DIType *BaseType = getCompositeType();
1132   uint64_t SizeInBits = 2;
1133   uint64_t AlignInBits = 3;
1134   uint64_t OffsetInBits = 4;
1135   unsigned Flags = 5;
1136   MDTuple *Elements = getTuple();
1137   unsigned RuntimeLang = 6;
1138   DIType *VTableHolder = getCompositeType();
1139   MDTuple *TemplateParams = getTuple();
1140   StringRef Identifier = "some id";
1141
1142   auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1143                                  BaseType, SizeInBits, AlignInBits,
1144                                  OffsetInBits, Flags, Elements, RuntimeLang,
1145                                  VTableHolder, TemplateParams, Identifier);
1146   EXPECT_EQ(Tag, N->getTag());
1147   EXPECT_EQ(Name, N->getName());
1148   EXPECT_EQ(File, N->getFile());
1149   EXPECT_EQ(Line, N->getLine());
1150   EXPECT_EQ(Scope, N->getScope());
1151   EXPECT_EQ(BaseType, N->getBaseType());
1152   EXPECT_EQ(SizeInBits, N->getSizeInBits());
1153   EXPECT_EQ(AlignInBits, N->getAlignInBits());
1154   EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1155   EXPECT_EQ(Flags, N->getFlags());
1156   EXPECT_EQ(Elements, N->getElements().get());
1157   EXPECT_EQ(RuntimeLang, N->getRuntimeLang());
1158   EXPECT_EQ(VTableHolder, N->getVTableHolder());
1159   EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
1160   EXPECT_EQ(Identifier, N->getIdentifier());
1161
1162   EXPECT_EQ(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1163                                     BaseType, SizeInBits, AlignInBits,
1164                                     OffsetInBits, Flags, Elements, RuntimeLang,
1165                                     VTableHolder, TemplateParams, Identifier));
1166
1167   EXPECT_NE(N, DICompositeType::get(Context, Tag + 1, Name, File, Line, Scope,
1168                                     BaseType, SizeInBits, AlignInBits,
1169                                     OffsetInBits, Flags, Elements, RuntimeLang,
1170                                     VTableHolder, TemplateParams, Identifier));
1171   EXPECT_NE(N, DICompositeType::get(Context, Tag, "abc", File, Line, Scope,
1172                                     BaseType, SizeInBits, AlignInBits,
1173                                     OffsetInBits, Flags, Elements, RuntimeLang,
1174                                     VTableHolder, TemplateParams, Identifier));
1175   EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, getFile(), Line, Scope,
1176                                     BaseType, SizeInBits, AlignInBits,
1177                                     OffsetInBits, Flags, Elements, RuntimeLang,
1178                                     VTableHolder, TemplateParams, Identifier));
1179   EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line + 1, Scope,
1180                                     BaseType, SizeInBits, AlignInBits,
1181                                     OffsetInBits, Flags, Elements, RuntimeLang,
1182                                     VTableHolder, TemplateParams, Identifier));
1183   EXPECT_NE(N, DICompositeType::get(
1184                    Context, Tag, Name, File, Line, getSubprogram(), BaseType,
1185                    SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1186                    RuntimeLang, VTableHolder, TemplateParams, Identifier));
1187   EXPECT_NE(N, DICompositeType::get(
1188                    Context, Tag, Name, File, Line, Scope, getBasicType("other"),
1189                    SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
1190                    RuntimeLang, VTableHolder, TemplateParams, Identifier));
1191   EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1192                                     BaseType, SizeInBits + 1, AlignInBits,
1193                                     OffsetInBits, Flags, Elements, RuntimeLang,
1194                                     VTableHolder, TemplateParams, Identifier));
1195   EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1196                                     BaseType, SizeInBits, AlignInBits + 1,
1197                                     OffsetInBits, Flags, Elements, RuntimeLang,
1198                                     VTableHolder, TemplateParams, Identifier));
1199   EXPECT_NE(N, DICompositeType::get(
1200                    Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1201                    AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang,
1202                    VTableHolder, TemplateParams, Identifier));
1203   EXPECT_NE(N, DICompositeType::get(
1204                    Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1205                    AlignInBits, OffsetInBits, Flags + 1, Elements, RuntimeLang,
1206                    VTableHolder, TemplateParams, Identifier));
1207   EXPECT_NE(N, DICompositeType::get(
1208                    Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1209                    AlignInBits, OffsetInBits, Flags, getTuple(), RuntimeLang,
1210                    VTableHolder, TemplateParams, Identifier));
1211   EXPECT_NE(N, DICompositeType::get(
1212                    Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1213                    AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1,
1214                    VTableHolder, TemplateParams, Identifier));
1215   EXPECT_NE(N, DICompositeType::get(
1216                    Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
1217                    AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
1218                    getCompositeType(), TemplateParams, Identifier));
1219   EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1220                                     BaseType, SizeInBits, AlignInBits,
1221                                     OffsetInBits, Flags, Elements, RuntimeLang,
1222                                     VTableHolder, getTuple(), Identifier));
1223   EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1224                                     BaseType, SizeInBits, AlignInBits,
1225                                     OffsetInBits, Flags, Elements, RuntimeLang,
1226                                     VTableHolder, TemplateParams, "other"));
1227
1228   // Be sure that missing identifiers get null pointers.
1229   EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1230                                     BaseType, SizeInBits, AlignInBits,
1231                                     OffsetInBits, Flags, Elements, RuntimeLang,
1232                                     VTableHolder, TemplateParams, "")
1233                    ->getRawIdentifier());
1234   EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1235                                     BaseType, SizeInBits, AlignInBits,
1236                                     OffsetInBits, Flags, Elements, RuntimeLang,
1237                                     VTableHolder, TemplateParams)
1238                    ->getRawIdentifier());
1239
1240   TempDICompositeType Temp = N->clone();
1241   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1242 }
1243
1244 TEST_F(DICompositeTypeTest, getWithLargeValues) {
1245   unsigned Tag = dwarf::DW_TAG_structure_type;
1246   StringRef Name = "some name";
1247   DIFile *File = getFile();
1248   unsigned Line = 1;
1249   DIScope *Scope = getSubprogram();
1250   DIType *BaseType = getCompositeType();
1251   uint64_t SizeInBits = UINT64_MAX;
1252   uint64_t AlignInBits = UINT64_MAX - 1;
1253   uint64_t OffsetInBits = UINT64_MAX - 2;
1254   unsigned Flags = 5;
1255   MDTuple *Elements = getTuple();
1256   unsigned RuntimeLang = 6;
1257   DIType *VTableHolder = getCompositeType();
1258   MDTuple *TemplateParams = getTuple();
1259   StringRef Identifier = "some id";
1260
1261   auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope,
1262                                  BaseType, SizeInBits, AlignInBits,
1263                                  OffsetInBits, Flags, Elements, RuntimeLang,
1264                                  VTableHolder, TemplateParams, Identifier);
1265   EXPECT_EQ(SizeInBits, N->getSizeInBits());
1266   EXPECT_EQ(AlignInBits, N->getAlignInBits());
1267   EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
1268 }
1269
1270 TEST_F(DICompositeTypeTest, replaceOperands) {
1271   unsigned Tag = dwarf::DW_TAG_structure_type;
1272   StringRef Name = "some name";
1273   DIFile *File = getFile();
1274   unsigned Line = 1;
1275   DIScope *Scope = getSubprogram();
1276   DIType *BaseType = getCompositeType();
1277   uint64_t SizeInBits = 2;
1278   uint64_t AlignInBits = 3;
1279   uint64_t OffsetInBits = 4;
1280   unsigned Flags = 5;
1281   unsigned RuntimeLang = 6;
1282   StringRef Identifier = "some id";
1283
1284   auto *N = DICompositeType::get(
1285       Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
1286       OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier);
1287
1288   auto *Elements = MDTuple::getDistinct(Context, None);
1289   EXPECT_EQ(nullptr, N->getElements().get());
1290   N->replaceElements(Elements);
1291   EXPECT_EQ(Elements, N->getElements().get());
1292   N->replaceElements(nullptr);
1293   EXPECT_EQ(nullptr, N->getElements().get());
1294
1295   DIType *VTableHolder = getCompositeType();
1296   EXPECT_EQ(nullptr, N->getVTableHolder());
1297   N->replaceVTableHolder(VTableHolder);
1298   EXPECT_EQ(VTableHolder, N->getVTableHolder());
1299   N->replaceVTableHolder(nullptr);
1300   EXPECT_EQ(nullptr, N->getVTableHolder());
1301
1302   auto *TemplateParams = MDTuple::getDistinct(Context, None);
1303   EXPECT_EQ(nullptr, N->getTemplateParams().get());
1304   N->replaceTemplateParams(TemplateParams);
1305   EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
1306   N->replaceTemplateParams(nullptr);
1307   EXPECT_EQ(nullptr, N->getTemplateParams().get());
1308 }
1309
1310 typedef MetadataTest DISubroutineTypeTest;
1311
1312 TEST_F(DISubroutineTypeTest, get) {
1313   unsigned Flags = 1;
1314   MDTuple *TypeArray = getTuple();
1315
1316   auto *N = DISubroutineType::get(Context, Flags, 0, TypeArray);
1317   EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag());
1318   EXPECT_EQ(Flags, N->getFlags());
1319   EXPECT_EQ(TypeArray, N->getTypeArray().get());
1320   EXPECT_EQ(N, DISubroutineType::get(Context, Flags, 0, TypeArray));
1321
1322   EXPECT_NE(N, DISubroutineType::get(Context, Flags + 1, 0, TypeArray));
1323   EXPECT_NE(N, DISubroutineType::get(Context, Flags, 0, getTuple()));
1324
1325   // Test the hashing of calling conventions.
1326   auto *Fast = DISubroutineType::get(
1327       Context, Flags, dwarf::DW_CC_BORLAND_msfastcall, TypeArray);
1328   auto *Std = DISubroutineType::get(Context, Flags,
1329                                     dwarf::DW_CC_BORLAND_stdcall, TypeArray);
1330   EXPECT_EQ(Fast,
1331             DISubroutineType::get(Context, Flags,
1332                                   dwarf::DW_CC_BORLAND_msfastcall, TypeArray));
1333   EXPECT_EQ(Std, DISubroutineType::get(
1334                      Context, Flags, dwarf::DW_CC_BORLAND_stdcall, TypeArray));
1335
1336   EXPECT_NE(N, Fast);
1337   EXPECT_NE(N, Std);
1338   EXPECT_NE(Fast, Std);
1339
1340   TempDISubroutineType Temp = N->clone();
1341   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1342
1343   // Test always-empty operands.
1344   EXPECT_EQ(nullptr, N->getScope());
1345   EXPECT_EQ(nullptr, N->getFile());
1346   EXPECT_EQ("", N->getName());
1347 }
1348
1349 typedef MetadataTest DIFileTest;
1350
1351 TEST_F(DIFileTest, get) {
1352   StringRef Filename = "file";
1353   StringRef Directory = "dir";
1354   auto *N = DIFile::get(Context, Filename, Directory);
1355
1356   EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
1357   EXPECT_EQ(Filename, N->getFilename());
1358   EXPECT_EQ(Directory, N->getDirectory());
1359   EXPECT_EQ(N, DIFile::get(Context, Filename, Directory));
1360
1361   EXPECT_NE(N, DIFile::get(Context, "other", Directory));
1362   EXPECT_NE(N, DIFile::get(Context, Filename, "other"));
1363
1364   TempDIFile Temp = N->clone();
1365   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1366 }
1367
1368 TEST_F(DIFileTest, ScopeGetFile) {
1369   // Ensure that DIScope::getFile() returns itself.
1370   DIScope *N = DIFile::get(Context, "file", "dir");
1371   EXPECT_EQ(N, N->getFile());
1372 }
1373
1374 typedef MetadataTest DICompileUnitTest;
1375
1376 TEST_F(DICompileUnitTest, get) {
1377   unsigned SourceLanguage = 1;
1378   DIFile *File = getFile();
1379   StringRef Producer = "some producer";
1380   bool IsOptimized = false;
1381   StringRef Flags = "flag after flag";
1382   unsigned RuntimeVersion = 2;
1383   StringRef SplitDebugFilename = "another/file";
1384   auto EmissionKind = DICompileUnit::FullDebug;
1385   MDTuple *EnumTypes = getTuple();
1386   MDTuple *RetainedTypes = getTuple();
1387   MDTuple *GlobalVariables = getTuple();
1388   MDTuple *ImportedEntities = getTuple();
1389   uint64_t DWOId = 0x10000000c0ffee;
1390   MDTuple *Macros = getTuple();
1391   auto *N = DICompileUnit::getDistinct(
1392       Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1393       RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1394       RetainedTypes, GlobalVariables, ImportedEntities, Macros, DWOId, true);
1395
1396   EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
1397   EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
1398   EXPECT_EQ(File, N->getFile());
1399   EXPECT_EQ(Producer, N->getProducer());
1400   EXPECT_EQ(IsOptimized, N->isOptimized());
1401   EXPECT_EQ(Flags, N->getFlags());
1402   EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
1403   EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
1404   EXPECT_EQ(EmissionKind, N->getEmissionKind());
1405   EXPECT_EQ(EnumTypes, N->getEnumTypes().get());
1406   EXPECT_EQ(RetainedTypes, N->getRetainedTypes().get());
1407   EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
1408   EXPECT_EQ(ImportedEntities, N->getImportedEntities().get());
1409   EXPECT_EQ(Macros, N->getMacros().get());
1410   EXPECT_EQ(DWOId, N->getDWOId());
1411
1412   TempDICompileUnit Temp = N->clone();
1413   EXPECT_EQ(dwarf::DW_TAG_compile_unit, Temp->getTag());
1414   EXPECT_EQ(SourceLanguage, Temp->getSourceLanguage());
1415   EXPECT_EQ(File, Temp->getFile());
1416   EXPECT_EQ(Producer, Temp->getProducer());
1417   EXPECT_EQ(IsOptimized, Temp->isOptimized());
1418   EXPECT_EQ(Flags, Temp->getFlags());
1419   EXPECT_EQ(RuntimeVersion, Temp->getRuntimeVersion());
1420   EXPECT_EQ(SplitDebugFilename, Temp->getSplitDebugFilename());
1421   EXPECT_EQ(EmissionKind, Temp->getEmissionKind());
1422   EXPECT_EQ(EnumTypes, Temp->getEnumTypes().get());
1423   EXPECT_EQ(RetainedTypes, Temp->getRetainedTypes().get());
1424   EXPECT_EQ(GlobalVariables, Temp->getGlobalVariables().get());
1425   EXPECT_EQ(ImportedEntities, Temp->getImportedEntities().get());
1426   EXPECT_EQ(Macros, Temp->getMacros().get());
1427   EXPECT_EQ(DWOId, Temp->getDWOId());
1428
1429   auto *TempAddress = Temp.get();
1430   auto *Clone = MDNode::replaceWithPermanent(std::move(Temp));
1431   EXPECT_TRUE(Clone->isDistinct());
1432   EXPECT_EQ(TempAddress, Clone);
1433 }
1434
1435 TEST_F(DICompileUnitTest, replaceArrays) {
1436   unsigned SourceLanguage = 1;
1437   DIFile *File = getFile();
1438   StringRef Producer = "some producer";
1439   bool IsOptimized = false;
1440   StringRef Flags = "flag after flag";
1441   unsigned RuntimeVersion = 2;
1442   StringRef SplitDebugFilename = "another/file";
1443   auto EmissionKind = DICompileUnit::FullDebug;
1444   MDTuple *EnumTypes = MDTuple::getDistinct(Context, None);
1445   MDTuple *RetainedTypes = MDTuple::getDistinct(Context, None);
1446   MDTuple *ImportedEntities = MDTuple::getDistinct(Context, None);
1447   uint64_t DWOId = 0xc0ffee;
1448   auto *N = DICompileUnit::getDistinct(
1449       Context, SourceLanguage, File, Producer, IsOptimized, Flags,
1450       RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
1451       RetainedTypes, nullptr, ImportedEntities, nullptr, DWOId, true);
1452
1453   auto *GlobalVariables = MDTuple::getDistinct(Context, None);
1454   EXPECT_EQ(nullptr, N->getGlobalVariables().get());
1455   N->replaceGlobalVariables(GlobalVariables);
1456   EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
1457   N->replaceGlobalVariables(nullptr);
1458   EXPECT_EQ(nullptr, N->getGlobalVariables().get());
1459
1460   auto *Macros = MDTuple::getDistinct(Context, None);
1461   EXPECT_EQ(nullptr, N->getMacros().get());
1462   N->replaceMacros(Macros);
1463   EXPECT_EQ(Macros, N->getMacros().get());
1464   N->replaceMacros(nullptr);
1465   EXPECT_EQ(nullptr, N->getMacros().get());
1466 }
1467
1468 typedef MetadataTest DISubprogramTest;
1469
1470 TEST_F(DISubprogramTest, get) {
1471   DIScope *Scope = getCompositeType();
1472   StringRef Name = "name";
1473   StringRef LinkageName = "linkage";
1474   DIFile *File = getFile();
1475   unsigned Line = 2;
1476   DISubroutineType *Type = getSubroutineType();
1477   bool IsLocalToUnit = false;
1478   bool IsDefinition = true;
1479   unsigned ScopeLine = 3;
1480   DIType *ContainingType = getCompositeType();
1481   unsigned Virtuality = 2;
1482   unsigned VirtualIndex = 5;
1483   int ThisAdjustment = -3;
1484   unsigned Flags = 6;
1485   unsigned NotFlags = (~Flags) & ((1 << 27) - 1);
1486   bool IsOptimized = false;
1487   MDTuple *TemplateParams = getTuple();
1488   DISubprogram *Declaration = getSubprogram();
1489   MDTuple *Variables = getTuple();
1490   DICompileUnit *Unit = getUnit();
1491
1492   auto *N = DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1493                               Type, IsLocalToUnit, IsDefinition, ScopeLine,
1494                               ContainingType, Virtuality, VirtualIndex,
1495                               ThisAdjustment, Flags, IsOptimized, Unit,
1496                               TemplateParams, Declaration, Variables);
1497
1498   EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
1499   EXPECT_EQ(Scope, N->getScope());
1500   EXPECT_EQ(Name, N->getName());
1501   EXPECT_EQ(LinkageName, N->getLinkageName());
1502   EXPECT_EQ(File, N->getFile());
1503   EXPECT_EQ(Line, N->getLine());
1504   EXPECT_EQ(Type, N->getType());
1505   EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1506   EXPECT_EQ(IsDefinition, N->isDefinition());
1507   EXPECT_EQ(ScopeLine, N->getScopeLine());
1508   EXPECT_EQ(ContainingType, N->getContainingType());
1509   EXPECT_EQ(Virtuality, N->getVirtuality());
1510   EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
1511   EXPECT_EQ(ThisAdjustment, N->getThisAdjustment());
1512   EXPECT_EQ(Flags, N->getFlags());
1513   EXPECT_EQ(IsOptimized, N->isOptimized());
1514   EXPECT_EQ(Unit, N->getUnit());
1515   EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
1516   EXPECT_EQ(Declaration, N->getDeclaration());
1517   EXPECT_EQ(Variables, N->getVariables().get());
1518   EXPECT_EQ(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1519                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1520                                  ContainingType, Virtuality, VirtualIndex,
1521                                  ThisAdjustment, Flags, IsOptimized, Unit,
1522                                  TemplateParams, Declaration, Variables));
1523
1524   EXPECT_NE(N, DISubprogram::get(
1525                    Context, getCompositeType(), Name, LinkageName, File, Line,
1526                    Type, IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1527                    Virtuality, VirtualIndex, ThisAdjustment, Flags, IsOptimized,
1528                    Unit, TemplateParams, Declaration, Variables));
1529   EXPECT_NE(N, DISubprogram::get(
1530                    Context, Scope, "other", LinkageName, File, Line, Type,
1531                    IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1532                    Virtuality, VirtualIndex, ThisAdjustment, Flags, IsOptimized,
1533                    Unit, TemplateParams, Declaration, Variables));
1534   EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, "other", File, Line,
1535                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1536                                  ContainingType, Virtuality, VirtualIndex,
1537                                  ThisAdjustment, Flags, IsOptimized, Unit,
1538                                  TemplateParams, Declaration, Variables));
1539   EXPECT_NE(N, DISubprogram::get(
1540                    Context, Scope, Name, LinkageName, getFile(), Line, Type,
1541                    IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1542                    Virtuality, VirtualIndex, ThisAdjustment, Flags, IsOptimized,
1543                    Unit, TemplateParams, Declaration, Variables));
1544   EXPECT_NE(N, DISubprogram::get(
1545                    Context, Scope, Name, LinkageName, File, Line + 1, Type,
1546                    IsLocalToUnit, IsDefinition, ScopeLine, ContainingType,
1547                    Virtuality, VirtualIndex, ThisAdjustment, Flags, IsOptimized,
1548                    Unit, TemplateParams, Declaration, Variables));
1549   EXPECT_NE(N,
1550             DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1551                               getSubroutineType(), IsLocalToUnit, IsDefinition,
1552                               ScopeLine, ContainingType, Virtuality,
1553                               VirtualIndex, ThisAdjustment, Flags, IsOptimized,
1554                               Unit, TemplateParams, Declaration, Variables));
1555   EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1556                                  Type, !IsLocalToUnit, IsDefinition, ScopeLine,
1557                                  ContainingType, Virtuality, VirtualIndex,
1558                                  ThisAdjustment, Flags, IsOptimized, Unit,
1559                                  TemplateParams, Declaration, Variables));
1560   EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1561                                  Type, IsLocalToUnit, !IsDefinition, ScopeLine,
1562                                  ContainingType, Virtuality, VirtualIndex,
1563                                  ThisAdjustment, Flags, IsOptimized, Unit,
1564                                  TemplateParams, Declaration, Variables));
1565   EXPECT_NE(N, DISubprogram::get(
1566                    Context, Scope, Name, LinkageName, File, Line, Type,
1567                    IsLocalToUnit, IsDefinition, ScopeLine + 1, ContainingType,
1568                    Virtuality, VirtualIndex, ThisAdjustment, Flags, IsOptimized,
1569                    Unit, TemplateParams, Declaration, Variables));
1570   EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1571                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1572                                  getCompositeType(), Virtuality, VirtualIndex,
1573                                  ThisAdjustment, Flags, IsOptimized, Unit,
1574                                  TemplateParams, Declaration, Variables));
1575   EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1576                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1577                                  ContainingType, Virtuality + 1, VirtualIndex,
1578                                  ThisAdjustment, Flags, IsOptimized, Unit,
1579                                  TemplateParams, Declaration, Variables));
1580   EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1581                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1582                                  ContainingType, Virtuality, VirtualIndex + 1,
1583                                  ThisAdjustment, Flags, IsOptimized, Unit,
1584                                  TemplateParams, Declaration, Variables));
1585   EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1586                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1587                                  ContainingType, Virtuality, VirtualIndex,
1588                                  ThisAdjustment, NotFlags, IsOptimized, Unit,
1589                                  TemplateParams, Declaration, Variables));
1590   EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1591                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1592                                  ContainingType, Virtuality, VirtualIndex,
1593                                  ThisAdjustment, Flags, !IsOptimized, Unit,
1594                                  TemplateParams, Declaration, Variables));
1595   EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1596                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1597                                  ContainingType, Virtuality, VirtualIndex,
1598                                  ThisAdjustment, Flags, IsOptimized, nullptr,
1599                                  TemplateParams, Declaration, Variables));
1600   EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1601                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1602                                  ContainingType, Virtuality, VirtualIndex,
1603                                  ThisAdjustment, Flags, IsOptimized, Unit,
1604                                  getTuple(), Declaration, Variables));
1605   EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1606                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1607                                  ContainingType, Virtuality, VirtualIndex,
1608                                  ThisAdjustment, Flags, IsOptimized, Unit,
1609                                  TemplateParams, getSubprogram(), Variables));
1610   EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
1611                                  Type, IsLocalToUnit, IsDefinition, ScopeLine,
1612                                  ContainingType, Virtuality, VirtualIndex,
1613                                  ThisAdjustment, Flags, IsOptimized, Unit,
1614                                  TemplateParams, Declaration, getTuple()));
1615
1616   TempDISubprogram Temp = N->clone();
1617   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1618 }
1619
1620 typedef MetadataTest DILexicalBlockTest;
1621
1622 TEST_F(DILexicalBlockTest, get) {
1623   DILocalScope *Scope = getSubprogram();
1624   DIFile *File = getFile();
1625   unsigned Line = 5;
1626   unsigned Column = 8;
1627
1628   auto *N = DILexicalBlock::get(Context, Scope, File, Line, Column);
1629
1630   EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1631   EXPECT_EQ(Scope, N->getScope());
1632   EXPECT_EQ(File, N->getFile());
1633   EXPECT_EQ(Line, N->getLine());
1634   EXPECT_EQ(Column, N->getColumn());
1635   EXPECT_EQ(N, DILexicalBlock::get(Context, Scope, File, Line, Column));
1636
1637   EXPECT_NE(N,
1638             DILexicalBlock::get(Context, getSubprogram(), File, Line, Column));
1639   EXPECT_NE(N, DILexicalBlock::get(Context, Scope, getFile(), Line, Column));
1640   EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line + 1, Column));
1641   EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line, Column + 1));
1642
1643   TempDILexicalBlock Temp = N->clone();
1644   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1645 }
1646
1647 TEST_F(DILexicalBlockTest, Overflow) {
1648   DISubprogram *SP = getSubprogram();
1649   DIFile *F = getFile();
1650   {
1651     auto *LB = DILexicalBlock::get(Context, SP, F, 2, 7);
1652     EXPECT_EQ(2u, LB->getLine());
1653     EXPECT_EQ(7u, LB->getColumn());
1654   }
1655   unsigned U16 = 1u << 16;
1656   {
1657     auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16 - 1);
1658     EXPECT_EQ(UINT32_MAX, LB->getLine());
1659     EXPECT_EQ(U16 - 1, LB->getColumn());
1660   }
1661   {
1662     auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16);
1663     EXPECT_EQ(UINT32_MAX, LB->getLine());
1664     EXPECT_EQ(0u, LB->getColumn());
1665   }
1666   {
1667     auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16 + 1);
1668     EXPECT_EQ(UINT32_MAX, LB->getLine());
1669     EXPECT_EQ(0u, LB->getColumn());
1670   }
1671 }
1672
1673 typedef MetadataTest DILexicalBlockFileTest;
1674
1675 TEST_F(DILexicalBlockFileTest, get) {
1676   DILocalScope *Scope = getSubprogram();
1677   DIFile *File = getFile();
1678   unsigned Discriminator = 5;
1679
1680   auto *N = DILexicalBlockFile::get(Context, Scope, File, Discriminator);
1681
1682   EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
1683   EXPECT_EQ(Scope, N->getScope());
1684   EXPECT_EQ(File, N->getFile());
1685   EXPECT_EQ(Discriminator, N->getDiscriminator());
1686   EXPECT_EQ(N, DILexicalBlockFile::get(Context, Scope, File, Discriminator));
1687
1688   EXPECT_NE(N, DILexicalBlockFile::get(Context, getSubprogram(), File,
1689                                        Discriminator));
1690   EXPECT_NE(N,
1691             DILexicalBlockFile::get(Context, Scope, getFile(), Discriminator));
1692   EXPECT_NE(N,
1693             DILexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
1694
1695   TempDILexicalBlockFile Temp = N->clone();
1696   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1697 }
1698
1699 typedef MetadataTest DINamespaceTest;
1700
1701 TEST_F(DINamespaceTest, get) {
1702   DIScope *Scope = getFile();
1703   DIFile *File = getFile();
1704   StringRef Name = "namespace";
1705   unsigned Line = 5;
1706
1707   auto *N = DINamespace::get(Context, Scope, File, Name, Line);
1708
1709   EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
1710   EXPECT_EQ(Scope, N->getScope());
1711   EXPECT_EQ(File, N->getFile());
1712   EXPECT_EQ(Name, N->getName());
1713   EXPECT_EQ(Line, N->getLine());
1714   EXPECT_EQ(N, DINamespace::get(Context, Scope, File, Name, Line));
1715
1716   EXPECT_NE(N, DINamespace::get(Context, getFile(), File, Name, Line));
1717   EXPECT_NE(N, DINamespace::get(Context, Scope, getFile(), Name, Line));
1718   EXPECT_NE(N, DINamespace::get(Context, Scope, File, "other", Line));
1719   EXPECT_NE(N, DINamespace::get(Context, Scope, File, Name, Line + 1));
1720
1721   TempDINamespace Temp = N->clone();
1722   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1723 }
1724
1725 typedef MetadataTest DIModuleTest;
1726
1727 TEST_F(DIModuleTest, get) {
1728   DIScope *Scope = getFile();
1729   StringRef Name = "module";
1730   StringRef ConfigMacro = "-DNDEBUG";
1731   StringRef Includes = "-I.";
1732   StringRef Sysroot = "/";
1733
1734   auto *N = DIModule::get(Context, Scope, Name, ConfigMacro, Includes, Sysroot);
1735
1736   EXPECT_EQ(dwarf::DW_TAG_module, N->getTag());
1737   EXPECT_EQ(Scope, N->getScope());
1738   EXPECT_EQ(Name, N->getName());
1739   EXPECT_EQ(ConfigMacro, N->getConfigurationMacros());
1740   EXPECT_EQ(Includes, N->getIncludePath());
1741   EXPECT_EQ(Sysroot, N->getISysRoot());
1742   EXPECT_EQ(N, DIModule::get(Context, Scope, Name,
1743                              ConfigMacro, Includes, Sysroot));
1744   EXPECT_NE(N, DIModule::get(Context, getFile(), Name,
1745                              ConfigMacro, Includes, Sysroot));
1746   EXPECT_NE(N, DIModule::get(Context, Scope, "other",
1747                              ConfigMacro, Includes, Sysroot));
1748   EXPECT_NE(N, DIModule::get(Context, Scope, Name,
1749                              "other", Includes, Sysroot));
1750   EXPECT_NE(N, DIModule::get(Context, Scope, Name,
1751                              ConfigMacro, "other", Sysroot));
1752   EXPECT_NE(N, DIModule::get(Context, Scope, Name,
1753                              ConfigMacro, Includes, "other"));
1754
1755   TempDIModule Temp = N->clone();
1756   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1757 }
1758
1759 typedef MetadataTest DITemplateTypeParameterTest;
1760
1761 TEST_F(DITemplateTypeParameterTest, get) {
1762   StringRef Name = "template";
1763   DIType *Type = getBasicType("basic");
1764
1765   auto *N = DITemplateTypeParameter::get(Context, Name, Type);
1766
1767   EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
1768   EXPECT_EQ(Name, N->getName());
1769   EXPECT_EQ(Type, N->getType());
1770   EXPECT_EQ(N, DITemplateTypeParameter::get(Context, Name, Type));
1771
1772   EXPECT_NE(N, DITemplateTypeParameter::get(Context, "other", Type));
1773   EXPECT_NE(N,
1774             DITemplateTypeParameter::get(Context, Name, getBasicType("other")));
1775
1776   TempDITemplateTypeParameter Temp = N->clone();
1777   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1778 }
1779
1780 typedef MetadataTest DITemplateValueParameterTest;
1781
1782 TEST_F(DITemplateValueParameterTest, get) {
1783   unsigned Tag = dwarf::DW_TAG_template_value_parameter;
1784   StringRef Name = "template";
1785   DIType *Type = getBasicType("basic");
1786   Metadata *Value = getConstantAsMetadata();
1787
1788   auto *N = DITemplateValueParameter::get(Context, Tag, Name, Type, Value);
1789   EXPECT_EQ(Tag, N->getTag());
1790   EXPECT_EQ(Name, N->getName());
1791   EXPECT_EQ(Type, N->getType());
1792   EXPECT_EQ(Value, N->getValue());
1793   EXPECT_EQ(N, DITemplateValueParameter::get(Context, Tag, Name, Type, Value));
1794
1795   EXPECT_NE(N, DITemplateValueParameter::get(
1796                    Context, dwarf::DW_TAG_GNU_template_template_param, Name,
1797                    Type, Value));
1798   EXPECT_NE(N,
1799             DITemplateValueParameter::get(Context, Tag, "other", Type, Value));
1800   EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name,
1801                                              getBasicType("other"), Value));
1802   EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name, Type,
1803                                              getConstantAsMetadata()));
1804
1805   TempDITemplateValueParameter Temp = N->clone();
1806   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1807 }
1808
1809 typedef MetadataTest DIGlobalVariableTest;
1810
1811 TEST_F(DIGlobalVariableTest, get) {
1812   DIScope *Scope = getSubprogram();
1813   StringRef Name = "name";
1814   StringRef LinkageName = "linkage";
1815   DIFile *File = getFile();
1816   unsigned Line = 5;
1817   DIType *Type = getDerivedType();
1818   bool IsLocalToUnit = false;
1819   bool IsDefinition = true;
1820   Constant *Variable = getConstant();
1821   DIDerivedType *StaticDataMemberDeclaration =
1822       cast<DIDerivedType>(getDerivedType());
1823
1824   auto *N = DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1825                                   Type, IsLocalToUnit, IsDefinition, Variable,
1826                                   StaticDataMemberDeclaration);
1827   EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
1828   EXPECT_EQ(Scope, N->getScope());
1829   EXPECT_EQ(Name, N->getName());
1830   EXPECT_EQ(LinkageName, N->getLinkageName());
1831   EXPECT_EQ(File, N->getFile());
1832   EXPECT_EQ(Line, N->getLine());
1833   EXPECT_EQ(Type, N->getType());
1834   EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
1835   EXPECT_EQ(IsDefinition, N->isDefinition());
1836   EXPECT_EQ(Variable, N->getVariable());
1837   EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
1838   EXPECT_EQ(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1839                                      Line, Type, IsLocalToUnit, IsDefinition,
1840                                      Variable, StaticDataMemberDeclaration));
1841
1842   EXPECT_NE(N,
1843             DIGlobalVariable::get(Context, getSubprogram(), Name, LinkageName,
1844                                   File, Line, Type, IsLocalToUnit, IsDefinition,
1845                                   Variable, StaticDataMemberDeclaration));
1846   EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, "other", LinkageName, File,
1847                                      Line, Type, IsLocalToUnit, IsDefinition,
1848                                      Variable, StaticDataMemberDeclaration));
1849   EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, "other", File, Line,
1850                                      Type, IsLocalToUnit, IsDefinition,
1851                                      Variable, StaticDataMemberDeclaration));
1852   EXPECT_NE(N,
1853             DIGlobalVariable::get(Context, Scope, Name, LinkageName, getFile(),
1854                                   Line, Type, IsLocalToUnit, IsDefinition,
1855                                   Variable, StaticDataMemberDeclaration));
1856   EXPECT_NE(N,
1857             DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1858                                   Line + 1, Type, IsLocalToUnit, IsDefinition,
1859                                   Variable, StaticDataMemberDeclaration));
1860   EXPECT_NE(N,
1861             DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1862                                   getDerivedType(), IsLocalToUnit, IsDefinition,
1863                                   Variable, StaticDataMemberDeclaration));
1864   EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1865                                      Line, Type, !IsLocalToUnit, IsDefinition,
1866                                      Variable, StaticDataMemberDeclaration));
1867   EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
1868                                      Line, Type, IsLocalToUnit, !IsDefinition,
1869                                      Variable, StaticDataMemberDeclaration));
1870   EXPECT_NE(N,
1871             DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1872                                   Type, IsLocalToUnit, IsDefinition,
1873                                   getConstant(), StaticDataMemberDeclaration));
1874   EXPECT_NE(N,
1875             DIGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
1876                                   Type, IsLocalToUnit, IsDefinition, Variable,
1877                                   cast<DIDerivedType>(getDerivedType())));
1878
1879   TempDIGlobalVariable Temp = N->clone();
1880   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1881 }
1882
1883 typedef MetadataTest DILocalVariableTest;
1884
1885 TEST_F(DILocalVariableTest, get) {
1886   DILocalScope *Scope = getSubprogram();
1887   StringRef Name = "name";
1888   DIFile *File = getFile();
1889   unsigned Line = 5;
1890   DIType *Type = getDerivedType();
1891   unsigned Arg = 6;
1892   unsigned Flags = 7;
1893   unsigned NotFlags = (~Flags) & ((1 << 16) - 1);
1894
1895   auto *N =
1896       DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg, Flags);
1897   EXPECT_TRUE(N->isParameter());
1898   EXPECT_EQ(Scope, N->getScope());
1899   EXPECT_EQ(Name, N->getName());
1900   EXPECT_EQ(File, N->getFile());
1901   EXPECT_EQ(Line, N->getLine());
1902   EXPECT_EQ(Type, N->getType());
1903   EXPECT_EQ(Arg, N->getArg());
1904   EXPECT_EQ(Flags, N->getFlags());
1905   EXPECT_EQ(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg,
1906                                     Flags));
1907
1908   EXPECT_FALSE(
1909       DILocalVariable::get(Context, Scope, Name, File, Line, Type, 0, Flags)
1910           ->isParameter());
1911   EXPECT_NE(N, DILocalVariable::get(Context, getSubprogram(), Name, File, Line,
1912                                     Type, Arg, Flags));
1913   EXPECT_NE(N, DILocalVariable::get(Context, Scope, "other", File, Line, Type,
1914                                     Arg, Flags));
1915   EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, getFile(), Line, Type,
1916                                     Arg, Flags));
1917   EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line + 1, Type,
1918                                     Arg, Flags));
1919   EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line,
1920                                     getDerivedType(), Arg, Flags));
1921   EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type,
1922                                     Arg + 1, Flags));
1923   EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg,
1924                                     NotFlags));
1925
1926   TempDILocalVariable Temp = N->clone();
1927   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1928 }
1929
1930 TEST_F(DILocalVariableTest, getArg256) {
1931   EXPECT_EQ(255u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
1932                                        0, nullptr, 255, 0)
1933                       ->getArg());
1934   EXPECT_EQ(256u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
1935                                        0, nullptr, 256, 0)
1936                       ->getArg());
1937   EXPECT_EQ(257u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
1938                                        0, nullptr, 257, 0)
1939                       ->getArg());
1940   unsigned Max = UINT16_MAX;
1941   EXPECT_EQ(Max, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
1942                                       0, nullptr, Max, 0)
1943                      ->getArg());
1944 }
1945
1946 typedef MetadataTest DIExpressionTest;
1947
1948 TEST_F(DIExpressionTest, get) {
1949   uint64_t Elements[] = {2, 6, 9, 78, 0};
1950   auto *N = DIExpression::get(Context, Elements);
1951   EXPECT_EQ(makeArrayRef(Elements), N->getElements());
1952   EXPECT_EQ(N, DIExpression::get(Context, Elements));
1953
1954   EXPECT_EQ(5u, N->getNumElements());
1955   EXPECT_EQ(2u, N->getElement(0));
1956   EXPECT_EQ(6u, N->getElement(1));
1957   EXPECT_EQ(9u, N->getElement(2));
1958   EXPECT_EQ(78u, N->getElement(3));
1959   EXPECT_EQ(0u, N->getElement(4));
1960
1961   TempDIExpression Temp = N->clone();
1962   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
1963 }
1964
1965 TEST_F(DIExpressionTest, isValid) {
1966 #define EXPECT_VALID(...)                                                      \
1967   do {                                                                         \
1968     uint64_t Elements[] = {__VA_ARGS__};                                       \
1969     EXPECT_TRUE(DIExpression::get(Context, Elements)->isValid());              \
1970   } while (false)
1971 #define EXPECT_INVALID(...)                                                    \
1972   do {                                                                         \
1973     uint64_t Elements[] = {__VA_ARGS__};                                       \
1974     EXPECT_FALSE(DIExpression::get(Context, Elements)->isValid());             \
1975   } while (false)
1976
1977   // Empty expression should be valid.
1978   EXPECT_TRUE(DIExpression::get(Context, None));
1979
1980   // Valid constructions.
1981   EXPECT_VALID(dwarf::DW_OP_plus, 6);
1982   EXPECT_VALID(dwarf::DW_OP_deref);
1983   EXPECT_VALID(dwarf::DW_OP_bit_piece, 3, 7);
1984   EXPECT_VALID(dwarf::DW_OP_plus, 6, dwarf::DW_OP_deref);
1985   EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6);
1986   EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_bit_piece, 3, 7);
1987   EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus, 6, dwarf::DW_OP_bit_piece, 3, 7);
1988
1989   // Invalid constructions.
1990   EXPECT_INVALID(~0u);
1991   EXPECT_INVALID(dwarf::DW_OP_plus);
1992   EXPECT_INVALID(dwarf::DW_OP_bit_piece);
1993   EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3);
1994   EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_plus, 3);
1995   EXPECT_INVALID(dwarf::DW_OP_bit_piece, 3, 7, dwarf::DW_OP_deref);
1996
1997 #undef EXPECT_VALID
1998 #undef EXPECT_INVALID
1999 }
2000
2001 typedef MetadataTest DIObjCPropertyTest;
2002
2003 TEST_F(DIObjCPropertyTest, get) {
2004   StringRef Name = "name";
2005   DIFile *File = getFile();
2006   unsigned Line = 5;
2007   StringRef GetterName = "getter";
2008   StringRef SetterName = "setter";
2009   unsigned Attributes = 7;
2010   DIType *Type = getBasicType("basic");
2011
2012   auto *N = DIObjCProperty::get(Context, Name, File, Line, GetterName,
2013                                 SetterName, Attributes, Type);
2014
2015   EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
2016   EXPECT_EQ(Name, N->getName());
2017   EXPECT_EQ(File, N->getFile());
2018   EXPECT_EQ(Line, N->getLine());
2019   EXPECT_EQ(GetterName, N->getGetterName());
2020   EXPECT_EQ(SetterName, N->getSetterName());
2021   EXPECT_EQ(Attributes, N->getAttributes());
2022   EXPECT_EQ(Type, N->getType());
2023   EXPECT_EQ(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
2024                                    SetterName, Attributes, Type));
2025
2026   EXPECT_NE(N, DIObjCProperty::get(Context, "other", File, Line, GetterName,
2027                                    SetterName, Attributes, Type));
2028   EXPECT_NE(N, DIObjCProperty::get(Context, Name, getFile(), Line, GetterName,
2029                                    SetterName, Attributes, Type));
2030   EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line + 1, GetterName,
2031                                    SetterName, Attributes, Type));
2032   EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, "other",
2033                                    SetterName, Attributes, Type));
2034   EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
2035                                    "other", Attributes, Type));
2036   EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
2037                                    SetterName, Attributes + 1, Type));
2038   EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
2039                                    SetterName, Attributes,
2040                                    getBasicType("other")));
2041
2042   TempDIObjCProperty Temp = N->clone();
2043   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
2044 }
2045
2046 typedef MetadataTest DIImportedEntityTest;
2047
2048 TEST_F(DIImportedEntityTest, get) {
2049   unsigned Tag = dwarf::DW_TAG_imported_module;
2050   DIScope *Scope = getSubprogram();
2051   DINode *Entity = getCompositeType();
2052   unsigned Line = 5;
2053   StringRef Name = "name";
2054
2055   auto *N = DIImportedEntity::get(Context, Tag, Scope, Entity, Line, Name);
2056
2057   EXPECT_EQ(Tag, N->getTag());
2058   EXPECT_EQ(Scope, N->getScope());
2059   EXPECT_EQ(Entity, N->getEntity());
2060   EXPECT_EQ(Line, N->getLine());
2061   EXPECT_EQ(Name, N->getName());
2062   EXPECT_EQ(N, DIImportedEntity::get(Context, Tag, Scope, Entity, Line, Name));
2063
2064   EXPECT_NE(N,
2065             DIImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
2066                                   Scope, Entity, Line, Name));
2067   EXPECT_NE(N, DIImportedEntity::get(Context, Tag, getSubprogram(), Entity,
2068                                      Line, Name));
2069   EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, getCompositeType(),
2070                                      Line, Name));
2071   EXPECT_NE(N,
2072             DIImportedEntity::get(Context, Tag, Scope, Entity, Line + 1, Name));
2073   EXPECT_NE(N,
2074             DIImportedEntity::get(Context, Tag, Scope, Entity, Line, "other"));
2075
2076   TempDIImportedEntity Temp = N->clone();
2077   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
2078 }
2079
2080 typedef MetadataTest MetadataAsValueTest;
2081
2082 TEST_F(MetadataAsValueTest, MDNode) {
2083   MDNode *N = MDNode::get(Context, None);
2084   auto *V = MetadataAsValue::get(Context, N);
2085   EXPECT_TRUE(V->getType()->isMetadataTy());
2086   EXPECT_EQ(N, V->getMetadata());
2087
2088   auto *V2 = MetadataAsValue::get(Context, N);
2089   EXPECT_EQ(V, V2);
2090 }
2091
2092 TEST_F(MetadataAsValueTest, MDNodeMDNode) {
2093   MDNode *N = MDNode::get(Context, None);
2094   Metadata *Ops[] = {N};
2095   MDNode *N2 = MDNode::get(Context, Ops);
2096   auto *V = MetadataAsValue::get(Context, N2);
2097   EXPECT_TRUE(V->getType()->isMetadataTy());
2098   EXPECT_EQ(N2, V->getMetadata());
2099
2100   auto *V2 = MetadataAsValue::get(Context, N2);
2101   EXPECT_EQ(V, V2);
2102
2103   auto *V3 = MetadataAsValue::get(Context, N);
2104   EXPECT_TRUE(V3->getType()->isMetadataTy());
2105   EXPECT_NE(V, V3);
2106   EXPECT_EQ(N, V3->getMetadata());
2107 }
2108
2109 TEST_F(MetadataAsValueTest, MDNodeConstant) {
2110   auto *C = ConstantInt::getTrue(Context);
2111   auto *MD = ConstantAsMetadata::get(C);
2112   Metadata *Ops[] = {MD};
2113   auto *N = MDNode::get(Context, Ops);
2114
2115   auto *V = MetadataAsValue::get(Context, MD);
2116   EXPECT_TRUE(V->getType()->isMetadataTy());
2117   EXPECT_EQ(MD, V->getMetadata());
2118
2119   auto *V2 = MetadataAsValue::get(Context, N);
2120   EXPECT_EQ(MD, V2->getMetadata());
2121   EXPECT_EQ(V, V2);
2122 }
2123
2124 typedef MetadataTest ValueAsMetadataTest;
2125
2126 TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
2127   Type *Ty = Type::getInt1PtrTy(Context);
2128   std::unique_ptr<GlobalVariable> GV0(
2129       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2130   auto *MD = ValueAsMetadata::get(GV0.get());
2131   EXPECT_TRUE(MD->getValue() == GV0.get());
2132   ASSERT_TRUE(GV0->use_empty());
2133
2134   std::unique_ptr<GlobalVariable> GV1(
2135       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2136   GV0->replaceAllUsesWith(GV1.get());
2137   EXPECT_TRUE(MD->getValue() == GV1.get());
2138 }
2139
2140 TEST_F(ValueAsMetadataTest, TempTempReplacement) {
2141   // Create a constant.
2142   ConstantAsMetadata *CI =
2143       ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0)));
2144
2145   auto Temp1 = MDTuple::getTemporary(Context, None);
2146   auto Temp2 = MDTuple::getTemporary(Context, {CI});
2147   auto *N = MDTuple::get(Context, {Temp1.get()});
2148
2149   // Test replacing a temporary node with another temporary node.
2150   Temp1->replaceAllUsesWith(Temp2.get());
2151   EXPECT_EQ(N->getOperand(0), Temp2.get());
2152
2153   // Clean up Temp2 for teardown.
2154   Temp2->replaceAllUsesWith(nullptr);
2155 }
2156
2157 TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
2158   // Create a constant.
2159   ConstantAsMetadata *CI =
2160       ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0)));
2161
2162   // Create a temporary to prevent nodes from resolving.
2163   auto Temp = MDTuple::getTemporary(Context, None);
2164
2165   // When the first operand of N1 gets reset to nullptr, it'll collide with N2.
2166   Metadata *Ops1[] = {CI, CI, Temp.get()};
2167   Metadata *Ops2[] = {nullptr, CI, Temp.get()};
2168
2169   auto *N1 = MDTuple::get(Context, Ops1);
2170   auto *N2 = MDTuple::get(Context, Ops2);
2171   ASSERT_NE(N1, N2);
2172
2173   // Tell metadata that the constant is getting deleted.
2174   //
2175   // After this, N1 will be invalid, so don't touch it.
2176   ValueAsMetadata::handleDeletion(CI->getValue());
2177   EXPECT_EQ(nullptr, N2->getOperand(0));
2178   EXPECT_EQ(nullptr, N2->getOperand(1));
2179   EXPECT_EQ(Temp.get(), N2->getOperand(2));
2180
2181   // Clean up Temp for teardown.
2182   Temp->replaceAllUsesWith(nullptr);
2183 }
2184
2185 typedef MetadataTest TrackingMDRefTest;
2186
2187 TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
2188   Type *Ty = Type::getInt1PtrTy(Context);
2189   std::unique_ptr<GlobalVariable> GV0(
2190       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2191   TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
2192   EXPECT_TRUE(MD->getValue() == GV0.get());
2193   ASSERT_TRUE(GV0->use_empty());
2194
2195   std::unique_ptr<GlobalVariable> GV1(
2196       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2197   GV0->replaceAllUsesWith(GV1.get());
2198   EXPECT_TRUE(MD->getValue() == GV1.get());
2199
2200   // Reset it, so we don't inadvertently test deletion.
2201   MD.reset();
2202 }
2203
2204 TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
2205   Type *Ty = Type::getInt1PtrTy(Context);
2206   std::unique_ptr<GlobalVariable> GV(
2207       new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
2208   TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
2209   EXPECT_TRUE(MD->getValue() == GV.get());
2210   ASSERT_TRUE(GV->use_empty());
2211
2212   GV.reset();
2213   EXPECT_TRUE(!MD);
2214 }
2215
2216 TEST(NamedMDNodeTest, Search) {
2217   LLVMContext Context;
2218   ConstantAsMetadata *C =
2219       ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
2220   ConstantAsMetadata *C2 =
2221       ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
2222
2223   Metadata *const V = C;
2224   Metadata *const V2 = C2;
2225   MDNode *n = MDNode::get(Context, V);
2226   MDNode *n2 = MDNode::get(Context, V2);
2227
2228   Module M("MyModule", Context);
2229   const char *Name = "llvm.NMD1";
2230   NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
2231   NMD->addOperand(n);
2232   NMD->addOperand(n2);
2233
2234   std::string Str;
2235   raw_string_ostream oss(Str);
2236   NMD->print(oss);
2237   EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
2238                oss.str().c_str());
2239 }
2240
2241 typedef MetadataTest FunctionAttachmentTest;
2242 TEST_F(FunctionAttachmentTest, setMetadata) {
2243   Function *F = getFunction("foo");
2244   ASSERT_FALSE(F->hasMetadata());
2245   EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2246   EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2247   EXPECT_EQ(nullptr, F->getMetadata("other"));
2248
2249   DISubprogram *SP1 = getSubprogram();
2250   DISubprogram *SP2 = getSubprogram();
2251   ASSERT_NE(SP1, SP2);
2252
2253   F->setMetadata("dbg", SP1);
2254   EXPECT_TRUE(F->hasMetadata());
2255   EXPECT_EQ(SP1, F->getMetadata(LLVMContext::MD_dbg));
2256   EXPECT_EQ(SP1, F->getMetadata("dbg"));
2257   EXPECT_EQ(nullptr, F->getMetadata("other"));
2258
2259   F->setMetadata(LLVMContext::MD_dbg, SP2);
2260   EXPECT_TRUE(F->hasMetadata());
2261   EXPECT_EQ(SP2, F->getMetadata(LLVMContext::MD_dbg));
2262   EXPECT_EQ(SP2, F->getMetadata("dbg"));
2263   EXPECT_EQ(nullptr, F->getMetadata("other"));
2264
2265   F->setMetadata("dbg", nullptr);
2266   EXPECT_FALSE(F->hasMetadata());
2267   EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
2268   EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2269   EXPECT_EQ(nullptr, F->getMetadata("other"));
2270
2271   MDTuple *T1 = getTuple();
2272   MDTuple *T2 = getTuple();
2273   ASSERT_NE(T1, T2);
2274
2275   F->setMetadata("other1", T1);
2276   F->setMetadata("other2", T2);
2277   EXPECT_TRUE(F->hasMetadata());
2278   EXPECT_EQ(T1, F->getMetadata("other1"));
2279   EXPECT_EQ(T2, F->getMetadata("other2"));
2280   EXPECT_EQ(nullptr, F->getMetadata("dbg"));
2281
2282   F->setMetadata("other1", T2);
2283   F->setMetadata("other2", T1);
2284   EXPECT_EQ(T2, F->getMetadata("other1"));
2285   EXPECT_EQ(T1, F->getMetadata("other2"));
2286
2287   F->setMetadata("other1", nullptr);
2288   F->setMetadata("other2", nullptr);
2289   EXPECT_FALSE(F->hasMetadata());
2290   EXPECT_EQ(nullptr, F->getMetadata("other1"));
2291   EXPECT_EQ(nullptr, F->getMetadata("other2"));
2292 }
2293
2294 TEST_F(FunctionAttachmentTest, getAll) {
2295   Function *F = getFunction("foo");
2296
2297   MDTuple *T1 = getTuple();
2298   MDTuple *T2 = getTuple();
2299   MDTuple *P = getTuple();
2300   DISubprogram *SP = getSubprogram();
2301
2302   F->setMetadata("other1", T2);
2303   F->setMetadata(LLVMContext::MD_dbg, SP);
2304   F->setMetadata("other2", T1);
2305   F->setMetadata(LLVMContext::MD_prof, P);
2306   F->setMetadata("other2", T2);
2307   F->setMetadata("other1", T1);
2308
2309   SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
2310   F->getAllMetadata(MDs);
2311   ASSERT_EQ(4u, MDs.size());
2312   EXPECT_EQ(LLVMContext::MD_dbg, MDs[0].first);
2313   EXPECT_EQ(LLVMContext::MD_prof, MDs[1].first);
2314   EXPECT_EQ(Context.getMDKindID("other1"), MDs[2].first);
2315   EXPECT_EQ(Context.getMDKindID("other2"), MDs[3].first);
2316   EXPECT_EQ(SP, MDs[0].second);
2317   EXPECT_EQ(P, MDs[1].second);
2318   EXPECT_EQ(T1, MDs[2].second);
2319   EXPECT_EQ(T2, MDs[3].second);
2320 }
2321
2322 TEST_F(FunctionAttachmentTest, Verifier) {
2323   Function *F = getFunction("foo");
2324   F->setMetadata("attach", getTuple());
2325   F->setIsMaterializable(true);
2326
2327   // Confirm this is materializable.
2328   ASSERT_TRUE(F->isMaterializable());
2329
2330   // Materializable functions cannot have metadata attachments.
2331   EXPECT_TRUE(verifyFunction(*F));
2332
2333   // Function declarations can.
2334   F->setIsMaterializable(false);
2335   EXPECT_FALSE(verifyModule(*F->getParent()));
2336   EXPECT_FALSE(verifyFunction(*F));
2337
2338   // So can definitions.
2339   (void)new UnreachableInst(Context, BasicBlock::Create(Context, "bb", F));
2340   EXPECT_FALSE(verifyModule(*F->getParent()));
2341   EXPECT_FALSE(verifyFunction(*F));
2342 }
2343
2344 TEST_F(FunctionAttachmentTest, EntryCount) {
2345   Function *F = getFunction("foo");
2346   EXPECT_FALSE(F->getEntryCount().hasValue());
2347   F->setEntryCount(12304);
2348   EXPECT_TRUE(F->getEntryCount().hasValue());
2349   EXPECT_EQ(12304u, *F->getEntryCount());
2350 }
2351
2352 TEST_F(FunctionAttachmentTest, SubprogramAttachment) {
2353   Function *F = getFunction("foo");
2354   DISubprogram *SP = getSubprogram();
2355   F->setSubprogram(SP);
2356
2357   // Note that the static_cast confirms that F->getSubprogram() actually
2358   // returns an DISubprogram.
2359   EXPECT_EQ(SP, static_cast<DISubprogram *>(F->getSubprogram()));
2360   EXPECT_EQ(SP, F->getMetadata("dbg"));
2361   EXPECT_EQ(SP, F->getMetadata(LLVMContext::MD_dbg));
2362 }
2363
2364 typedef MetadataTest DistinctMDOperandPlaceholderTest;
2365 TEST_F(DistinctMDOperandPlaceholderTest, getID) {
2366   EXPECT_EQ(7u, DistinctMDOperandPlaceholder(7).getID());
2367 }
2368
2369 TEST_F(DistinctMDOperandPlaceholderTest, replaceUseWith) {
2370   // Set up some placeholders.
2371   DistinctMDOperandPlaceholder PH0(7);
2372   DistinctMDOperandPlaceholder PH1(3);
2373   DistinctMDOperandPlaceholder PH2(0);
2374   Metadata *Ops[] = {&PH0, &PH1, &PH2};
2375   auto *D = MDTuple::getDistinct(Context, Ops);
2376   ASSERT_EQ(&PH0, D->getOperand(0));
2377   ASSERT_EQ(&PH1, D->getOperand(1));
2378   ASSERT_EQ(&PH2, D->getOperand(2));
2379
2380   // Replace them.
2381   auto *N0 = MDTuple::get(Context, None);
2382   auto *N1 = MDTuple::get(Context, N0);
2383   PH0.replaceUseWith(N0);
2384   PH1.replaceUseWith(N1);
2385   PH2.replaceUseWith(nullptr);
2386   EXPECT_EQ(N0, D->getOperand(0));
2387   EXPECT_EQ(N1, D->getOperand(1));
2388   EXPECT_EQ(nullptr, D->getOperand(2));
2389 }
2390
2391 TEST_F(DistinctMDOperandPlaceholderTest, replaceUseWithNoUser) {
2392   // There is no user, but we can still call replace.
2393   DistinctMDOperandPlaceholder(7).replaceUseWith(MDTuple::get(Context, None));
2394 }
2395
2396 #ifndef NDEBUG
2397 #ifdef GTEST_HAS_DEATH_TEST
2398 TEST_F(DistinctMDOperandPlaceholderTest, MetadataAsValue) {
2399   // This shouldn't crash.
2400   DistinctMDOperandPlaceholder PH(7);
2401   EXPECT_DEATH(MetadataAsValue::get(Context, &PH),
2402                "Unexpected callback to owner");
2403 }
2404
2405 TEST_F(DistinctMDOperandPlaceholderTest, UniquedMDNode) {
2406   // This shouldn't crash.
2407   DistinctMDOperandPlaceholder PH(7);
2408   EXPECT_DEATH(MDTuple::get(Context, &PH), "Unexpected callback to owner");
2409 }
2410
2411 TEST_F(DistinctMDOperandPlaceholderTest, SecondDistinctMDNode) {
2412   // This shouldn't crash.
2413   DistinctMDOperandPlaceholder PH(7);
2414   MDTuple::getDistinct(Context, &PH);
2415   EXPECT_DEATH(MDTuple::getDistinct(Context, &PH),
2416                "Placeholders can only be used once");
2417 }
2418
2419 TEST_F(DistinctMDOperandPlaceholderTest, TrackingMDRefAndDistinctMDNode) {
2420   // TrackingMDRef doesn't install an owner callback, so it can't be detected
2421   // as an invalid use.  However, using a placeholder in a TrackingMDRef *and*
2422   // a distinct node isn't possible and we should assert.
2423   //
2424   // (There's no positive test for using TrackingMDRef because it's not a
2425   // useful thing to do.)
2426   {
2427     DistinctMDOperandPlaceholder PH(7);
2428     MDTuple::getDistinct(Context, &PH);
2429     EXPECT_DEATH(TrackingMDRef Ref(&PH), "Placeholders can only be used once");
2430   }
2431   {
2432     DistinctMDOperandPlaceholder PH(7);
2433     TrackingMDRef Ref(&PH);
2434     EXPECT_DEATH(MDTuple::getDistinct(Context, &PH),
2435                  "Placeholders can only be used once");
2436   }
2437 }
2438 #endif
2439 #endif
2440
2441 } // end namespace