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"
15 class DebugCrashes : public ListReducer<const PassInfo*> {
18 DebugCrashes(BugDriver &bd) : BD(bd) {}
20 // doTest - Return true iff running the "removed" passes succeeds, and running
21 // the "Kept" passes fail when run on the output of the "removed" passes. If
22 // we return true, we update the current module of bugpoint.
24 virtual bool doTest(const std::vector<ElTy> &Removed,
25 const std::vector<ElTy> &Kept) {
26 return BD.runPasses(Kept);
31 /// debugCrash - This method is called when some pass crashes on input. It
32 /// attempts to prune down the testcase to something reasonable, and figure
33 /// out exactly which pass is crashing.
35 bool BugDriver::debugCrash() {
36 std::cout << "\n*** Debugging optimizer crash!\n";
39 // Reduce the list of passes which causes the optimizer to crash...
40 DebugCrashes(*this).reduceList(PassesToRun);
43 unsigned LastToPass = 0, LastToCrash = PassesToRun.size();
44 while (LastToPass != LastToCrash) {
45 unsigned Mid = (LastToCrash+LastToPass+1) / 2;
46 std::vector<const PassInfo*> P(PassesToRun.begin(),
47 PassesToRun.begin()+Mid);
48 std::cout << "Checking to see if the first " << Mid << " passes crash: ";
56 // Make sure something crashed. :)
57 if (LastToCrash >= PassesToRun.size()) {
58 std::cerr << "ERROR: No passes crashed!\n";
62 // Calculate which pass it is that crashes...
63 const PassInfo *CrashingPass = PassesToRun[LastToCrash];
65 std::cout << "\n*** Found crashing pass '-" << CrashingPass->getPassArgument()
66 << "': " << CrashingPass->getPassName() << "\n";
68 // Compile the program with just the passes that don't crash.
69 if (LastToPass != 0) { // Don't bother doing this if the first pass crashes...
70 std::vector<const PassInfo*> P(PassesToRun.begin(),
71 PassesToRun.begin()+LastToPass);
73 std::cout << "Running passes that don't crash to get input for pass: ";
74 if (runPasses(P, Filename)) {
75 std::cerr << "ERROR: Running the first " << LastToPass
76 << " passes crashed this time!\n";
80 // Assuming everything was successful, we now have a valid bytecode file in
81 // OutputName. Use it for "Program" Instead.
83 Program = ParseInputFile(Filename);
85 // Delete the file now.
90 PassesToRun.push_back(CrashingPass);
92 return debugPassCrash(CrashingPass);
95 /// CountFunctions - return the number of non-external functions defined in the
97 static unsigned CountFunctions(Module *M) {
99 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
100 if (!I->isExternal())
105 /// debugPassCrash - This method is called when the specified pass crashes on
106 /// Program as input. It tries to reduce the testcase to something that still
107 /// crashes, but it smaller.
109 bool BugDriver::debugPassCrash(const PassInfo *Pass) {
110 EmitProgressBytecode("passinput");
111 bool Reduced = false, AnyReduction = false;
113 if (CountFunctions(Program) > 1) {
114 // Attempt to reduce the input program down to a single function that still
115 // crashes. Do this by removing everything except for that one function...
117 std::cout << "\n*** Attempting to reduce the testcase to one function\n";
119 for (Module::iterator I = Program->begin(), E = Program->end(); I != E; ++I)
120 if (!I->isExternal()) {
121 // Extract one function from the module...
122 Module *M = extractFunctionFromModule(I);
124 // Make the function the current program...
125 std::swap(Program, M);
127 // Find out if the pass still crashes on this pass...
128 std::cout << "Checking function '" << I->getName() << "': ";
130 // Yup, it does, we delete the old module, and continue trying to
131 // reduce the testcase...
134 Reduced = AnyReduction = true;
138 // This pass didn't crash on this function, try the next one.
143 if (CountFunctions(Program) > 1) {
144 std::cout << "\n*** Couldn't reduce testcase to one function.\n"
145 << " Attempting to remove individual functions.\n";
146 std::cout << "XXX Individual function removal unimplemented!\n";
151 EmitProgressBytecode("reduced-function");
155 // FIXME: This should attempt to delete entire basic blocks at a time to speed
158 unsigned Simplification = 4;
161 std::cout << "\n*** Attempting to reduce testcase by deleting instruc"
162 << "tions: Simplification Level #" << Simplification << "\n";
164 // Now that we have deleted the functions that are unneccesary for the
165 // program, try to remove instructions that are not neccesary to cause the
166 // crash. To do this, we loop through all of the instructions in the
167 // remaining functions, deleting them (replacing any values produced with
168 // nulls), and then running ADCE and SimplifyCFG. If the transformed input
169 // still triggers failure, keep deleting until we cannot trigger failure
174 // Loop over all of the (non-terminator) instructions remaining in the
175 // function, attempting to delete them.
176 for (Module::iterator FI = Program->begin(), E = Program->end();
178 if (!FI->isExternal()) {
179 for (Function::iterator BI = FI->begin(), E = FI->end(); BI != E; ++BI)
180 for (BasicBlock::iterator I = BI->begin(), E = --BI->end();
182 Module *M = deleteInstructionFromProgram(I, Simplification);
184 // Make the function the current program...
185 std::swap(Program, M);
187 // Find out if the pass still crashes on this pass...
188 std::cout << "Checking instruction '" << I->getName() << "': ";
190 // Yup, it does, we delete the old module, and continue trying to
191 // reduce the testcase...
193 Reduced = AnyReduction = true;
194 goto TryAgain; // I wish I had a multi-level break here!
197 // This pass didn't crash without this instruction, try the next
203 } while (Simplification);
205 // Try to clean up the testcase by running funcresolve and globaldce...
207 std::cout << "\n*** Attempting to perform final cleanups: ";
208 Module *M = performFinalCleanups();
209 std::swap(Program, M);
211 // Find out if the pass still crashes on the cleaned up program...
213 // Yup, it does, keep the reduced version...
215 Reduced = AnyReduction = true;
217 delete Program; // Otherwise, restore the original module...
223 EmitProgressBytecode("reduced-simplified");