1 //===- CrashDebugger.cpp - Debug compilation crashes ----------------------===//
3 // This file defines the bugpoint internals that narrow down compilation crashes
5 //===----------------------------------------------------------------------===//
8 #include "SystemUtils.h"
9 #include "llvm/Module.h"
10 #include "llvm/Bytecode/Writer.h"
11 #include "llvm/Pass.h"
14 /// debugCrash - This method is called when some pass crashes on input. It
15 /// attempts to prune down the testcase to something reasonable, and figure
16 /// out exactly which pass is crashing.
18 bool BugDriver::debugCrash() {
19 std::cout << "\n*** Debugging optimizer crash!\n";
21 // Determine which pass causes the optimizer to crash... using binary search
22 unsigned LastToPass = 0, LastToCrash = PassesToRun.size();
23 while (LastToPass != LastToCrash) {
24 unsigned Mid = (LastToCrash+LastToPass+1) / 2;
25 std::vector<const PassInfo*> P(PassesToRun.begin(),
26 PassesToRun.begin()+Mid);
27 std::cout << "Checking to see if the first " << Mid << " passes crash: ";
35 // Make sure something crashed. :)
36 if (LastToCrash >= PassesToRun.size()) {
37 std::cerr << "ERROR: No passes crashed!\n";
41 // Calculate which pass it is that crashes...
42 const PassInfo *CrashingPass = PassesToRun[LastToCrash];
44 std::cout << "\n*** Found crashing pass '-" << CrashingPass->getPassArgument()
45 << "': " << CrashingPass->getPassName() << "\n";
47 // Compile the program with just the passes that don't crash.
48 if (LastToPass != 0) { // Don't bother doing this if the first pass crashes...
49 std::vector<const PassInfo*> P(PassesToRun.begin(),
50 PassesToRun.begin()+LastToPass);
52 std::cout << "Running passes that don't crash to get input for pass: ";
53 if (runPasses(P, Filename)) {
54 std::cerr << "ERROR: Running the first " << LastToPass
55 << " passes crashed this time!\n";
59 // Assuming everything was successful, we now have a valid bytecode file in
60 // OutputName. Use it for "Program" Instead.
62 Program = ParseInputFile(Filename);
64 // Delete the file now.
68 return debugPassCrash(CrashingPass);
71 /// CountFunctions - return the number of non-external functions defined in the
73 static unsigned CountFunctions(Module *M) {
75 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
81 /// debugPassCrash - This method is called when the specified pass crashes on
82 /// Program as input. It tries to reduce the testcase to something that still
83 /// crashes, but it smaller.
85 bool BugDriver::debugPassCrash(const PassInfo *Pass) {
86 EmitProgressBytecode(Pass, "passinput");
88 if (CountFunctions(Program) > 1) {
89 // Attempt to reduce the input program down to a single function that still
90 // crashes. Do this by removing everything except for that one function...
92 std::cout << "\n*** Attempting to reduce the testcase to one function\n";
94 for (Module::iterator I = Program->begin(), E = Program->end(); I != E; ++I)
95 if (!I->isExternal()) {
96 // Extract one function from the module...
97 Module *M = extractFunctionFromModule(I);
99 // Make the function the current program...
100 std::swap(Program, M);
102 // Find out if the pass still crashes on this pass...
103 std::cout << "Checking function '" << I->getName() << "': ";
105 // Yup, it does, we delete the old module, and continue trying to
106 // reduce the testcase...
109 EmitProgressBytecode(Pass, "reduced-"+I->getName());
113 // This pass didn't crash on this function, try the next one.
118 if (CountFunctions(Program) > 1) {
119 std::cout << "\n*** Couldn't reduce testcase to one function.\n"
120 << " Attempting to remove individual functions.\n";
121 std::cout << "XXX Individual function removal unimplemented!\n";
125 // FIXME: This should attempt to delete entire basic blocks at a time to speed
128 unsigned Simplification = 4;
131 std::cout << "\n*** Attempting to reduce testcase by deleting instruc"
132 << "tions: Simplification Level #" << Simplification << "\n";
134 // Now that we have deleted the functions that are unneccesary for the
135 // program, try to remove instructions that are not neccesary to cause the
136 // crash. To do this, we loop through all of the instructions in the
137 // remaining functions, deleting them (replacing any values produced with
138 // nulls), and then running ADCE and SimplifyCFG. If the transformed input
139 // still triggers failure, keep deleting until we cannot trigger failure
144 // Loop over all of the (non-terminator) instructions remaining in the
145 // function, attempting to delete them.
146 for (Module::iterator FI = Program->begin(), E = Program->end();
148 if (!FI->isExternal()) {
149 for (Function::iterator BI = FI->begin(), E = FI->end(); BI != E; ++BI)
150 for (BasicBlock::iterator I = BI->begin(), E = --BI->end();
152 Module *M = deleteInstructionFromProgram(I, Simplification);
154 // Make the function the current program...
155 std::swap(Program, M);
157 // Find out if the pass still crashes on this pass...
158 std::cout << "Checking instruction '" << I->getName() << "': ";
160 // Yup, it does, we delete the old module, and continue trying to
161 // reduce the testcase...
162 EmitProgressBytecode(Pass, "reduced-" + I->getName());
164 goto TryAgain; // I wish I had a multi-level break here!
167 // This pass didn't crash without this instruction, try the next
173 } while (Simplification);