1 //===- CrashDebugger.cpp - Debug compilation crashes ----------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the bugpoint internals that narrow down compilation crashes 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "BugDriver.h" 15 #include "ListReducer.h" 16 #include "ToolRunner.h" 17 #include "llvm/ADT/SmallPtrSet.h" 18 #include "llvm/IR/CFG.h" 19 #include "llvm/IR/Constants.h" 20 #include "llvm/IR/DerivedTypes.h" 21 #include "llvm/IR/Instructions.h" 22 #include "llvm/IR/LegacyPassManager.h" 23 #include "llvm/IR/Module.h" 24 #include "llvm/IR/ValueSymbolTable.h" 25 #include "llvm/IR/Verifier.h" 26 #include "llvm/Pass.h" 27 #include "llvm/Support/CommandLine.h" 28 #include "llvm/Support/FileUtilities.h" 29 #include "llvm/Transforms/Scalar.h" 30 #include "llvm/Transforms/Utils/Cloning.h" 31 #include <set> 32 using namespace llvm; 33 34 namespace { 35 cl::opt<bool> 36 KeepMain("keep-main", 37 cl::desc("Force function reduction to keep main"), 38 cl::init(false)); 39 cl::opt<bool> 40 NoGlobalRM ("disable-global-remove", 41 cl::desc("Do not remove global variables"), 42 cl::init(false)); 43 44 cl::opt<bool> 45 ReplaceFuncsWithNull("replace-funcs-with-null", 46 cl::desc("When stubbing functions, replace all uses will null"), 47 cl::init(false)); 48 cl::opt<bool> 49 DontReducePassList("disable-pass-list-reduction", 50 cl::desc("Skip pass list reduction steps"), 51 cl::init(false)); 52 } 53 54 namespace llvm { 55 class ReducePassList : public ListReducer<std::string> { 56 BugDriver &BD; 57 public: 58 ReducePassList(BugDriver &bd) : BD(bd) {} 59 60 // doTest - Return true iff running the "removed" passes succeeds, and 61 // running the "Kept" passes fail when run on the output of the "removed" 62 // passes. If we return true, we update the current module of bugpoint. 63 // 64 TestResult doTest(std::vector<std::string> &Removed, 65 std::vector<std::string> &Kept, 66 std::string &Error) override; 67 }; 68 } 69 70 ReducePassList::TestResult 71 ReducePassList::doTest(std::vector<std::string> &Prefix, 72 std::vector<std::string> &Suffix, 73 std::string &Error) { 74 std::string PrefixOutput; 75 Module *OrigProgram = nullptr; 76 if (!Prefix.empty()) { 77 outs() << "Checking to see if these passes crash: " 78 << getPassesString(Prefix) << ": "; 79 if (BD.runPasses(BD.getProgram(), Prefix, PrefixOutput)) 80 return KeepPrefix; 81 82 OrigProgram = BD.Program; 83 84 BD.Program = parseInputFile(PrefixOutput, BD.getContext()).release(); 85 if (BD.Program == nullptr) { 86 errs() << BD.getToolName() << ": Error reading bitcode file '" 87 << PrefixOutput << "'!\n"; 88 exit(1); 89 } 90 sys::fs::remove(PrefixOutput); 91 } 92 93 outs() << "Checking to see if these passes crash: " 94 << getPassesString(Suffix) << ": "; 95 96 if (BD.runPasses(BD.getProgram(), Suffix)) { 97 delete OrigProgram; // The suffix crashes alone... 98 return KeepSuffix; 99 } 100 101 // Nothing failed, restore state... 102 if (OrigProgram) { 103 delete BD.Program; 104 BD.Program = OrigProgram; 105 } 106 return NoFailure; 107 } 108 109 namespace { 110 /// ReduceCrashingGlobalVariables - This works by removing the global 111 /// variable's initializer and seeing if the program still crashes. If it 112 /// does, then we keep that program and try again. 113 /// 114 class ReduceCrashingGlobalVariables : public ListReducer<GlobalVariable*> { 115 BugDriver &BD; 116 bool (*TestFn)(const BugDriver &, Module *); 117 public: 118 ReduceCrashingGlobalVariables(BugDriver &bd, 119 bool (*testFn)(const BugDriver &, Module *)) 120 : BD(bd), TestFn(testFn) {} 121 122 TestResult doTest(std::vector<GlobalVariable*> &Prefix, 123 std::vector<GlobalVariable*> &Kept, 124 std::string &Error) override { 125 if (!Kept.empty() && TestGlobalVariables(Kept)) 126 return KeepSuffix; 127 if (!Prefix.empty() && TestGlobalVariables(Prefix)) 128 return KeepPrefix; 129 return NoFailure; 130 } 131 132 bool TestGlobalVariables(std::vector<GlobalVariable*> &GVs); 133 }; 134 } 135 136 bool 137 ReduceCrashingGlobalVariables::TestGlobalVariables( 138 std::vector<GlobalVariable*> &GVs) { 139 // Clone the program to try hacking it apart... 140 ValueToValueMapTy VMap; 141 Module *M = CloneModule(BD.getProgram(), VMap); 142 143 // Convert list to set for fast lookup... 144 std::set<GlobalVariable*> GVSet; 145 146 for (unsigned i = 0, e = GVs.size(); i != e; ++i) { 147 GlobalVariable* CMGV = cast<GlobalVariable>(VMap[GVs[i]]); 148 assert(CMGV && "Global Variable not in module?!"); 149 GVSet.insert(CMGV); 150 } 151 152 outs() << "Checking for crash with only these global variables: "; 153 PrintGlobalVariableList(GVs); 154 outs() << ": "; 155 156 // Loop over and delete any global variables which we aren't supposed to be 157 // playing with... 158 for (GlobalVariable &I : M->globals()) 159 if (I.hasInitializer() && !GVSet.count(&I)) { 160 I.setInitializer(nullptr); 161 I.setLinkage(GlobalValue::ExternalLinkage); 162 } 163 164 // Try running the hacked up program... 165 if (TestFn(BD, M)) { 166 BD.setNewProgram(M); // It crashed, keep the trimmed version... 167 168 // Make sure to use global variable pointers that point into the now-current 169 // module. 170 GVs.assign(GVSet.begin(), GVSet.end()); 171 return true; 172 } 173 174 delete M; 175 return false; 176 } 177 178 namespace { 179 /// ReduceCrashingFunctions reducer - This works by removing functions and 180 /// seeing if the program still crashes. If it does, then keep the newer, 181 /// smaller program. 182 /// 183 class ReduceCrashingFunctions : public ListReducer<Function*> { 184 BugDriver &BD; 185 bool (*TestFn)(const BugDriver &, Module *); 186 public: 187 ReduceCrashingFunctions(BugDriver &bd, 188 bool (*testFn)(const BugDriver &, Module *)) 189 : BD(bd), TestFn(testFn) {} 190 191 TestResult doTest(std::vector<Function*> &Prefix, 192 std::vector<Function*> &Kept, 193 std::string &Error) override { 194 if (!Kept.empty() && TestFuncs(Kept)) 195 return KeepSuffix; 196 if (!Prefix.empty() && TestFuncs(Prefix)) 197 return KeepPrefix; 198 return NoFailure; 199 } 200 201 bool TestFuncs(std::vector<Function*> &Prefix); 202 }; 203 } 204 205 static void RemoveFunctionReferences(Module *M, const char* Name) { 206 auto *UsedVar = M->getGlobalVariable(Name, true); 207 if (!UsedVar || !UsedVar->hasInitializer()) return; 208 if (isa<ConstantAggregateZero>(UsedVar->getInitializer())) { 209 assert(UsedVar->use_empty()); 210 UsedVar->eraseFromParent(); 211 return; 212 } 213 auto *OldUsedVal = cast<ConstantArray>(UsedVar->getInitializer()); 214 std::vector<Constant*> Used; 215 for(Value *V : OldUsedVal->operand_values()) { 216 Constant *Op = cast<Constant>(V->stripPointerCasts()); 217 if(!Op->isNullValue()) { 218 Used.push_back(cast<Constant>(V)); 219 } 220 } 221 auto *NewValElemTy = OldUsedVal->getType()->getElementType(); 222 auto *NewValTy = ArrayType::get(NewValElemTy, Used.size()); 223 auto *NewUsedVal = ConstantArray::get(NewValTy, Used); 224 UsedVar->mutateType(NewUsedVal->getType()->getPointerTo()); 225 UsedVar->setInitializer(NewUsedVal); 226 } 227 228 bool ReduceCrashingFunctions::TestFuncs(std::vector<Function*> &Funcs) { 229 // If main isn't present, claim there is no problem. 230 if (KeepMain && std::find(Funcs.begin(), Funcs.end(), 231 BD.getProgram()->getFunction("main")) == 232 Funcs.end()) 233 return false; 234 235 // Clone the program to try hacking it apart... 236 ValueToValueMapTy VMap; 237 Module *M = CloneModule(BD.getProgram(), VMap); 238 239 // Convert list to set for fast lookup... 240 std::set<Function*> Functions; 241 for (unsigned i = 0, e = Funcs.size(); i != e; ++i) { 242 Function *CMF = cast<Function>(VMap[Funcs[i]]); 243 assert(CMF && "Function not in module?!"); 244 assert(CMF->getFunctionType() == Funcs[i]->getFunctionType() && "wrong ty"); 245 assert(CMF->getName() == Funcs[i]->getName() && "wrong name"); 246 Functions.insert(CMF); 247 } 248 249 outs() << "Checking for crash with only these functions: "; 250 PrintFunctionList(Funcs); 251 outs() << ": "; 252 if (!ReplaceFuncsWithNull) { 253 // Loop over and delete any functions which we aren't supposed to be playing 254 // with... 255 for (Function &I : *M) 256 if (!I.isDeclaration() && !Functions.count(&I)) 257 DeleteFunctionBody(&I); 258 } else { 259 std::vector<GlobalValue*> ToRemove; 260 // First, remove aliases to functions we're about to purge. 261 for (GlobalAlias &Alias : M->aliases()) { 262 Constant *Root = Alias.getAliasee()->stripPointerCasts(); 263 Function *F = dyn_cast<Function>(Root); 264 if (F) { 265 if (Functions.count(F)) 266 // We're keeping this function. 267 continue; 268 } else if (Root->isNullValue()) { 269 // This referenced a globalalias that we've already replaced, 270 // so we still need to replace this alias. 271 } else if (!F) { 272 // Not a function, therefore not something we mess with. 273 continue; 274 } 275 276 PointerType *Ty = cast<PointerType>(Alias.getType()); 277 Constant *Replacement = ConstantPointerNull::get(Ty); 278 Alias.replaceAllUsesWith(Replacement); 279 ToRemove.push_back(&Alias); 280 } 281 282 for (Function &I : *M) { 283 if (!I.isDeclaration() && !Functions.count(&I)) { 284 PointerType *Ty = cast<PointerType>(I.getType()); 285 Constant *Replacement = ConstantPointerNull::get(Ty); 286 I.replaceAllUsesWith(Replacement); 287 ToRemove.push_back(&I); 288 } 289 } 290 291 for (auto *F : ToRemove) { 292 F->eraseFromParent(); 293 } 294 295 // Finally, remove any null members from any global intrinsic. 296 RemoveFunctionReferences(M, "llvm.used"); 297 RemoveFunctionReferences(M, "llvm.compiler.used"); 298 } 299 // Try running the hacked up program... 300 if (TestFn(BD, M)) { 301 BD.setNewProgram(M); // It crashed, keep the trimmed version... 302 303 // Make sure to use function pointers that point into the now-current 304 // module. 305 Funcs.assign(Functions.begin(), Functions.end()); 306 return true; 307 } 308 delete M; 309 return false; 310 } 311 312 313 namespace { 314 /// ReduceCrashingBlocks reducer - This works by setting the terminators of 315 /// all terminators except the specified basic blocks to a 'ret' instruction, 316 /// then running the simplify-cfg pass. This has the effect of chopping up 317 /// the CFG really fast which can reduce large functions quickly. 318 /// 319 class ReduceCrashingBlocks : public ListReducer<const BasicBlock*> { 320 BugDriver &BD; 321 bool (*TestFn)(const BugDriver &, Module *); 322 public: 323 ReduceCrashingBlocks(BugDriver &bd, 324 bool (*testFn)(const BugDriver &, Module *)) 325 : BD(bd), TestFn(testFn) {} 326 327 TestResult doTest(std::vector<const BasicBlock*> &Prefix, 328 std::vector<const BasicBlock*> &Kept, 329 std::string &Error) override { 330 if (!Kept.empty() && TestBlocks(Kept)) 331 return KeepSuffix; 332 if (!Prefix.empty() && TestBlocks(Prefix)) 333 return KeepPrefix; 334 return NoFailure; 335 } 336 337 bool TestBlocks(std::vector<const BasicBlock*> &Prefix); 338 }; 339 } 340 341 bool ReduceCrashingBlocks::TestBlocks(std::vector<const BasicBlock*> &BBs) { 342 // Clone the program to try hacking it apart... 343 ValueToValueMapTy VMap; 344 Module *M = CloneModule(BD.getProgram(), VMap); 345 346 // Convert list to set for fast lookup... 347 SmallPtrSet<BasicBlock*, 8> Blocks; 348 for (unsigned i = 0, e = BBs.size(); i != e; ++i) 349 Blocks.insert(cast<BasicBlock>(VMap[BBs[i]])); 350 351 outs() << "Checking for crash with only these blocks:"; 352 unsigned NumPrint = Blocks.size(); 353 if (NumPrint > 10) NumPrint = 10; 354 for (unsigned i = 0, e = NumPrint; i != e; ++i) 355 outs() << " " << BBs[i]->getName(); 356 if (NumPrint < Blocks.size()) 357 outs() << "... <" << Blocks.size() << " total>"; 358 outs() << ": "; 359 360 // Loop over and delete any hack up any blocks that are not listed... 361 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) 362 for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB) 363 if (!Blocks.count(&*BB) && BB->getTerminator()->getNumSuccessors()) { 364 // Loop over all of the successors of this block, deleting any PHI nodes 365 // that might include it. 366 for (succ_iterator SI = succ_begin(&*BB), E = succ_end(&*BB); SI != E; 367 ++SI) 368 (*SI)->removePredecessor(&*BB); 369 370 TerminatorInst *BBTerm = BB->getTerminator(); 371 372 if (!BB->getTerminator()->getType()->isVoidTy()) 373 BBTerm->replaceAllUsesWith(Constant::getNullValue(BBTerm->getType())); 374 375 // Replace the old terminator instruction. 376 BB->getInstList().pop_back(); 377 new UnreachableInst(BB->getContext(), &*BB); 378 } 379 380 // The CFG Simplifier pass may delete one of the basic blocks we are 381 // interested in. If it does we need to take the block out of the list. Make 382 // a "persistent mapping" by turning basic blocks into <function, name> pairs. 383 // This won't work well if blocks are unnamed, but that is just the risk we 384 // have to take. 385 std::vector<std::pair<std::string, std::string> > BlockInfo; 386 387 for (BasicBlock *BB : Blocks) 388 BlockInfo.emplace_back(BB->getParent()->getName(), BB->getName()); 389 390 // Now run the CFG simplify pass on the function... 391 std::vector<std::string> Passes; 392 Passes.push_back("simplifycfg"); 393 Passes.push_back("verify"); 394 std::unique_ptr<Module> New = BD.runPassesOn(M, Passes); 395 delete M; 396 if (!New) { 397 errs() << "simplifycfg failed!\n"; 398 exit(1); 399 } 400 M = New.release(); 401 402 // Try running on the hacked up program... 403 if (TestFn(BD, M)) { 404 BD.setNewProgram(M); // It crashed, keep the trimmed version... 405 406 // Make sure to use basic block pointers that point into the now-current 407 // module, and that they don't include any deleted blocks. 408 BBs.clear(); 409 const ValueSymbolTable &GST = M->getValueSymbolTable(); 410 for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) { 411 Function *F = cast<Function>(GST.lookup(BlockInfo[i].first)); 412 ValueSymbolTable &ST = F->getValueSymbolTable(); 413 Value* V = ST.lookup(BlockInfo[i].second); 414 if (V && V->getType() == Type::getLabelTy(V->getContext())) 415 BBs.push_back(cast<BasicBlock>(V)); 416 } 417 return true; 418 } 419 delete M; // It didn't crash, try something else. 420 return false; 421 } 422 423 namespace { 424 /// ReduceCrashingInstructions reducer - This works by removing the specified 425 /// non-terminator instructions and replacing them with undef. 426 /// 427 class ReduceCrashingInstructions : public ListReducer<const Instruction*> { 428 BugDriver &BD; 429 bool (*TestFn)(const BugDriver &, Module *); 430 public: 431 ReduceCrashingInstructions(BugDriver &bd, 432 bool (*testFn)(const BugDriver &, Module *)) 433 : BD(bd), TestFn(testFn) {} 434 435 TestResult doTest(std::vector<const Instruction*> &Prefix, 436 std::vector<const Instruction*> &Kept, 437 std::string &Error) override { 438 if (!Kept.empty() && TestInsts(Kept)) 439 return KeepSuffix; 440 if (!Prefix.empty() && TestInsts(Prefix)) 441 return KeepPrefix; 442 return NoFailure; 443 } 444 445 bool TestInsts(std::vector<const Instruction*> &Prefix); 446 }; 447 } 448 449 bool ReduceCrashingInstructions::TestInsts(std::vector<const Instruction*> 450 &Insts) { 451 // Clone the program to try hacking it apart... 452 ValueToValueMapTy VMap; 453 Module *M = CloneModule(BD.getProgram(), VMap); 454 455 // Convert list to set for fast lookup... 456 SmallPtrSet<Instruction*, 64> Instructions; 457 for (unsigned i = 0, e = Insts.size(); i != e; ++i) { 458 assert(!isa<TerminatorInst>(Insts[i])); 459 Instructions.insert(cast<Instruction>(VMap[Insts[i]])); 460 } 461 462 outs() << "Checking for crash with only " << Instructions.size(); 463 if (Instructions.size() == 1) 464 outs() << " instruction: "; 465 else 466 outs() << " instructions: "; 467 468 for (Module::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI) 469 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE; ++FI) 470 for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E;) { 471 Instruction *Inst = &*I++; 472 if (!Instructions.count(Inst) && !isa<TerminatorInst>(Inst) && 473 !Inst->isEHPad()) { 474 if (!Inst->getType()->isVoidTy()) 475 Inst->replaceAllUsesWith(UndefValue::get(Inst->getType())); 476 Inst->eraseFromParent(); 477 } 478 } 479 480 // Verify that this is still valid. 481 legacy::PassManager Passes; 482 Passes.add(createVerifierPass()); 483 Passes.run(*M); 484 485 // Try running on the hacked up program... 486 if (TestFn(BD, M)) { 487 BD.setNewProgram(M); // It crashed, keep the trimmed version... 488 489 // Make sure to use instruction pointers that point into the now-current 490 // module, and that they don't include any deleted blocks. 491 Insts.clear(); 492 for (Instruction *Inst : Instructions) 493 Insts.push_back(Inst); 494 return true; 495 } 496 delete M; // It didn't crash, try something else. 497 return false; 498 } 499 500 /// DebugACrash - Given a predicate that determines whether a component crashes 501 /// on a program, try to destructively reduce the program while still keeping 502 /// the predicate true. 503 static bool DebugACrash(BugDriver &BD, 504 bool (*TestFn)(const BugDriver &, Module *), 505 std::string &Error) { 506 // See if we can get away with nuking some of the global variable initializers 507 // in the program... 508 if (!NoGlobalRM && 509 BD.getProgram()->global_begin() != BD.getProgram()->global_end()) { 510 // Now try to reduce the number of global variable initializers in the 511 // module to something small. 512 Module *M = CloneModule(BD.getProgram()); 513 bool DeletedInit = false; 514 515 for (Module::global_iterator I = M->global_begin(), E = M->global_end(); 516 I != E; ++I) 517 if (I->hasInitializer()) { 518 I->setInitializer(nullptr); 519 I->setLinkage(GlobalValue::ExternalLinkage); 520 DeletedInit = true; 521 } 522 523 if (!DeletedInit) { 524 delete M; // No change made... 525 } else { 526 // See if the program still causes a crash... 527 outs() << "\nChecking to see if we can delete global inits: "; 528 529 if (TestFn(BD, M)) { // Still crashes? 530 BD.setNewProgram(M); 531 outs() << "\n*** Able to remove all global initializers!\n"; 532 } else { // No longer crashes? 533 outs() << " - Removing all global inits hides problem!\n"; 534 delete M; 535 536 std::vector<GlobalVariable*> GVs; 537 538 for (Module::global_iterator I = BD.getProgram()->global_begin(), 539 E = BD.getProgram()->global_end(); I != E; ++I) 540 if (I->hasInitializer()) 541 GVs.push_back(&*I); 542 543 if (GVs.size() > 1 && !BugpointIsInterrupted) { 544 outs() << "\n*** Attempting to reduce the number of global " 545 << "variables in the testcase\n"; 546 547 unsigned OldSize = GVs.size(); 548 ReduceCrashingGlobalVariables(BD, TestFn).reduceList(GVs, Error); 549 if (!Error.empty()) 550 return true; 551 552 if (GVs.size() < OldSize) 553 BD.EmitProgressBitcode(BD.getProgram(), "reduced-global-variables"); 554 } 555 } 556 } 557 } 558 559 // Now try to reduce the number of functions in the module to something small. 560 std::vector<Function*> Functions; 561 for (Function &F : *BD.getProgram()) 562 if (!F.isDeclaration()) 563 Functions.push_back(&F); 564 565 if (Functions.size() > 1 && !BugpointIsInterrupted) { 566 outs() << "\n*** Attempting to reduce the number of functions " 567 "in the testcase\n"; 568 569 unsigned OldSize = Functions.size(); 570 ReduceCrashingFunctions(BD, TestFn).reduceList(Functions, Error); 571 572 if (Functions.size() < OldSize) 573 BD.EmitProgressBitcode(BD.getProgram(), "reduced-function"); 574 } 575 576 // Attempt to delete entire basic blocks at a time to speed up 577 // convergence... this actually works by setting the terminator of the blocks 578 // to a return instruction then running simplifycfg, which can potentially 579 // shrinks the code dramatically quickly 580 // 581 if (!DisableSimplifyCFG && !BugpointIsInterrupted) { 582 std::vector<const BasicBlock*> Blocks; 583 for (Function &F : *BD.getProgram()) 584 for (BasicBlock &BB : F) 585 Blocks.push_back(&BB); 586 unsigned OldSize = Blocks.size(); 587 ReduceCrashingBlocks(BD, TestFn).reduceList(Blocks, Error); 588 if (Blocks.size() < OldSize) 589 BD.EmitProgressBitcode(BD.getProgram(), "reduced-blocks"); 590 } 591 592 // Attempt to delete instructions using bisection. This should help out nasty 593 // cases with large basic blocks where the problem is at one end. 594 if (!BugpointIsInterrupted) { 595 std::vector<const Instruction*> Insts; 596 for (const Function &F : *BD.getProgram()) 597 for (const BasicBlock &BB : F) 598 for (const Instruction &I : BB) 599 if (!isa<TerminatorInst>(&I)) 600 Insts.push_back(&I); 601 602 ReduceCrashingInstructions(BD, TestFn).reduceList(Insts, Error); 603 } 604 605 // FIXME: This should use the list reducer to converge faster by deleting 606 // larger chunks of instructions at a time! 607 unsigned Simplification = 2; 608 do { 609 if (BugpointIsInterrupted) break; 610 --Simplification; 611 outs() << "\n*** Attempting to reduce testcase by deleting instruc" 612 << "tions: Simplification Level #" << Simplification << '\n'; 613 614 // Now that we have deleted the functions that are unnecessary for the 615 // program, try to remove instructions that are not necessary to cause the 616 // crash. To do this, we loop through all of the instructions in the 617 // remaining functions, deleting them (replacing any values produced with 618 // nulls), and then running ADCE and SimplifyCFG. If the transformed input 619 // still triggers failure, keep deleting until we cannot trigger failure 620 // anymore. 621 // 622 unsigned InstructionsToSkipBeforeDeleting = 0; 623 TryAgain: 624 625 // Loop over all of the (non-terminator) instructions remaining in the 626 // function, attempting to delete them. 627 unsigned CurInstructionNum = 0; 628 for (Module::const_iterator FI = BD.getProgram()->begin(), 629 E = BD.getProgram()->end(); FI != E; ++FI) 630 if (!FI->isDeclaration()) 631 for (Function::const_iterator BI = FI->begin(), E = FI->end(); BI != E; 632 ++BI) 633 for (BasicBlock::const_iterator I = BI->begin(), E = --BI->end(); 634 I != E; ++I, ++CurInstructionNum) { 635 if (InstructionsToSkipBeforeDeleting) { 636 --InstructionsToSkipBeforeDeleting; 637 } else { 638 if (BugpointIsInterrupted) goto ExitLoops; 639 640 if (isa<LandingPadInst>(I)) 641 continue; 642 643 outs() << "Checking instruction: " << *I; 644 std::unique_ptr<Module> M = 645 BD.deleteInstructionFromProgram(&*I, Simplification); 646 647 // Find out if the pass still crashes on this pass... 648 if (TestFn(BD, M.get())) { 649 // Yup, it does, we delete the old module, and continue trying 650 // to reduce the testcase... 651 BD.setNewProgram(M.release()); 652 InstructionsToSkipBeforeDeleting = CurInstructionNum; 653 goto TryAgain; // I wish I had a multi-level break here! 654 } 655 } 656 } 657 658 if (InstructionsToSkipBeforeDeleting) { 659 InstructionsToSkipBeforeDeleting = 0; 660 goto TryAgain; 661 } 662 663 } while (Simplification); 664 ExitLoops: 665 666 // Try to clean up the testcase by running funcresolve and globaldce... 667 if (!BugpointIsInterrupted) { 668 outs() << "\n*** Attempting to perform final cleanups: "; 669 Module *M = CloneModule(BD.getProgram()); 670 M = BD.performFinalCleanups(M, true).release(); 671 672 // Find out if the pass still crashes on the cleaned up program... 673 if (TestFn(BD, M)) { 674 BD.setNewProgram(M); // Yup, it does, keep the reduced version... 675 } else { 676 delete M; 677 } 678 } 679 680 BD.EmitProgressBitcode(BD.getProgram(), "reduced-simplified"); 681 682 return false; 683 } 684 685 static bool TestForOptimizerCrash(const BugDriver &BD, Module *M) { 686 return BD.runPasses(M); 687 } 688 689 /// debugOptimizerCrash - This method is called when some pass crashes on input. 690 /// It attempts to prune down the testcase to something reasonable, and figure 691 /// out exactly which pass is crashing. 692 /// 693 bool BugDriver::debugOptimizerCrash(const std::string &ID) { 694 outs() << "\n*** Debugging optimizer crash!\n"; 695 696 std::string Error; 697 // Reduce the list of passes which causes the optimizer to crash... 698 if (!BugpointIsInterrupted && !DontReducePassList) 699 ReducePassList(*this).reduceList(PassesToRun, Error); 700 assert(Error.empty()); 701 702 outs() << "\n*** Found crashing pass" 703 << (PassesToRun.size() == 1 ? ": " : "es: ") 704 << getPassesString(PassesToRun) << '\n'; 705 706 EmitProgressBitcode(Program, ID); 707 708 bool Success = DebugACrash(*this, TestForOptimizerCrash, Error); 709 assert(Error.empty()); 710 return Success; 711 } 712 713 static bool TestForCodeGenCrash(const BugDriver &BD, Module *M) { 714 std::string Error; 715 BD.compileProgram(M, &Error); 716 if (!Error.empty()) { 717 errs() << "<crash>\n"; 718 return true; // Tool is still crashing. 719 } 720 errs() << '\n'; 721 return false; 722 } 723 724 /// debugCodeGeneratorCrash - This method is called when the code generator 725 /// crashes on an input. It attempts to reduce the input as much as possible 726 /// while still causing the code generator to crash. 727 bool BugDriver::debugCodeGeneratorCrash(std::string &Error) { 728 errs() << "*** Debugging code generator crash!\n"; 729 730 return DebugACrash(*this, TestForCodeGenCrash, Error); 731 } 732