Debian Codegraph @

llvm/unittests/IR/BasicBlockDbgInfoTest.cpp

raw
1 //===- llvm/unittest/IR/BasicBlockTest.cpp - BasicBlock unit 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/BasicBlock.h"
10 #include "llvm/IR/DebugInfo.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/AsmParser/Parser.h"
13 #include "llvm/IR/Function.h"
14 #include "llvm/IR/IRBuilder.h"
15 #include "llvm/IR/Instruction.h"
16 #include "llvm/IR/Instructions.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/IR/NoFolder.h"
20 #include "llvm/IR/Verifier.h"
21 #include "llvm/Support/SourceMgr.h"
22 #include "gmock/gmock-matchers.h"
23 #include "gtest/gtest.h"
24 #include <memory>
25
26 using namespace llvm;
27
28 static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
29 SMDiagnostic Err;
30 std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
31 if (!Mod)
32 Err.print("BasicBlockDbgInfoTest", errs());
33 return Mod;
34 }
35
36 namespace {
37
38 // We can occasionally moveAfter an instruction so that it moves to the
39 // position that it already resides at. This is fine -- but gets complicated
40 // with dbg.value intrinsics. By moving an instruction, we can end up changing
41 // nothing but the location of debug-info intrinsics. That has to be modelled
42 // by DbgVariableRecords, the dbg.value replacement.
43 TEST(BasicBlockDbgInfoTest, InsertAfterSelf) {
44 LLVMContext C;
45 std::unique_ptr<Module> M = parseIR(C, R"(
46 define i16 @f(i16 %a) !dbg !6 {
47 call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata !DIExpression()), !dbg !11
48 %b = add i16 %a, 1, !dbg !11
49 call void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata !DIExpression()), !dbg !11
50 %c = add i16 %b, 1, !dbg !11
51 ret i16 0, !dbg !11
52 }
53 declare void @llvm.dbg.value(metadata, metadata, metadata) #0
54 attributes #0 = { nounwind readnone speculatable willreturn }
55
56 !llvm.dbg.cu = !{!0}
57 !llvm.module.flags = !{!5}
58
59 !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
60 !1 = !DIFile(filename: "t.ll", directory: "/")
61 !2 = !{}
62 !5 = !{i32 2, !"Debug Info Version", i32 3}
63 !6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
64 !7 = !DISubroutineType(types: !2)
65 !8 = !{!9}
66 !9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
67 !10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
68 !11 = !DILocation(line: 1, column: 1, scope: !6)
69 )");
70
71 // Fetch the entry block.
72 BasicBlock &BB = M->getFunction("f")->getEntryBlock();
73
74 Instruction *Inst1 = &*BB.begin();
75 Instruction *Inst2 = &*std::next(BB.begin());
76 Instruction *RetInst = &*std::next(Inst2->getIterator());
77 EXPECT_TRUE(Inst1->hasDbgRecords());
78 EXPECT_TRUE(Inst2->hasDbgRecords());
79 EXPECT_FALSE(RetInst->hasDbgRecords());
80
81 // If we move Inst2 to be after Inst1, then it comes _immediately_ after. Were
82 // we in dbg.value form we would then have:
83 // dbg.value
84 // %b = add
85 // %c = add
86 // dbg.value
87 // Check that this is replicated by DbgVariableRecords.
88 Inst2->moveAfter(Inst1);
89
90 // Inst1 should only have one DbgVariableRecord on it.
91 EXPECT_TRUE(Inst1->hasDbgRecords());
92 auto Range1 = Inst1->getDbgRecordRange();
93 EXPECT_EQ(std::distance(Range1.begin(), Range1.end()), 1u);
94 // Inst2 should have none.
95 EXPECT_FALSE(Inst2->hasDbgRecords());
96 // While the return inst should now have one on it.
97 EXPECT_TRUE(RetInst->hasDbgRecords());
98 auto Range2 = RetInst->getDbgRecordRange();
99 EXPECT_EQ(std::distance(Range2.begin(), Range2.end()), 1u);
100 }
101
102 TEST(BasicBlockDbgInfoTest, SplitBasicBlockBefore) {
103 LLVMContext C;
104 std::unique_ptr<Module> M = parseIR(C, R"---(
105 define dso_local void @func() #0 !dbg !10 {
106 %1 = alloca i32, align 4
107 tail call void @llvm.dbg.declare(metadata ptr %1, metadata !14, metadata !DIExpression()), !dbg !16
108 store i32 2, ptr %1, align 4, !dbg !16
109 ret void, !dbg !17
110 }
111
112 declare void @llvm.dbg.declare(metadata, metadata, metadata) #0
113
114 attributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
115
116 !llvm.dbg.cu = !{!0}
117 !llvm.module.flags = !{!2, !3, !4, !5, !6, !7, !8}
118 !llvm.ident = !{!9}
119
120 !0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "dummy", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
121 !1 = !DIFile(filename: "dummy", directory: "dummy")
122 !2 = !{i32 7, !"Dwarf Version", i32 5}
123 !3 = !{i32 2, !"Debug Info Version", i32 3}
124 !4 = !{i32 1, !"wchar_size", i32 4}
125 !5 = !{i32 8, !"PIC Level", i32 2}
126 !6 = !{i32 7, !"PIE Level", i32 2}
127 !7 = !{i32 7, !"uwtable", i32 2}
128 !8 = !{i32 7, !"frame-pointer", i32 2}
129 !9 = !{!"dummy"}
130 !10 = distinct !DISubprogram(name: "func", scope: !1, file: !1, line: 1, type: !11, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !13)
131 !11 = !DISubroutineType(types: !12)
132 !12 = !{null}
133 !13 = !{}
134 !14 = !DILocalVariable(name: "a", scope: !10, file: !1, line: 2, type: !15)
135 !15 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
136 !16 = !DILocation(line: 2, column: 6, scope: !10)
137 !17 = !DILocation(line: 3, column: 2, scope: !10)
138 )---");
139 ASSERT_TRUE(M);
140
141 Function *F = M->getFunction("func");
142
143 BasicBlock &BB = F->getEntryBlock();
144 auto I = std::prev(BB.end(), 2); // store i32 2, ptr %1.
145 BB.splitBasicBlockBefore(I, "before");
146
147 BasicBlock &BBBefore = F->getEntryBlock();
148 auto I2 = std::prev(BBBefore.end()); // br label %1 (new).
149 ASSERT_TRUE(I2->hasDbgRecords());
150 }
151
152 TEST(BasicBlockDbgInfoTest, DropSourceAtomOnSplit) {
153 LLVMContext C;
154 std::unique_ptr<Module> M = parseIR(C, R"---(
155 define dso_local void @func() !dbg !10 {
156 %1 = alloca i32, align 4
157 ret void, !dbg !DILocation(line: 3, column: 2, scope: !10, atomGroup: 1, atomRank: 1)
158 }
159
160 !llvm.dbg.cu = !{!0}
161 !llvm.module.flags = !{!2, !3}
162
163 !0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "dummy", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
164 !1 = !DIFile(filename: "dummy", directory: "dummy")
165 !2 = !{i32 7, !"Dwarf Version", i32 5}
166 !3 = !{i32 2, !"Debug Info Version", i32 3}
167 !10 = distinct !DISubprogram(name: "func", scope: !1, file: !1, line: 1, type: !11, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !13, keyInstructions: true)
168 !11 = !DISubroutineType(types: !12)
169 !12 = !{null}
170 !13 = !{}
171 !14 = !DILocalVariable(name: "a", scope: !10, file: !1, line: 2, type: !15)
172 !15 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
173 )---");
174 ASSERT_TRUE(M);
175
176 Function *F = M->getFunction("func");
177
178 // Test splitBasicBlockBefore.
179 {
180 BasicBlock &BB = F->back();
181 // Split at `ret void`.
182 BasicBlock *Before =
183 BB.splitBasicBlockBefore(std::prev(BB.end(), 1), "before");
184 const DebugLoc &BrToAfterDL = Before->getTerminator()->getDebugLoc();
185 ASSERT_TRUE(BrToAfterDL);
186 EXPECT_EQ(BrToAfterDL->getAtomGroup(), 0u);
187
188 BasicBlock *After = Before->getSingleSuccessor();
189 ASSERT_TRUE(After);
190 const DebugLoc &OrigTerminatorDL = After->getTerminator()->getDebugLoc();
191 ASSERT_TRUE(OrigTerminatorDL);
192 #ifdef EXPERIMENTAL_KEY_INSTRUCTIONS
193 EXPECT_EQ(OrigTerminatorDL->getAtomGroup(), 1u);
194 #else
195 EXPECT_EQ(OrigTerminatorDL->getAtomGroup(), 0u);
196 #endif
197 }
198
199 // Test splitBasicBlock.
200 {
201 BasicBlock &BB = F->back();
202 // Split at `ret void`.
203 BasicBlock *After = BB.splitBasicBlock(std::prev(BB.end(), 1), "before");
204
205 const DebugLoc &OrigTerminatorDL = After->getTerminator()->getDebugLoc();
206 ASSERT_TRUE(OrigTerminatorDL);
207 #ifdef EXPERIMENTAL_KEY_INSTRUCTIONS
208 EXPECT_EQ(OrigTerminatorDL->getAtomGroup(), 1u);
209 #else
210 EXPECT_EQ(OrigTerminatorDL->getAtomGroup(), 0u);
211 #endif
212
213 BasicBlock *Before = After->getSinglePredecessor();
214 ASSERT_TRUE(Before);
215 const DebugLoc &BrToAfterDL = Before->getTerminator()->getDebugLoc();
216 ASSERT_TRUE(BrToAfterDL);
217 EXPECT_EQ(BrToAfterDL->getAtomGroup(), 0u);
218 }
219 }
220
221 TEST(BasicBlockDbgInfoTest, MarkerOperations) {
222 LLVMContext C;
223 std::unique_ptr<Module> M = parseIR(C, R"(
224 define i16 @f(i16 %a) !dbg !6 {
225 call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata !DIExpression()), !dbg !11
226 %b = add i16 %a, 1, !dbg !11
227 call void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata !DIExpression()), !dbg !11
228 ret i16 0, !dbg !11
229 }
230 declare void @llvm.dbg.value(metadata, metadata, metadata) #0
231 attributes #0 = { nounwind readnone speculatable willreturn }
232
233 !llvm.dbg.cu = !{!0}
234 !llvm.module.flags = !{!5}
235
236 !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
237 !1 = !DIFile(filename: "t.ll", directory: "/")
238 !2 = !{}
239 !5 = !{i32 2, !"Debug Info Version", i32 3}
240 !6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
241 !7 = !DISubroutineType(types: !2)
242 !8 = !{!9}
243 !9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
244 !10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
245 !11 = !DILocation(line: 1, column: 1, scope: !6)
246 )");
247
248 // Fetch the entry block,
249 BasicBlock &BB = M->getFunction("f")->getEntryBlock();
250 EXPECT_EQ(BB.size(), 2u);
251
252 // Fetch out our two markers,
253 Instruction *Instr1 = &*BB.begin();
254 Instruction *Instr2 = Instr1->getNextNode();
255 DbgMarker *Marker1 = Instr1->DebugMarker;
256 DbgMarker *Marker2 = Instr2->DebugMarker;
257 // There's no TrailingDbgRecords marker allocated yet.
258 DbgMarker *EndMarker = nullptr;
259
260 // Check that the "getMarker" utilities operate as expected.
261 EXPECT_EQ(BB.getMarker(Instr1->getIterator()), Marker1);
262 EXPECT_EQ(BB.getMarker(Instr2->getIterator()), Marker2);
263 EXPECT_EQ(BB.getNextMarker(Instr1), Marker2);
264 EXPECT_EQ(BB.getNextMarker(Instr2), EndMarker); // Is nullptr.
265
266 // There should be two DbgVariableRecords,
267 EXPECT_EQ(Marker1->StoredDbgRecords.size(), 1u);
268 EXPECT_EQ(Marker2->StoredDbgRecords.size(), 1u);
269
270 // Unlink them and try to re-insert them through the basic block.
271 DbgRecord *DVR1 = &*Marker1->StoredDbgRecords.begin();
272 DbgRecord *DVR2 = &*Marker2->StoredDbgRecords.begin();
273 DVR1->removeFromParent();
274 DVR2->removeFromParent();
275 EXPECT_TRUE(Marker1->StoredDbgRecords.empty());
276 EXPECT_TRUE(Marker2->StoredDbgRecords.empty());
277
278 // This should appear in Marker1.
279 BB.insertDbgRecordBefore(DVR1, BB.begin());
280 EXPECT_EQ(Marker1->StoredDbgRecords.size(), 1u);
281 EXPECT_EQ(DVR1, &*Marker1->StoredDbgRecords.begin());
282
283 // This should attach to Marker2.
284 BB.insertDbgRecordAfter(DVR2, &*BB.begin());
285 EXPECT_EQ(Marker2->StoredDbgRecords.size(), 1u);
286 EXPECT_EQ(DVR2, &*Marker2->StoredDbgRecords.begin());
287
288 // Now, how about removing instructions? That should cause any
289 // DbgVariableRecords to "fall down".
290 Instr1->removeFromParent();
291 Marker1 = nullptr;
292 // DbgVariableRecords should now be in Marker2.
293 EXPECT_EQ(BB.size(), 1u);
294 EXPECT_EQ(Marker2->StoredDbgRecords.size(), 2u);
295 // They should also be in the correct order.
296 SmallVector<DbgRecord *, 2> DVRs(
297 llvm::make_pointer_range(Marker2->getDbgRecordRange()));
298 EXPECT_EQ(DVRs[0], DVR1);
299 EXPECT_EQ(DVRs[1], DVR2);
300
301 // If we remove the end instruction, the DbgVariableRecords should fall down
302 // into the trailing marker.
303 EXPECT_EQ(BB.getTrailingDbgRecords(), nullptr);
304 Instr2->removeFromParent();
305 EXPECT_TRUE(BB.empty());
306 EndMarker = BB.getTrailingDbgRecords();
307 ASSERT_NE(EndMarker, nullptr);
308 EXPECT_EQ(EndMarker->StoredDbgRecords.size(), 2u);
309 // Again, these should arrive in the correct order.
310
311 DVRs.clear();
312 for (DbgRecord &DVR : EndMarker->getDbgRecordRange())
313 DVRs.push_back(&DVR);
314 EXPECT_EQ(DVRs[0], DVR1);
315 EXPECT_EQ(DVRs[1], DVR2);
316
317 // Inserting a normal instruction at the beginning: shouldn't dislodge the
318 // DbgVariableRecords. It's intended to not go at the start.
319 Instr1->insertBefore(BB, BB.begin());
320 EXPECT_EQ(EndMarker->StoredDbgRecords.size(), 2u);
321 Instr1->removeFromParent();
322
323 // Inserting at end(): should dislodge the DbgVariableRecords, if they were
324 // dbg.values then they would sit "above" the new instruction.
325 Instr1->insertBefore(BB, BB.end());
326 EXPECT_EQ(Instr1->DebugMarker->StoredDbgRecords.size(), 2u);
327 // We should de-allocate the trailing marker when something is inserted
328 // at end().
329 EXPECT_EQ(BB.getTrailingDbgRecords(), nullptr);
330
331 // Remove Instr1: now the DbgVariableRecords will fall down again,
332 Instr1->removeFromParent();
333 EndMarker = BB.getTrailingDbgRecords();
334 EXPECT_EQ(EndMarker->StoredDbgRecords.size(), 2u);
335
336 // Inserting a terminator, however it's intended, should dislodge the
337 // trailing DbgVariableRecords, as it's the clear intention of the caller that
338 // this be the final instr in the block, and DbgVariableRecords aren't allowed
339 // to live off the end forever.
340 Instr2->insertBefore(BB, BB.begin());
341 EXPECT_EQ(Instr2->DebugMarker->StoredDbgRecords.size(), 2u);
342 EXPECT_EQ(BB.getTrailingDbgRecords(), nullptr);
343
344 // Teardown,
345 Instr1->insertBefore(BB, BB.begin());
346 }
347
348 TEST(BasicBlockDbgInfoTest, HeadBitOperations) {
349 LLVMContext C;
350 std::unique_ptr<Module> M = parseIR(C, R"(
351 define i16 @f(i16 %a) !dbg !6 {
352 %b = add i16 %a, 1, !dbg !11
353 call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata !DIExpression()), !dbg !11
354 %c = add i16 %a, 1, !dbg !11
355 %d = add i16 %a, 1, !dbg !11
356 ret i16 0, !dbg !11
357 }
358 declare void @llvm.dbg.value(metadata, metadata, metadata) #0
359 attributes #0 = { nounwind readnone speculatable willreturn }
360
361 !llvm.dbg.cu = !{!0}
362 !llvm.module.flags = !{!5}
363
364 !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
365 !1 = !DIFile(filename: "t.ll", directory: "/")
366 !2 = !{}
367 !5 = !{i32 2, !"Debug Info Version", i32 3}
368 !6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
369 !7 = !DISubroutineType(types: !2)
370 !8 = !{!9}
371 !9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
372 !10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
373 !11 = !DILocation(line: 1, column: 1, scope: !6)
374 )");
375
376 // Test that the movement of debug-data when using moveBefore etc and
377 // insertBefore etc are governed by the "head" bit of iterators.
378 BasicBlock &BB = M->getFunction("f")->getEntryBlock();
379
380 // Test that the head bit behaves as expected: it should be set when the
381 // code wants the _start_ of the block, but not otherwise.
382 EXPECT_TRUE(BB.getFirstInsertionPt().getHeadBit());
383 BasicBlock::iterator BeginIt = BB.begin();
384 EXPECT_TRUE(BeginIt.getHeadBit());
385 // If you launder the instruction pointer through dereferencing and then
386 // get the iterator again with getIterator, the head bit is lost. This is
387 // deliberate: if you're calling getIterator, then you're requesting an
388 // iterator for the position of _this_ instruction, not "the start of this
389 // block".
390 BasicBlock::iterator BeginIt2 = BeginIt->getIterator();
391 EXPECT_FALSE(BeginIt2.getHeadBit());
392
393 // Fetch some instruction pointers.
394 Instruction *BInst = &*BeginIt;
395 Instruction *CInst = BInst->getNextNode();
396 Instruction *DInst = CInst->getNextNode();
397 // CInst should have debug-info.
398 ASSERT_TRUE(CInst->DebugMarker);
399 EXPECT_FALSE(CInst->DebugMarker->StoredDbgRecords.empty());
400
401 // If we move "c" to the start of the block, just normally, then the
402 // DbgVariableRecords should fall down to "d".
403 CInst->moveBefore(BB, BeginIt2);
404 EXPECT_TRUE(!CInst->DebugMarker ||
405 CInst->DebugMarker->StoredDbgRecords.empty());
406 ASSERT_TRUE(DInst->DebugMarker);
407 EXPECT_FALSE(DInst->DebugMarker->StoredDbgRecords.empty());
408
409 // Wheras if we move D to the start of the block with moveBeforePreserving,
410 // the DbgVariableRecords should move with it.
411 DInst->moveBeforePreserving(BB, BB.begin());
412 EXPECT_FALSE(DInst->DebugMarker->StoredDbgRecords.empty());
413 EXPECT_EQ(&*BB.begin(), DInst);
414
415 // Similarly, moveAfterPreserving "D" to "C" should move DbgVariableRecords
416 // with "D".
417 DInst->moveAfterPreserving(CInst);
418 EXPECT_FALSE(DInst->DebugMarker->StoredDbgRecords.empty());
419
420 // (move back to the start...)
421 DInst->moveBeforePreserving(BB, BB.begin());
422
423 // Current order of insts: "D -> C -> B -> Ret". DbgVariableRecords on "D".
424 // If we move "C" to the beginning of the block, it should go before the
425 // DbgVariableRecords. They'll stay on "D".
426 CInst->moveBefore(BB, BB.begin());
427 EXPECT_TRUE(!CInst->DebugMarker ||
428 CInst->DebugMarker->StoredDbgRecords.empty());
429 EXPECT_FALSE(DInst->DebugMarker->StoredDbgRecords.empty());
430 EXPECT_EQ(&*BB.begin(), CInst);
431 EXPECT_EQ(CInst->getNextNode(), DInst);
432
433 // Move back.
434 CInst->moveBefore(BInst->getIterator());
435 EXPECT_EQ(&*BB.begin(), DInst);
436
437 // Current order of insts: "D -> C -> B -> Ret". DbgVariableRecords on "D".
438 // Now move CInst to the position of DInst, but using getIterator instead of
439 // BasicBlock::begin. This signals that we want the "C" instruction to be
440 // immediately before "D", with any DbgVariableRecords on "D" now moving to
441 // "C". It's the equivalent of moving an instruction to the position between a
442 // run of dbg.values and the next instruction.
443 CInst->moveBefore(BB, DInst->getIterator());
444 // CInst gains the DbgVariableRecords.
445 EXPECT_TRUE(!DInst->DebugMarker ||
446 DInst->DebugMarker->StoredDbgRecords.empty());
447 EXPECT_FALSE(CInst->DebugMarker->StoredDbgRecords.empty());
448 EXPECT_EQ(&*BB.begin(), CInst);
449 }
450
451 TEST(BasicBlockDbgInfoTest, InstrDbgAccess) {
452 LLVMContext C;
453 std::unique_ptr<Module> M = parseIR(C, R"(
454 define i16 @f(i16 %a) !dbg !6 {
455 %b = add i16 %a, 1, !dbg !11
456 call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata !DIExpression()), !dbg !11
457 %c = add i16 %a, 1, !dbg !11
458 %d = add i16 %a, 1, !dbg !11
459 ret i16 0, !dbg !11
460 }
461 declare void @llvm.dbg.value(metadata, metadata, metadata) #0
462 attributes #0 = { nounwind readnone speculatable willreturn }
463
464 !llvm.dbg.cu = !{!0}
465 !llvm.module.flags = !{!5}
466
467 !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
468 !1 = !DIFile(filename: "t.ll", directory: "/")
469 !2 = !{}
470 !5 = !{i32 2, !"Debug Info Version", i32 3}
471 !6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
472 !7 = !DISubroutineType(types: !2)
473 !8 = !{!9}
474 !9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
475 !10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
476 !11 = !DILocation(line: 1, column: 1, scope: !6)
477 )");
478
479 // Check that DbgVariableRecords can be accessed from Instructions without
480 // digging into the depths of DbgMarkers.
481 BasicBlock &BB = M->getFunction("f")->getEntryBlock();
482
483 Instruction *BInst = &*BB.begin();
484 Instruction *CInst = BInst->getNextNode();
485 Instruction *DInst = CInst->getNextNode();
486
487 ASSERT_FALSE(BInst->DebugMarker);
488 ASSERT_TRUE(CInst->DebugMarker);
489 ASSERT_EQ(CInst->DebugMarker->StoredDbgRecords.size(), 1u);
490 DbgRecord *DVR1 = &*CInst->DebugMarker->StoredDbgRecords.begin();
491 ASSERT_TRUE(DVR1);
492 EXPECT_FALSE(BInst->hasDbgRecords());
493
494 // Clone DbgVariableRecords from one inst to another. Other arguments to clone
495 // are tested in DbgMarker test.
496 auto Range1 = BInst->cloneDebugInfoFrom(CInst);
497 EXPECT_EQ(BInst->DebugMarker->StoredDbgRecords.size(), 1u);
498 DbgRecord *DVR2 = &*BInst->DebugMarker->StoredDbgRecords.begin();
499 EXPECT_EQ(std::distance(Range1.begin(), Range1.end()), 1u);
500 EXPECT_EQ(&*Range1.begin(), DVR2);
501 EXPECT_NE(DVR1, DVR2);
502
503 // We should be able to get a range over exactly the same information.
504 auto Range2 = BInst->getDbgRecordRange();
505 EXPECT_EQ(Range1.begin(), Range2.begin());
506 EXPECT_EQ(Range1.end(), Range2.end());
507
508 // We should be able to query if there are DbgVariableRecords,
509 EXPECT_TRUE(BInst->hasDbgRecords());
510 EXPECT_TRUE(CInst->hasDbgRecords());
511 EXPECT_FALSE(DInst->hasDbgRecords());
512
513 // Dropping should be easy,
514 BInst->dropDbgRecords();
515 EXPECT_FALSE(BInst->hasDbgRecords());
516 EXPECT_EQ(BInst->DebugMarker->StoredDbgRecords.size(), 0u);
517
518 // And we should be able to drop individual DbgVariableRecords.
519 CInst->dropOneDbgRecord(DVR1);
520 EXPECT_FALSE(CInst->hasDbgRecords());
521 EXPECT_EQ(CInst->DebugMarker->StoredDbgRecords.size(), 0u);
522 }
523
524 /* Let's recall the big illustration from BasicBlock::spliceDebugInfo:
525
526 Dest
527 |
528 this-block: A----A----A ====A----A----A----A---A---A
529 Src-block ++++B---B---B---B:::C
530 | |
531 First Last
532
533 in all it's glory. Depending on the bit-configurations for the iterator head
534 / tail bits on the three named iterators, there are eight ways for a splice to
535 occur. To save the amount of thinking needed to pack this into one unit test,
536 just test the same IR eight times with difference splices. The IR shall be
537 thus:
538
539 define i16 @f(i16 %a) !dbg !6 {
540 entry:
541 call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata !DIExpression()), !dbg !11
542 %b = add i16 %a, 1, !dbg !11
543 call void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata !DIExpression()), !dbg !11
544 br label %exit, !dbg !11
545
546 exit:
547 call void @llvm.dbg.value(metadata i16 0, metadata !9, metadata !DIExpression()), !dbg !11
548 %c = add i16 %b, 1, !dbg !11
549 ret i16 0, !dbg !11
550 }
551
552 The iterators will be:
553 Dest: exit block, "c" instruction.
554 First: entry block, "b" instruction.
555 Last: entry block, branch instruction.
556
557 The numbered configurations will be:
558
559 | Dest-Head | First-Head | Last-tail
560 ----+----------------+----------------+------------
561 0 | false | false | false
562 1 | true | false | false
563 2 | false | true | false
564 3 | true | true | false
565 4 | false | false | true
566 5 | true | false | true
567 6 | false | true | true
568 7 | true | true | true
569
570 Each numbered test scenario will also have a short explanation indicating what
571 this bit configuration represents.
572 */
573
574 static const std::string SpliceTestIR = R"(
575 define i16 @f(i16 %a) !dbg !6 {
576 call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata !DIExpression()), !dbg !11
577 %b = add i16 %a, 1, !dbg !11
578 call void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata !DIExpression()), !dbg !11
579 br label %exit, !dbg !11
580
581 exit:
582 call void @llvm.dbg.value(metadata i16 0, metadata !9, metadata !DIExpression()), !dbg !11
583 %c = add i16 %b, 1, !dbg !11
584 ret i16 0, !dbg !11
585 }
586 declare void @llvm.dbg.value(metadata, metadata, metadata) #0
587 attributes #0 = { nounwind readnone speculatable willreturn }
588
589 !llvm.dbg.cu = !{!0}
590 !llvm.module.flags = !{!5}
591
592 !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
593 !1 = !DIFile(filename: "t.ll", directory: "/")
594 !2 = !{}
595 !5 = !{i32 2, !"Debug Info Version", i32 3}
596 !6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
597 !7 = !DISubroutineType(types: !2)
598 !8 = !{!9}
599 !9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
600 !10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
601 !11 = !DILocation(line: 1, column: 1, scope: !6)
602 )";
603
604 class DbgSpliceTest : public ::testing::Test {
605 protected:
606 LLVMContext C;
607 std::unique_ptr<Module> M;
608 BasicBlock *BBEntry, *BBExit;
609 BasicBlock::iterator Dest, First, Last;
610 Instruction *BInst, *Branch, *CInst;
611 DbgVariableRecord *DVRA, *DVRB, *DVRConst;
612
613 void SetUp() override {
614 M = parseIR(C, SpliceTestIR.c_str());
615
616 BBEntry = &M->getFunction("f")->getEntryBlock();
617 BBExit = BBEntry->getNextNode();
618
619 Dest = BBExit->begin();
620 First = BBEntry->begin();
621 Last = BBEntry->getTerminator()->getIterator();
622 BInst = &*First;
623 Branch = &*Last;
624 CInst = &*Dest;
625
626 DVRA =
627 cast<DbgVariableRecord>(&*BInst->DebugMarker->StoredDbgRecords.begin());
628 DVRB = cast<DbgVariableRecord>(
629 &*Branch->DebugMarker->StoredDbgRecords.begin());
630 DVRConst =
631 cast<DbgVariableRecord>(&*CInst->DebugMarker->StoredDbgRecords.begin());
632 }
633
634 bool InstContainsDbgVariableRecord(Instruction *I, DbgVariableRecord *DVR) {
635 for (DbgRecord &D : I->getDbgRecordRange()) {
636 if (&D == DVR) {
637 // Confirm too that the links between the records are correct.
638 EXPECT_EQ(DVR->Marker, I->DebugMarker);
639 EXPECT_EQ(I->DebugMarker->MarkedInstr, I);
640 return true;
641 }
642 }
643 return false;
644 }
645
646 bool CheckDVROrder(Instruction *I,
647 SmallVector<DbgVariableRecord *> CheckVals) {
648 SmallVector<DbgRecord *> Vals(
649 llvm::make_pointer_range(I->getDbgRecordRange()));
650
651 EXPECT_EQ(Vals.size(), CheckVals.size());
652 if (Vals.size() != CheckVals.size())
653 return false;
654
655 for (unsigned int I = 0; I < Vals.size(); ++I) {
656 EXPECT_EQ(Vals[I], CheckVals[I]);
657 // Provide another expectation failure to let us localise what goes wrong,
658 // by returning a flag to the caller.
659 if (Vals[I] != CheckVals[I])
660 return false;
661 }
662 return true;
663 }
664 };
665
666 TEST_F(DbgSpliceTest, DbgSpliceTest0) {
667 Dest.setHeadBit(false);
668 First.setHeadBit(false);
669 Last.setTailBit(false);
670
671 /*
672 define i16 @f(i16 %a) !dbg !6 {
673 BBEntry entry:
674 DVRA call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata
675 !DIExpression()), !dbg !11 First %b = add i16 %a, 1, !dbg !11 DVRB call
676 void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata !DIExpression()),
677 !dbg !11 Last br label %exit, !dbg !11
678
679 BBExit exit:
680 DVRConst call void @llvm.dbg.value(metadata i16 0, metadata !9, metadata
681 !DIExpression()), !dbg !11 Dest %c = add i16 %b, 1, !dbg !11 ret i16 0,
682 !dbg !11
683 }
684
685 Splice from First, not including leading dbg.value, to Last, including the
686 trailing dbg.value. Place at Dest, between the constant dbg.value and %c.
687 %b, and the following dbg.value, should move, to:
688
689 define i16 @f(i16 %a) !dbg !6 {
690 BBEntry entry:
691 DVRA call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata
692 !DIExpression()), !dbg !11 Last br label %exit, !dbg !11
693
694 BBExit exit:
695 DVRConst call void @llvm.dbg.value(metadata i16 0, metadata !9, metadata
696 !DIExpression()), !dbg !11 First %b = add i16 %a, 1, !dbg !11 DVRB call
697 void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata !DIExpression()),
698 !dbg !11 Dest %c = add i16 %b, 1, !dbg !11 ret i16 0, !dbg !11
699 }
700
701
702 */
703 BBExit->splice(Dest, BBEntry, First, Last);
704 EXPECT_EQ(BInst->getParent(), BBExit);
705 EXPECT_EQ(CInst->getParent(), BBExit);
706 EXPECT_EQ(Branch->getParent(), BBEntry);
707
708 // DVRB: should be on Dest, in exit block.
709 EXPECT_TRUE(InstContainsDbgVariableRecord(CInst, DVRB));
710
711 // DVRA, should have "fallen" onto the branch, remained in entry block.
712 EXPECT_TRUE(InstContainsDbgVariableRecord(Branch, DVRA));
713
714 // DVRConst should be on the moved %b instruction.
715 EXPECT_TRUE(InstContainsDbgVariableRecord(BInst, DVRConst));
716 }
717
718 TEST_F(DbgSpliceTest, DbgSpliceTest1) {
719 Dest.setHeadBit(true);
720 First.setHeadBit(false);
721 Last.setTailBit(false);
722
723 /*
724 define i16 @f(i16 %a) !dbg !6 {
725 BBEntry entry:
726 DVRA call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata
727 !DIExpression()), !dbg !11 First %b = add i16 %a, 1, !dbg !11 DVRB call
728 void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata !DIExpression()),
729 !dbg !11 Last br label %exit, !dbg !11
730
731 BBExit exit:
732 DVRConst call void @llvm.dbg.value(metadata i16 0, metadata !9, metadata
733 !DIExpression()), !dbg !11 Dest %c = add i16 %b, 1, !dbg !11 ret i16 0,
734 !dbg !11
735 }
736
737 Splice from First, not including leading dbg.value, to Last, including the
738 trailing dbg.value. Place at the head of Dest, i.e. at the very start of
739 BBExit, before any debug-info there. Becomes:
740
741 define i16 @f(i16 %a) !dbg !6 {
742 BBEntry entry:
743 DVRA call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata
744 !DIExpression()), !dbg !11 Last br label %exit, !dbg !11
745
746 BBExit exit:
747 First %b = add i16 %a, 1, !dbg !11
748 DVRB call void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata
749 !DIExpression()), !dbg !11 DVRConst call void @llvm.dbg.value(metadata i16 0,
750 metadata !9, metadata !DIExpression()), !dbg !11 Dest %c = add i16 %b, 1,
751 !dbg !11 ret i16 0, !dbg !11
752 }
753
754
755 */
756 BBExit->splice(Dest, BBEntry, First, Last);
757 EXPECT_EQ(BInst->getParent(), BBExit);
758 EXPECT_EQ(CInst->getParent(), BBExit);
759 EXPECT_EQ(Branch->getParent(), BBEntry);
760
761 // DVRB: should be on CInst, in exit block.
762 EXPECT_TRUE(InstContainsDbgVariableRecord(CInst, DVRB));
763
764 // DVRA, should have "fallen" onto the branch, remained in entry block.
765 EXPECT_TRUE(InstContainsDbgVariableRecord(Branch, DVRA));
766
767 // DVRConst should be behind / after the moved instructions, remain on CInst.
768 EXPECT_TRUE(InstContainsDbgVariableRecord(CInst, DVRConst));
769
770 // Order of DVRB and DVRConst should be thus:
771 EXPECT_TRUE(CheckDVROrder(CInst, {DVRB, DVRConst}));
772 }
773
774 TEST_F(DbgSpliceTest, DbgSpliceTest2) {
775 Dest.setHeadBit(false);
776 First.setHeadBit(true);
777 Last.setTailBit(false);
778
779 /*
780 define i16 @f(i16 %a) !dbg !6 {
781 BBEntry entry:
782 DVRA call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata
783 !DIExpression()), !dbg !11 First %b = add i16 %a, 1, !dbg !11 DVRB call
784 void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata !DIExpression()),
785 !dbg !11 Last br label %exit, !dbg !11
786
787 BBExit exit:
788 DVRConst call void @llvm.dbg.value(metadata i16 0, metadata !9, metadata
789 !DIExpression()), !dbg !11 Dest %c = add i16 %b, 1, !dbg !11 ret i16 0,
790 !dbg !11
791 }
792
793 Splice from head of First, which includes the leading dbg.value, to Last,
794 including the trailing dbg.value. Place in front of Dest, but after any
795 debug-info there. Becomes:
796
797 define i16 @f(i16 %a) !dbg !6 {
798 BBEntry entry:
799 Last br label %exit, !dbg !11
800
801 BBExit exit:
802 DVRConst call void @llvm.dbg.value(metadata i16 0, metadata !9, metadata
803 !DIExpression()), !dbg !11 DVRA call void @llvm.dbg.value(metadata i16 %a,
804 metadata !9, metadata !DIExpression()), !dbg !11 First %b = add i16 %a, 1,
805 !dbg !11 DVRB call void @llvm.dbg.value(metadata i16 %b, metadata !9,
806 metadata !DIExpression()), !dbg !11 Dest %c = add i16 %b, 1, !dbg !11 ret
807 i16 0, !dbg !11
808 }
809
810
811 */
812 BBExit->splice(Dest, BBEntry, First, Last);
813 EXPECT_EQ(BInst->getParent(), BBExit);
814 EXPECT_EQ(CInst->getParent(), BBExit);
815
816 // DVRB: should be on CInst, in exit block.
817 EXPECT_TRUE(InstContainsDbgVariableRecord(CInst, DVRB));
818
819 // DVRA, should have transferred with the spliced instructions, remains on
820 // the "b" inst.
821 EXPECT_TRUE(InstContainsDbgVariableRecord(BInst, DVRA));
822
823 // DVRConst should be ahead of the moved instructions, ahead of BInst.
824 EXPECT_TRUE(InstContainsDbgVariableRecord(BInst, DVRConst));
825
826 // Order of DVRA and DVRConst should be thus:
827 EXPECT_TRUE(CheckDVROrder(BInst, {DVRConst, DVRA}));
828 }
829
830 TEST_F(DbgSpliceTest, DbgSpliceTest3) {
831 Dest.setHeadBit(true);
832 First.setHeadBit(true);
833 Last.setTailBit(false);
834
835 /*
836 define i16 @f(i16 %a) !dbg !6 {
837 BBEntry entry:
838 DVRA call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata
839 !DIExpression()), !dbg !11 First %b = add i16 %a, 1, !dbg !11 DVRB call
840 void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata !DIExpression()),
841 !dbg !11 Last br label %exit, !dbg !11
842
843 BBExit exit:
844 DVRConst call void @llvm.dbg.value(metadata i16 0, metadata !9, metadata
845 !DIExpression()), !dbg !11 Dest %c = add i16 %b, 1, !dbg !11 ret i16 0,
846 !dbg !11
847 }
848
849 Splice from head of First, which includes the leading dbg.value, to Last,
850 including the trailing dbg.value. Place at head of Dest, before any
851 debug-info there. Becomes:
852
853 define i16 @f(i16 %a) !dbg !6 {
854 BBEntry entry:
855 Last br label %exit, !dbg !11
856
857 BBExit exit:
858 DVRA call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata
859 !DIExpression()), !dbg !11 First %b = add i16 %a, 1, !dbg !11 DVRB call
860 void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata !DIExpression()),
861 !dbg !11 DVRConst call void @llvm.dbg.value(metadata i16 0, metadata !9,
862 metadata !DIExpression()), !dbg !11 Dest %c = add i16 %b, 1, !dbg !11 ret
863 i16 0, !dbg !11
864 }
865
866 */
867 BBExit->splice(Dest, BBEntry, First, Last);
868 EXPECT_EQ(BInst->getParent(), BBExit);
869 EXPECT_EQ(CInst->getParent(), BBExit);
870
871 // DVRB: should be on CInst, in exit block.
872 EXPECT_TRUE(InstContainsDbgVariableRecord(CInst, DVRB));
873
874 // DVRA, should have transferred with the spliced instructions, remains on
875 // the "b" inst.
876 EXPECT_TRUE(InstContainsDbgVariableRecord(BInst, DVRA));
877
878 // DVRConst should be behind the moved instructions, ahead of CInst.
879 EXPECT_TRUE(InstContainsDbgVariableRecord(CInst, DVRConst));
880
881 // Order of DVRB and DVRConst should be thus:
882 EXPECT_TRUE(CheckDVROrder(CInst, {DVRB, DVRConst}));
883 }
884
885 TEST_F(DbgSpliceTest, DbgSpliceTest4) {
886 Dest.setHeadBit(false);
887 First.setHeadBit(false);
888 Last.setTailBit(true);
889
890 /*
891 define i16 @f(i16 %a) !dbg !6 {
892 BBEntry entry:
893 DVRA call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata
894 !DIExpression()), !dbg !11 First %b = add i16 %a, 1, !dbg !11 DVRB call
895 void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata !DIExpression()),
896 !dbg !11 Last br label %exit, !dbg !11
897
898 BBExit exit:
899 DVRConst call void @llvm.dbg.value(metadata i16 0, metadata !9, metadata
900 !DIExpression()), !dbg !11 Dest %c = add i16 %b, 1, !dbg !11 ret i16 0,
901 !dbg !11
902 }
903
904 Splice from First, not including the leading dbg.value, to Last, but NOT
905 including the trailing dbg.value because the tail bit is set. Place at Dest,
906 after any debug-info there. Becomes:
907
908 define i16 @f(i16 %a) !dbg !6 {
909 BBEntry entry:
910 DVRA call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata
911 !DIExpression()), !dbg !11 DVRB call void @llvm.dbg.value(metadata i16 %b,
912 metadata !9, metadata !DIExpression()), !dbg !11 Last br label %exit, !dbg
913 !11
914
915 BBExit exit:
916 DVRConst call void @llvm.dbg.value(metadata i16 0, metadata !9, metadata
917 !DIExpression()), !dbg !11 First %b = add i16 %a, 1, !dbg !11 Dest %c =
918 add i16 %b, 1, !dbg !11 ret i16 0, !dbg !11
919 }
920
921 */
922 BBExit->splice(Dest, BBEntry, First, Last);
923 EXPECT_EQ(BInst->getParent(), BBExit);
924 EXPECT_EQ(CInst->getParent(), BBExit);
925 EXPECT_EQ(Branch->getParent(), BBEntry);
926
927 // DVRB: should be on Branch as before, remain in entry block.
928 EXPECT_TRUE(InstContainsDbgVariableRecord(Branch, DVRB));
929
930 // DVRA, should have remained in entry block, falls onto Branch inst.
931 EXPECT_TRUE(InstContainsDbgVariableRecord(Branch, DVRA));
932
933 // DVRConst should be ahead of the moved instructions, BInst.
934 EXPECT_TRUE(InstContainsDbgVariableRecord(BInst, DVRConst));
935
936 // Order of DVRA and DVRA should be thus:
937 EXPECT_TRUE(CheckDVROrder(Branch, {DVRA, DVRB}));
938 }
939
940 TEST_F(DbgSpliceTest, DbgSpliceTest5) {
941 Dest.setHeadBit(true);
942 First.setHeadBit(false);
943 Last.setTailBit(true);
944
945 /*
946 define i16 @f(i16 %a) !dbg !6 {
947 BBEntry entry:
948 DVRA call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata
949 !DIExpression()), !dbg !11 First %b = add i16 %a, 1, !dbg !11 DVRB call
950 void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata !DIExpression()),
951 !dbg !11 Last br label %exit, !dbg !11
952
953 BBExit exit:
954 DVRConst call void @llvm.dbg.value(metadata i16 0, metadata !9, metadata
955 !DIExpression()), !dbg !11 Dest %c = add i16 %b, 1, !dbg !11 ret i16 0,
956 !dbg !11
957 }
958
959 Splice from First, not including the leading dbg.value, to Last, but NOT
960 including the trailing dbg.value because the tail bit is set. Place at head
961 of Dest, before any debug-info there. Becomes:
962
963 define i16 @f(i16 %a) !dbg !6 {
964 BBEntry entry:
965 DVRA call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata
966 !DIExpression()), !dbg !11 DVRB call void @llvm.dbg.value(metadata i16 %b,
967 metadata !9, metadata !DIExpression()), !dbg !11 Last br label %exit, !dbg
968 !11
969
970 BBExit exit:
971 First %b = add i16 %a, 1, !dbg !11
972 DVRConst call void @llvm.dbg.value(metadata i16 0, metadata !9, metadata
973 !DIExpression()), !dbg !11 Dest %c = add i16 %b, 1, !dbg !11 ret i16 0,
974 !dbg !11
975 }
976
977 */
978 BBExit->splice(Dest, BBEntry, First, Last);
979 EXPECT_EQ(BInst->getParent(), BBExit);
980 EXPECT_EQ(CInst->getParent(), BBExit);
981 EXPECT_EQ(Branch->getParent(), BBEntry);
982
983 // DVRB: should be on Branch as before, remain in entry block.
984 EXPECT_TRUE(InstContainsDbgVariableRecord(Branch, DVRB));
985
986 // DVRA, should have remained in entry block, falls onto Branch inst.
987 EXPECT_TRUE(InstContainsDbgVariableRecord(Branch, DVRA));
988
989 // DVRConst should be behind of the moved instructions, on CInst.
990 EXPECT_TRUE(InstContainsDbgVariableRecord(CInst, DVRConst));
991
992 // Order of DVRA and DVRB should be thus:
993 EXPECT_TRUE(CheckDVROrder(Branch, {DVRA, DVRB}));
994 }
995
996 TEST_F(DbgSpliceTest, DbgSpliceTest6) {
997 Dest.setHeadBit(false);
998 First.setHeadBit(true);
999 Last.setTailBit(true);
1000
1001 /*
1002 define i16 @f(i16 %a) !dbg !6 {
1003 BBEntry entry:
1004 DVRA call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata
1005 !DIExpression()), !dbg !11 First %b = add i16 %a, 1, !dbg !11 DVRB call
1006 void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata !DIExpression()),
1007 !dbg !11 Last br label %exit, !dbg !11
1008
1009 BBExit exit:
1010 DVRConst call void @llvm.dbg.value(metadata i16 0, metadata !9, metadata
1011 !DIExpression()), !dbg !11 Dest %c = add i16 %b, 1, !dbg !11 ret i16 0,
1012 !dbg !11
1013 }
1014
1015 Splice from First, including the leading dbg.value, to Last, but NOT
1016 including the trailing dbg.value because the tail bit is set. Place at Dest,
1017 after any debug-info there. Becomes:
1018
1019 define i16 @f(i16 %a) !dbg !6 {
1020 BBEntry entry:
1021 DVRB call void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata
1022 !DIExpression()), !dbg !11 Last br label %exit, !dbg !11
1023
1024 BBExit exit:
1025 DVRConst call void @llvm.dbg.value(metadata i16 0, metadata !9, metadata
1026 !DIExpression()), !dbg !11 DVRA call void @llvm.dbg.value(metadata i16 %a,
1027 metadata !9, metadata !DIExpression()), !dbg !11 First %b = add i16 %a, 1,
1028 !dbg !11 Dest %c = add i16 %b, 1, !dbg !11 ret i16 0, !dbg !11
1029 }
1030
1031 */
1032 BBExit->splice(Dest, BBEntry, First, Last);
1033 EXPECT_EQ(BInst->getParent(), BBExit);
1034 EXPECT_EQ(CInst->getParent(), BBExit);
1035 EXPECT_EQ(Branch->getParent(), BBEntry);
1036
1037 // DVRB: should be on Branch as before, remain in entry block.
1038 EXPECT_TRUE(InstContainsDbgVariableRecord(Branch, DVRB));
1039
1040 // DVRA, should have transferred to BBExit, on B inst.
1041 EXPECT_TRUE(InstContainsDbgVariableRecord(BInst, DVRA));
1042
1043 // DVRConst should be ahead of the moved instructions, on BInst.
1044 EXPECT_TRUE(InstContainsDbgVariableRecord(BInst, DVRConst));
1045
1046 // Order of DVRA and DVRConst should be thus:
1047 EXPECT_TRUE(CheckDVROrder(BInst, {DVRConst, DVRA}));
1048 }
1049
1050 TEST_F(DbgSpliceTest, DbgSpliceTest7) {
1051 Dest.setHeadBit(true);
1052 First.setHeadBit(true);
1053 Last.setTailBit(true);
1054
1055 /*
1056 define i16 @f(i16 %a) !dbg !6 {
1057 BBEntry entry:
1058 DVRA call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata
1059 !DIExpression()), !dbg !11 First %b = add i16 %a, 1, !dbg !11 DVRB call
1060 void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata !DIExpression()),
1061 !dbg !11 Last br label %exit, !dbg !11
1062
1063 BBExit exit:
1064 DVRConst call void @llvm.dbg.value(metadata i16 0, metadata !9, metadata
1065 !DIExpression()), !dbg !11 Dest %c = add i16 %b, 1, !dbg !11 ret i16 0,
1066 !dbg !11
1067 }
1068
1069 Splice from First, including the leading dbg.value, to Last, but NOT
1070 including the trailing dbg.value because the tail bit is set. Place at head
1071 of Dest, before any debug-info there. Becomes:
1072
1073 define i16 @f(i16 %a) !dbg !6 {
1074 BBEntry entry:
1075 DVRB call void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata
1076 !DIExpression()), !dbg !11 Last br label %exit, !dbg !11
1077
1078 BBExit exit:
1079 DVRA call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata
1080 !DIExpression()), !dbg !11 First %b = add i16 %a, 1, !dbg !11 DVRConst call
1081 void @llvm.dbg.value(metadata i16 0, metadata !9, metadata !DIExpression()),
1082 !dbg !11 Dest %c = add i16 %b, 1, !dbg !11 ret i16 0, !dbg !11
1083 }
1084
1085 */
1086 BBExit->splice(Dest, BBEntry, First, Last);
1087 EXPECT_EQ(BInst->getParent(), BBExit);
1088 EXPECT_EQ(CInst->getParent(), BBExit);
1089 EXPECT_EQ(Branch->getParent(), BBEntry);
1090
1091 // DVRB: should be on Branch as before, remain in entry block.
1092 EXPECT_TRUE(InstContainsDbgVariableRecord(Branch, DVRB));
1093
1094 // DVRA, should have transferred to BBExit, on B inst.
1095 EXPECT_TRUE(InstContainsDbgVariableRecord(BInst, DVRA));
1096
1097 // DVRConst should be after of the moved instructions, on CInst.
1098 EXPECT_TRUE(InstContainsDbgVariableRecord(CInst, DVRConst));
1099 }
1100
1101 // But wait, there's more! What if you splice a range that is empty, but
1102 // implicitly contains debug-info? In the dbg.value design for debug-info,
1103 // this would be an explicit range, but in DbgVariableRecord debug-info, it
1104 // isn't. Check that if we try to do that, with differing head-bit values, that
1105 // DbgVariableRecords are transferred.
1106 // Test with empty transfers to Dest, with head bit set and not set.
1107
1108 TEST_F(DbgSpliceTest, DbgSpliceEmpty0) {
1109 Dest.setHeadBit(false);
1110 First.setHeadBit(false);
1111 Last.setHeadBit(false);
1112 /*
1113 define i16 @f(i16 %a) !dbg !6 {
1114 BBEntry entry:
1115 DVRA call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata
1116 !DIExpression()), !dbg !11 First %b = add i16 %a, 1, !dbg !11 DVRB call
1117 void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata !DIExpression()),
1118 !dbg !11 Last br label %exit, !dbg !11
1119
1120 BBExit exit:
1121 DVRConst call void @llvm.dbg.value(metadata i16 0, metadata !9, metadata
1122 !DIExpression()), !dbg !11 Dest %c = add i16 %b, 1, !dbg !11 ret i16 0,
1123 !dbg !11
1124 }
1125
1126 Splice from BBEntry.getFirstInsertionPt to First -- this implicitly is a
1127 splice of DVRA, but the iterators are pointing at the same instruction. The
1128 only difference is the setting of the head bit. Becomes;
1129
1130 define i16 @f(i16 %a) !dbg !6 {
1131 First %b = add i16 %a, 1, !dbg !11
1132 DVRB call void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata
1133 !DIExpression()), !dbg !11 Last br label %exit, !dbg !11
1134
1135 BBExit exit:
1136 DVRConst call void @llvm.dbg.value(metadata i16 0, metadata !9, metadata
1137 !DIExpression()), !dbg !11 DVRA call void @llvm.dbg.value(metadata i16 %a,
1138 metadata !9, metadata !DIExpression()), !dbg !11 Dest %c = add i16 %b, 1,
1139 !dbg !11 ret i16 0, !dbg !11
1140 }
1141
1142 */
1143 BBExit->splice(Dest, BBEntry, BBEntry->getFirstInsertionPt(), First);
1144 EXPECT_EQ(BInst->getParent(), BBEntry);
1145 EXPECT_EQ(CInst->getParent(), BBExit);
1146 EXPECT_EQ(Branch->getParent(), BBEntry);
1147
1148 // DVRB: should be on Branch as before, remain in entry block.
1149 EXPECT_TRUE(InstContainsDbgVariableRecord(Branch, DVRB));
1150
1151 // DVRA, should have transferred to BBExit, on C inst.
1152 EXPECT_TRUE(InstContainsDbgVariableRecord(CInst, DVRA));
1153
1154 // DVRConst should be ahead of the moved DbgVariableRecord, on CInst.
1155 EXPECT_TRUE(InstContainsDbgVariableRecord(CInst, DVRConst));
1156
1157 // Order of DVRA and DVRConst should be thus:
1158 EXPECT_TRUE(CheckDVROrder(CInst, {DVRConst, DVRA}));
1159 }
1160
1161 TEST_F(DbgSpliceTest, DbgSpliceEmpty1) {
1162 Dest.setHeadBit(true);
1163 First.setHeadBit(false);
1164 Last.setHeadBit(false);
1165 /*
1166 define i16 @f(i16 %a) !dbg !6 {
1167 BBEntry entry:
1168 DVRA call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata
1169 !DIExpression()), !dbg !11 First %b = add i16 %a, 1, !dbg !11 DVRB call
1170 void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata !DIExpression()),
1171 !dbg !11 Last br label %exit, !dbg !11
1172
1173 BBExit exit:
1174 DVRConst call void @llvm.dbg.value(metadata i16 0, metadata !9, metadata
1175 !DIExpression()), !dbg !11 Dest %c = add i16 %b, 1, !dbg !11 ret i16 0,
1176 !dbg !11
1177 }
1178
1179 Splice from BBEntry.getFirstInsertionPt to First -- this implicitly is a
1180 splice of DVRA, but the iterators are pointing at the same instruction. The
1181 only difference is the setting of the head bit. Insert at head of Dest,
1182 i.e. before DVRConst. Becomes;
1183
1184 define i16 @f(i16 %a) !dbg !6 {
1185 First %b = add i16 %a, 1, !dbg !11
1186 DVRB call void @llvm.dbg.value(metadata i16 %b, metadata !9, metadata
1187 !DIExpression()), !dbg !11 Last br label %exit, !dbg !11
1188
1189 BBExit exit:
1190 DVRA call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata
1191 !DIExpression()), !dbg !11 DVRConst call void @llvm.dbg.value(metadata i16 0,
1192 metadata !9, metadata !DIExpression()), !dbg !11 Dest %c = add i16 %b, 1,
1193 !dbg !11 ret i16 0, !dbg !11
1194 }
1195
1196 */
1197 BBExit->splice(Dest, BBEntry, BBEntry->getFirstInsertionPt(), First);
1198 EXPECT_EQ(BInst->getParent(), BBEntry);
1199 EXPECT_EQ(CInst->getParent(), BBExit);
1200 EXPECT_EQ(Branch->getParent(), BBEntry);
1201
1202 // DVRB: should be on Branch as before, remain in entry block.
1203 EXPECT_TRUE(InstContainsDbgVariableRecord(Branch, DVRB));
1204
1205 // DVRA, should have transferred to BBExit, on C inst.
1206 EXPECT_TRUE(InstContainsDbgVariableRecord(CInst, DVRA));
1207
1208 // DVRConst should be ahead of the moved DbgVariableRecord, on CInst.
1209 EXPECT_TRUE(InstContainsDbgVariableRecord(CInst, DVRConst));
1210
1211 // Order of DVRA and DVRConst should be thus:
1212 EXPECT_TRUE(CheckDVROrder(CInst, {DVRA, DVRConst}));
1213 }
1214
1215 // If we splice new instructions into a block with trailing DbgVariableRecords,
1216 // then the trailing DbgVariableRecords should get flushed back out.
1217 TEST(BasicBlockDbgInfoTest, DbgSpliceTrailing) {
1218 LLVMContext C;
1219 std::unique_ptr<Module> M = parseIR(C, R"(
1220 define i16 @f(i16 %a) !dbg !6 {
1221 entry:
1222 call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata !DIExpression()), !dbg !11
1223 br label %exit
1224
1225 exit:
1226 %b = add i16 %a, 1, !dbg !11
1227 ret i16 0, !dbg !11
1228 }
1229 declare void @llvm.dbg.value(metadata, metadata, metadata) #0
1230 attributes #0 = { nounwind readnone speculatable willreturn }
1231
1232 !llvm.dbg.cu = !{!0}
1233 !llvm.module.flags = !{!5}
1234
1235 !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
1236 !1 = !DIFile(filename: "t.ll", directory: "/")
1237 !2 = !{}
1238 !5 = !{i32 2, !"Debug Info Version", i32 3}
1239 !6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
1240 !7 = !DISubroutineType(types: !2)
1241 !8 = !{!9}
1242 !9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
1243 !10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
1244 !11 = !DILocation(line: 1, column: 1, scope: !6)
1245 )");
1246
1247 BasicBlock &Entry = M->getFunction("f")->getEntryBlock();
1248 BasicBlock &Exit = *Entry.getNextNode();
1249
1250 // Begin by forcing entry block to have dangling DbgVariableRecord.
1251 Entry.getTerminator()->eraseFromParent();
1252 ASSERT_NE(Entry.getTrailingDbgRecords(), nullptr);
1253 EXPECT_TRUE(Entry.empty());
1254
1255 // Now transfer the entire contents of the exit block into the entry.
1256 Entry.splice(Entry.end(), &Exit, Exit.begin(), Exit.end());
1257
1258 // The trailing DbgVariableRecord should have been placed at the front of
1259 // what's been spliced in.
1260 Instruction *BInst = &*Entry.begin();
1261 ASSERT_TRUE(BInst->DebugMarker);
1262 EXPECT_EQ(BInst->DebugMarker->StoredDbgRecords.size(), 1u);
1263 }
1264
1265 // When we remove instructions from the program, adjacent DbgVariableRecords
1266 // coalesce together into one DbgMarker. In "old" dbg.value mode you could
1267 // re-insert the removed instruction back into the middle of a sequence of
1268 // dbg.values. Test that this can be replicated correctly by DbgVariableRecords
1269 TEST(BasicBlockDbgInfoTest, RemoveInstAndReinsert) {
1270 LLVMContext C;
1271 std::unique_ptr<Module> M = parseIR(C, R"(
1272 define i16 @f(i16 %a) !dbg !6 {
1273 entry:
1274 %qux = sub i16 %a, 0
1275 call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata !DIExpression()), !dbg !11
1276 %foo = add i16 %a, %a
1277 call void @llvm.dbg.value(metadata i16 0, metadata !9, metadata !DIExpression()), !dbg !11
1278 ret i16 1
1279 }
1280 declare void @llvm.dbg.value(metadata, metadata, metadata)
1281
1282 !llvm.dbg.cu = !{!0}
1283 !llvm.module.flags = !{!5}
1284
1285 !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
1286 !1 = !DIFile(filename: "t.ll", directory: "/")
1287 !2 = !{}
1288 !5 = !{i32 2, !"Debug Info Version", i32 3}
1289 !6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
1290 !7 = !DISubroutineType(types: !2)
1291 !8 = !{!9}
1292 !9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
1293 !10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
1294 !11 = !DILocation(line: 1, column: 1, scope: !6)
1295 )");
1296
1297 BasicBlock &Entry = M->getFunction("f")->getEntryBlock();
1298
1299 // Fetch the relevant instructions from the converted function.
1300 Instruction *SubInst = &*Entry.begin();
1301 ASSERT_TRUE(isa<BinaryOperator>(SubInst));
1302 Instruction *AddInst = SubInst->getNextNode();
1303 ASSERT_TRUE(isa<BinaryOperator>(AddInst));
1304 Instruction *RetInst = AddInst->getNextNode();
1305 ASSERT_TRUE(isa<ReturnInst>(RetInst));
1306
1307 // add and sub should both have one DbgVariableRecord on add and ret.
1308 EXPECT_FALSE(SubInst->hasDbgRecords());
1309 EXPECT_TRUE(AddInst->hasDbgRecords());
1310 EXPECT_TRUE(RetInst->hasDbgRecords());
1311 auto R1 = AddInst->getDbgRecordRange();
1312 EXPECT_EQ(std::distance(R1.begin(), R1.end()), 1u);
1313 auto R2 = RetInst->getDbgRecordRange();
1314 EXPECT_EQ(std::distance(R2.begin(), R2.end()), 1u);
1315
1316 // The Supported (TM) code sequence for removing then reinserting insts
1317 // after another instruction:
1318 std::optional<DbgVariableRecord::self_iterator> Pos =
1319 AddInst->getDbgReinsertionPosition();
1320 AddInst->removeFromParent();
1321
1322 // We should have a re-insertion position.
1323 ASSERT_TRUE(Pos);
1324 // Both DbgVariableRecords should now be attached to the ret inst.
1325 auto R3 = RetInst->getDbgRecordRange();
1326 EXPECT_EQ(std::distance(R3.begin(), R3.end()), 2u);
1327
1328 // Re-insert and re-insert.
1329 AddInst->insertAfter(SubInst->getIterator());
1330 Entry.reinsertInstInDbgRecords(AddInst, Pos);
1331 // We should be back into a position of having one DbgVariableRecord on add
1332 // and ret.
1333 EXPECT_FALSE(SubInst->hasDbgRecords());
1334 EXPECT_TRUE(AddInst->hasDbgRecords());
1335 EXPECT_TRUE(RetInst->hasDbgRecords());
1336 auto R4 = AddInst->getDbgRecordRange();
1337 EXPECT_EQ(std::distance(R4.begin(), R4.end()), 1u);
1338 auto R5 = RetInst->getDbgRecordRange();
1339 EXPECT_EQ(std::distance(R5.begin(), R5.end()), 1u);
1340 }
1341
1342 // Test instruction removal and re-insertion, this time with one
1343 // DbgVariableRecord that should hop up one instruction.
1344 TEST(BasicBlockDbgInfoTest, RemoveInstAndReinsertForOneDbgVariableRecord) {
1345 LLVMContext C;
1346 std::unique_ptr<Module> M = parseIR(C, R"(
1347 define i16 @f(i16 %a) !dbg !6 {
1348 entry:
1349 %qux = sub i16 %a, 0
1350 call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata !DIExpression()), !dbg !11
1351 %foo = add i16 %a, %a
1352 ret i16 1
1353 }
1354 declare void @llvm.dbg.value(metadata, metadata, metadata)
1355
1356 !llvm.dbg.cu = !{!0}
1357 !llvm.module.flags = !{!5}
1358
1359 !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
1360 !1 = !DIFile(filename: "t.ll", directory: "/")
1361 !2 = !{}
1362 !5 = !{i32 2, !"Debug Info Version", i32 3}
1363 !6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
1364 !7 = !DISubroutineType(types: !2)
1365 !8 = !{!9}
1366 !9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
1367 !10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
1368 !11 = !DILocation(line: 1, column: 1, scope: !6)
1369 )");
1370
1371 BasicBlock &Entry = M->getFunction("f")->getEntryBlock();
1372
1373 // Fetch the relevant instructions from the converted function.
1374 Instruction *SubInst = &*Entry.begin();
1375 ASSERT_TRUE(isa<BinaryOperator>(SubInst));
1376 Instruction *AddInst = SubInst->getNextNode();
1377 ASSERT_TRUE(isa<BinaryOperator>(AddInst));
1378 Instruction *RetInst = AddInst->getNextNode();
1379 ASSERT_TRUE(isa<ReturnInst>(RetInst));
1380
1381 // There should be one DbgVariableRecord.
1382 EXPECT_FALSE(SubInst->hasDbgRecords());
1383 EXPECT_TRUE(AddInst->hasDbgRecords());
1384 EXPECT_FALSE(RetInst->hasDbgRecords());
1385 auto R1 = AddInst->getDbgRecordRange();
1386 EXPECT_EQ(std::distance(R1.begin(), R1.end()), 1u);
1387
1388 // The Supported (TM) code sequence for removing then reinserting insts:
1389 std::optional<DbgVariableRecord::self_iterator> Pos =
1390 AddInst->getDbgReinsertionPosition();
1391 AddInst->removeFromParent();
1392
1393 // No re-insertion position as there were no DbgVariableRecords on the ret.
1394 ASSERT_FALSE(Pos);
1395 // The single DbgVariableRecord should now be attached to the ret inst.
1396 EXPECT_TRUE(RetInst->hasDbgRecords());
1397 auto R2 = RetInst->getDbgRecordRange();
1398 EXPECT_EQ(std::distance(R2.begin(), R2.end()), 1u);
1399
1400 // Re-insert and re-insert.
1401 AddInst->insertAfter(SubInst->getIterator());
1402 Entry.reinsertInstInDbgRecords(AddInst, Pos);
1403 // We should be back into a position of having one DbgVariableRecord on the
1404 // AddInst.
1405 EXPECT_FALSE(SubInst->hasDbgRecords());
1406 EXPECT_TRUE(AddInst->hasDbgRecords());
1407 EXPECT_FALSE(RetInst->hasDbgRecords());
1408 auto R3 = AddInst->getDbgRecordRange();
1409 EXPECT_EQ(std::distance(R3.begin(), R3.end()), 1u);
1410 }
1411
1412 // Similar to the above, what if we splice into an empty block with debug-info,
1413 // with debug-info at the start of the moving range, that we intend to be
1414 // transferred. The dbg.value of %a should remain at the start, but come ahead
1415 // of the i16 0 dbg.value.
1416 TEST(BasicBlockDbgInfoTest, DbgSpliceToEmpty1) {
1417 LLVMContext C;
1418 std::unique_ptr<Module> M = parseIR(C, R"(
1419 define i16 @f(i16 %a) !dbg !6 {
1420 entry:
1421 call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata !DIExpression()), !dbg !11
1422 br label %exit
1423
1424 exit:
1425 call void @llvm.dbg.value(metadata i16 0, metadata !9, metadata !DIExpression()), !dbg !11
1426 %b = add i16 %a, 1, !dbg !11
1427 call void @llvm.dbg.value(metadata i16 1, metadata !9, metadata !DIExpression()), !dbg !11
1428 ret i16 0, !dbg !11
1429 }
1430 declare void @llvm.dbg.value(metadata, metadata, metadata) #0
1431 attributes #0 = { nounwind readnone speculatable willreturn }
1432
1433 !llvm.dbg.cu = !{!0}
1434 !llvm.module.flags = !{!5}
1435
1436 !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
1437 !1 = !DIFile(filename: "t.ll", directory: "/")
1438 !2 = !{}
1439 !5 = !{i32 2, !"Debug Info Version", i32 3}
1440 !6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
1441 !7 = !DISubroutineType(types: !2)
1442 !8 = !{!9}
1443 !9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
1444 !10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
1445 !11 = !DILocation(line: 1, column: 1, scope: !6)
1446 )");
1447
1448 Function &F = *M->getFunction("f");
1449 BasicBlock &Entry = F.getEntryBlock();
1450 BasicBlock &Exit = *Entry.getNextNode();
1451
1452 // Begin by forcing entry block to have dangling DbgVariableRecord.
1453 Entry.getTerminator()->eraseFromParent();
1454 ASSERT_NE(Entry.getTrailingDbgRecords(), nullptr);
1455 EXPECT_TRUE(Entry.empty());
1456
1457 // Now transfer the entire contents of the exit block into the entry. This
1458 // includes both dbg.values.
1459 Entry.splice(Entry.end(), &Exit, Exit.begin(), Exit.end());
1460
1461 // We should now have two dbg.values on the first instruction, and they
1462 // should be in the correct order of %a, then 0.
1463 Instruction *BInst = &*Entry.begin();
1464 ASSERT_TRUE(BInst->hasDbgRecords());
1465 EXPECT_EQ(BInst->DebugMarker->StoredDbgRecords.size(), 2u);
1466 SmallVector<DbgVariableRecord *, 2> DbgVariableRecords;
1467 for (DbgRecord &DVR : BInst->getDbgRecordRange())
1468 DbgVariableRecords.push_back(cast<DbgVariableRecord>(&DVR));
1469
1470 EXPECT_EQ(DbgVariableRecords[0]->getVariableLocationOp(0), F.getArg(0));
1471 Value *SecondDVRValue = DbgVariableRecords[1]->getVariableLocationOp(0);
1472 ASSERT_TRUE(isa<ConstantInt>(SecondDVRValue));
1473 EXPECT_EQ(cast<ConstantInt>(SecondDVRValue)->getZExtValue(), 0ull);
1474
1475 // No trailing DbgVariableRecords in the entry block now.
1476 EXPECT_EQ(Entry.getTrailingDbgRecords(), nullptr);
1477 }
1478
1479 // Similar test again, but this time: splice the contents of exit into entry,
1480 // with the intention of leaving the first dbg.value (i16 0) behind.
1481 TEST(BasicBlockDbgInfoTest, DbgSpliceToEmpty2) {
1482 LLVMContext C;
1483 std::unique_ptr<Module> M = parseIR(C, R"(
1484 define i16 @f(i16 %a) !dbg !6 {
1485 entry:
1486 call void @llvm.dbg.value(metadata i16 %a, metadata !9, metadata !DIExpression()), !dbg !11
1487 br label %exit
1488
1489 exit:
1490 call void @llvm.dbg.value(metadata i16 0, metadata !9, metadata !DIExpression()), !dbg !11
1491 %b = add i16 %a, 1, !dbg !11
1492 call void @llvm.dbg.value(metadata i16 1, metadata !9, metadata !DIExpression()), !dbg !11
1493 ret i16 0, !dbg !11
1494 }
1495 declare void @llvm.dbg.value(metadata, metadata, metadata) #0
1496 attributes #0 = { nounwind readnone speculatable willreturn }
1497
1498 !llvm.dbg.cu = !{!0}
1499 !llvm.module.flags = !{!5}
1500
1501 !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
1502 !1 = !DIFile(filename: "t.ll", directory: "/")
1503 !2 = !{}
1504 !5 = !{i32 2, !"Debug Info Version", i32 3}
1505 !6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
1506 !7 = !DISubroutineType(types: !2)
1507 !8 = !{!9}
1508 !9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
1509 !10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
1510 !11 = !DILocation(line: 1, column: 1, scope: !6)
1511 )");
1512
1513 Function &F = *M->getFunction("f");
1514 BasicBlock &Entry = F.getEntryBlock();
1515 BasicBlock &Exit = *Entry.getNextNode();
1516
1517 // Begin by forcing entry block to have dangling DbgVariableRecord.
1518 Entry.getTerminator()->eraseFromParent();
1519 ASSERT_NE(Entry.getTrailingDbgRecords(), nullptr);
1520 EXPECT_TRUE(Entry.empty());
1521
1522 // Now transfer into the entry block -- fetching the first instruction with
1523 // begin and then calling getIterator clears the "head" bit, meaning that the
1524 // range to move will not include any leading DbgVariableRecords.
1525 Entry.splice(Entry.end(), &Exit, Exit.begin()->getIterator(), Exit.end());
1526
1527 // We should now have one dbg.values on the first instruction, %a.
1528 Instruction *BInst = &*Entry.begin();
1529 ASSERT_TRUE(BInst->hasDbgRecords());
1530 EXPECT_EQ(BInst->DebugMarker->StoredDbgRecords.size(), 1u);
1531 SmallVector<DbgVariableRecord *, 2> DbgVariableRecords;
1532 for (DbgRecord &DVR : BInst->getDbgRecordRange())
1533 DbgVariableRecords.push_back(cast<DbgVariableRecord>(&DVR));
1534
1535 EXPECT_EQ(DbgVariableRecords[0]->getVariableLocationOp(0), F.getArg(0));
1536 // No trailing DbgVariableRecords in the entry block now.
1537 EXPECT_EQ(Entry.getTrailingDbgRecords(), nullptr);
1538
1539 // We should have nothing left in the exit block...
1540 EXPECT_TRUE(Exit.empty());
1541 // ... except for some dangling DbgVariableRecords.
1542 EXPECT_NE(Exit.getTrailingDbgRecords(), nullptr);
1543 EXPECT_FALSE(Exit.getTrailingDbgRecords()->empty());
1544 Exit.getTrailingDbgRecords()->eraseFromParent();
1545 Exit.deleteTrailingDbgRecords();
1546 }
1547
1548 // What if we moveBefore end() -- there might be no debug-info there, in which
1549 // case we shouldn't crash.
1550 TEST(BasicBlockDbgInfoTest, DbgMoveToEnd) {
1551 LLVMContext C;
1552 std::unique_ptr<Module> M = parseIR(C, R"(
1553 define i16 @f(i16 %a) !dbg !6 {
1554 entry:
1555 br label %exit
1556
1557 exit:
1558 ret i16 0, !dbg !11
1559 }
1560 declare void @llvm.dbg.value(metadata, metadata, metadata) #0
1561 attributes #0 = { nounwind readnone speculatable willreturn }
1562
1563 !llvm.dbg.cu = !{!0}
1564 !llvm.module.flags = !{!5}
1565
1566 !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
1567 !1 = !DIFile(filename: "t.ll", directory: "/")
1568 !2 = !{}
1569 !5 = !{i32 2, !"Debug Info Version", i32 3}
1570 !6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
1571 !7 = !DISubroutineType(types: !2)
1572 !8 = !{!9}
1573 !9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
1574 !10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
1575 !11 = !DILocation(line: 1, column: 1, scope: !6)
1576 )");
1577
1578 Function &F = *M->getFunction("f");
1579 BasicBlock &Entry = F.getEntryBlock();
1580 BasicBlock &Exit = *Entry.getNextNode();
1581
1582 // Move the return to the end of the entry block.
1583 Instruction *Br = Entry.getTerminator();
1584 Instruction *Ret = Exit.getTerminator();
1585 EXPECT_EQ(Entry.getTrailingDbgRecords(), nullptr);
1586 Ret->moveBefore(Entry, Entry.end());
1587 Br->eraseFromParent();
1588
1589 // There should continue to not be any debug-info anywhere.
1590 EXPECT_EQ(Entry.getTrailingDbgRecords(), nullptr);
1591 EXPECT_EQ(Exit.getTrailingDbgRecords(), nullptr);
1592 EXPECT_FALSE(Ret->hasDbgRecords());
1593 }
1594
1595 TEST(BasicBlockDbgInfoTest, CloneTrailingRecordsToEmptyBlock) {
1596 LLVMContext C;
1597 std::unique_ptr<Module> M = parseIR(C, R"(
1598 define i16 @foo(i16 %a) !dbg !6 {
1599 entry:
1600 %b = add i16 %a, 0
1601 #dbg_value(i16 %b, !9, !DIExpression(), !11)
1602 ret i16 0, !dbg !11
1603 }
1604
1605 !llvm.dbg.cu = !{!0}
1606 !llvm.module.flags = !{!5}
1607
1608 !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
1609 !1 = !DIFile(filename: "t.ll", directory: "/")
1610 !2 = !{}
1611 !5 = !{i32 2, !"Debug Info Version", i32 3}
1612 !6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: null, file: !1, line: 1, type: !7, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
1613 !7 = !DISubroutineType(types: !2)
1614 !8 = !{!9}
1615 !9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10)
1616 !10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
1617 !11 = !DILocation(line: 1, column: 1, scope: !6)
1618 )");
1619 ASSERT_TRUE(M);
1620
1621 Function *F = M->getFunction("foo");
1622 BasicBlock &BB = F->getEntryBlock();
1623 // Start with no trailing records.
1624 ASSERT_FALSE(BB.getTrailingDbgRecords());
1625
1626 BasicBlock::iterator Ret = std::prev(BB.end());
1627 BasicBlock::iterator B = std::prev(Ret);
1628
1629 // Delete terminator which has debug records: we now get trailing records.
1630 Ret->eraseFromParent();
1631 EXPECT_TRUE(BB.getTrailingDbgRecords());
1632
1633 BasicBlock *NewBB = BasicBlock::Create(C, "NewBB", F);
1634 NewBB->splice(NewBB->end(), &BB, B, BB.end());
1635
1636 // The trailing records should've been absorbed into NewBB.
1637 EXPECT_FALSE(BB.getTrailingDbgRecords());
1638 EXPECT_TRUE(NewBB->getTrailingDbgRecords());
1639 if (DbgMarker *Trailing = NewBB->getTrailingDbgRecords()) {
1640 EXPECT_EQ(llvm::range_size(Trailing->getDbgRecordRange()), 1u);
1641 // Drop the trailing records now, to prevent a cleanup assertion.
1642 Trailing->eraseFromParent();
1643 NewBB->deleteTrailingDbgRecords();
1644 }
1645 }
1646
1647 } // End anonymous namespace.