xref: /llvm-project/llvm/unittests/IR/DebugInfoTest.cpp (revision 4259198d65c1454b5cb5e60a46b2cce2544f1ca5)
1 //===- llvm/unittest/IR/DebugInfo.cpp - DebugInfo tests -------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/IR/DebugInfo.h"
10 #include "llvm/ADT/APSInt.h"
11 #include "llvm/AsmParser/Parser.h"
12 #include "llvm/IR/DIBuilder.h"
13 #include "llvm/IR/DebugInfoMetadata.h"
14 #include "llvm/IR/DebugProgramInstruction.h"
15 #include "llvm/IR/IRBuilder.h"
16 #include "llvm/IR/IntrinsicInst.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/IR/Metadata.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/IR/Verifier.h"
21 #include "llvm/Support/SourceMgr.h"
22 #include "llvm/Transforms/Utils/Local.h"
23 #include "gtest/gtest.h"
24 
25 using namespace llvm;
26 
27 extern cl::opt<bool> UseNewDbgInfoFormat;
28 
29 static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
30   SMDiagnostic Err;
31   std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
32   if (!Mod)
33     Err.print("DebugInfoTest", errs());
34   return Mod;
35 }
36 
37 namespace {
38 
39 TEST(DINodeTest, getFlag) {
40   // Some valid flags.
41   EXPECT_EQ(DINode::FlagPublic, DINode::getFlag("DIFlagPublic"));
42   EXPECT_EQ(DINode::FlagProtected, DINode::getFlag("DIFlagProtected"));
43   EXPECT_EQ(DINode::FlagPrivate, DINode::getFlag("DIFlagPrivate"));
44   EXPECT_EQ(DINode::FlagVector, DINode::getFlag("DIFlagVector"));
45   EXPECT_EQ(DINode::FlagRValueReference,
46             DINode::getFlag("DIFlagRValueReference"));
47 
48   // FlagAccessibility shouldn't work.
49   EXPECT_EQ(0u, DINode::getFlag("DIFlagAccessibility"));
50 
51   // Some other invalid strings.
52   EXPECT_EQ(0u, DINode::getFlag("FlagVector"));
53   EXPECT_EQ(0u, DINode::getFlag("Vector"));
54   EXPECT_EQ(0u, DINode::getFlag("other things"));
55   EXPECT_EQ(0u, DINode::getFlag("DIFlagOther"));
56 }
57 
58 TEST(DINodeTest, getFlagString) {
59   // Some valid flags.
60   EXPECT_EQ(StringRef("DIFlagPublic"),
61             DINode::getFlagString(DINode::FlagPublic));
62   EXPECT_EQ(StringRef("DIFlagProtected"),
63             DINode::getFlagString(DINode::FlagProtected));
64   EXPECT_EQ(StringRef("DIFlagPrivate"),
65             DINode::getFlagString(DINode::FlagPrivate));
66   EXPECT_EQ(StringRef("DIFlagVector"),
67             DINode::getFlagString(DINode::FlagVector));
68   EXPECT_EQ(StringRef("DIFlagRValueReference"),
69             DINode::getFlagString(DINode::FlagRValueReference));
70 
71   // FlagAccessibility actually equals FlagPublic.
72   EXPECT_EQ(StringRef("DIFlagPublic"),
73             DINode::getFlagString(DINode::FlagAccessibility));
74 
75   // Some other invalid flags.
76   EXPECT_EQ(StringRef(),
77             DINode::getFlagString(DINode::FlagPublic | DINode::FlagVector));
78   EXPECT_EQ(StringRef(), DINode::getFlagString(DINode::FlagFwdDecl |
79                                                DINode::FlagArtificial));
80   EXPECT_EQ(StringRef(),
81             DINode::getFlagString(static_cast<DINode::DIFlags>(0xffff)));
82 }
83 
84 TEST(DINodeTest, splitFlags) {
85 // Some valid flags.
86 #define CHECK_SPLIT(FLAGS, VECTOR, REMAINDER)                                  \
87   {                                                                            \
88     SmallVector<DINode::DIFlags, 8> V;                                         \
89     EXPECT_EQ(REMAINDER, DINode::splitFlags(FLAGS, V));                        \
90     EXPECT_TRUE(ArrayRef(V).equals(VECTOR));                                   \
91   }
92   CHECK_SPLIT(DINode::FlagPublic, {DINode::FlagPublic}, DINode::FlagZero);
93   CHECK_SPLIT(DINode::FlagProtected, {DINode::FlagProtected}, DINode::FlagZero);
94   CHECK_SPLIT(DINode::FlagPrivate, {DINode::FlagPrivate}, DINode::FlagZero);
95   CHECK_SPLIT(DINode::FlagVector, {DINode::FlagVector}, DINode::FlagZero);
96   CHECK_SPLIT(DINode::FlagRValueReference, {DINode::FlagRValueReference},
97               DINode::FlagZero);
98   DINode::DIFlags Flags[] = {DINode::FlagFwdDecl, DINode::FlagVector};
99   CHECK_SPLIT(DINode::FlagFwdDecl | DINode::FlagVector, Flags,
100               DINode::FlagZero);
101   CHECK_SPLIT(DINode::FlagZero, {}, DINode::FlagZero);
102 #undef CHECK_SPLIT
103 }
104 
105 TEST(StripTest, LoopMetadata) {
106   LLVMContext C;
107   std::unique_ptr<Module> M = parseIR(C, R"(
108     define void @f() !dbg !5 {
109       ret void, !dbg !10, !llvm.loop !11
110     }
111 
112     !llvm.dbg.cu = !{!0}
113     !llvm.debugify = !{!3, !3}
114     !llvm.module.flags = !{!4}
115 
116     !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
117     !1 = !DIFile(filename: "loop.ll", directory: "/")
118     !2 = !{}
119     !3 = !{i32 1}
120     !4 = !{i32 2, !"Debug Info Version", i32 3}
121     !5 = distinct !DISubprogram(name: "f", linkageName: "f", scope: null, file: !1, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !7)
122     !6 = !DISubroutineType(types: !2)
123     !7 = !{!8}
124     !8 = !DILocalVariable(name: "1", scope: !5, file: !1, line: 1, type: !9)
125     !9 = !DIBasicType(name: "ty32", size: 32, encoding: DW_ATE_unsigned)
126     !10 = !DILocation(line: 1, column: 1, scope: !5)
127     !11 = distinct !{!11, !10, !10}
128 )");
129 
130   // Look up the debug info emission kind for the CU via the loop metadata
131   // attached to the terminator. If, when stripping non-line table debug info,
132   // we update the terminator's metadata correctly, we should be able to
133   // observe the change in emission kind for the CU.
134   auto getEmissionKind = [&]() {
135     Instruction &I = *M->getFunction("f")->getEntryBlock().getFirstNonPHI();
136     MDNode *LoopMD = I.getMetadata(LLVMContext::MD_loop);
137     return cast<DILocation>(LoopMD->getOperand(1))
138         ->getScope()
139         ->getSubprogram()
140         ->getUnit()
141         ->getEmissionKind();
142   };
143 
144   EXPECT_EQ(getEmissionKind(), DICompileUnit::FullDebug);
145 
146   bool Changed = stripNonLineTableDebugInfo(*M);
147   EXPECT_TRUE(Changed);
148 
149   EXPECT_EQ(getEmissionKind(), DICompileUnit::LineTablesOnly);
150 
151   bool BrokenDebugInfo = false;
152   bool HardError = verifyModule(*M, &errs(), &BrokenDebugInfo);
153   EXPECT_FALSE(HardError);
154   EXPECT_FALSE(BrokenDebugInfo);
155 }
156 
157 TEST(MetadataTest, DeleteInstUsedByDbgValue) {
158   LLVMContext C;
159   std::unique_ptr<Module> M = parseIR(C, R"(
160     define i16 @f(i16 %a) !dbg !6 {
161       %b = add i16 %a, 1, !dbg !11
162       call void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata !DIExpression()), !dbg !11
163       ret i16 0, !dbg !11
164     }
165     declare void @llvm.dbg.value(metadata, metadata, metadata) #0
166     attributes #0 = { nounwind readnone speculatable willreturn }
167 
168     !llvm.dbg.cu = !{!0}
169     !llvm.module.flags = !{!5}
170 
171     !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
172     !1 = !DIFile(filename: "t.ll", directory: "/")
173     !2 = !{}
174     !5 = !{i32 2, !"Debug Info Version", i32 3}
175     !6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
176     !7 = !DISubroutineType(types: !2)
177     !8 = !{!9}
178     !9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
179     !10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
180     !11 = !DILocation(line: 1, column: 1, scope: !6)
181 )");
182 
183   // Find %b = add ...
184   Instruction &I = *M->getFunction("f")->getEntryBlock().getFirstNonPHI();
185 
186   // Find the dbg.value using %b.
187   SmallVector<DbgValueInst *, 1> DVIs;
188   findDbgValues(DVIs, &I);
189 
190   // Delete %b. The dbg.value should now point to undef.
191   I.eraseFromParent();
192   EXPECT_EQ(DVIs[0]->getNumVariableLocationOps(), 1u);
193   EXPECT_TRUE(isa<UndefValue>(DVIs[0]->getValue(0)));
194 }
195 
196 TEST(DbgVariableIntrinsic, EmptyMDIsKillLocation) {
197   LLVMContext Ctx;
198   std::unique_ptr<Module> M = parseIR(Ctx, R"(
199     define dso_local void @fun() local_unnamed_addr #0 !dbg !9 {
200     entry:
201       call void @llvm.dbg.declare(metadata !{}, metadata !13, metadata !DIExpression()), !dbg !16
202       ret void, !dbg !16
203     }
204 
205     declare void @llvm.dbg.declare(metadata, metadata, metadata)
206 
207     !llvm.dbg.cu = !{!0}
208     !llvm.module.flags = !{!2, !3}
209     !llvm.ident = !{!8}
210 
211     !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 16.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
212     !1 = !DIFile(filename: "test.c", directory: "/")
213     !2 = !{i32 7, !"Dwarf Version", i32 5}
214     !3 = !{i32 2, !"Debug Info Version", i32 3}
215     !8 = !{!"clang version 16.0.0"}
216     !9 = distinct !DISubprogram(name: "fun", scope: !1, file: !1, line: 1, type: !10, scopeLine: 1, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !12)
217     !10 = !DISubroutineType(types: !11)
218     !11 = !{null}
219     !12 = !{!13}
220     !13 = !DILocalVariable(name: "a", scope: !9, file: !1, line: 1, type: !14)
221     !14 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
222     !16 = !DILocation(line: 1, column: 21, scope: !9)
223     )");
224 
225   bool BrokenDebugInfo = true;
226   verifyModule(*M, &errs(), &BrokenDebugInfo);
227   ASSERT_FALSE(BrokenDebugInfo);
228 
229   // Get the dbg.declare.
230   Function &F = *cast<Function>(M->getNamedValue("fun"));
231   DbgVariableIntrinsic *DbgDeclare =
232       cast<DbgVariableIntrinsic>(&F.front().front());
233   // Check that this form counts as a "no location" marker.
234   EXPECT_TRUE(DbgDeclare->isKillLocation());
235 }
236 
237 // Duplicate of above test, but in DPValue representation.
238 TEST(MetadataTest, DeleteInstUsedByDPValue) {
239   LLVMContext C;
240   std::unique_ptr<Module> M = parseIR(C, R"(
241     define i16 @f(i16 %a) !dbg !6 {
242       %b = add i16 %a, 1, !dbg !11
243       call void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata !DIExpression()), !dbg !11
244       call void @llvm.dbg.value(metadata !DIArgList(i16 %a, i16 %b), metadata !9, metadata !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_plus)), !dbg !11
245       ret i16 0, !dbg !11
246     }
247     declare void @llvm.dbg.value(metadata, metadata, metadata) #0
248     attributes #0 = { nounwind readnone speculatable willreturn }
249 
250     !llvm.dbg.cu = !{!0}
251     !llvm.module.flags = !{!5}
252 
253     !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
254     !1 = !DIFile(filename: "t.ll", directory: "/")
255     !2 = !{}
256     !5 = !{i32 2, !"Debug Info Version", i32 3}
257     !6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
258     !7 = !DISubroutineType(types: !2)
259     !8 = !{!9}
260     !9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
261     !10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
262     !11 = !DILocation(line: 1, column: 1, scope: !6)
263 )");
264 
265   bool OldDbgValueMode = UseNewDbgInfoFormat;
266   UseNewDbgInfoFormat = true;
267   Instruction &I = *M->getFunction("f")->getEntryBlock().getFirstNonPHI();
268   M->convertToNewDbgValues();
269 
270   // Find the DPValues using %b.
271   SmallVector<DbgValueInst *, 2> DVIs;
272   SmallVector<DPValue *, 2> DPVs;
273   findDbgValues(DVIs, &I, &DPVs);
274   ASSERT_EQ(DPVs.size(), 2u);
275 
276   // Delete %b. The DPValue should now point to undef.
277   I.eraseFromParent();
278   EXPECT_EQ(DPVs[0]->getNumVariableLocationOps(), 1u);
279   EXPECT_TRUE(isa<UndefValue>(DPVs[0]->getVariableLocationOp(0)));
280   EXPECT_TRUE(DPVs[0]->isKillLocation());
281   EXPECT_EQ(DPVs[1]->getNumVariableLocationOps(), 2u);
282   EXPECT_TRUE(isa<UndefValue>(DPVs[1]->getVariableLocationOp(1)));
283   EXPECT_TRUE(DPVs[1]->isKillLocation());
284   UseNewDbgInfoFormat = OldDbgValueMode;
285 }
286 
287 TEST(DIBuiler, CreateFile) {
288   LLVMContext Ctx;
289   std::unique_ptr<Module> M(new Module("MyModule", Ctx));
290   DIBuilder DIB(*M);
291 
292   DIFile *F = DIB.createFile("main.c", "/");
293   EXPECT_EQ(std::nullopt, F->getSource());
294 
295   std::optional<DIFile::ChecksumInfo<StringRef>> Checksum;
296   std::optional<StringRef> Source;
297   F = DIB.createFile("main.c", "/", Checksum, Source);
298   EXPECT_EQ(Source, F->getSource());
299 
300   Source = "";
301   F = DIB.createFile("main.c", "/", Checksum, Source);
302   EXPECT_EQ(Source, F->getSource());
303 }
304 
305 TEST(DIBuilder, CreateFortranArrayTypeWithAttributes) {
306   LLVMContext Ctx;
307   std::unique_ptr<Module> M(new Module("MyModule", Ctx));
308   DIBuilder DIB(*M);
309 
310   DISubrange *Subrange = DIB.getOrCreateSubrange(1,1);
311   SmallVector<Metadata*, 4> Subranges;
312   Subranges.push_back(Subrange);
313   DINodeArray Subscripts = DIB.getOrCreateArray(Subranges);
314 
315   auto getDIExpression = [&DIB](int offset) {
316     SmallVector<uint64_t, 4> ops;
317     ops.push_back(llvm::dwarf::DW_OP_push_object_address);
318     DIExpression::appendOffset(ops, offset);
319     ops.push_back(llvm::dwarf::DW_OP_deref);
320 
321     return DIB.createExpression(ops);
322   };
323 
324   DIFile *F = DIB.createFile("main.c", "/");
325   DICompileUnit *CU = DIB.createCompileUnit(
326       dwarf::DW_LANG_C, DIB.createFile("main.c", "/"), "llvm-c", true, "", 0);
327 
328   DIVariable *DataLocation =
329       DIB.createTempGlobalVariableFwdDecl(CU, "dl", "_dl", F, 1, nullptr, true);
330   DIExpression *Associated = getDIExpression(1);
331   DIExpression *Allocated = getDIExpression(2);
332   DIExpression *Rank = DIB.createConstantValueExpression(3);
333 
334   DICompositeType *ArrayType = DIB.createArrayType(0, 0, nullptr, Subscripts,
335                                                    DataLocation, Associated,
336                                                    Allocated, Rank);
337 
338   EXPECT_TRUE(isa_and_nonnull<DICompositeType>(ArrayType));
339   EXPECT_EQ(ArrayType->getRawDataLocation(), DataLocation);
340   EXPECT_EQ(ArrayType->getRawAssociated(), Associated);
341   EXPECT_EQ(ArrayType->getRawAllocated(), Allocated);
342   EXPECT_EQ(ArrayType->getRawRank(), Rank);
343 
344   // Avoid memory leak.
345   DIVariable::deleteTemporary(DataLocation);
346 }
347 
348 TEST(DIBuilder, CreateSetType) {
349   LLVMContext Ctx;
350   std::unique_ptr<Module> M(new Module("MyModule", Ctx));
351   DIBuilder DIB(*M);
352   DIScope *Scope = DISubprogram::getDistinct(
353       Ctx, nullptr, "", "", nullptr, 0, nullptr, 0, nullptr, 0, 0,
354       DINode::FlagZero, DISubprogram::SPFlagZero, nullptr);
355   DIType *Type = DIB.createBasicType("Int", 64, dwarf::DW_ATE_signed);
356   DIFile *F = DIB.createFile("main.c", "/");
357 
358   DIDerivedType *SetType = DIB.createSetType(Scope, "set1", F, 1, 64, 64, Type);
359   EXPECT_TRUE(isa_and_nonnull<DIDerivedType>(SetType));
360 }
361 
362 TEST(DIBuilder, CreateStringType) {
363   LLVMContext Ctx;
364   std::unique_ptr<Module> M(new Module("MyModule", Ctx));
365   DIBuilder DIB(*M);
366   DIScope *Scope = DISubprogram::getDistinct(
367       Ctx, nullptr, "", "", nullptr, 0, nullptr, 0, nullptr, 0, 0,
368       DINode::FlagZero, DISubprogram::SPFlagZero, nullptr);
369   DIFile *F = DIB.createFile("main.c", "/");
370   StringRef StrName = "string";
371   DIVariable *StringLen = DIB.createAutoVariable(Scope, StrName, F, 0, nullptr,
372                                                  false, DINode::FlagZero, 0);
373   auto getDIExpression = [&DIB](int offset) {
374     SmallVector<uint64_t, 4> ops;
375     ops.push_back(llvm::dwarf::DW_OP_push_object_address);
376     DIExpression::appendOffset(ops, offset);
377     ops.push_back(llvm::dwarf::DW_OP_deref);
378 
379     return DIB.createExpression(ops);
380   };
381   DIExpression *StringLocationExp = getDIExpression(1);
382   DIStringType *StringType =
383       DIB.createStringType(StrName, StringLen, StringLocationExp);
384 
385   EXPECT_TRUE(isa_and_nonnull<DIStringType>(StringType));
386   EXPECT_EQ(StringType->getName(), StrName);
387   EXPECT_EQ(StringType->getStringLength(), StringLen);
388   EXPECT_EQ(StringType->getStringLocationExp(), StringLocationExp);
389 
390   StringRef StrNameExp = "stringexp";
391   DIExpression *StringLengthExp = getDIExpression(2);
392   DIStringType *StringTypeExp =
393       DIB.createStringType(StrNameExp, StringLengthExp, StringLocationExp);
394 
395   EXPECT_TRUE(isa_and_nonnull<DIStringType>(StringTypeExp));
396   EXPECT_EQ(StringTypeExp->getName(), StrNameExp);
397   EXPECT_EQ(StringTypeExp->getStringLocationExp(), StringLocationExp);
398   EXPECT_EQ(StringTypeExp->getStringLengthExp(), StringLengthExp);
399 }
400 
401 TEST(DIBuilder, DIEnumerator) {
402   LLVMContext Ctx;
403   std::unique_ptr<Module> M(new Module("MyModule", Ctx));
404   DIBuilder DIB(*M);
405   APSInt I1(APInt(32, 1));
406   APSInt I2(APInt(33, 1));
407 
408   auto *E = DIEnumerator::get(Ctx, I1, I1.isSigned(), "name");
409   EXPECT_TRUE(E);
410 
411   auto *E1 = DIEnumerator::getIfExists(Ctx, I1, I1.isSigned(), "name");
412   EXPECT_TRUE(E1);
413 
414   auto *E2 = DIEnumerator::getIfExists(Ctx, I2, I1.isSigned(), "name");
415   EXPECT_FALSE(E2);
416 }
417 
418 TEST(DbgAssignIntrinsicTest, replaceVariableLocationOp) {
419   LLVMContext C;
420   std::unique_ptr<Module> M = parseIR(C, R"(
421     define dso_local void @fun(i32 %v1, ptr %p1, ptr %p2) !dbg !7 {
422     entry:
423       call void @llvm.dbg.assign(metadata i32 %v1, metadata !14, metadata !DIExpression(), metadata !17, metadata ptr %p1, metadata !DIExpression()), !dbg !16
424       ret void
425     }
426 
427     declare void @llvm.dbg.assign(metadata, metadata, metadata, metadata, metadata, metadata)
428 
429     !llvm.dbg.cu = !{!0}
430     !llvm.module.flags = !{!3}
431 
432     !0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 14.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
433     !1 = !DIFile(filename: "test.cpp", directory: "/")
434     !3 = !{i32 2, !"Debug Info Version", i32 3}
435     !7 = distinct !DISubprogram(name: "fun", linkageName: "fun", scope: !1, file: !1, line: 2, type: !8, scopeLine: 2, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !11)
436     !8 = !DISubroutineType(types: !9)
437     !9 = !{null}
438     !10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
439     !11 = !{}
440     !14 = !DILocalVariable(name: "Local", scope: !7, file: !1, line: 3, type: !10)
441     !16 = !DILocation(line: 0, scope: !7)
442     !17 = distinct !DIAssignID()
443     )");
444   // Check the test IR isn't malformed.
445   ASSERT_TRUE(M);
446 
447   Function &Fun = *M->getFunction("fun");
448   Value *V1 = Fun.getArg(0);
449   Value *P1 = Fun.getArg(1);
450   Value *P2 = Fun.getArg(2);
451   DbgAssignIntrinsic *DAI = cast<DbgAssignIntrinsic>(Fun.begin()->begin());
452   ASSERT_TRUE(V1 == DAI->getVariableLocationOp(0));
453   ASSERT_TRUE(P1 == DAI->getAddress());
454 
455 #define TEST_REPLACE(Old, New, ExpectedValue, ExpectedAddr)                    \
456   DAI->replaceVariableLocationOp(Old, New);                                    \
457   EXPECT_EQ(DAI->getVariableLocationOp(0), ExpectedValue);                     \
458   EXPECT_EQ(DAI->getAddress(), ExpectedAddr);
459 
460   // Replace address only.
461   TEST_REPLACE(/*Old*/ P1, /*New*/ P2, /*Value*/ V1, /*Address*/ P2);
462   // Replace value only.
463   TEST_REPLACE(/*Old*/ V1, /*New*/ P2, /*Value*/ P2, /*Address*/ P2);
464   // Replace both.
465   TEST_REPLACE(/*Old*/ P2, /*New*/ P1, /*Value*/ P1, /*Address*/ P1);
466 
467   // Replace address only, value uses a DIArgList.
468   // Value = {DIArgList(V1)}, Addr = P1.
469   DAI->setRawLocation(DIArgList::get(C, ValueAsMetadata::get(V1)));
470   DAI->setExpression(DIExpression::get(
471       C, {dwarf::DW_OP_LLVM_arg, 0, dwarf::DW_OP_stack_value}));
472   TEST_REPLACE(/*Old*/ P1, /*New*/ P2, /*Value*/ V1, /*Address*/ P2);
473 #undef TEST_REPLACE
474 }
475 
476 TEST(AssignmentTrackingTest, Utils) {
477   // Test the assignment tracking utils defined in DebugInfo.h namespace at {}.
478   // This includes:
479   //     getAssignmentInsts
480   //     getAssignmentMarkers
481   //     RAUW
482   //     deleteAll
483   //
484   // The input IR includes two functions, fun1 and fun2. Both contain an alloca
485   // with a DIAssignID tag. fun1's alloca is linked to two llvm.dbg.assign
486   // intrinsics, one of which is for an inlined variable and appears before the
487   // alloca.
488 
489   LLVMContext C;
490   std::unique_ptr<Module> M = parseIR(C, R"(
491     define dso_local void @fun1() !dbg !7 {
492     entry:
493       call void @llvm.dbg.assign(metadata i32 undef, metadata !10, metadata !DIExpression(), metadata !12, metadata i32 undef, metadata !DIExpression()), !dbg !13
494       %local = alloca i32, align 4, !DIAssignID !12
495       call void @llvm.dbg.assign(metadata i32 undef, metadata !16, metadata !DIExpression(), metadata !12, metadata i32 undef, metadata !DIExpression()), !dbg !15
496       ret void, !dbg !15
497     }
498 
499     define dso_local void @fun2() !dbg !17 {
500     entry:
501       %local = alloca i32, align 4, !DIAssignID !20
502       call void @llvm.dbg.assign(metadata i32 undef, metadata !18, metadata !DIExpression(), metadata !20, metadata i32 undef, metadata !DIExpression()), !dbg !19
503       ret void, !dbg !19
504     }
505 
506     define dso_local void @fun3() !dbg !21 {
507     entry:
508       %local = alloca i32, align 4, !DIAssignID !24
509       call void @llvm.dbg.assign(metadata i32 undef, metadata !22, metadata !DIExpression(), metadata !24, metadata i32* undef, metadata !DIExpression()), !dbg !23
510       ret void
511     }
512 
513     declare void @llvm.dbg.assign(metadata, metadata, metadata, metadata, metadata, metadata)
514 
515     !llvm.dbg.cu = !{!0}
516     !llvm.module.flags = !{!3, !4, !5}
517     !llvm.ident = !{!6}
518 
519     !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 14.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None)
520     !1 = !DIFile(filename: "test.c", directory: "/")
521     !2 = !{}
522     !3 = !{i32 7, !"Dwarf Version", i32 4}
523     !4 = !{i32 2, !"Debug Info Version", i32 3}
524     !5 = !{i32 1, !"wchar_size", i32 4}
525     !6 = !{!"clang version 14.0.0"}
526     !7 = distinct !DISubprogram(name: "fun1", scope: !1, file: !1, line: 1, type: !8, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
527     !8 = !DISubroutineType(types: !9)
528     !9 = !{null}
529     !10 = !DILocalVariable(name: "local3", scope: !14, file: !1, line: 2, type: !11)
530     !11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
531     !12 = distinct !DIAssignID()
532     !13 = !DILocation(line: 5, column: 1, scope: !14, inlinedAt: !15)
533     !14 = distinct !DISubprogram(name: "inline", scope: !1, file: !1, line: 1, type: !8, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
534     !15 = !DILocation(line: 3, column: 1, scope: !7)
535     !16 = !DILocalVariable(name: "local1", scope: !7, file: !1, line: 2, type: !11)
536     !17 = distinct !DISubprogram(name: "fun2", scope: !1, file: !1, line: 1, type: !8, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
537     !18 = !DILocalVariable(name: "local2", scope: !17, file: !1, line: 2, type: !11)
538     !19 = !DILocation(line: 4, column: 1, scope: !17)
539     !20 = distinct !DIAssignID()
540     !21 = distinct !DISubprogram(name: "fun3", scope: !1, file: !1, line: 1, type: !8, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
541     !22 = !DILocalVariable(name: "local4", scope: !21, file: !1, line: 2, type: !11)
542     !23 = !DILocation(line: 4, column: 1, scope: !21)
543     !24 = distinct !DIAssignID()
544     )");
545 
546   // Check the test IR isn't malformed.
547   ASSERT_TRUE(M);
548 
549   Function &Fun1 = *M->getFunction("fun1");
550   Instruction &Alloca = *Fun1.getEntryBlock().getFirstNonPHIOrDbg();
551 
552   // 1. Check the Instruction <-> Intrinsic mappings work in fun1.
553   //
554   // Check there are two llvm.dbg.assign intrinsics linked to Alloca.
555   auto CheckFun1Mapping = [&Alloca]() {
556     auto Markers = at::getAssignmentMarkers(&Alloca);
557     EXPECT_TRUE(std::distance(Markers.begin(), Markers.end()) == 2);
558     // Check those two entries are distinct.
559     DbgAssignIntrinsic *First = *Markers.begin();
560     DbgAssignIntrinsic *Second = *std::next(Markers.begin());
561     EXPECT_NE(First, Second);
562 
563     // Check that we can get back to Alloca from each llvm.dbg.assign.
564     for (auto *DAI : Markers) {
565       auto Insts = at::getAssignmentInsts(DAI);
566       // Check there is exactly one instruction linked to each intrinsic. Use
567       // ASSERT_TRUE because we're going to dereference the begin iterator.
568       ASSERT_TRUE(std::distance(Insts.begin(), Insts.end()) == 1);
569       EXPECT_FALSE(Insts.empty());
570       // Check the linked instruction is Alloca.
571       Instruction *LinkedInst = *Insts.begin();
572       EXPECT_EQ(LinkedInst, &Alloca);
573     }
574   };
575   CheckFun1Mapping();
576 
577   // 2. Check DIAssignID RAUW replaces attachments and uses.
578   //
579   DIAssignID *Old =
580       cast_or_null<DIAssignID>(Alloca.getMetadata(LLVMContext::MD_DIAssignID));
581   DIAssignID *New = DIAssignID::getDistinct(C);
582   ASSERT_TRUE(Old && New && New != Old);
583   at::RAUW(Old, New);
584   // Check fun1's alloca and intrinsics have been updated and the mapping still
585   // works.
586   EXPECT_EQ(New, cast_or_null<DIAssignID>(
587                      Alloca.getMetadata(LLVMContext::MD_DIAssignID)));
588   CheckFun1Mapping();
589 
590   // Check that fun2's alloca and intrinsic have not not been updated.
591   Instruction &Fun2Alloca =
592       *M->getFunction("fun2")->getEntryBlock().getFirstNonPHIOrDbg();
593   DIAssignID *Fun2ID = cast_or_null<DIAssignID>(
594       Fun2Alloca.getMetadata(LLVMContext::MD_DIAssignID));
595   EXPECT_NE(New, Fun2ID);
596   auto Fun2Markers = at::getAssignmentMarkers(&Fun2Alloca);
597   ASSERT_TRUE(std::distance(Fun2Markers.begin(), Fun2Markers.end()) == 1);
598   auto Fun2Insts = at::getAssignmentInsts(*Fun2Markers.begin());
599   ASSERT_TRUE(std::distance(Fun2Insts.begin(), Fun2Insts.end()) == 1);
600   EXPECT_EQ(*Fun2Insts.begin(), &Fun2Alloca);
601 
602   // 3. Check that deleting dbg.assigns from a specific instruction works.
603   Instruction &Fun3Alloca =
604       *M->getFunction("fun3")->getEntryBlock().getFirstNonPHIOrDbg();
605   auto Fun3Markers = at::getAssignmentMarkers(&Fun3Alloca);
606   ASSERT_TRUE(std::distance(Fun3Markers.begin(), Fun3Markers.end()) == 1);
607   at::deleteAssignmentMarkers(&Fun3Alloca);
608   Fun3Markers = at::getAssignmentMarkers(&Fun3Alloca);
609   EXPECT_EQ(Fun3Markers.empty(), true);
610 
611   // 4. Check that deleting works and applies only to the target function.
612   at::deleteAll(&Fun1);
613   // There should now only be the alloca and ret in fun1.
614   EXPECT_EQ(Fun1.begin()->size(), 2u);
615   // fun2's alloca should have the same DIAssignID and remain linked to its
616   // llvm.dbg.assign.
617   EXPECT_EQ(Fun2ID, cast_or_null<DIAssignID>(
618                         Fun2Alloca.getMetadata(LLVMContext::MD_DIAssignID)));
619   EXPECT_FALSE(at::getAssignmentMarkers(&Fun2Alloca).empty());
620 }
621 
622 TEST(IRBuilder, GetSetInsertionPointWithEmptyBasicBlock) {
623   LLVMContext C;
624   std::unique_ptr<BasicBlock> BB(BasicBlock::Create(C, "start"));
625   Module *M = new Module("module", C);
626   IRBuilder<> Builder(BB.get());
627   Function *DbgDeclare = Intrinsic::getDeclaration(M, Intrinsic::dbg_declare);
628   Value *DIV = MetadataAsValue::get(C, (Metadata *)nullptr);
629   SmallVector<Value *, 3> Args = {DIV, DIV, DIV};
630   Builder.CreateCall(DbgDeclare, Args);
631   auto IP = BB->getFirstInsertionPt();
632   Builder.SetInsertPoint(BB.get(), IP);
633 }
634 
635 TEST(AssignmentTrackingTest, InstrMethods) {
636   // Test the assignment tracking Instruction methods.
637   // This includes:
638   //     Instruction::mergeDIAssignID
639 
640   LLVMContext C;
641   std::unique_ptr<Module> M = parseIR(C, R"(
642     define dso_local void @fun() #0 !dbg !8 {
643     entry:
644       %Local = alloca [2 x i32], align 4, !DIAssignID !12
645       call void @llvm.dbg.assign(metadata i1 undef, metadata !13, metadata !DIExpression(), metadata !12, metadata [2 x i32]* %Local, metadata !DIExpression()), !dbg !18
646       %arrayidx = getelementptr inbounds [2 x i32], [2 x i32]* %Local, i64 0, i64 0, !dbg !19
647       store i32 5, i32* %arrayidx, align 4, !dbg !20, !DIAssignID !21
648       call void @llvm.dbg.assign(metadata i32 5, metadata !13, metadata !DIExpression(DW_OP_LLVM_fragment, 0, 32), metadata !21, metadata i32* %arrayidx, metadata !DIExpression()), !dbg !18
649       %arrayidx1 = getelementptr inbounds [2 x i32], [2 x i32]* %Local, i64 0, i64 1, !dbg !22
650       store i32 6, i32* %arrayidx1, align 4, !dbg !23, !DIAssignID !24
651       call void @llvm.dbg.assign(metadata i32 6, metadata !13, metadata !DIExpression(DW_OP_LLVM_fragment, 32, 32), metadata !24, metadata i32* %arrayidx1, metadata !DIExpression()), !dbg !18
652       ret void, !dbg !25
653     }
654 
655     declare void @llvm.dbg.assign(metadata, metadata, metadata, metadata, metadata, metadata) #1
656 
657     !llvm.dbg.cu = !{!0}
658     !llvm.module.flags = !{!2, !3, !4, !5, !6}
659     !llvm.ident = !{!7}
660 
661     !0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 14.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
662     !1 = !DIFile(filename: "test.cpp", directory: "/")
663     !2 = !{i32 7, !"Dwarf Version", i32 5}
664     !3 = !{i32 2, !"Debug Info Version", i32 3}
665     !4 = !{i32 1, !"wchar_size", i32 4}
666     !5 = !{i32 7, !"uwtable", i32 1}
667     !6 = !{i32 7, !"frame-pointer", i32 2}
668     !7 = !{!"clang version 14.0.0"}
669     !8 = distinct !DISubprogram(name: "fun", linkageName: "fun", scope: !1, file: !1, line: 1, type: !9, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !11)
670     !9 = !DISubroutineType(types: !10)
671     !10 = !{null}
672     !11 = !{}
673     !12 = distinct !DIAssignID()
674     !13 = !DILocalVariable(name: "Local", scope: !8, file: !1, line: 2, type: !14)
675     !14 = !DICompositeType(tag: DW_TAG_array_type, baseType: !15, size: 64, elements: !16)
676     !15 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
677     !16 = !{!17}
678     !17 = !DISubrange(count: 2)
679     !18 = !DILocation(line: 0, scope: !8)
680     !19 = !DILocation(line: 3, column: 3, scope: !8)
681     !20 = !DILocation(line: 3, column: 12, scope: !8)
682     !21 = distinct !DIAssignID()
683     !22 = !DILocation(line: 4, column: 3, scope: !8)
684     !23 = !DILocation(line: 4, column: 12, scope: !8)
685     !24 = distinct !DIAssignID()
686     !25 = !DILocation(line: 5, column: 1, scope: !8)
687   )");
688 
689   // Check the test IR isn't malformed.
690   ASSERT_TRUE(M);
691   Function &Fun = *M->getFunction("fun");
692   SmallVector<Instruction *> Stores;
693   for (auto &BB : Fun) {
694     for (auto &I : BB) {
695       if (isa<StoreInst>(&I))
696         Stores.push_back(&I);
697     }
698   }
699 
700   // The test requires (at least) 2 stores.
701   ASSERT_TRUE(Stores.size() == 2);
702   // Use SetVectors to check that the attachments and markers are unique
703   // (another test requirement).
704   SetVector<Metadata *> OrigIDs;
705   SetVector<DbgAssignIntrinsic *> Markers;
706   for (const Instruction *SI : Stores) {
707     Metadata *ID = SI->getMetadata(LLVMContext::MD_DIAssignID);
708     ASSERT_TRUE(OrigIDs.insert(ID));
709     ASSERT_TRUE(ID != nullptr);
710     auto Range = at::getAssignmentMarkers(SI);
711     ASSERT_TRUE(std::distance(Range.begin(), Range.end()) == 1);
712     ASSERT_TRUE(Markers.insert(*Range.begin()));
713   }
714 
715   // Test 1 - mergeDIAssignID.
716   //
717   // Input            store0->mergeDIAssignID(store1)
718   // -----            -------------------------
719   // store0 !x        store0 !x
720   // dbg.assign0 !x   dbg.assign !x
721   // store1 !y        store1 !x
722   // dbg.assign1 !y   dbg.assign1 !x
723   {
724     Stores[0]->mergeDIAssignID(Stores[1]);
725     // Check that the stores share the same ID.
726     Metadata *NewID0 = Stores[0]->getMetadata(LLVMContext::MD_DIAssignID);
727     Metadata *NewID1 = Stores[1]->getMetadata(LLVMContext::MD_DIAssignID);
728     EXPECT_NE(NewID0, nullptr);
729     EXPECT_EQ(NewID0, NewID1);
730     EXPECT_EQ(Markers[0]->getAssignID(), NewID0);
731     EXPECT_EQ(Markers[1]->getAssignID(), NewID0);
732   }
733 
734   // Test 2 - mergeDIAssignID.
735   //
736   // Input            store0->mergeDIAssignID(store1)
737   // -----            -------------------------
738   // store0 !x        store0 !x
739   // dbg.assign0 !x   dbg.assign !x
740   // store1           store1
741   {
742     Stores[1]->setMetadata(LLVMContext::MD_DIAssignID, nullptr);
743     Stores[0]->mergeDIAssignID(Stores[1]);
744     // Check that store1 doesn't get a new ID.
745     Metadata *NewID0 = Stores[0]->getMetadata(LLVMContext::MD_DIAssignID);
746     Metadata *NewID1 = Stores[1]->getMetadata(LLVMContext::MD_DIAssignID);
747     EXPECT_NE(NewID0, nullptr);
748     EXPECT_EQ(NewID1, nullptr);
749     EXPECT_EQ(Markers[0]->getAssignID(), NewID0);
750   }
751 
752   // Test 3 - mergeDIAssignID.
753   //
754   // Input            store1->mergeDIAssignID(store0)
755   // -----            -------------------------
756   // store0 !x        store0 !x
757   // dbg.assign0 !x   dbg.assign !x
758   // store1           store1 !x
759   {
760     Stores[1]->setMetadata(LLVMContext::MD_DIAssignID, nullptr);
761     Stores[1]->mergeDIAssignID(Stores[0]);
762     // Check that the stores share the same ID (note store1 starts with none).
763     Metadata *NewID0 = Stores[0]->getMetadata(LLVMContext::MD_DIAssignID);
764     Metadata *NewID1 = Stores[1]->getMetadata(LLVMContext::MD_DIAssignID);
765     EXPECT_NE(NewID0, nullptr);
766     EXPECT_EQ(NewID0, NewID1);
767     EXPECT_EQ(Markers[0]->getAssignID(), NewID0);
768   }
769 
770   // Test 4 - mergeDIAssignID.
771   //
772   // Input            store1->mergeDIAssignID(store0)
773   // -----            -------------------------
774   // store0 !x        store0 !x
775   // dbg.assign0 !x   dbg.assign !x
776   // store1 !x        store1 !x
777   {
778     Stores[0]->mergeDIAssignID(Stores[1]);
779     // Check that the stores share the same ID.
780     Metadata *NewID0 = Stores[0]->getMetadata(LLVMContext::MD_DIAssignID);
781     Metadata *NewID1 = Stores[1]->getMetadata(LLVMContext::MD_DIAssignID);
782     EXPECT_NE(NewID0, nullptr);
783     EXPECT_EQ(NewID0, NewID1);
784     EXPECT_EQ(Markers[0]->getAssignID(), NewID0);
785   }
786 
787   // Test 5 - dropUnknownNonDebugMetadata.
788   //
789   // Input            store0->dropUnknownNonDebugMetadata()
790   // -----            -------------------------
791   // store0 !x        store0 !x
792   {
793     Stores[0]->dropUnknownNonDebugMetadata();
794     Metadata *NewID0 = Stores[0]->getMetadata(LLVMContext::MD_DIAssignID);
795     EXPECT_NE(NewID0, nullptr);
796   }
797 }
798 
799 // Test some very straight-forward operations on DPValues -- these are
800 // dbg.values that have been converted to a non-instruction format.
801 TEST(MetadataTest, ConvertDbgToDPValue) {
802   LLVMContext C;
803   std::unique_ptr<Module> M = parseIR(C, R"(
804     define i16 @f(i16 %a) !dbg !6 {
805       call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata !DIExpression()), !dbg !11
806       %b = add i16 %a, 1, !dbg !11
807       call void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata !DIExpression()), !dbg !11
808       ret i16 0, !dbg !11
809 
810     exit:
811       %c = add i16 %b, 1, !dbg !11
812       ret i16 0, !dbg !11
813     }
814     declare void @llvm.dbg.value(metadata, metadata, metadata) #0
815     attributes #0 = { nounwind readnone speculatable willreturn }
816 
817     !llvm.dbg.cu = !{!0}
818     !llvm.module.flags = !{!5}
819 
820     !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
821     !1 = !DIFile(filename: "t.ll", directory: "/")
822     !2 = !{}
823     !5 = !{i32 2, !"Debug Info Version", i32 3}
824     !6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
825     !7 = !DISubroutineType(types: !2)
826     !8 = !{!9}
827     !9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
828     !10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
829     !11 = !DILocation(line: 1, column: 1, scope: !6)
830 )");
831 
832   // Find the first dbg.value,
833   Instruction &I = *M->getFunction("f")->getEntryBlock().getFirstNonPHI();
834   const DILocalVariable *Var = nullptr;
835   const DIExpression *Expr = nullptr;
836   const DILocation *Loc = nullptr;
837   const Metadata *MLoc = nullptr;
838   DPValue *DPV1 = nullptr;
839   {
840     DbgValueInst *DPI = dyn_cast<DbgValueInst>(&I);
841     ASSERT_TRUE(DPI);
842     Var = DPI->getVariable();
843     Expr = DPI->getExpression();
844     Loc = DPI->getDebugLoc().get();
845     MLoc = DPI->getRawLocation();
846 
847     // Test the creation of a DPValue and it's conversion back to a dbg.value.
848     DPV1 = new DPValue(DPI);
849     EXPECT_EQ(DPV1->getVariable(), Var);
850     EXPECT_EQ(DPV1->getExpression(), Expr);
851     EXPECT_EQ(DPV1->getDebugLoc().get(), Loc);
852     EXPECT_EQ(DPV1->getRawLocation(), MLoc);
853 
854     // Erase dbg.value,
855     DPI->eraseFromParent();
856     // Re-create from DPV1, inserting at front.
857     DPV1->createDebugIntrinsic(&*M,
858                                &M->getFunction("f")->getEntryBlock().front());
859 
860     Instruction *NewDPI = &M->getFunction("f")->getEntryBlock().front();
861     DbgValueInst *DPI2 = dyn_cast<DbgValueInst>(NewDPI);
862     ASSERT_TRUE(DPI2);
863     EXPECT_EQ(DPI2->getVariable(), Var);
864     EXPECT_EQ(DPI2->getExpression(), Expr);
865     EXPECT_EQ(DPI2->getDebugLoc().get(), Loc);
866     EXPECT_EQ(DPI2->getRawLocation(), MLoc);
867   }
868 
869   // Fetch the second dbg.value, convert it to a DPValue,
870   BasicBlock::iterator It = M->getFunction("f")->getEntryBlock().begin();
871   It = std::next(std::next(It));
872   DbgValueInst *DPI3 = dyn_cast<DbgValueInst>(It);
873   ASSERT_TRUE(DPI3);
874   DPValue *DPV2 = new DPValue(DPI3);
875 
876   // These dbg.values are supposed to refer to different values.
877   EXPECT_NE(DPV1->getRawLocation(), DPV2->getRawLocation());
878 
879   // Try manipulating DPValues and markers in the exit block.
880   BasicBlock *ExitBlock = &*std::next(M->getFunction("f")->getEntryBlock().getIterator());
881   Instruction *FirstInst = &ExitBlock->front();
882   Instruction *RetInst = &*std::next(FirstInst->getIterator());
883 
884   // Set-up DPMarkers in this block.
885   ExitBlock->IsNewDbgInfoFormat = true;
886   ExitBlock->createMarker(FirstInst);
887   ExitBlock->createMarker(RetInst);
888 
889   // Insert DPValues into markers, order should come out DPV2, DPV1.
890   FirstInst->DbgMarker->insertDPValue(DPV1, false);
891   FirstInst->DbgMarker->insertDPValue(DPV2, true);
892   unsigned int ItCount = 0;
893   for (DPValue &Item : FirstInst->DbgMarker->getDbgValueRange()) {
894     EXPECT_TRUE((&Item == DPV2 && ItCount == 0) ||
895               (&Item == DPV1 && ItCount == 1));
896     EXPECT_EQ(Item.getMarker(), FirstInst->DbgMarker);
897     ++ItCount;
898   }
899 
900   // Clone them onto the second marker -- should allocate new DPVs.
901   RetInst->DbgMarker->cloneDebugInfoFrom(FirstInst->DbgMarker, std::nullopt, false);
902   EXPECT_EQ(RetInst->DbgMarker->StoredDPValues.size(), 2u);
903   ItCount = 0;
904   // Check these things store the same information; but that they're not the same
905   // objects.
906   for (DPValue &Item : RetInst->DbgMarker->getDbgValueRange()) {
907     EXPECT_TRUE((Item.getRawLocation() == DPV2->getRawLocation() && ItCount == 0) ||
908                 (Item.getRawLocation() == DPV1->getRawLocation() && ItCount == 1));
909 
910     EXPECT_EQ(Item.getMarker(), RetInst->DbgMarker);
911     EXPECT_NE(&Item, DPV1);
912     EXPECT_NE(&Item, DPV2);
913     ++ItCount;
914   }
915 
916   RetInst->DbgMarker->dropDPValues();
917   EXPECT_EQ(RetInst->DbgMarker->StoredDPValues.size(), 0u);
918 
919   // Try cloning one single DPValue.
920   auto DIIt = std::next(FirstInst->DbgMarker->getDbgValueRange().begin());
921   RetInst->DbgMarker->cloneDebugInfoFrom(FirstInst->DbgMarker, DIIt, false);
922   EXPECT_EQ(RetInst->DbgMarker->StoredDPValues.size(), 1u);
923   // The second DPValue should have been cloned; it should have the same values
924   // as DPV1.
925   EXPECT_EQ(RetInst->DbgMarker->StoredDPValues.begin()->getRawLocation(),
926             DPV1->getRawLocation());
927   // We should be able to drop individual DPValues.
928   RetInst->DbgMarker->dropOneDPValue(&*RetInst->DbgMarker->StoredDPValues.begin());
929 
930   // "Aborb" a DPMarker: this means pretend that the instruction it's attached
931   // to is disappearing so it needs to be transferred into "this" marker.
932   RetInst->DbgMarker->absorbDebugValues(*FirstInst->DbgMarker, true);
933   EXPECT_EQ(RetInst->DbgMarker->StoredDPValues.size(), 2u);
934   // Should be the DPV1 and DPV2 objects.
935   ItCount = 0;
936   for (DPValue &Item : RetInst->DbgMarker->getDbgValueRange()) {
937     EXPECT_TRUE((&Item == DPV2 && ItCount == 0) ||
938               (&Item == DPV1 && ItCount == 1));
939     EXPECT_EQ(Item.getMarker(), RetInst->DbgMarker);
940     ++ItCount;
941   }
942 
943   // Finally -- there are two DPValues left over. If we remove evrything in the
944   // basic block, then they should sink down into the "TrailingDPValues"
945   // container for dangling debug-info. Future facilities will restore them
946   // back when a terminator is inserted.
947   FirstInst->DbgMarker->removeMarker();
948   FirstInst->eraseFromParent();
949   RetInst->DbgMarker->removeMarker();
950   RetInst->eraseFromParent();
951 
952   DPMarker *EndMarker = ExitBlock->getTrailingDPValues();
953   ASSERT_NE(EndMarker, nullptr);
954   EXPECT_EQ(EndMarker->StoredDPValues.size(), 2u);
955   // Test again that it's those two DPValues, DPV1 and DPV2.
956   ItCount = 0;
957   for (DPValue &Item : EndMarker->getDbgValueRange()) {
958     EXPECT_TRUE((&Item == DPV2 && ItCount == 0) ||
959               (&Item == DPV1 && ItCount == 1));
960     EXPECT_EQ(Item.getMarker(), EndMarker);
961     ++ItCount;
962   }
963 
964   // Cleanup the trailing DPValue records and marker.
965   EndMarker->eraseFromParent();
966 
967   // The record of those trailing DPValues would dangle and cause an assertion
968   // failure if it lived until the end of the LLVMContext.
969   ExitBlock->deleteTrailingDPValues();
970 }
971 
972 TEST(MetadataTest, DPValueConversionRoutines) {
973   LLVMContext C;
974 
975   // For the purpose of this test, set and un-set the command line option
976   // corresponding to UseNewDbgInfoFormat.
977   UseNewDbgInfoFormat = true;
978 
979   std::unique_ptr<Module> M = parseIR(C, R"(
980     define i16 @f(i16 %a) !dbg !6 {
981       call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata !DIExpression()), !dbg !11
982       %b = add i16 %a, 1, !dbg !11
983       call void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata !DIExpression()), !dbg !11
984       ret i16 0, !dbg !11
985 
986     exit:
987       %c = add i16 %b, 1, !dbg !11
988       ret i16 0, !dbg !11
989     }
990     declare void @llvm.dbg.value(metadata, metadata, metadata) #0
991     attributes #0 = { nounwind readnone speculatable willreturn }
992 
993     !llvm.dbg.cu = !{!0}
994     !llvm.module.flags = !{!5}
995 
996     !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
997     !1 = !DIFile(filename: "t.ll", directory: "/")
998     !2 = !{}
999     !5 = !{i32 2, !"Debug Info Version", i32 3}
1000     !6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
1001     !7 = !DISubroutineType(types: !2)
1002     !8 = !{!9}
1003     !9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
1004     !10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
1005     !11 = !DILocation(line: 1, column: 1, scope: !6)
1006 )");
1007 
1008   // Check that the conversion routines and utilities between dbg.value
1009   // debug-info format and DPValues works.
1010   Function *F = M->getFunction("f");
1011   BasicBlock *BB1 = &F->getEntryBlock();
1012   // First instruction should be a dbg.value.
1013   EXPECT_TRUE(isa<DbgValueInst>(BB1->front()));
1014   EXPECT_FALSE(BB1->IsNewDbgInfoFormat);
1015   // Validating the block for DPValues / DPMarkers shouldn't fail -- there's
1016   // no data stored right now.
1017   EXPECT_FALSE(BB1->validateDbgValues(false, false));
1018 
1019   // Function and module should be marked as not having the new format too.
1020   EXPECT_FALSE(F->IsNewDbgInfoFormat);
1021   EXPECT_FALSE(M->IsNewDbgInfoFormat);
1022 
1023   // Now convert.
1024   M->convertToNewDbgValues();
1025   EXPECT_TRUE(M->IsNewDbgInfoFormat);
1026   EXPECT_TRUE(F->IsNewDbgInfoFormat);
1027   EXPECT_TRUE(BB1->IsNewDbgInfoFormat);
1028 
1029   // There should now be no dbg.value instructions!
1030   // Ensure the first instruction exists, the test all of them.
1031   EXPECT_FALSE(isa<DbgValueInst>(BB1->front()));
1032   for (auto &BB : *F)
1033     for (auto &I : BB)
1034       EXPECT_FALSE(isa<DbgValueInst>(I));
1035 
1036   // There should be a DPMarker on each of the two instructions in the entry
1037   // block, each containing one DPValue.
1038   EXPECT_EQ(BB1->size(), 2u);
1039   Instruction *FirstInst = &BB1->front();
1040   Instruction *SecondInst = FirstInst->getNextNode();
1041   ASSERT_TRUE(FirstInst->DbgMarker);
1042   ASSERT_TRUE(SecondInst->DbgMarker);
1043   EXPECT_NE(FirstInst->DbgMarker, SecondInst->DbgMarker);
1044   EXPECT_EQ(FirstInst, FirstInst->DbgMarker->MarkedInstr);
1045   EXPECT_EQ(SecondInst, SecondInst->DbgMarker->MarkedInstr);
1046 
1047   EXPECT_EQ(FirstInst->DbgMarker->StoredDPValues.size(), 1u);
1048   DPValue *DPV1 = &*FirstInst->DbgMarker->getDbgValueRange().begin();
1049   EXPECT_EQ(DPV1->getMarker(), FirstInst->DbgMarker);
1050   // Should point at %a, an argument.
1051   EXPECT_TRUE(isa<Argument>(DPV1->getVariableLocationOp(0)));
1052 
1053   EXPECT_EQ(SecondInst->DbgMarker->StoredDPValues.size(), 1u);
1054   DPValue *DPV2 = &*SecondInst->DbgMarker->getDbgValueRange().begin();
1055   EXPECT_EQ(DPV2->getMarker(), SecondInst->DbgMarker);
1056   // Should point at FirstInst.
1057   EXPECT_EQ(DPV2->getVariableLocationOp(0), FirstInst);
1058 
1059   // There should be no DPValues / DPMarkers in the second block, but it should
1060   // be marked as being in the new format.
1061   BasicBlock *BB2 = BB1->getNextNode();
1062   EXPECT_TRUE(BB2->IsNewDbgInfoFormat);
1063   for (auto &Inst : *BB2)
1064     // Either there should be no marker, or it should be empty.
1065     EXPECT_TRUE(!Inst.DbgMarker || Inst.DbgMarker->StoredDPValues.empty());
1066 
1067   // Validating the first block should continue to not be a problem,
1068   EXPECT_FALSE(BB1->validateDbgValues(false, false));
1069   // But if we were to break something, it should be able to fire. Don't attempt
1070   // to comprehensively test the validator, it's a smoke-test rather than a
1071   // "proper" verification pass.
1072   DPV1->setMarker(nullptr);
1073   // A marker pointing the wrong way should be an error.
1074   EXPECT_TRUE(BB1->validateDbgValues(false, false));
1075   DPV1->setMarker(FirstInst->DbgMarker);
1076 
1077   DILocalVariable *DLV1 = DPV1->getVariable();
1078   DIExpression *Expr1 = DPV1->getExpression();
1079   DILocalVariable *DLV2 = DPV2->getVariable();
1080   DIExpression *Expr2 = DPV2->getExpression();
1081 
1082   // Convert everything back to the "old" format and ensure it's right.
1083   M->convertFromNewDbgValues();
1084   EXPECT_FALSE(M->IsNewDbgInfoFormat);
1085   EXPECT_FALSE(F->IsNewDbgInfoFormat);
1086   EXPECT_FALSE(BB1->IsNewDbgInfoFormat);
1087 
1088   EXPECT_EQ(BB1->size(), 4u);
1089   ASSERT_TRUE(isa<DbgValueInst>(BB1->front()));
1090   DbgValueInst *DVI1 = cast<DbgValueInst>(&BB1->front());
1091   // These dbg.values should still point at the same places.
1092   EXPECT_TRUE(isa<Argument>(DVI1->getVariableLocationOp(0)));
1093   DbgValueInst *DVI2 = cast<DbgValueInst>(DVI1->getNextNode()->getNextNode());
1094   EXPECT_EQ(DVI2->getVariableLocationOp(0), FirstInst);
1095 
1096   // Check a few fields too,
1097   EXPECT_EQ(DVI1->getVariable(), DLV1);
1098   EXPECT_EQ(DVI1->getExpression(), Expr1);
1099   EXPECT_EQ(DVI2->getVariable(), DLV2);
1100   EXPECT_EQ(DVI2->getExpression(), Expr2);
1101 
1102   UseNewDbgInfoFormat = false;
1103 }
1104 
1105 } // end namespace
1106