1 //===- CrashDebugger.cpp - Debug compilation crashes ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the bugpoint internals that narrow down compilation crashes
12 //===----------------------------------------------------------------------===//
14 #include "BugDriver.h"
15 #include "ListReducer.h"
16 #include "ToolRunner.h"
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/ADT/StringSet.h"
19 #include "llvm/IR/CFG.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/DerivedTypes.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/LegacyPassManager.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/IR/ValueSymbolTable.h"
26 #include "llvm/IR/Verifier.h"
27 #include "llvm/Pass.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/FileUtilities.h"
30 #include "llvm/Transforms/Scalar.h"
31 #include "llvm/Transforms/Utils/Cloning.h"
38 cl::desc("Force function reduction to keep main"),
41 NoGlobalRM ("disable-global-remove",
42 cl::desc("Do not remove global variables"),
46 ReplaceFuncsWithNull("replace-funcs-with-null",
47 cl::desc("When stubbing functions, replace all uses will null"),
50 DontReducePassList("disable-pass-list-reduction",
51 cl::desc("Skip pass list reduction steps"),
54 cl::opt<bool> NoNamedMDRM("disable-namedmd-remove",
55 cl::desc("Do not remove global named metadata"),
60 class ReducePassList : public ListReducer<std::string> {
63 ReducePassList(BugDriver &bd) : BD(bd) {}
65 // doTest - Return true iff running the "removed" passes succeeds, and
66 // running the "Kept" passes fail when run on the output of the "removed"
67 // passes. If we return true, we update the current module of bugpoint.
69 TestResult doTest(std::vector<std::string> &Removed,
70 std::vector<std::string> &Kept,
71 std::string &Error) override;
75 ReducePassList::TestResult
76 ReducePassList::doTest(std::vector<std::string> &Prefix,
77 std::vector<std::string> &Suffix,
79 std::string PrefixOutput;
80 Module *OrigProgram = nullptr;
81 if (!Prefix.empty()) {
82 outs() << "Checking to see if these passes crash: "
83 << getPassesString(Prefix) << ": ";
84 if (BD.runPasses(BD.getProgram(), Prefix, PrefixOutput))
87 OrigProgram = BD.Program;
89 BD.Program = parseInputFile(PrefixOutput, BD.getContext()).release();
90 if (BD.Program == nullptr) {
91 errs() << BD.getToolName() << ": Error reading bitcode file '"
92 << PrefixOutput << "'!\n";
95 sys::fs::remove(PrefixOutput);
98 outs() << "Checking to see if these passes crash: "
99 << getPassesString(Suffix) << ": ";
101 if (BD.runPasses(BD.getProgram(), Suffix)) {
102 delete OrigProgram; // The suffix crashes alone...
106 // Nothing failed, restore state...
109 BD.Program = OrigProgram;
115 /// ReduceCrashingGlobalVariables - This works by removing the global
116 /// variable's initializer and seeing if the program still crashes. If it
117 /// does, then we keep that program and try again.
119 class ReduceCrashingGlobalVariables : public ListReducer<GlobalVariable*> {
121 bool (*TestFn)(const BugDriver &, Module *);
123 ReduceCrashingGlobalVariables(BugDriver &bd,
124 bool (*testFn)(const BugDriver &, Module *))
125 : BD(bd), TestFn(testFn) {}
127 TestResult doTest(std::vector<GlobalVariable*> &Prefix,
128 std::vector<GlobalVariable*> &Kept,
129 std::string &Error) override {
130 if (!Kept.empty() && TestGlobalVariables(Kept))
132 if (!Prefix.empty() && TestGlobalVariables(Prefix))
137 bool TestGlobalVariables(std::vector<GlobalVariable*> &GVs);
142 ReduceCrashingGlobalVariables::TestGlobalVariables(
143 std::vector<GlobalVariable*> &GVs) {
144 // Clone the program to try hacking it apart...
145 ValueToValueMapTy VMap;
146 Module *M = CloneModule(BD.getProgram(), VMap).release();
148 // Convert list to set for fast lookup...
149 std::set<GlobalVariable*> GVSet;
151 for (unsigned i = 0, e = GVs.size(); i != e; ++i) {
152 GlobalVariable* CMGV = cast<GlobalVariable>(VMap[GVs[i]]);
153 assert(CMGV && "Global Variable not in module?!");
157 outs() << "Checking for crash with only these global variables: ";
158 PrintGlobalVariableList(GVs);
161 // Loop over and delete any global variables which we aren't supposed to be
163 for (GlobalVariable &I : M->globals())
164 if (I.hasInitializer() && !GVSet.count(&I)) {
165 DeleteGlobalInitializer(&I);
166 I.setLinkage(GlobalValue::ExternalLinkage);
169 // Try running the hacked up program...
171 BD.setNewProgram(M); // It crashed, keep the trimmed version...
173 // Make sure to use global variable pointers that point into the now-current
175 GVs.assign(GVSet.begin(), GVSet.end());
184 /// ReduceCrashingFunctions reducer - This works by removing functions and
185 /// seeing if the program still crashes. If it does, then keep the newer,
188 class ReduceCrashingFunctions : public ListReducer<Function*> {
190 bool (*TestFn)(const BugDriver &, Module *);
192 ReduceCrashingFunctions(BugDriver &bd,
193 bool (*testFn)(const BugDriver &, Module *))
194 : BD(bd), TestFn(testFn) {}
196 TestResult doTest(std::vector<Function*> &Prefix,
197 std::vector<Function*> &Kept,
198 std::string &Error) override {
199 if (!Kept.empty() && TestFuncs(Kept))
201 if (!Prefix.empty() && TestFuncs(Prefix))
206 bool TestFuncs(std::vector<Function*> &Prefix);
210 static void RemoveFunctionReferences(Module *M, const char* Name) {
211 auto *UsedVar = M->getGlobalVariable(Name, true);
212 if (!UsedVar || !UsedVar->hasInitializer()) return;
213 if (isa<ConstantAggregateZero>(UsedVar->getInitializer())) {
214 assert(UsedVar->use_empty());
215 UsedVar->eraseFromParent();
218 auto *OldUsedVal = cast<ConstantArray>(UsedVar->getInitializer());
219 std::vector<Constant*> Used;
220 for(Value *V : OldUsedVal->operand_values()) {
221 Constant *Op = cast<Constant>(V->stripPointerCasts());
222 if(!Op->isNullValue()) {
223 Used.push_back(cast<Constant>(V));
226 auto *NewValElemTy = OldUsedVal->getType()->getElementType();
227 auto *NewValTy = ArrayType::get(NewValElemTy, Used.size());
228 auto *NewUsedVal = ConstantArray::get(NewValTy, Used);
229 UsedVar->mutateType(NewUsedVal->getType()->getPointerTo());
230 UsedVar->setInitializer(NewUsedVal);
233 bool ReduceCrashingFunctions::TestFuncs(std::vector<Function*> &Funcs) {
234 // If main isn't present, claim there is no problem.
235 if (KeepMain && std::find(Funcs.begin(), Funcs.end(),
236 BD.getProgram()->getFunction("main")) ==
240 // Clone the program to try hacking it apart...
241 ValueToValueMapTy VMap;
242 Module *M = CloneModule(BD.getProgram(), VMap).release();
244 // Convert list to set for fast lookup...
245 std::set<Function*> Functions;
246 for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
247 Function *CMF = cast<Function>(VMap[Funcs[i]]);
248 assert(CMF && "Function not in module?!");
249 assert(CMF->getFunctionType() == Funcs[i]->getFunctionType() && "wrong ty");
250 assert(CMF->getName() == Funcs[i]->getName() && "wrong name");
251 Functions.insert(CMF);
254 outs() << "Checking for crash with only these functions: ";
255 PrintFunctionList(Funcs);
257 if (!ReplaceFuncsWithNull) {
258 // Loop over and delete any functions which we aren't supposed to be playing
260 for (Function &I : *M)
261 if (!I.isDeclaration() && !Functions.count(&I))
262 DeleteFunctionBody(&I);
264 std::vector<GlobalValue*> ToRemove;
265 // First, remove aliases to functions we're about to purge.
266 for (GlobalAlias &Alias : M->aliases()) {
267 Constant *Root = Alias.getAliasee()->stripPointerCasts();
268 Function *F = dyn_cast<Function>(Root);
270 if (Functions.count(F))
271 // We're keeping this function.
273 } else if (Root->isNullValue()) {
274 // This referenced a globalalias that we've already replaced,
275 // so we still need to replace this alias.
277 // Not a function, therefore not something we mess with.
281 PointerType *Ty = cast<PointerType>(Alias.getType());
282 Constant *Replacement = ConstantPointerNull::get(Ty);
283 Alias.replaceAllUsesWith(Replacement);
284 ToRemove.push_back(&Alias);
287 for (Function &I : *M) {
288 if (!I.isDeclaration() && !Functions.count(&I)) {
289 PointerType *Ty = cast<PointerType>(I.getType());
290 Constant *Replacement = ConstantPointerNull::get(Ty);
291 I.replaceAllUsesWith(Replacement);
292 ToRemove.push_back(&I);
296 for (auto *F : ToRemove) {
297 F->eraseFromParent();
300 // Finally, remove any null members from any global intrinsic.
301 RemoveFunctionReferences(M, "llvm.used");
302 RemoveFunctionReferences(M, "llvm.compiler.used");
304 // Try running the hacked up program...
306 BD.setNewProgram(M); // It crashed, keep the trimmed version...
308 // Make sure to use function pointers that point into the now-current
310 Funcs.assign(Functions.begin(), Functions.end());
319 /// ReduceCrashingBlocks reducer - This works by setting the terminators of
320 /// all terminators except the specified basic blocks to a 'ret' instruction,
321 /// then running the simplify-cfg pass. This has the effect of chopping up
322 /// the CFG really fast which can reduce large functions quickly.
324 class ReduceCrashingBlocks : public ListReducer<const BasicBlock*> {
326 bool (*TestFn)(const BugDriver &, Module *);
328 ReduceCrashingBlocks(BugDriver &bd,
329 bool (*testFn)(const BugDriver &, Module *))
330 : BD(bd), TestFn(testFn) {}
332 TestResult doTest(std::vector<const BasicBlock*> &Prefix,
333 std::vector<const BasicBlock*> &Kept,
334 std::string &Error) override {
335 if (!Kept.empty() && TestBlocks(Kept))
337 if (!Prefix.empty() && TestBlocks(Prefix))
342 bool TestBlocks(std::vector<const BasicBlock*> &Prefix);
346 bool ReduceCrashingBlocks::TestBlocks(std::vector<const BasicBlock*> &BBs) {
347 // Clone the program to try hacking it apart...
348 ValueToValueMapTy VMap;
349 Module *M = CloneModule(BD.getProgram(), VMap).release();
351 // Convert list to set for fast lookup...
352 SmallPtrSet<BasicBlock*, 8> Blocks;
353 for (unsigned i = 0, e = BBs.size(); i != e; ++i)
354 Blocks.insert(cast<BasicBlock>(VMap[BBs[i]]));
356 outs() << "Checking for crash with only these blocks:";
357 unsigned NumPrint = Blocks.size();
358 if (NumPrint > 10) NumPrint = 10;
359 for (unsigned i = 0, e = NumPrint; i != e; ++i)
360 outs() << " " << BBs[i]->getName();
361 if (NumPrint < Blocks.size())
362 outs() << "... <" << Blocks.size() << " total>";
365 // Loop over and delete any hack up any blocks that are not listed...
366 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
367 for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
368 if (!Blocks.count(&*BB) && BB->getTerminator()->getNumSuccessors()) {
369 // Loop over all of the successors of this block, deleting any PHI nodes
370 // that might include it.
371 for (succ_iterator SI = succ_begin(&*BB), E = succ_end(&*BB); SI != E;
373 (*SI)->removePredecessor(&*BB);
375 TerminatorInst *BBTerm = BB->getTerminator();
376 if (BBTerm->isEHPad())
378 if (!BBTerm->getType()->isVoidTy() && !BBTerm->getType()->isTokenTy())
379 BBTerm->replaceAllUsesWith(Constant::getNullValue(BBTerm->getType()));
381 // Replace the old terminator instruction.
382 BB->getInstList().pop_back();
383 new UnreachableInst(BB->getContext(), &*BB);
386 // The CFG Simplifier pass may delete one of the basic blocks we are
387 // interested in. If it does we need to take the block out of the list. Make
388 // a "persistent mapping" by turning basic blocks into <function, name> pairs.
389 // This won't work well if blocks are unnamed, but that is just the risk we
391 std::vector<std::pair<std::string, std::string> > BlockInfo;
393 for (BasicBlock *BB : Blocks)
394 BlockInfo.emplace_back(BB->getParent()->getName(), BB->getName());
396 // Now run the CFG simplify pass on the function...
397 std::vector<std::string> Passes;
398 Passes.push_back("simplifycfg");
399 Passes.push_back("verify");
400 std::unique_ptr<Module> New = BD.runPassesOn(M, Passes);
403 errs() << "simplifycfg failed!\n";
408 // Try running on the hacked up program...
410 BD.setNewProgram(M); // It crashed, keep the trimmed version...
412 // Make sure to use basic block pointers that point into the now-current
413 // module, and that they don't include any deleted blocks.
415 const ValueSymbolTable &GST = M->getValueSymbolTable();
416 for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) {
417 Function *F = cast<Function>(GST.lookup(BlockInfo[i].first));
418 ValueSymbolTable &ST = F->getValueSymbolTable();
419 Value* V = ST.lookup(BlockInfo[i].second);
420 if (V && V->getType() == Type::getLabelTy(V->getContext()))
421 BBs.push_back(cast<BasicBlock>(V));
425 delete M; // It didn't crash, try something else.
430 /// ReduceCrashingInstructions reducer - This works by removing the specified
431 /// non-terminator instructions and replacing them with undef.
433 class ReduceCrashingInstructions : public ListReducer<const Instruction*> {
435 bool (*TestFn)(const BugDriver &, Module *);
437 ReduceCrashingInstructions(BugDriver &bd,
438 bool (*testFn)(const BugDriver &, Module *))
439 : BD(bd), TestFn(testFn) {}
441 TestResult doTest(std::vector<const Instruction*> &Prefix,
442 std::vector<const Instruction*> &Kept,
443 std::string &Error) override {
444 if (!Kept.empty() && TestInsts(Kept))
446 if (!Prefix.empty() && TestInsts(Prefix))
451 bool TestInsts(std::vector<const Instruction*> &Prefix);
455 bool ReduceCrashingInstructions::TestInsts(std::vector<const Instruction*>
457 // Clone the program to try hacking it apart...
458 ValueToValueMapTy VMap;
459 Module *M = CloneModule(BD.getProgram(), VMap).release();
461 // Convert list to set for fast lookup...
462 SmallPtrSet<Instruction*, 64> Instructions;
463 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
464 assert(!isa<TerminatorInst>(Insts[i]));
465 Instructions.insert(cast<Instruction>(VMap[Insts[i]]));
468 outs() << "Checking for crash with only " << Instructions.size();
469 if (Instructions.size() == 1)
470 outs() << " instruction: ";
472 outs() << " instructions: ";
474 for (Module::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI)
475 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE; ++FI)
476 for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E;) {
477 Instruction *Inst = &*I++;
478 if (!Instructions.count(Inst) && !isa<TerminatorInst>(Inst) &&
480 if (!Inst->getType()->isVoidTy() && !Inst->getType()->isTokenTy())
481 Inst->replaceAllUsesWith(UndefValue::get(Inst->getType()));
482 Inst->eraseFromParent();
486 // Verify that this is still valid.
487 legacy::PassManager Passes;
488 Passes.add(createVerifierPass());
491 // Try running on the hacked up program...
493 BD.setNewProgram(M); // It crashed, keep the trimmed version...
495 // Make sure to use instruction pointers that point into the now-current
496 // module, and that they don't include any deleted blocks.
498 for (Instruction *Inst : Instructions)
499 Insts.push_back(Inst);
502 delete M; // It didn't crash, try something else.
507 // Reduce the list of Named Metadata nodes. We keep this as a list of
508 // names to avoid having to convert back and forth every time.
509 class ReduceCrashingNamedMD : public ListReducer<std::string> {
511 bool (*TestFn)(const BugDriver &, Module *);
514 ReduceCrashingNamedMD(BugDriver &bd,
515 bool (*testFn)(const BugDriver &, Module *))
516 : BD(bd), TestFn(testFn) {}
518 TestResult doTest(std::vector<std::string> &Prefix,
519 std::vector<std::string> &Kept,
520 std::string &Error) override {
521 if (!Kept.empty() && TestNamedMDs(Kept))
523 if (!Prefix.empty() && TestNamedMDs(Prefix))
528 bool TestNamedMDs(std::vector<std::string> &NamedMDs);
532 bool ReduceCrashingNamedMD::TestNamedMDs(std::vector<std::string> &NamedMDs) {
534 ValueToValueMapTy VMap;
535 Module *M = CloneModule(BD.getProgram(), VMap).release();
537 outs() << "Checking for crash with only these named metadata nodes:";
538 unsigned NumPrint = std::min<size_t>(NamedMDs.size(), 10);
539 for (unsigned i = 0, e = NumPrint; i != e; ++i)
540 outs() << " " << NamedMDs[i];
541 if (NumPrint < NamedMDs.size())
542 outs() << "... <" << NamedMDs.size() << " total>";
545 // Make a StringMap for faster lookup
547 for (const std::string &Name : NamedMDs)
550 // First collect all the metadata to delete in a vector, then
551 // delete them all at once to avoid invalidating the iterator
552 std::vector<NamedMDNode *> ToDelete;
553 ToDelete.reserve(M->named_metadata_size() - Names.size());
554 for (auto &NamedMD : M->named_metadata())
555 if (!Names.count(NamedMD.getName()))
556 ToDelete.push_back(&NamedMD);
558 for (auto *NamedMD : ToDelete)
559 NamedMD->eraseFromParent();
561 // Verify that this is still valid.
562 legacy::PassManager Passes;
563 Passes.add(createVerifierPass());
566 // Try running on the hacked up program...
568 BD.setNewProgram(M); // It crashed, keep the trimmed version...
571 delete M; // It didn't crash, try something else.
576 // Reduce the list of operands to named metadata nodes
577 class ReduceCrashingNamedMDOps : public ListReducer<const MDNode *> {
579 bool (*TestFn)(const BugDriver &, Module *);
582 ReduceCrashingNamedMDOps(BugDriver &bd,
583 bool (*testFn)(const BugDriver &, Module *))
584 : BD(bd), TestFn(testFn) {}
586 TestResult doTest(std::vector<const MDNode *> &Prefix,
587 std::vector<const MDNode *> &Kept,
588 std::string &Error) override {
589 if (!Kept.empty() && TestNamedMDOps(Kept))
591 if (!Prefix.empty() && TestNamedMDOps(Prefix))
596 bool TestNamedMDOps(std::vector<const MDNode *> &NamedMDOps);
600 bool ReduceCrashingNamedMDOps::TestNamedMDOps(
601 std::vector<const MDNode *> &NamedMDOps) {
602 // Convert list to set for fast lookup...
603 SmallPtrSet<const MDNode *, 64> OldMDNodeOps;
604 for (unsigned i = 0, e = NamedMDOps.size(); i != e; ++i) {
605 OldMDNodeOps.insert(NamedMDOps[i]);
608 outs() << "Checking for crash with only " << OldMDNodeOps.size();
609 if (OldMDNodeOps.size() == 1)
610 outs() << " named metadata operand: ";
612 outs() << " named metadata operands: ";
614 ValueToValueMapTy VMap;
615 Module *M = CloneModule(BD.getProgram(), VMap).release();
617 // This is a little wasteful. In the future it might be good if we could have
618 // these dropped during cloning.
619 for (auto &NamedMD : BD.getProgram()->named_metadata()) {
620 // Drop the old one and create a new one
621 M->eraseNamedMetadata(M->getNamedMetadata(NamedMD.getName()));
622 NamedMDNode *NewNamedMDNode =
623 M->getOrInsertNamedMetadata(NamedMD.getName());
624 for (MDNode *op : NamedMD.operands())
625 if (OldMDNodeOps.count(op))
626 NewNamedMDNode->addOperand(cast<MDNode>(MapMetadata(op, VMap)));
629 // Verify that this is still valid.
630 legacy::PassManager Passes;
631 Passes.add(createVerifierPass());
634 // Try running on the hacked up program...
636 // Make sure to use instruction pointers that point into the now-current
637 // module, and that they don't include any deleted blocks.
639 for (const MDNode *Node : OldMDNodeOps)
640 NamedMDOps.push_back(cast<MDNode>(VMap.MD()[Node].get()));
642 BD.setNewProgram(M); // It crashed, keep the trimmed version...
645 delete M; // It didn't crash, try something else.
649 /// DebugACrash - Given a predicate that determines whether a component crashes
650 /// on a program, try to destructively reduce the program while still keeping
651 /// the predicate true.
652 static bool DebugACrash(BugDriver &BD,
653 bool (*TestFn)(const BugDriver &, Module *),
654 std::string &Error) {
655 // See if we can get away with nuking some of the global variable initializers
658 BD.getProgram()->global_begin() != BD.getProgram()->global_end()) {
659 // Now try to reduce the number of global variable initializers in the
660 // module to something small.
661 Module *M = CloneModule(BD.getProgram()).release();
662 bool DeletedInit = false;
664 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
666 if (I->hasInitializer()) {
667 DeleteGlobalInitializer(&*I);
668 I->setLinkage(GlobalValue::ExternalLinkage);
673 delete M; // No change made...
675 // See if the program still causes a crash...
676 outs() << "\nChecking to see if we can delete global inits: ";
678 if (TestFn(BD, M)) { // Still crashes?
680 outs() << "\n*** Able to remove all global initializers!\n";
681 } else { // No longer crashes?
682 outs() << " - Removing all global inits hides problem!\n";
685 std::vector<GlobalVariable*> GVs;
687 for (Module::global_iterator I = BD.getProgram()->global_begin(),
688 E = BD.getProgram()->global_end(); I != E; ++I)
689 if (I->hasInitializer())
692 if (GVs.size() > 1 && !BugpointIsInterrupted) {
693 outs() << "\n*** Attempting to reduce the number of global "
694 << "variables in the testcase\n";
696 unsigned OldSize = GVs.size();
697 ReduceCrashingGlobalVariables(BD, TestFn).reduceList(GVs, Error);
701 if (GVs.size() < OldSize)
702 BD.EmitProgressBitcode(BD.getProgram(), "reduced-global-variables");
708 // Now try to reduce the number of functions in the module to something small.
709 std::vector<Function*> Functions;
710 for (Function &F : *BD.getProgram())
711 if (!F.isDeclaration())
712 Functions.push_back(&F);
714 if (Functions.size() > 1 && !BugpointIsInterrupted) {
715 outs() << "\n*** Attempting to reduce the number of functions "
718 unsigned OldSize = Functions.size();
719 ReduceCrashingFunctions(BD, TestFn).reduceList(Functions, Error);
721 if (Functions.size() < OldSize)
722 BD.EmitProgressBitcode(BD.getProgram(), "reduced-function");
725 // Attempt to delete entire basic blocks at a time to speed up
726 // convergence... this actually works by setting the terminator of the blocks
727 // to a return instruction then running simplifycfg, which can potentially
728 // shrinks the code dramatically quickly
730 if (!DisableSimplifyCFG && !BugpointIsInterrupted) {
731 std::vector<const BasicBlock*> Blocks;
732 for (Function &F : *BD.getProgram())
733 for (BasicBlock &BB : F)
734 Blocks.push_back(&BB);
735 unsigned OldSize = Blocks.size();
736 ReduceCrashingBlocks(BD, TestFn).reduceList(Blocks, Error);
737 if (Blocks.size() < OldSize)
738 BD.EmitProgressBitcode(BD.getProgram(), "reduced-blocks");
741 // Attempt to delete instructions using bisection. This should help out nasty
742 // cases with large basic blocks where the problem is at one end.
743 if (!BugpointIsInterrupted) {
744 std::vector<const Instruction*> Insts;
745 for (const Function &F : *BD.getProgram())
746 for (const BasicBlock &BB : F)
747 for (const Instruction &I : BB)
748 if (!isa<TerminatorInst>(&I))
751 ReduceCrashingInstructions(BD, TestFn).reduceList(Insts, Error);
754 // FIXME: This should use the list reducer to converge faster by deleting
755 // larger chunks of instructions at a time!
756 unsigned Simplification = 2;
758 if (BugpointIsInterrupted) break;
760 outs() << "\n*** Attempting to reduce testcase by deleting instruc"
761 << "tions: Simplification Level #" << Simplification << '\n';
763 // Now that we have deleted the functions that are unnecessary for the
764 // program, try to remove instructions that are not necessary to cause the
765 // crash. To do this, we loop through all of the instructions in the
766 // remaining functions, deleting them (replacing any values produced with
767 // nulls), and then running ADCE and SimplifyCFG. If the transformed input
768 // still triggers failure, keep deleting until we cannot trigger failure
771 unsigned InstructionsToSkipBeforeDeleting = 0;
774 // Loop over all of the (non-terminator) instructions remaining in the
775 // function, attempting to delete them.
776 unsigned CurInstructionNum = 0;
777 for (Module::const_iterator FI = BD.getProgram()->begin(),
778 E = BD.getProgram()->end(); FI != E; ++FI)
779 if (!FI->isDeclaration())
780 for (Function::const_iterator BI = FI->begin(), E = FI->end(); BI != E;
782 for (BasicBlock::const_iterator I = BI->begin(), E = --BI->end();
783 I != E; ++I, ++CurInstructionNum) {
784 if (InstructionsToSkipBeforeDeleting) {
785 --InstructionsToSkipBeforeDeleting;
787 if (BugpointIsInterrupted) goto ExitLoops;
789 if (I->isEHPad() || I->getType()->isTokenTy())
792 outs() << "Checking instruction: " << *I;
793 std::unique_ptr<Module> M =
794 BD.deleteInstructionFromProgram(&*I, Simplification);
796 // Find out if the pass still crashes on this pass...
797 if (TestFn(BD, M.get())) {
798 // Yup, it does, we delete the old module, and continue trying
799 // to reduce the testcase...
800 BD.setNewProgram(M.release());
801 InstructionsToSkipBeforeDeleting = CurInstructionNum;
802 goto TryAgain; // I wish I had a multi-level break here!
807 if (InstructionsToSkipBeforeDeleting) {
808 InstructionsToSkipBeforeDeleting = 0;
812 } while (Simplification);
815 BD.EmitProgressBitcode(BD.getProgram(), "reduced-instructions");
817 if (!BugpointIsInterrupted) {
818 // Try to reduce the amount of global metadata (particularly debug info),
819 // by dropping global named metadata that anchors them
820 outs() << "\n*** Attempting to remove named metadata: ";
821 std::vector<std::string> NamedMDNames;
822 for (auto &NamedMD : BD.getProgram()->named_metadata())
823 NamedMDNames.push_back(NamedMD.getName().str());
824 ReduceCrashingNamedMD(BD, TestFn).reduceList(NamedMDNames, Error);
827 if (!BugpointIsInterrupted) {
828 // Now that we quickly dropped all the named metadata that doesn't
829 // contribute to the crash, bisect the operands of the remaining ones
830 std::vector<const MDNode *> NamedMDOps;
831 for (auto &NamedMD : BD.getProgram()->named_metadata())
832 for (auto op : NamedMD.operands())
833 NamedMDOps.push_back(op);
834 ReduceCrashingNamedMDOps(BD, TestFn).reduceList(NamedMDOps, Error);
840 // Try to clean up the testcase by running funcresolve and globaldce...
841 if (!BugpointIsInterrupted) {
842 outs() << "\n*** Attempting to perform final cleanups: ";
843 Module *M = CloneModule(BD.getProgram()).release();
844 M = BD.performFinalCleanups(M, true).release();
846 // Find out if the pass still crashes on the cleaned up program...
848 BD.setNewProgram(M); // Yup, it does, keep the reduced version...
854 BD.EmitProgressBitcode(BD.getProgram(), "reduced-simplified");
859 static bool TestForOptimizerCrash(const BugDriver &BD, Module *M) {
860 return BD.runPasses(M);
863 /// debugOptimizerCrash - This method is called when some pass crashes on input.
864 /// It attempts to prune down the testcase to something reasonable, and figure
865 /// out exactly which pass is crashing.
867 bool BugDriver::debugOptimizerCrash(const std::string &ID) {
868 outs() << "\n*** Debugging optimizer crash!\n";
871 // Reduce the list of passes which causes the optimizer to crash...
872 if (!BugpointIsInterrupted && !DontReducePassList)
873 ReducePassList(*this).reduceList(PassesToRun, Error);
874 assert(Error.empty());
876 outs() << "\n*** Found crashing pass"
877 << (PassesToRun.size() == 1 ? ": " : "es: ")
878 << getPassesString(PassesToRun) << '\n';
880 EmitProgressBitcode(Program, ID);
882 bool Success = DebugACrash(*this, TestForOptimizerCrash, Error);
883 assert(Error.empty());
887 static bool TestForCodeGenCrash(const BugDriver &BD, Module *M) {
889 BD.compileProgram(M, &Error);
890 if (!Error.empty()) {
891 errs() << "<crash>\n";
892 return true; // Tool is still crashing.
898 /// debugCodeGeneratorCrash - This method is called when the code generator
899 /// crashes on an input. It attempts to reduce the input as much as possible
900 /// while still causing the code generator to crash.
901 bool BugDriver::debugCodeGeneratorCrash(std::string &Error) {
902 errs() << "*** Debugging code generator crash!\n";
904 return DebugACrash(*this, TestForCodeGenCrash, Error);