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