blob: 315e1823ce0afe67a23abe48b6c87c5e5461fb53 [file] [log] [blame]
Chris Lattnerafade922002-11-20 22:28:10 +00001//===- CrashDebugger.cpp - Debug compilation crashes ----------------------===//
2//
3// This file defines the bugpoint internals that narrow down compilation crashes
4//
5//===----------------------------------------------------------------------===//
6
7#include "BugDriver.h"
Chris Lattner218e26e2002-12-23 23:49:59 +00008#include "SystemUtils.h"
Chris Lattnerafade922002-11-20 22:28:10 +00009#include "llvm/Module.h"
10#include "llvm/Bytecode/Writer.h"
11#include "llvm/Pass.h"
12#include <fstream>
13
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.
17///
18bool BugDriver::debugCrash() {
19 std::cout << "\n*** Debugging optimizer crash!\n";
20
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: ";
28
29 if (runPasses(P))
30 LastToCrash = Mid-1;
31 else
32 LastToPass = Mid;
33 }
34
35 // Make sure something crashed. :)
36 if (LastToCrash >= PassesToRun.size()) {
37 std::cerr << "ERROR: No passes crashed!\n";
38 return true;
39 }
40
41 // Calculate which pass it is that crashes...
42 const PassInfo *CrashingPass = PassesToRun[LastToCrash];
43
44 std::cout << "\n*** Found crashing pass '-" << CrashingPass->getPassArgument()
45 << "': " << CrashingPass->getPassName() << "\n";
46
47 // Compile the program with just the passes that don't crash.
Chris Lattner218e26e2002-12-23 23:49:59 +000048 if (LastToPass != 0) { // Don't bother doing this if the first pass crashes...
Chris Lattnerafade922002-11-20 22:28:10 +000049 std::vector<const PassInfo*> P(PassesToRun.begin(),
50 PassesToRun.begin()+LastToPass);
51 std::string Filename;
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";
56 return true;
57 }
58
59 // Assuming everything was successful, we now have a valid bytecode file in
60 // OutputName. Use it for "Program" Instead.
61 delete Program;
62 Program = ParseInputFile(Filename);
63
64 // Delete the file now.
65 removeFile(Filename);
66 }
67
68 return debugPassCrash(CrashingPass);
69}
70
71/// CountFunctions - return the number of non-external functions defined in the
72/// module.
73static unsigned CountFunctions(Module *M) {
74 unsigned N = 0;
75 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
76 if (!I->isExternal())
77 ++N;
78 return N;
79}
80
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.
84///
85bool BugDriver::debugPassCrash(const PassInfo *Pass) {
86 EmitProgressBytecode(Pass, "passinput");
Chris Lattnerba386d92003-02-28 16:13:20 +000087 bool Reduced = false, AnyReduction = false;
Chris Lattnerafade922002-11-20 22:28:10 +000088
89 if (CountFunctions(Program) > 1) {
90 // Attempt to reduce the input program down to a single function that still
Chris Lattner218e26e2002-12-23 23:49:59 +000091 // crashes. Do this by removing everything except for that one function...
Chris Lattnerafade922002-11-20 22:28:10 +000092 //
93 std::cout << "\n*** Attempting to reduce the testcase to one function\n";
94
95 for (Module::iterator I = Program->begin(), E = Program->end(); I != E; ++I)
96 if (!I->isExternal()) {
97 // Extract one function from the module...
98 Module *M = extractFunctionFromModule(I);
99
100 // Make the function the current program...
101 std::swap(Program, M);
102
103 // Find out if the pass still crashes on this pass...
104 std::cout << "Checking function '" << I->getName() << "': ";
105 if (runPass(Pass)) {
106 // Yup, it does, we delete the old module, and continue trying to
107 // reduce the testcase...
108 delete M;
109
Chris Lattnerba386d92003-02-28 16:13:20 +0000110 Reduced = AnyReduction = true;
Chris Lattnerafade922002-11-20 22:28:10 +0000111 break;
112 }
113
114 // This pass didn't crash on this function, try the next one.
115 delete Program;
116 Program = M;
117 }
Chris Lattner65207852003-01-23 02:48:33 +0000118
119 if (CountFunctions(Program) > 1) {
120 std::cout << "\n*** Couldn't reduce testcase to one function.\n"
121 << " Attempting to remove individual functions.\n";
122 std::cout << "XXX Individual function removal unimplemented!\n";
123 }
Chris Lattnerafade922002-11-20 22:28:10 +0000124 }
125
Chris Lattnerba386d92003-02-28 16:13:20 +0000126 if (Reduced) {
127 EmitProgressBytecode(Pass, "reduced-function");
128 Reduced = false;
129 }
130
Chris Lattner65207852003-01-23 02:48:33 +0000131 // FIXME: This should attempt to delete entire basic blocks at a time to speed
132 // up convergence...
Chris Lattner218e26e2002-12-23 23:49:59 +0000133
Chris Lattner65207852003-01-23 02:48:33 +0000134 unsigned Simplification = 4;
135 do {
136 --Simplification;
137 std::cout << "\n*** Attempting to reduce testcase by deleting instruc"
138 << "tions: Simplification Level #" << Simplification << "\n";
139
140 // Now that we have deleted the functions that are unneccesary for the
141 // program, try to remove instructions that are not neccesary to cause the
142 // crash. To do this, we loop through all of the instructions in the
143 // remaining functions, deleting them (replacing any values produced with
144 // nulls), and then running ADCE and SimplifyCFG. If the transformed input
145 // still triggers failure, keep deleting until we cannot trigger failure
146 // anymore.
147 //
148 TryAgain:
149
150 // Loop over all of the (non-terminator) instructions remaining in the
151 // function, attempting to delete them.
152 for (Module::iterator FI = Program->begin(), E = Program->end();
153 FI != E; ++FI)
154 if (!FI->isExternal()) {
155 for (Function::iterator BI = FI->begin(), E = FI->end(); BI != E; ++BI)
156 for (BasicBlock::iterator I = BI->begin(), E = --BI->end();
157 I != E; ++I) {
158 Module *M = deleteInstructionFromProgram(I, Simplification);
159
160 // Make the function the current program...
161 std::swap(Program, M);
162
163 // Find out if the pass still crashes on this pass...
164 std::cout << "Checking instruction '" << I->getName() << "': ";
165 if (runPass(Pass)) {
166 // Yup, it does, we delete the old module, and continue trying to
167 // reduce the testcase...
Chris Lattner65207852003-01-23 02:48:33 +0000168 delete M;
Chris Lattnerba386d92003-02-28 16:13:20 +0000169 Reduced = AnyReduction = true;
Chris Lattner65207852003-01-23 02:48:33 +0000170 goto TryAgain; // I wish I had a multi-level break here!
171 }
172
173 // This pass didn't crash without this instruction, try the next
174 // one.
175 delete Program;
176 Program = M;
177 }
178 }
179 } while (Simplification);
Chris Lattnerba386d92003-02-28 16:13:20 +0000180
181 // Try to clean up the testcase by running funcresolve and globaldce...
182 if (AnyReduction) {
183 std::cout << "\n*** Attempting to perform final cleanups: ";
184 Module *M = performFinalCleanups();
185 std::swap(Program, M);
186
187 // Find out if the pass still crashes on the cleaned up program...
188 if (runPass(Pass)) {
189 // Yup, it does, keep the reduced version...
190 delete M;
191 Reduced = AnyReduction = true;
192 } else {
193 delete Program; // Otherwise, restore the original module...
194 Program = M;
195 }
196 }
197
198 if (Reduced) {
199 EmitProgressBytecode(Pass, "reduced-simplified");
200 Reduced = false;
201 }
202
Chris Lattnerafade922002-11-20 22:28:10 +0000203 return false;
204}