1 //===- Local.cpp - Unit tests for Local -----------------------------------===// 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/Transforms/Utils/Local.h" 10 #include "llvm/Analysis/DomTreeUpdater.h" 11 #include "llvm/Analysis/InstructionSimplify.h" 12 #include "llvm/Analysis/PostDominators.h" 13 #include "llvm/Analysis/TargetTransformInfo.h" 14 #include "llvm/AsmParser/Parser.h" 15 #include "llvm/IR/BasicBlock.h" 16 #include "llvm/IR/DIBuilder.h" 17 #include "llvm/IR/DebugInfo.h" 18 #include "llvm/IR/DebugProgramInstruction.h" 19 #include "llvm/IR/IRBuilder.h" 20 #include "llvm/IR/Instructions.h" 21 #include "llvm/IR/IntrinsicInst.h" 22 #include "llvm/IR/LLVMContext.h" 23 #include "llvm/IR/Verifier.h" 24 #include "llvm/Support/SourceMgr.h" 25 #include "gtest/gtest.h" 26 27 using namespace llvm; 28 29 TEST(Local, RecursivelyDeleteDeadPHINodes) { 30 LLVMContext C; 31 32 IRBuilder<> builder(C); 33 34 // Make blocks 35 BasicBlock *bb0 = BasicBlock::Create(C); 36 BasicBlock *bb1 = BasicBlock::Create(C); 37 38 builder.SetInsertPoint(bb0); 39 PHINode *phi = builder.CreatePHI(Type::getInt32Ty(C), 2); 40 BranchInst *br0 = builder.CreateCondBr(builder.getTrue(), bb0, bb1); 41 42 builder.SetInsertPoint(bb1); 43 BranchInst *br1 = builder.CreateBr(bb0); 44 45 phi->addIncoming(phi, bb0); 46 phi->addIncoming(phi, bb1); 47 48 // The PHI will be removed 49 EXPECT_TRUE(RecursivelyDeleteDeadPHINode(phi)); 50 51 // Make sure the blocks only contain the branches 52 EXPECT_EQ(&bb0->front(), br0); 53 EXPECT_EQ(&bb1->front(), br1); 54 55 builder.SetInsertPoint(bb0); 56 phi = builder.CreatePHI(Type::getInt32Ty(C), 0); 57 58 EXPECT_TRUE(RecursivelyDeleteDeadPHINode(phi)); 59 60 builder.SetInsertPoint(bb0); 61 phi = builder.CreatePHI(Type::getInt32Ty(C), 0); 62 builder.CreateAdd(phi, phi); 63 64 EXPECT_TRUE(RecursivelyDeleteDeadPHINode(phi)); 65 66 bb0->dropAllReferences(); 67 bb1->dropAllReferences(); 68 delete bb0; 69 delete bb1; 70 } 71 72 TEST(Local, RemoveDuplicatePHINodes) { 73 LLVMContext C; 74 IRBuilder<> B(C); 75 76 std::unique_ptr<Function> F( 77 Function::Create(FunctionType::get(B.getVoidTy(), false), 78 GlobalValue::ExternalLinkage, "F")); 79 BasicBlock *Entry(BasicBlock::Create(C, "", F.get())); 80 BasicBlock *BB(BasicBlock::Create(C, "", F.get())); 81 BranchInst::Create(BB, Entry); 82 83 B.SetInsertPoint(BB); 84 85 AssertingVH<PHINode> P1 = B.CreatePHI(Type::getInt32Ty(C), 2); 86 P1->addIncoming(B.getInt32(42), Entry); 87 88 PHINode *P2 = B.CreatePHI(Type::getInt32Ty(C), 2); 89 P2->addIncoming(B.getInt32(42), Entry); 90 91 AssertingVH<PHINode> P3 = B.CreatePHI(Type::getInt32Ty(C), 2); 92 P3->addIncoming(B.getInt32(42), Entry); 93 P3->addIncoming(B.getInt32(23), BB); 94 95 PHINode *P4 = B.CreatePHI(Type::getInt32Ty(C), 2); 96 P4->addIncoming(B.getInt32(42), Entry); 97 P4->addIncoming(B.getInt32(23), BB); 98 99 P1->addIncoming(P3, BB); 100 P2->addIncoming(P4, BB); 101 BranchInst::Create(BB, BB); 102 103 // Verify that we can eliminate PHIs that become duplicates after chaning PHIs 104 // downstream. 105 EXPECT_TRUE(EliminateDuplicatePHINodes(BB)); 106 EXPECT_EQ(3U, BB->size()); 107 } 108 109 static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) { 110 SMDiagnostic Err; 111 std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C); 112 if (!Mod) 113 Err.print("UtilsTests", errs()); 114 return Mod; 115 } 116 117 TEST(Local, ReplaceDbgDeclare) { 118 LLVMContext C; 119 120 // Original C source to get debug info for a local variable: 121 // void f() { int x; } 122 std::unique_ptr<Module> M = parseIR(C, 123 R"( 124 define void @f() !dbg !8 { 125 entry: 126 %x = alloca i32, align 4 127 call void @llvm.dbg.declare(metadata i32* %x, metadata !11, metadata !DIExpression()), !dbg !13 128 call void @llvm.dbg.declare(metadata i32* %x, metadata !11, metadata !DIExpression()), !dbg !13 129 ret void, !dbg !14 130 } 131 declare void @llvm.dbg.declare(metadata, metadata, metadata) 132 !llvm.dbg.cu = !{!0} 133 !llvm.module.flags = !{!3, !4} 134 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 6.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2) 135 !1 = !DIFile(filename: "t2.c", directory: "foo") 136 !2 = !{} 137 !3 = !{i32 2, !"Dwarf Version", i32 4} 138 !4 = !{i32 2, !"Debug Info Version", i32 3} 139 !8 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !9, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: false, unit: !0, retainedNodes: !2) 140 !9 = !DISubroutineType(types: !10) 141 !10 = !{null} 142 !11 = !DILocalVariable(name: "x", scope: !8, file: !1, line: 2, type: !12) 143 !12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) 144 !13 = !DILocation(line: 2, column: 7, scope: !8) 145 !14 = !DILocation(line: 3, column: 1, scope: !8) 146 )"); 147 auto *GV = M->getNamedValue("f"); 148 ASSERT_TRUE(GV); 149 auto *F = dyn_cast<Function>(GV); 150 ASSERT_TRUE(F); 151 Instruction *Inst = &F->front().front(); 152 auto *AI = dyn_cast<AllocaInst>(Inst); 153 ASSERT_TRUE(AI); 154 Inst = Inst->getNextNode()->getNextNode(); 155 ASSERT_TRUE(Inst); 156 auto *DII = dyn_cast<DbgDeclareInst>(Inst); 157 ASSERT_TRUE(DII); 158 Value *NewBase = Constant::getNullValue(PointerType::getUnqual(C)); 159 DIBuilder DIB(*M); 160 replaceDbgDeclare(AI, NewBase, DIB, DIExpression::ApplyOffset, 0); 161 162 // There should be exactly two dbg.declares. 163 int Declares = 0; 164 for (const Instruction &I : F->front()) 165 if (isa<DbgDeclareInst>(I)) 166 Declares++; 167 EXPECT_EQ(2, Declares); 168 } 169 170 /// Build the dominator tree for the function and run the Test. 171 static void runWithDomTree( 172 Module &M, StringRef FuncName, 173 function_ref<void(Function &F, DominatorTree *DT)> Test) { 174 auto *F = M.getFunction(FuncName); 175 ASSERT_NE(F, nullptr) << "Could not find " << FuncName; 176 // Compute the dominator tree for the function. 177 DominatorTree DT(*F); 178 Test(*F, &DT); 179 } 180 181 TEST(Local, MergeBasicBlockIntoOnlyPred) { 182 LLVMContext C; 183 std::unique_ptr<Module> M; 184 auto resetIR = [&]() { 185 M = parseIR(C, 186 R"( 187 define i32 @f(i8* %str) { 188 entry: 189 br label %bb2.i 190 bb2.i: ; preds = %bb4.i, %entry 191 br i1 false, label %bb4.i, label %base2flt.exit204 192 bb4.i: ; preds = %bb2.i 193 br i1 false, label %base2flt.exit204, label %bb2.i 194 bb10.i196.bb7.i197_crit_edge: ; No predecessors! 195 br label %bb7.i197 196 bb7.i197: ; preds = %bb10.i196.bb7.i197_crit_edge 197 %.reg2mem.0 = phi i32 [ %.reg2mem.0, %bb10.i196.bb7.i197_crit_edge ] 198 br i1 undef, label %base2flt.exit204, label %base2flt.exit204 199 base2flt.exit204: ; preds = %bb7.i197, %bb7.i197, %bb2.i, %bb4.i 200 ret i32 0 201 } 202 )"); 203 }; 204 205 auto resetIRReplaceEntry = [&]() { 206 M = parseIR(C, 207 R"( 208 define i32 @f() { 209 entry: 210 br label %bb2.i 211 bb2.i: ; preds = %entry 212 ret i32 0 213 } 214 )"); 215 }; 216 217 auto Test = [&](Function &F, DomTreeUpdater &DTU) { 218 for (Function::iterator I = F.begin(), E = F.end(); I != E;) { 219 BasicBlock *BB = &*I++; 220 BasicBlock *SinglePred = BB->getSinglePredecessor(); 221 if (!SinglePred || SinglePred == BB || BB->hasAddressTaken()) 222 continue; 223 BranchInst *Term = dyn_cast<BranchInst>(SinglePred->getTerminator()); 224 if (Term && !Term->isConditional()) 225 MergeBasicBlockIntoOnlyPred(BB, &DTU); 226 } 227 if (DTU.hasDomTree()) { 228 EXPECT_TRUE(DTU.getDomTree().verify()); 229 } 230 if (DTU.hasPostDomTree()) { 231 EXPECT_TRUE(DTU.getPostDomTree().verify()); 232 } 233 }; 234 235 // Test MergeBasicBlockIntoOnlyPred working under Eager UpdateStrategy with 236 // both DT and PDT. 237 resetIR(); 238 runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) { 239 PostDominatorTree PDT = PostDominatorTree(F); 240 DomTreeUpdater DTU(*DT, PDT, DomTreeUpdater::UpdateStrategy::Eager); 241 Test(F, DTU); 242 }); 243 244 // Test MergeBasicBlockIntoOnlyPred working under Eager UpdateStrategy with 245 // DT. 246 resetIR(); 247 runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) { 248 DomTreeUpdater DTU(*DT, DomTreeUpdater::UpdateStrategy::Eager); 249 Test(F, DTU); 250 }); 251 252 // Test MergeBasicBlockIntoOnlyPred working under Eager UpdateStrategy with 253 // PDT. 254 resetIR(); 255 runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) { 256 PostDominatorTree PDT = PostDominatorTree(F); 257 DomTreeUpdater DTU(PDT, DomTreeUpdater::UpdateStrategy::Eager); 258 Test(F, DTU); 259 }); 260 261 // Test MergeBasicBlockIntoOnlyPred working under Lazy UpdateStrategy with 262 // both DT and PDT. 263 resetIR(); 264 runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) { 265 PostDominatorTree PDT = PostDominatorTree(F); 266 DomTreeUpdater DTU(*DT, PDT, DomTreeUpdater::UpdateStrategy::Lazy); 267 Test(F, DTU); 268 }); 269 270 // Test MergeBasicBlockIntoOnlyPred working under Lazy UpdateStrategy with 271 // PDT. 272 resetIR(); 273 runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) { 274 PostDominatorTree PDT = PostDominatorTree(F); 275 DomTreeUpdater DTU(PDT, DomTreeUpdater::UpdateStrategy::Lazy); 276 Test(F, DTU); 277 }); 278 279 // Test MergeBasicBlockIntoOnlyPred working under Lazy UpdateStrategy with DT. 280 resetIR(); 281 runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) { 282 DomTreeUpdater DTU(*DT, DomTreeUpdater::UpdateStrategy::Lazy); 283 Test(F, DTU); 284 }); 285 286 // Test MergeBasicBlockIntoOnlyPred working under Eager UpdateStrategy with 287 // both DT and PDT. 288 resetIRReplaceEntry(); 289 runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) { 290 PostDominatorTree PDT = PostDominatorTree(F); 291 DomTreeUpdater DTU(*DT, PDT, DomTreeUpdater::UpdateStrategy::Eager); 292 Test(F, DTU); 293 }); 294 295 // Test MergeBasicBlockIntoOnlyPred working under Eager UpdateStrategy with 296 // DT. 297 resetIRReplaceEntry(); 298 runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) { 299 DomTreeUpdater DTU(*DT, DomTreeUpdater::UpdateStrategy::Eager); 300 Test(F, DTU); 301 }); 302 303 // Test MergeBasicBlockIntoOnlyPred working under Eager UpdateStrategy with 304 // PDT. 305 resetIRReplaceEntry(); 306 runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) { 307 PostDominatorTree PDT = PostDominatorTree(F); 308 DomTreeUpdater DTU(PDT, DomTreeUpdater::UpdateStrategy::Eager); 309 Test(F, DTU); 310 }); 311 312 // Test MergeBasicBlockIntoOnlyPred working under Lazy UpdateStrategy with 313 // both DT and PDT. 314 resetIRReplaceEntry(); 315 runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) { 316 PostDominatorTree PDT = PostDominatorTree(F); 317 DomTreeUpdater DTU(*DT, PDT, DomTreeUpdater::UpdateStrategy::Lazy); 318 Test(F, DTU); 319 }); 320 321 // Test MergeBasicBlockIntoOnlyPred working under Lazy UpdateStrategy with 322 // PDT. 323 resetIRReplaceEntry(); 324 runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) { 325 PostDominatorTree PDT = PostDominatorTree(F); 326 DomTreeUpdater DTU(PDT, DomTreeUpdater::UpdateStrategy::Lazy); 327 Test(F, DTU); 328 }); 329 330 // Test MergeBasicBlockIntoOnlyPred working under Lazy UpdateStrategy with DT. 331 resetIRReplaceEntry(); 332 runWithDomTree(*M, "f", [&](Function &F, DominatorTree *DT) { 333 DomTreeUpdater DTU(*DT, DomTreeUpdater::UpdateStrategy::Lazy); 334 Test(F, DTU); 335 }); 336 } 337 338 TEST(Local, ConstantFoldTerminator) { 339 LLVMContext C; 340 341 std::unique_ptr<Module> M = parseIR(C, 342 R"( 343 define void @br_same_dest() { 344 entry: 345 br i1 false, label %bb0, label %bb0 346 bb0: 347 ret void 348 } 349 350 define void @br_different_dest() { 351 entry: 352 br i1 true, label %bb0, label %bb1 353 bb0: 354 br label %exit 355 bb1: 356 br label %exit 357 exit: 358 ret void 359 } 360 361 define void @switch_2_different_dest() { 362 entry: 363 switch i32 0, label %default [ i32 0, label %bb0 ] 364 default: 365 ret void 366 bb0: 367 ret void 368 } 369 define void @switch_2_different_dest_default() { 370 entry: 371 switch i32 1, label %default [ i32 0, label %bb0 ] 372 default: 373 ret void 374 bb0: 375 ret void 376 } 377 define void @switch_3_different_dest() { 378 entry: 379 switch i32 0, label %default [ i32 0, label %bb0 380 i32 1, label %bb1 ] 381 default: 382 ret void 383 bb0: 384 ret void 385 bb1: 386 ret void 387 } 388 389 define void @switch_variable_2_default_dest(i32 %arg) { 390 entry: 391 switch i32 %arg, label %default [ i32 0, label %default ] 392 default: 393 ret void 394 } 395 396 define void @switch_constant_2_default_dest() { 397 entry: 398 switch i32 1, label %default [ i32 0, label %default ] 399 default: 400 ret void 401 } 402 403 define void @switch_constant_3_repeated_dest() { 404 entry: 405 switch i32 0, label %default [ i32 0, label %bb0 406 i32 1, label %bb0 ] 407 bb0: 408 ret void 409 default: 410 ret void 411 } 412 413 define void @indirectbr() { 414 entry: 415 indirectbr i8* blockaddress(@indirectbr, %bb0), [label %bb0, label %bb1] 416 bb0: 417 ret void 418 bb1: 419 ret void 420 } 421 422 define void @indirectbr_repeated() { 423 entry: 424 indirectbr i8* blockaddress(@indirectbr_repeated, %bb0), [label %bb0, label %bb0] 425 bb0: 426 ret void 427 } 428 429 define void @indirectbr_unreachable() { 430 entry: 431 indirectbr i8* blockaddress(@indirectbr_unreachable, %bb0), [label %bb1] 432 bb0: 433 ret void 434 bb1: 435 ret void 436 } 437 )"); 438 439 auto CFAllTerminatorsEager = [&](Function &F, DominatorTree *DT) { 440 PostDominatorTree PDT = PostDominatorTree(F); 441 DomTreeUpdater DTU(*DT, PDT, DomTreeUpdater::UpdateStrategy::Eager); 442 for (Function::iterator I = F.begin(), E = F.end(); I != E;) { 443 BasicBlock *BB = &*I++; 444 ConstantFoldTerminator(BB, true, nullptr, &DTU); 445 } 446 447 EXPECT_TRUE(DTU.getDomTree().verify()); 448 EXPECT_TRUE(DTU.getPostDomTree().verify()); 449 }; 450 451 auto CFAllTerminatorsLazy = [&](Function &F, DominatorTree *DT) { 452 PostDominatorTree PDT = PostDominatorTree(F); 453 DomTreeUpdater DTU(*DT, PDT, DomTreeUpdater::UpdateStrategy::Lazy); 454 for (Function::iterator I = F.begin(), E = F.end(); I != E;) { 455 BasicBlock *BB = &*I++; 456 ConstantFoldTerminator(BB, true, nullptr, &DTU); 457 } 458 459 EXPECT_TRUE(DTU.getDomTree().verify()); 460 EXPECT_TRUE(DTU.getPostDomTree().verify()); 461 }; 462 463 // Test ConstantFoldTerminator under Eager UpdateStrategy. 464 runWithDomTree(*M, "br_same_dest", CFAllTerminatorsEager); 465 runWithDomTree(*M, "br_different_dest", CFAllTerminatorsEager); 466 runWithDomTree(*M, "switch_2_different_dest", CFAllTerminatorsEager); 467 runWithDomTree(*M, "switch_2_different_dest_default", CFAllTerminatorsEager); 468 runWithDomTree(*M, "switch_3_different_dest", CFAllTerminatorsEager); 469 runWithDomTree(*M, "switch_variable_2_default_dest", CFAllTerminatorsEager); 470 runWithDomTree(*M, "switch_constant_2_default_dest", CFAllTerminatorsEager); 471 runWithDomTree(*M, "switch_constant_3_repeated_dest", CFAllTerminatorsEager); 472 runWithDomTree(*M, "indirectbr", CFAllTerminatorsEager); 473 runWithDomTree(*M, "indirectbr_repeated", CFAllTerminatorsEager); 474 runWithDomTree(*M, "indirectbr_unreachable", CFAllTerminatorsEager); 475 476 // Test ConstantFoldTerminator under Lazy UpdateStrategy. 477 runWithDomTree(*M, "br_same_dest", CFAllTerminatorsLazy); 478 runWithDomTree(*M, "br_different_dest", CFAllTerminatorsLazy); 479 runWithDomTree(*M, "switch_2_different_dest", CFAllTerminatorsLazy); 480 runWithDomTree(*M, "switch_2_different_dest_default", CFAllTerminatorsLazy); 481 runWithDomTree(*M, "switch_3_different_dest", CFAllTerminatorsLazy); 482 runWithDomTree(*M, "switch_variable_2_default_dest", CFAllTerminatorsLazy); 483 runWithDomTree(*M, "switch_constant_2_default_dest", CFAllTerminatorsLazy); 484 runWithDomTree(*M, "switch_constant_3_repeated_dest", CFAllTerminatorsLazy); 485 runWithDomTree(*M, "indirectbr", CFAllTerminatorsLazy); 486 runWithDomTree(*M, "indirectbr_repeated", CFAllTerminatorsLazy); 487 runWithDomTree(*M, "indirectbr_unreachable", CFAllTerminatorsLazy); 488 } 489 490 struct SalvageDebugInfoTest : ::testing::Test { 491 LLVMContext C; 492 std::unique_ptr<Module> M; 493 Function *F = nullptr; 494 495 void SetUp() override { 496 M = parseIR(C, 497 R"( 498 define void @f() !dbg !8 { 499 entry: 500 %x = add i32 0, 1 501 %y = add i32 %x, 2 502 call void @llvm.dbg.value(metadata i32 %x, metadata !11, metadata !DIExpression()), !dbg !13 503 call void @llvm.dbg.value(metadata i32 %y, metadata !11, metadata !DIExpression()), !dbg !13 504 ret void, !dbg !14 505 } 506 declare void @llvm.dbg.value(metadata, metadata, metadata) 507 !llvm.dbg.cu = !{!0} 508 !llvm.module.flags = !{!3, !4} 509 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 6.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2) 510 !1 = !DIFile(filename: "t2.c", directory: "foo") 511 !2 = !{} 512 !3 = !{i32 2, !"Dwarf Version", i32 4} 513 !4 = !{i32 2, !"Debug Info Version", i32 3} 514 !8 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !9, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: false, unit: !0, retainedNodes: !2) 515 !9 = !DISubroutineType(types: !10) 516 !10 = !{null} 517 !11 = !DILocalVariable(name: "x", scope: !8, file: !1, line: 2, type: !12) 518 !12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) 519 !13 = !DILocation(line: 2, column: 7, scope: !8) 520 !14 = !DILocation(line: 3, column: 1, scope: !8) 521 )"); 522 523 auto *GV = M->getNamedValue("f"); 524 ASSERT_TRUE(GV); 525 F = dyn_cast<Function>(GV); 526 ASSERT_TRUE(F); 527 } 528 529 bool doesDebugValueDescribeX(const DbgValueInst &DI) { 530 if (DI.getNumVariableLocationOps() != 1u) 531 return false; 532 const auto &CI = *cast<ConstantInt>(DI.getValue(0)); 533 if (CI.isZero()) 534 return DI.getExpression()->getElements().equals( 535 {dwarf::DW_OP_plus_uconst, 1, dwarf::DW_OP_stack_value}); 536 else if (CI.isOneValue()) 537 return DI.getExpression()->getElements().empty(); 538 return false; 539 } 540 541 bool doesDebugValueDescribeY(const DbgValueInst &DI) { 542 if (DI.getNumVariableLocationOps() != 1u) 543 return false; 544 const auto &CI = *cast<ConstantInt>(DI.getVariableLocationOp(0)); 545 if (CI.isZero()) 546 return DI.getExpression()->getElements().equals( 547 {dwarf::DW_OP_plus_uconst, 3, dwarf::DW_OP_stack_value}); 548 else if (CI.isOneValue()) 549 return DI.getExpression()->getElements().equals( 550 {dwarf::DW_OP_plus_uconst, 2, dwarf::DW_OP_stack_value}); 551 return false; 552 } 553 554 void verifyDebugValuesAreSalvaged() { 555 // Check that the debug values for %x and %y are preserved. 556 bool FoundX = false; 557 bool FoundY = false; 558 for (const Instruction &I : F->front()) { 559 auto DI = dyn_cast<DbgValueInst>(&I); 560 if (!DI) { 561 // The function should only contain debug values and a terminator. 562 ASSERT_TRUE(I.isTerminator()); 563 continue; 564 } 565 EXPECT_EQ(DI->getVariable()->getName(), "x"); 566 FoundX |= doesDebugValueDescribeX(*DI); 567 FoundY |= doesDebugValueDescribeY(*DI); 568 } 569 ASSERT_TRUE(FoundX); 570 ASSERT_TRUE(FoundY); 571 } 572 }; 573 574 TEST_F(SalvageDebugInfoTest, RecursiveInstDeletion) { 575 Instruction *Inst = &F->front().front(); 576 Inst = Inst->getNextNode(); // Get %y = add ... 577 ASSERT_TRUE(Inst); 578 bool Deleted = RecursivelyDeleteTriviallyDeadInstructions(Inst); 579 ASSERT_TRUE(Deleted); 580 verifyDebugValuesAreSalvaged(); 581 } 582 583 TEST_F(SalvageDebugInfoTest, RecursiveBlockSimplification) { 584 BasicBlock *BB = &F->front(); 585 ASSERT_TRUE(BB); 586 bool Deleted = SimplifyInstructionsInBlock(BB); 587 ASSERT_TRUE(Deleted); 588 verifyDebugValuesAreSalvaged(); 589 } 590 591 TEST(Local, wouldInstructionBeTriviallyDead) { 592 LLVMContext Ctx; 593 std::unique_ptr<Module> M = parseIR(Ctx, 594 R"( 595 define dso_local void @fun() local_unnamed_addr #0 !dbg !9 { 596 entry: 597 call void @llvm.dbg.declare(metadata !{}, metadata !13, metadata !DIExpression()), !dbg !16 598 ret void, !dbg !16 599 } 600 601 declare void @llvm.dbg.declare(metadata, metadata, metadata) 602 603 !llvm.dbg.cu = !{!0} 604 !llvm.module.flags = !{!2, !3} 605 !llvm.ident = !{!8} 606 607 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 16.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None) 608 !1 = !DIFile(filename: "test.c", directory: "/") 609 !2 = !{i32 7, !"Dwarf Version", i32 5} 610 !3 = !{i32 2, !"Debug Info Version", i32 3} 611 !8 = !{!"clang version 16.0.0"} 612 !9 = distinct !DISubprogram(name: "fun", scope: !1, file: !1, line: 1, type: !10, scopeLine: 1, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !12) 613 !10 = !DISubroutineType(types: !11) 614 !11 = !{null} 615 !12 = !{!13} 616 !13 = !DILocalVariable(name: "a", scope: !9, file: !1, line: 1, type: !14) 617 !14 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) 618 !16 = !DILocation(line: 1, column: 21, scope: !9) 619 )"); 620 bool BrokenDebugInfo = true; 621 verifyModule(*M, &errs(), &BrokenDebugInfo); 622 ASSERT_FALSE(BrokenDebugInfo); 623 624 // Get the dbg.declare. 625 Function &F = *cast<Function>(M->getNamedValue("fun")); 626 Instruction *DbgDeclare = &F.front().front(); 627 ASSERT_TRUE(isa<DbgDeclareInst>(DbgDeclare)); 628 // Debug intrinsics with empty metadata arguments are not dead. 629 EXPECT_FALSE(wouldInstructionBeTriviallyDead(DbgDeclare)); 630 } 631 632 TEST(Local, ChangeToUnreachable) { 633 LLVMContext Ctx; 634 635 std::unique_ptr<Module> M = parseIR(Ctx, 636 R"( 637 define internal void @foo() !dbg !6 { 638 entry: 639 ret void, !dbg !8 640 } 641 642 !llvm.dbg.cu = !{!0} 643 !llvm.debugify = !{!3, !4} 644 !llvm.module.flags = !{!5} 645 646 !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2) 647 !1 = !DIFile(filename: "test.ll", directory: "/") 648 !2 = !{} 649 !3 = !{i32 1} 650 !4 = !{i32 0} 651 !5 = !{i32 2, !"Debug Info Version", i32 3} 652 !6 = distinct !DISubprogram(name: "foo", linkageName: "foo", scope: null, file: !1, line: 1, type: !7, isLocal: true, isDefinition: true, scopeLine: 1, isOptimized: true, unit: !0, retainedNodes: !2) 653 !7 = !DISubroutineType(types: !2) 654 !8 = !DILocation(line: 1, column: 1, scope: !6) 655 )"); 656 657 bool BrokenDebugInfo = true; 658 verifyModule(*M, &errs(), &BrokenDebugInfo); 659 ASSERT_FALSE(BrokenDebugInfo); 660 661 Function &F = *cast<Function>(M->getNamedValue("foo")); 662 663 BasicBlock &BB = F.front(); 664 Instruction &A = BB.front(); 665 DebugLoc DLA = A.getDebugLoc(); 666 667 ASSERT_TRUE(isa<ReturnInst>(&A)); 668 // One instruction should be affected. 669 EXPECT_EQ(changeToUnreachable(&A), 1U); 670 671 Instruction &B = BB.front(); 672 673 // There should be an uncreachable instruction. 674 ASSERT_TRUE(isa<UnreachableInst>(&B)); 675 676 DebugLoc DLB = B.getDebugLoc(); 677 EXPECT_EQ(DLA, DLB); 678 } 679 680 TEST(Local, FindDbgUsers) { 681 LLVMContext Ctx; 682 std::unique_ptr<Module> M = parseIR(Ctx, 683 R"( 684 define dso_local void @fun(ptr %a) #0 !dbg !11 { 685 entry: 686 call void @llvm.dbg.assign(metadata ptr %a, metadata !16, metadata !DIExpression(), metadata !15, metadata ptr %a, metadata !DIExpression()), !dbg !19 687 ret void 688 } 689 690 declare void @llvm.dbg.assign(metadata, metadata, metadata, metadata, metadata, metadata) 691 692 !llvm.dbg.cu = !{!0} 693 !llvm.module.flags = !{!2, !3, !9} 694 !llvm.ident = !{!10} 695 696 !0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 17.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None) 697 !1 = !DIFile(filename: "test.cpp", directory: "/") 698 !2 = !{i32 7, !"Dwarf Version", i32 5} 699 !3 = !{i32 2, !"Debug Info Version", i32 3} 700 !4 = !{i32 1, !"wchar_size", i32 4} 701 !9 = !{i32 7, !"debug-info-assignment-tracking", i1 true} 702 !10 = !{!"clang version 17.0.0"} 703 !11 = distinct !DISubprogram(name: "fun", linkageName: "fun", scope: !1, file: !1, line: 1, type: !12, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !14) 704 !12 = !DISubroutineType(types: !13) 705 !13 = !{null} 706 !14 = !{} 707 !15 = distinct !DIAssignID() 708 !16 = !DILocalVariable(name: "x", scope: !11, file: !1, line: 2, type: !17) 709 !17 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !18, size: 64) 710 !18 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) 711 !19 = !DILocation(line: 0, scope: !11) 712 )"); 713 714 bool BrokenDebugInfo = true; 715 verifyModule(*M, &errs(), &BrokenDebugInfo); 716 ASSERT_FALSE(BrokenDebugInfo); 717 718 Function &Fun = *cast<Function>(M->getNamedValue("fun")); 719 Value *Arg = Fun.getArg(0); 720 721 SmallVector<DbgVariableIntrinsic *> Users; 722 // Arg (%a) is used twice by a single dbg.assign. Check findDbgUsers returns 723 // only 1 pointer to it rather than 2. 724 findDbgUsers(Users, Arg); 725 EXPECT_EQ(Users.size(), 1u); 726 727 SmallVector<DbgValueInst *> Vals; 728 // Arg (%a) is used twice by a single dbg.assign. Check findDbgValues returns 729 // only 1 pointer to it rather than 2. 730 findDbgValues(Vals, Arg); 731 EXPECT_EQ(Vals.size(), 1u); 732 } 733 734 TEST(Local, FindDbgRecords) { 735 // DbgRecord copy of the FindDbgUsers test above. 736 LLVMContext Ctx; 737 std::unique_ptr<Module> M = parseIR(Ctx, 738 R"( 739 define dso_local void @fun(ptr %a) #0 !dbg !11 { 740 entry: 741 call void @llvm.dbg.assign(metadata ptr %a, metadata !16, metadata !DIExpression(), metadata !15, metadata ptr %a, metadata !DIExpression()), !dbg !19 742 ret void 743 } 744 745 !llvm.dbg.cu = !{!0} 746 !llvm.module.flags = !{!2, !3, !9} 747 !llvm.ident = !{!10} 748 749 !0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 17.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None) 750 !1 = !DIFile(filename: "test.cpp", directory: "/") 751 !2 = !{i32 7, !"Dwarf Version", i32 5} 752 !3 = !{i32 2, !"Debug Info Version", i32 3} 753 !4 = !{i32 1, !"wchar_size", i32 4} 754 !9 = !{i32 7, !"debug-info-assignment-tracking", i1 true} 755 !10 = !{!"clang version 17.0.0"} 756 !11 = distinct !DISubprogram(name: "fun", linkageName: "fun", scope: !1, file: !1, line: 1, type: !12, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !14) 757 !12 = !DISubroutineType(types: !13) 758 !13 = !{null} 759 !14 = !{} 760 !15 = distinct !DIAssignID() 761 !16 = !DILocalVariable(name: "x", scope: !11, file: !1, line: 2, type: !17) 762 !17 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !18, size: 64) 763 !18 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) 764 !19 = !DILocation(line: 0, scope: !11) 765 )"); 766 767 bool BrokenDebugInfo = true; 768 verifyModule(*M, &errs(), &BrokenDebugInfo); 769 ASSERT_FALSE(BrokenDebugInfo); 770 bool NewDbgInfoFormat = UseNewDbgInfoFormat; 771 UseNewDbgInfoFormat = true; 772 M->convertToNewDbgValues(); 773 774 Function &Fun = *cast<Function>(M->getNamedValue("fun")); 775 Value *Arg = Fun.getArg(0); 776 777 SmallVector<DbgVariableIntrinsic *> Users; 778 SmallVector<DbgVariableRecord *> Records; 779 // Arg (%a) is used twice by a single dbg_assign. Check findDbgUsers returns 780 // only 1 pointer to it rather than 2. 781 findDbgUsers(Users, Arg, &Records); 782 EXPECT_EQ(Users.size(), 0u); 783 EXPECT_EQ(Records.size(), 1u); 784 785 SmallVector<DbgValueInst *> Vals; 786 Records.clear(); 787 // Arg (%a) is used twice by a single dbg_assign. Check findDbgValues returns 788 // only 1 pointer to it rather than 2. 789 findDbgValues(Vals, Arg, &Records); 790 EXPECT_EQ(Vals.size(), 0u); 791 EXPECT_EQ(Records.size(), 1u); 792 UseNewDbgInfoFormat = NewDbgInfoFormat; 793 } 794 795 TEST(Local, ReplaceAllDbgUsesWith) { 796 using namespace llvm::dwarf; 797 798 LLVMContext Ctx; 799 800 // Note: The datalayout simulates Darwin/x86_64. 801 std::unique_ptr<Module> M = parseIR(Ctx, 802 R"( 803 target datalayout = "e-m:o-i63:64-f80:128-n8:16:32:64-S128" 804 805 declare i32 @escape(i32) 806 807 define void @f() !dbg !6 { 808 entry: 809 %a = add i32 0, 1, !dbg !15 810 call void @llvm.dbg.value(metadata i32 %a, metadata !9, metadata !DIExpression()), !dbg !15 811 812 %b = add i64 0, 1, !dbg !16 813 call void @llvm.dbg.value(metadata i64 %b, metadata !11, metadata !DIExpression()), !dbg !16 814 call void @llvm.dbg.value(metadata i64 %b, metadata !11, metadata !DIExpression(DW_OP_lit0, DW_OP_mul)), !dbg !16 815 call void @llvm.dbg.value(metadata i64 %b, metadata !11, metadata !DIExpression(DW_OP_lit0, DW_OP_mul, DW_OP_stack_value)), !dbg !16 816 call void @llvm.dbg.value(metadata i64 %b, metadata !11, metadata !DIExpression(DW_OP_LLVM_fragment, 0, 8)), !dbg !16 817 call void @llvm.dbg.value(metadata i64 %b, metadata !11, metadata !DIExpression(DW_OP_lit0, DW_OP_mul, DW_OP_LLVM_fragment, 0, 8)), !dbg !16 818 call void @llvm.dbg.value(metadata i64 %b, metadata !11, metadata !DIExpression(DW_OP_lit0, DW_OP_mul, DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 8)), !dbg !16 819 820 %c = inttoptr i64 0 to i64*, !dbg !17 821 call void @llvm.dbg.declare(metadata i64* %c, metadata !13, metadata !DIExpression()), !dbg !17 822 823 %d = inttoptr i64 0 to i32*, !dbg !18 824 call void @llvm.dbg.declare(metadata i32* %d, metadata !20, metadata !DIExpression()), !dbg !18 825 826 %e = add <2 x i16> zeroinitializer, zeroinitializer 827 call void @llvm.dbg.value(metadata <2 x i16> %e, metadata !14, metadata !DIExpression()), !dbg !18 828 829 %f = call i32 @escape(i32 0) 830 call void @llvm.dbg.value(metadata i32 %f, metadata !9, metadata !DIExpression()), !dbg !15 831 832 %barrier = call i32 @escape(i32 0) 833 834 %g = call i32 @escape(i32 %f) 835 call void @llvm.dbg.value(metadata i32 %g, metadata !9, metadata !DIExpression()), !dbg !15 836 837 ret void, !dbg !19 838 } 839 840 declare void @llvm.dbg.declare(metadata, metadata, metadata) 841 declare void @llvm.dbg.value(metadata, metadata, metadata) 842 843 !llvm.dbg.cu = !{!0} 844 !llvm.module.flags = !{!5} 845 846 !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2) 847 !1 = !DIFile(filename: "/Users/vsk/Desktop/foo.ll", directory: "/") 848 !2 = !{} 849 !5 = !{i32 2, !"Debug Info Version", i32 3} 850 !6 = distinct !DISubprogram(name: "f", linkageName: "f", scope: null, file: !1, line: 1, type: !7, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: true, unit: !0, retainedNodes: !8) 851 !7 = !DISubroutineType(types: !2) 852 !8 = !{!9, !11, !13, !14} 853 !9 = !DILocalVariable(name: "1", scope: !6, file: !1, line: 1, type: !10) 854 !10 = !DIBasicType(name: "ty32", size: 32, encoding: DW_ATE_signed) 855 !11 = !DILocalVariable(name: "2", scope: !6, file: !1, line: 2, type: !12) 856 !12 = !DIBasicType(name: "ty64", size: 64, encoding: DW_ATE_signed) 857 !13 = !DILocalVariable(name: "3", scope: !6, file: !1, line: 3, type: !12) 858 !14 = !DILocalVariable(name: "4", scope: !6, file: !1, line: 4, type: !10) 859 !15 = !DILocation(line: 1, column: 1, scope: !6) 860 !16 = !DILocation(line: 2, column: 1, scope: !6) 861 !17 = !DILocation(line: 3, column: 1, scope: !6) 862 !18 = !DILocation(line: 4, column: 1, scope: !6) 863 !19 = !DILocation(line: 5, column: 1, scope: !6) 864 !20 = !DILocalVariable(name: "5", scope: !6, file: !1, line: 5, type: !10) 865 )"); 866 867 bool BrokenDebugInfo = true; 868 verifyModule(*M, &errs(), &BrokenDebugInfo); 869 ASSERT_FALSE(BrokenDebugInfo); 870 871 Function &F = *cast<Function>(M->getNamedValue("f")); 872 DominatorTree DT{F}; 873 874 BasicBlock &BB = F.front(); 875 Instruction &A = BB.front(); 876 Instruction &B = *A.getNextNonDebugInstruction(); 877 Instruction &C = *B.getNextNonDebugInstruction(); 878 Instruction &D = *C.getNextNonDebugInstruction(); 879 Instruction &E = *D.getNextNonDebugInstruction(); 880 Instruction &F_ = *E.getNextNonDebugInstruction(); 881 Instruction &Barrier = *F_.getNextNonDebugInstruction(); 882 Instruction &G = *Barrier.getNextNonDebugInstruction(); 883 884 // Simulate i32 <-> i64* conversion. Expect no updates: the datalayout says 885 // pointers are 64 bits, so the conversion would be lossy. 886 EXPECT_FALSE(replaceAllDbgUsesWith(A, C, C, DT)); 887 EXPECT_FALSE(replaceAllDbgUsesWith(C, A, A, DT)); 888 889 // Simulate i32 <-> <2 x i16> conversion. This is unsupported. 890 EXPECT_FALSE(replaceAllDbgUsesWith(E, A, A, DT)); 891 EXPECT_FALSE(replaceAllDbgUsesWith(A, E, E, DT)); 892 893 // Simulate i32* <-> i64* conversion. 894 EXPECT_TRUE(replaceAllDbgUsesWith(D, C, C, DT)); 895 896 SmallVector<DbgVariableIntrinsic *, 2> CDbgVals; 897 findDbgUsers(CDbgVals, &C); 898 EXPECT_EQ(2U, CDbgVals.size()); 899 EXPECT_TRUE(all_of(CDbgVals, [](DbgVariableIntrinsic *DII) { 900 return isa<DbgDeclareInst>(DII); 901 })); 902 903 EXPECT_TRUE(replaceAllDbgUsesWith(C, D, D, DT)); 904 905 SmallVector<DbgVariableIntrinsic *, 2> DDbgVals; 906 findDbgUsers(DDbgVals, &D); 907 EXPECT_EQ(2U, DDbgVals.size()); 908 EXPECT_TRUE(all_of(DDbgVals, [](DbgVariableIntrinsic *DII) { 909 return isa<DbgDeclareInst>(DII); 910 })); 911 912 // Introduce a use-before-def. Check that the dbg.value for %a is salvaged. 913 EXPECT_TRUE(replaceAllDbgUsesWith(A, F_, F_, DT)); 914 915 auto *ADbgVal = cast<DbgValueInst>(A.getNextNode()); 916 EXPECT_EQ(ADbgVal->getNumVariableLocationOps(), 1u); 917 EXPECT_EQ(ConstantInt::get(A.getType(), 0), ADbgVal->getVariableLocationOp(0)); 918 919 // Introduce a use-before-def. Check that the dbg.values for %f become undef. 920 EXPECT_TRUE(replaceAllDbgUsesWith(F_, G, G, DT)); 921 922 auto *FDbgVal = cast<DbgValueInst>(F_.getNextNode()); 923 EXPECT_EQ(FDbgVal->getNumVariableLocationOps(), 1u); 924 EXPECT_TRUE(FDbgVal->isKillLocation()); 925 926 SmallVector<DbgValueInst *, 1> FDbgVals; 927 findDbgValues(FDbgVals, &F_); 928 EXPECT_EQ(0U, FDbgVals.size()); 929 930 // Simulate i32 -> i64 conversion to test sign-extension. Here are some 931 // interesting cases to handle: 932 // 1) debug user has empty DIExpression 933 // 2) debug user has non-empty, non-stack-value'd DIExpression 934 // 3) debug user has non-empty, stack-value'd DIExpression 935 // 4-6) like (1-3), but with a fragment 936 EXPECT_TRUE(replaceAllDbgUsesWith(B, A, A, DT)); 937 938 SmallVector<DbgValueInst *, 8> ADbgVals; 939 findDbgValues(ADbgVals, &A); 940 EXPECT_EQ(6U, ADbgVals.size()); 941 942 // Check that %a has a dbg.value with a DIExpression matching \p Ops. 943 auto hasADbgVal = [&](ArrayRef<uint64_t> Ops) { 944 return any_of(ADbgVals, [&](DbgValueInst *DVI) { 945 assert(DVI->getVariable()->getName() == "2"); 946 return DVI->getExpression()->getElements() == Ops; 947 }); 948 }; 949 950 // Case 1: The original expr is empty, so no deref is needed. 951 EXPECT_TRUE(hasADbgVal({DW_OP_LLVM_convert, 32, DW_ATE_signed, 952 DW_OP_LLVM_convert, 64, DW_ATE_signed, 953 DW_OP_stack_value})); 954 955 // Case 2: Perform an address calculation with the original expr, deref it, 956 // then sign-extend the result. 957 EXPECT_TRUE(hasADbgVal({DW_OP_lit0, DW_OP_mul, DW_OP_deref, 958 DW_OP_LLVM_convert, 32, DW_ATE_signed, 959 DW_OP_LLVM_convert, 64, DW_ATE_signed, 960 DW_OP_stack_value})); 961 962 // Case 3: Insert the sign-extension logic before the DW_OP_stack_value. 963 EXPECT_TRUE(hasADbgVal({DW_OP_lit0, DW_OP_mul, DW_OP_LLVM_convert, 32, 964 DW_ATE_signed, DW_OP_LLVM_convert, 64, DW_ATE_signed, 965 DW_OP_stack_value})); 966 967 // Cases 4-6: Just like cases 1-3, but preserve the fragment at the end. 968 EXPECT_TRUE(hasADbgVal({DW_OP_LLVM_convert, 32, DW_ATE_signed, 969 DW_OP_LLVM_convert, 64, DW_ATE_signed, 970 DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 8})); 971 972 EXPECT_TRUE(hasADbgVal({DW_OP_lit0, DW_OP_mul, DW_OP_deref, 973 DW_OP_LLVM_convert, 32, DW_ATE_signed, 974 DW_OP_LLVM_convert, 64, DW_ATE_signed, 975 DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 8})); 976 977 EXPECT_TRUE(hasADbgVal({DW_OP_lit0, DW_OP_mul, DW_OP_LLVM_convert, 32, 978 DW_ATE_signed, DW_OP_LLVM_convert, 64, DW_ATE_signed, 979 DW_OP_stack_value, DW_OP_LLVM_fragment, 0, 8})); 980 981 verifyModule(*M, &errs(), &BrokenDebugInfo); 982 ASSERT_FALSE(BrokenDebugInfo); 983 } 984 985 TEST(Local, RemoveUnreachableBlocks) { 986 LLVMContext C; 987 988 std::unique_ptr<Module> M = parseIR(C, 989 R"( 990 define void @br_simple() { 991 entry: 992 br label %bb0 993 bb0: 994 ret void 995 bb1: 996 ret void 997 } 998 999 define void @br_self_loop() { 1000 entry: 1001 br label %bb0 1002 bb0: 1003 br i1 true, label %bb1, label %bb0 1004 bb1: 1005 br i1 true, label %bb0, label %bb2 1006 bb2: 1007 br label %bb2 1008 } 1009 1010 define void @br_constant() { 1011 entry: 1012 br label %bb0 1013 bb0: 1014 br i1 true, label %bb1, label %bb2 1015 bb1: 1016 br i1 true, label %bb0, label %bb2 1017 bb2: 1018 br label %bb2 1019 } 1020 1021 define void @br_loop() { 1022 entry: 1023 br label %bb0 1024 bb0: 1025 br label %bb0 1026 bb1: 1027 br label %bb2 1028 bb2: 1029 br label %bb1 1030 } 1031 1032 declare i32 @__gxx_personality_v0(...) 1033 1034 define void @invoke_terminator() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) { 1035 entry: 1036 br i1 undef, label %invoke.block, label %exit 1037 1038 invoke.block: 1039 %cond = invoke zeroext i1 @invokable() 1040 to label %continue.block unwind label %lpad.block 1041 1042 continue.block: 1043 br i1 %cond, label %if.then, label %if.end 1044 1045 if.then: 1046 unreachable 1047 1048 if.end: 1049 unreachable 1050 1051 lpad.block: 1052 %lp = landingpad { i8*, i32 } 1053 catch i8* null 1054 br label %exit 1055 1056 exit: 1057 ret void 1058 } 1059 1060 declare i1 @invokable() 1061 )"); 1062 1063 auto runEager = [&](Function &F, DominatorTree *DT) { 1064 PostDominatorTree PDT = PostDominatorTree(F); 1065 DomTreeUpdater DTU(*DT, PDT, DomTreeUpdater::UpdateStrategy::Eager); 1066 removeUnreachableBlocks(F, &DTU); 1067 EXPECT_TRUE(DTU.getDomTree().verify()); 1068 EXPECT_TRUE(DTU.getPostDomTree().verify()); 1069 }; 1070 1071 auto runLazy = [&](Function &F, DominatorTree *DT) { 1072 PostDominatorTree PDT = PostDominatorTree(F); 1073 DomTreeUpdater DTU(*DT, PDT, DomTreeUpdater::UpdateStrategy::Lazy); 1074 removeUnreachableBlocks(F, &DTU); 1075 EXPECT_TRUE(DTU.getDomTree().verify()); 1076 EXPECT_TRUE(DTU.getPostDomTree().verify()); 1077 }; 1078 1079 // Test removeUnreachableBlocks under Eager UpdateStrategy. 1080 runWithDomTree(*M, "br_simple", runEager); 1081 runWithDomTree(*M, "br_self_loop", runEager); 1082 runWithDomTree(*M, "br_constant", runEager); 1083 runWithDomTree(*M, "br_loop", runEager); 1084 runWithDomTree(*M, "invoke_terminator", runEager); 1085 1086 // Test removeUnreachableBlocks under Lazy UpdateStrategy. 1087 runWithDomTree(*M, "br_simple", runLazy); 1088 runWithDomTree(*M, "br_self_loop", runLazy); 1089 runWithDomTree(*M, "br_constant", runLazy); 1090 runWithDomTree(*M, "br_loop", runLazy); 1091 runWithDomTree(*M, "invoke_terminator", runLazy); 1092 1093 M = parseIR(C, 1094 R"( 1095 define void @f() { 1096 entry: 1097 ret void 1098 bb0: 1099 ret void 1100 } 1101 )"); 1102 1103 auto checkRUBlocksRetVal = [&](Function &F, DominatorTree *DT) { 1104 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy); 1105 EXPECT_TRUE(removeUnreachableBlocks(F, &DTU)); 1106 EXPECT_FALSE(removeUnreachableBlocks(F, &DTU)); 1107 EXPECT_TRUE(DTU.getDomTree().verify()); 1108 }; 1109 1110 runWithDomTree(*M, "f", checkRUBlocksRetVal); 1111 } 1112 1113 TEST(Local, SimplifyCFGWithNullAC) { 1114 LLVMContext Ctx; 1115 1116 std::unique_ptr<Module> M = parseIR(Ctx, R"( 1117 declare void @true_path() 1118 declare void @false_path() 1119 declare void @llvm.assume(i1 %cond); 1120 1121 define i32 @foo(i1, i32) { 1122 entry: 1123 %cmp = icmp sgt i32 %1, 0 1124 br i1 %cmp, label %if.bb1, label %then.bb1 1125 if.bb1: 1126 call void @true_path() 1127 br label %test.bb 1128 then.bb1: 1129 call void @false_path() 1130 br label %test.bb 1131 test.bb: 1132 %phi = phi i1 [1, %if.bb1], [%0, %then.bb1] 1133 call void @llvm.assume(i1 %0) 1134 br i1 %phi, label %if.bb2, label %then.bb2 1135 if.bb2: 1136 ret i32 %1 1137 then.bb2: 1138 ret i32 0 1139 } 1140 )"); 1141 1142 Function &F = *cast<Function>(M->getNamedValue("foo")); 1143 TargetTransformInfo TTI(M->getDataLayout()); 1144 1145 SimplifyCFGOptions Options{}; 1146 Options.setAssumptionCache(nullptr); 1147 1148 // Obtain BasicBlock of interest to this test, %test.bb. 1149 BasicBlock *TestBB = nullptr; 1150 for (BasicBlock &BB : F) { 1151 if (BB.getName() == "test.bb") { 1152 TestBB = &BB; 1153 break; 1154 } 1155 } 1156 ASSERT_TRUE(TestBB); 1157 1158 DominatorTree DT(F); 1159 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager); 1160 1161 // %test.bb is expected to be simplified by FoldCondBranchOnPHI. 1162 EXPECT_TRUE(simplifyCFG(TestBB, TTI, 1163 RequireAndPreserveDomTree ? &DTU : nullptr, Options)); 1164 } 1165 1166 TEST(Local, CanReplaceOperandWithVariable) { 1167 LLVMContext Ctx; 1168 Module M("test_module", Ctx); 1169 IRBuilder<> B(Ctx); 1170 1171 FunctionType *FnType = 1172 FunctionType::get(Type::getVoidTy(Ctx), {}, false); 1173 1174 FunctionType *VarArgFnType = 1175 FunctionType::get(Type::getVoidTy(Ctx), {B.getInt32Ty()}, true); 1176 1177 Function *TestBody = Function::Create(FnType, GlobalValue::ExternalLinkage, 1178 0, "", &M); 1179 1180 BasicBlock *BB0 = BasicBlock::Create(Ctx, "", TestBody); 1181 B.SetInsertPoint(BB0); 1182 1183 FunctionCallee Intrin = M.getOrInsertFunction("llvm.foo", FnType); 1184 FunctionCallee Func = M.getOrInsertFunction("foo", FnType); 1185 FunctionCallee VarArgFunc 1186 = M.getOrInsertFunction("foo.vararg", VarArgFnType); 1187 FunctionCallee VarArgIntrin 1188 = M.getOrInsertFunction("llvm.foo.vararg", VarArgFnType); 1189 1190 auto *CallToIntrin = B.CreateCall(Intrin); 1191 auto *CallToFunc = B.CreateCall(Func); 1192 1193 // Test if it's valid to replace the callee operand. 1194 EXPECT_FALSE(canReplaceOperandWithVariable(CallToIntrin, 0)); 1195 EXPECT_TRUE(canReplaceOperandWithVariable(CallToFunc, 0)); 1196 1197 // That it's invalid to replace an argument in the variadic argument list for 1198 // an intrinsic, but OK for a normal function. 1199 auto *CallToVarArgFunc = B.CreateCall( 1200 VarArgFunc, {B.getInt32(0), B.getInt32(1), B.getInt32(2)}); 1201 EXPECT_TRUE(canReplaceOperandWithVariable(CallToVarArgFunc, 0)); 1202 EXPECT_TRUE(canReplaceOperandWithVariable(CallToVarArgFunc, 1)); 1203 EXPECT_TRUE(canReplaceOperandWithVariable(CallToVarArgFunc, 2)); 1204 EXPECT_TRUE(canReplaceOperandWithVariable(CallToVarArgFunc, 3)); 1205 1206 auto *CallToVarArgIntrin = B.CreateCall( 1207 VarArgIntrin, {B.getInt32(0), B.getInt32(1), B.getInt32(2)}); 1208 EXPECT_TRUE(canReplaceOperandWithVariable(CallToVarArgIntrin, 0)); 1209 EXPECT_FALSE(canReplaceOperandWithVariable(CallToVarArgIntrin, 1)); 1210 EXPECT_FALSE(canReplaceOperandWithVariable(CallToVarArgIntrin, 2)); 1211 EXPECT_FALSE(canReplaceOperandWithVariable(CallToVarArgIntrin, 3)); 1212 1213 // Test that it's invalid to replace gcroot operands, even though it can't use 1214 // immarg. 1215 Type *PtrPtr = B.getPtrTy(0); 1216 Value *Alloca = B.CreateAlloca(PtrPtr, (unsigned)0); 1217 CallInst *GCRoot = B.CreateIntrinsic(Intrinsic::gcroot, {}, 1218 {Alloca, Constant::getNullValue(PtrPtr)}); 1219 EXPECT_TRUE(canReplaceOperandWithVariable(GCRoot, 0)); // Alloca 1220 EXPECT_FALSE(canReplaceOperandWithVariable(GCRoot, 1)); 1221 EXPECT_FALSE(canReplaceOperandWithVariable(GCRoot, 2)); 1222 1223 BB0->dropAllReferences(); 1224 } 1225 1226 TEST(Local, ExpressionForConstant) { 1227 LLVMContext Context; 1228 Module M("test_module", Context); 1229 DIBuilder DIB(M); 1230 DIExpression *Expr = nullptr; 1231 1232 auto createExpression = [&](Constant *C, Type *Ty) -> DIExpression * { 1233 EXPECT_NE(C, nullptr); 1234 EXPECT_NE(Ty, nullptr); 1235 EXPECT_EQ(C->getType(), Ty); 1236 std::unique_ptr<GlobalVariable> GV = std::make_unique<GlobalVariable>( 1237 Ty, false, GlobalValue::ExternalLinkage, C, "GV"); 1238 EXPECT_NE(GV, nullptr); 1239 1240 DIExpression *Expr = getExpressionForConstant(DIB, *GV->getInitializer(), 1241 *GV->getValueType()); 1242 if (Expr) { 1243 EXPECT_EQ(Expr->getNumElements(), 3u); 1244 EXPECT_EQ(Expr->getElement(0), dwarf::DW_OP_constu); 1245 EXPECT_EQ(Expr->getElement(2), dwarf::DW_OP_stack_value); 1246 } 1247 return Expr; 1248 }; 1249 1250 // Integer. 1251 IntegerType *Int1Ty = Type::getInt1Ty(Context); 1252 Expr = createExpression(ConstantInt::getTrue(Context), Int1Ty); 1253 EXPECT_NE(Expr, nullptr); 1254 EXPECT_EQ(Expr->getElement(1), 18446744073709551615U); 1255 1256 Expr = createExpression(ConstantInt::getFalse(Context), Int1Ty); 1257 EXPECT_NE(Expr, nullptr); 1258 EXPECT_EQ(Expr->getElement(1), 0U); 1259 1260 IntegerType *Int8Ty = Type::getInt8Ty(Context); 1261 Expr = createExpression(ConstantInt::get(Int8Ty, 100), Int8Ty); 1262 EXPECT_NE(Expr, nullptr); 1263 EXPECT_EQ(Expr->getElement(1), 100U); 1264 1265 IntegerType *Int16Ty = Type::getInt16Ty(Context); 1266 Expr = createExpression(ConstantInt::getSigned(Int16Ty, -50), Int16Ty); 1267 EXPECT_NE(Expr, nullptr); 1268 EXPECT_EQ(Expr->getElement(1), -50ULL); 1269 1270 IntegerType *Int32Ty = Type::getInt32Ty(Context); 1271 Expr = createExpression(ConstantInt::get(Int32Ty, 0x7FFFFFFF), Int32Ty); 1272 EXPECT_NE(Expr, nullptr); 1273 EXPECT_EQ(Expr->getElement(1), 0x7FFFFFFFU); 1274 1275 IntegerType *Int64Ty = Type::getInt64Ty(Context); 1276 Expr = 1277 createExpression(ConstantInt::get(Int64Ty, 0x7FFFFFFFFFFFFFFF), Int64Ty); 1278 EXPECT_NE(Expr, nullptr); 1279 EXPECT_EQ(Expr->getElement(1), 0x7FFFFFFFFFFFFFFFU); 1280 1281 IntegerType *Int128Ty = Type::getInt128Ty(Context); 1282 Expr = createExpression(ConstantInt::get(Int128Ty, 0x7FFFFFFFFFFFFFFF), 1283 Int128Ty); 1284 EXPECT_NE(Expr, nullptr); 1285 EXPECT_EQ(Expr->getElement(1), 0x7FFFFFFFFFFFFFFFU); 1286 1287 GlobalVariable *String = 1288 IRBuilder<>(Context).CreateGlobalString("hello", "hello", 0, &M); 1289 Expr = createExpression(ConstantExpr::getPtrToInt(String, Int32Ty), Int32Ty); 1290 EXPECT_EQ(Expr, nullptr); 1291 1292 // Float. 1293 Type *FloatTy = Type::getFloatTy(Context); 1294 Expr = createExpression(ConstantFP::get(FloatTy, 5.55), FloatTy); 1295 EXPECT_NE(Expr, nullptr); 1296 EXPECT_EQ(Expr->getElement(1), 1085381018U); 1297 1298 // Double. 1299 Type *DoubleTy = Type::getDoubleTy(Context); 1300 Expr = createExpression(ConstantFP::get(DoubleTy, -5.55), DoubleTy); 1301 EXPECT_NE(Expr, nullptr); 1302 EXPECT_EQ(Expr->getElement(1), 13841306799765140275U); 1303 1304 // Half. 1305 Type *HalfTy = Type::getHalfTy(Context); 1306 Expr = createExpression(ConstantFP::get(HalfTy, 5.55), HalfTy); 1307 EXPECT_NE(Expr, nullptr); 1308 EXPECT_EQ(Expr->getElement(1), 17805U); 1309 1310 // BFloat. 1311 Type *BFloatTy = Type::getBFloatTy(Context); 1312 Expr = createExpression(ConstantFP::get(BFloatTy, -5.55), BFloatTy); 1313 EXPECT_NE(Expr, nullptr); 1314 EXPECT_EQ(Expr->getElement(1), 49330U); 1315 1316 // Pointer. 1317 PointerType *PtrTy = PointerType::get(Context, 0); 1318 Expr = createExpression(ConstantPointerNull::get(PtrTy), PtrTy); 1319 EXPECT_NE(Expr, nullptr); 1320 EXPECT_EQ(Expr->getElement(1), 0U); 1321 1322 ConstantInt *K1 = ConstantInt::get(Type::getInt32Ty(Context), 1234); 1323 Expr = createExpression(ConstantExpr::getIntToPtr(K1, PtrTy), PtrTy); 1324 EXPECT_NE(Expr, nullptr); 1325 EXPECT_EQ(Expr->getElement(1), 1234U); 1326 1327 ConstantInt *K2 = ConstantInt::get(Type::getInt64Ty(Context), 5678); 1328 Expr = createExpression(ConstantExpr::getIntToPtr(K2, PtrTy), PtrTy); 1329 EXPECT_NE(Expr, nullptr); 1330 EXPECT_EQ(Expr->getElement(1), 5678U); 1331 1332 Type *FP128Ty = Type::getFP128Ty(Context); 1333 Expr = createExpression(ConstantFP::get(FP128Ty, 32), FP128Ty); 1334 EXPECT_EQ(Expr, nullptr); 1335 1336 Type *X86_FP80Ty = Type::getX86_FP80Ty(Context); 1337 Expr = createExpression(ConstantFP::get(X86_FP80Ty, 32), X86_FP80Ty); 1338 EXPECT_EQ(Expr, nullptr); 1339 1340 Type *PPC_FP128Ty = Type::getPPC_FP128Ty(Context); 1341 Expr = createExpression(ConstantFP::get(PPC_FP128Ty, 32), PPC_FP128Ty); 1342 EXPECT_EQ(Expr, nullptr); 1343 } 1344 1345 TEST(Local, ReplaceDbgVariableRecord) { 1346 LLVMContext C; 1347 1348 // Test that RAUW also replaces the operands of DbgVariableRecord objects, 1349 // i.e. non-instruction stored debugging information. 1350 std::unique_ptr<Module> M = parseIR(C, 1351 R"( 1352 declare void @llvm.dbg.value(metadata, metadata, metadata) 1353 define void @f(i32 %a) !dbg !8 { 1354 entry: 1355 %foo = add i32 %a, 1, !dbg !13 1356 %bar = add i32 %foo, 0, !dbg !13 1357 call void @llvm.dbg.value(metadata i32 %bar, metadata !11, metadata !DIExpression()), !dbg !13 1358 ret void, !dbg !14 1359 } 1360 !llvm.dbg.cu = !{!0} 1361 !llvm.module.flags = !{!3, !4} 1362 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 6.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2) 1363 !1 = !DIFile(filename: "t2.c", directory: "foo") 1364 !2 = !{} 1365 !3 = !{i32 2, !"Dwarf Version", i32 4} 1366 !4 = !{i32 2, !"Debug Info Version", i32 3} 1367 !8 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !9, isLocal: false, isDefinition: true, scopeLine: 1, isOptimized: false, unit: !0, retainedNodes: !2) 1368 !9 = !DISubroutineType(types: !10) 1369 !10 = !{null} 1370 !11 = !DILocalVariable(name: "x", scope: !8, file: !1, line: 2, type: !12) 1371 !12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) 1372 !13 = !DILocation(line: 2, column: 7, scope: !8) 1373 !14 = !DILocation(line: 3, column: 1, scope: !8) 1374 )"); 1375 auto *GV = M->getNamedValue("f"); 1376 ASSERT_TRUE(GV); 1377 auto *F = dyn_cast<Function>(GV); 1378 ASSERT_TRUE(F); 1379 BasicBlock::iterator It = F->front().begin(); 1380 Instruction *FooInst = &*It; 1381 It = std::next(It); 1382 Instruction *BarInst = &*It; 1383 It = std::next(It); 1384 DbgValueInst *DVI = dyn_cast<DbgValueInst>(It); 1385 ASSERT_TRUE(DVI); 1386 It = std::next(It); 1387 Instruction *RetInst = &*It; 1388 1389 // Convert DVI into a DbgVariableRecord. 1390 RetInst->DebugMarker = new DbgMarker(); 1391 RetInst->DebugMarker->MarkedInstr = RetInst; 1392 DbgVariableRecord *DVR = new DbgVariableRecord(DVI); 1393 RetInst->DebugMarker->insertDbgRecord(DVR, false); 1394 // ... and erase the dbg.value. 1395 DVI->eraseFromParent(); 1396 1397 // DVR should originally refer to %bar, 1398 EXPECT_EQ(DVR->getVariableLocationOp(0), BarInst); 1399 1400 // Now try to replace the computation of %bar with %foo -- this should cause 1401 // the DbgVariableRecord's to have it's operand updated beneath it. 1402 BarInst->replaceAllUsesWith(FooInst); 1403 // Check DVR now points at %foo. 1404 EXPECT_EQ(DVR->getVariableLocationOp(0), FooInst); 1405 1406 // Teardown. 1407 RetInst->DebugMarker->eraseFromParent(); 1408 } 1409