blob: b34d8805657880947302ca7139c9e15837334b15 [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 Lattneraae33f92003-04-24 22:24:58 +00008#include "ListReducer.h"
Chris Lattner286921e2003-04-24 23:51:38 +00009#include "llvm/Constant.h"
10#include "llvm/iTerminators.h"
Misha Brukmane49603d2003-08-07 21:19:30 +000011#include "llvm/Module.h"
12#include "llvm/Pass.h"
13#include "llvm/PassManager.h"
Chris Lattner286921e2003-04-24 23:51:38 +000014#include "llvm/SymbolTable.h"
Misha Brukmane49603d2003-08-07 21:19:30 +000015#include "llvm/Type.h"
Chris Lattner286921e2003-04-24 23:51:38 +000016#include "llvm/Analysis/Verifier.h"
Misha Brukmane49603d2003-08-07 21:19:30 +000017#include "llvm/Bytecode/Writer.h"
18#include "llvm/Support/CFG.h"
Chris Lattner286921e2003-04-24 23:51:38 +000019#include "llvm/Transforms/Scalar.h"
Chris Lattneraae33f92003-04-24 22:24:58 +000020#include "llvm/Transforms/Utils/Cloning.h"
Misha Brukman3d9cafa2003-08-07 21:42:28 +000021#include "Support/FileUtilities.h"
Chris Lattnerafade922002-11-20 22:28:10 +000022#include <fstream>
Chris Lattneraae33f92003-04-24 22:24:58 +000023#include <set>
Chris Lattnerafade922002-11-20 22:28:10 +000024
Chris Lattner640f22e2003-04-24 17:02:17 +000025class DebugCrashes : public ListReducer<const PassInfo*> {
26 BugDriver &BD;
27public:
28 DebugCrashes(BugDriver &bd) : BD(bd) {}
29
30 // doTest - Return true iff running the "removed" passes succeeds, and running
31 // the "Kept" passes fail when run on the output of the "removed" passes. If
32 // we return true, we update the current module of bugpoint.
33 //
Chris Lattneraae33f92003-04-24 22:24:58 +000034 virtual TestResult doTest(std::vector<const PassInfo*> &Removed,
35 std::vector<const PassInfo*> &Kept);
Chris Lattner640f22e2003-04-24 17:02:17 +000036};
Chris Lattneraae33f92003-04-24 22:24:58 +000037
38DebugCrashes::TestResult
39DebugCrashes::doTest(std::vector<const PassInfo*> &Prefix,
40 std::vector<const PassInfo*> &Suffix) {
41 std::string PrefixOutput;
Chris Lattnerb417c792003-06-02 04:54:29 +000042 Module *OrigProgram = 0;
Chris Lattneraae33f92003-04-24 22:24:58 +000043 if (!Prefix.empty()) {
44 std::cout << "Checking to see if these passes crash: "
45 << getPassesString(Prefix) << ": ";
46 if (BD.runPasses(Prefix, PrefixOutput))
47 return KeepPrefix;
Chris Lattnerb417c792003-06-02 04:54:29 +000048
49 OrigProgram = BD.Program;
50
51 BD.Program = BD.ParseInputFile(PrefixOutput);
52 if (BD.Program == 0) {
53 std::cerr << BD.getToolName() << ": Error reading bytecode file '"
54 << PrefixOutput << "'!\n";
55 exit(1);
56 }
57 removeFile(PrefixOutput);
Chris Lattneraae33f92003-04-24 22:24:58 +000058 }
59
60 std::cout << "Checking to see if these passes crash: "
61 << getPassesString(Suffix) << ": ";
Chris Lattnerb417c792003-06-02 04:54:29 +000062
Chris Lattneraae33f92003-04-24 22:24:58 +000063 if (BD.runPasses(Suffix)) {
64 delete OrigProgram; // The suffix crashes alone...
65 return KeepSuffix;
66 }
67
68 // Nothing failed, restore state...
Chris Lattnerb417c792003-06-02 04:54:29 +000069 if (OrigProgram) {
70 delete BD.Program;
71 BD.Program = OrigProgram;
72 }
Chris Lattneraae33f92003-04-24 22:24:58 +000073 return NoFailure;
74}
75
76class ReduceCrashingFunctions : public ListReducer<Function*> {
77 BugDriver &BD;
78public:
79 ReduceCrashingFunctions(BugDriver &bd) : BD(bd) {}
80
81 virtual TestResult doTest(std::vector<Function*> &Prefix,
82 std::vector<Function*> &Kept) {
Misha Brukmana493ae32003-08-05 15:26:21 +000083 if (!Kept.empty() && TestFuncs(Kept))
Chris Lattneraae33f92003-04-24 22:24:58 +000084 return KeepSuffix;
85 if (!Prefix.empty() && TestFuncs(Prefix))
86 return KeepPrefix;
87 return NoFailure;
88 }
89
90 bool TestFuncs(std::vector<Function*> &Prefix);
91};
92
93bool ReduceCrashingFunctions::TestFuncs(std::vector<Function*> &Funcs) {
Misha Brukmancf00c4a2003-10-10 17:57:28 +000094 // Clone the program to try hacking it apart...
Chris Lattneraae33f92003-04-24 22:24:58 +000095 Module *M = CloneModule(BD.Program);
96
97 // Convert list to set for fast lookup...
98 std::set<Function*> Functions;
99 for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
100 Function *CMF = M->getFunction(Funcs[i]->getName(),
101 Funcs[i]->getFunctionType());
102 assert(CMF && "Function not in module?!");
Chris Lattnerf607b792003-04-24 22:54:06 +0000103 Functions.insert(CMF);
Chris Lattneraae33f92003-04-24 22:24:58 +0000104 }
105
106 std::cout << "Checking for crash with only these functions:";
107 for (unsigned i = 0, e = Funcs.size(); i != e; ++i)
108 std::cout << " " << Funcs[i]->getName();
109 std::cout << ": ";
110
111 // Loop over and delete any functions which we aren't supposed to be playing
112 // with...
113 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerf607b792003-04-24 22:54:06 +0000114 if (!I->isExternal() && !Functions.count(I))
Chris Lattneraae33f92003-04-24 22:24:58 +0000115 DeleteFunctionBody(I);
116
117 // Try running the hacked up program...
118 std::swap(BD.Program, M);
119 if (BD.runPasses(BD.PassesToRun)) {
120 delete M; // It crashed, keep the trimmed version...
121
122 // Make sure to use function pointers that point into the now-current
123 // module.
124 Funcs.assign(Functions.begin(), Functions.end());
125 return true;
126 }
127 delete BD.Program; // It didn't crash, revert...
128 BD.Program = M;
129 return false;
130}
131
Chris Lattner640f22e2003-04-24 17:02:17 +0000132
Chris Lattner286921e2003-04-24 23:51:38 +0000133/// ReduceCrashingBlocks reducer - This works by setting the terminators of all
134/// terminators except the specified basic blocks to a 'ret' instruction, then
135/// running the simplify-cfg pass. This has the effect of chopping up the CFG
136/// really fast which can reduce large functions quickly.
137///
138class ReduceCrashingBlocks : public ListReducer<BasicBlock*> {
139 BugDriver &BD;
140public:
141 ReduceCrashingBlocks(BugDriver &bd) : BD(bd) {}
142
143 virtual TestResult doTest(std::vector<BasicBlock*> &Prefix,
144 std::vector<BasicBlock*> &Kept) {
John Criswell5cfff252003-08-04 18:24:31 +0000145 if (!Kept.empty() && TestBlocks(Kept))
Chris Lattner286921e2003-04-24 23:51:38 +0000146 return KeepSuffix;
147 if (!Prefix.empty() && TestBlocks(Prefix))
148 return KeepPrefix;
149 return NoFailure;
150 }
151
152 bool TestBlocks(std::vector<BasicBlock*> &Prefix);
153};
154
155bool ReduceCrashingBlocks::TestBlocks(std::vector<BasicBlock*> &BBs) {
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000156 // Clone the program to try hacking it apart...
Chris Lattner286921e2003-04-24 23:51:38 +0000157 Module *M = CloneModule(BD.Program);
158
159 // Convert list to set for fast lookup...
160 std::set<BasicBlock*> Blocks;
161 for (unsigned i = 0, e = BBs.size(); i != e; ++i) {
162 // Convert the basic block from the original module to the new module...
163 Function *F = BBs[i]->getParent();
164 Function *CMF = M->getFunction(F->getName(), F->getFunctionType());
165 assert(CMF && "Function not in module?!");
166
167 // Get the mapped basic block...
168 Function::iterator CBI = CMF->begin();
169 std::advance(CBI, std::distance(F->begin(), Function::iterator(BBs[i])));
170 Blocks.insert(CBI);
171 }
172
173 std::cout << "Checking for crash with only these blocks:";
174 for (unsigned i = 0, e = Blocks.size(); i != e; ++i)
175 std::cout << " " << BBs[i]->getName();
176 std::cout << ": ";
177
178 // Loop over and delete any hack up any blocks that are not listed...
179 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
180 for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
181 if (!Blocks.count(BB) && !isa<ReturnInst>(BB->getTerminator())) {
182 // Loop over all of the successors of this block, deleting any PHI nodes
183 // that might include it.
184 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
185 (*SI)->removePredecessor(BB);
186
187 // Delete the old terminator instruction...
188 BB->getInstList().pop_back();
189
190 // Add a new return instruction of the appropriate type...
191 const Type *RetTy = BB->getParent()->getReturnType();
192 ReturnInst *RI = new ReturnInst(RetTy == Type::VoidTy ? 0 :
193 Constant::getNullValue(RetTy));
194 BB->getInstList().push_back(RI);
195 }
196
197 // The CFG Simplifier pass may delete one of the basic blocks we are
198 // interested in. If it does we need to take the block out of the list. Make
199 // a "persistent mapping" by turning basic blocks into <function, name> pairs.
200 // This won't work well if blocks are unnamed, but that is just the risk we
201 // have to take.
202 std::vector<std::pair<Function*, std::string> > BlockInfo;
203
204 for (std::set<BasicBlock*>::iterator I = Blocks.begin(), E = Blocks.end();
205 I != E; ++I)
206 BlockInfo.push_back(std::make_pair((*I)->getParent(), (*I)->getName()));
207
208 // Now run the CFG simplify pass on the function...
209 PassManager Passes;
210 Passes.add(createCFGSimplificationPass());
211 Passes.add(createVerifierPass());
212 Passes.run(*M);
213
214 // Try running on the hacked up program...
215 std::swap(BD.Program, M);
216 if (BD.runPasses(BD.PassesToRun)) {
217 delete M; // It crashed, keep the trimmed version...
218
219 // Make sure to use basic block pointers that point into the now-current
220 // module, and that they don't include any deleted blocks.
221 BBs.clear();
222 for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) {
223 SymbolTable &ST = BlockInfo[i].first->getSymbolTable();
224 SymbolTable::iterator I = ST.find(Type::LabelTy);
225 if (I != ST.end() && I->second.count(BlockInfo[i].second))
226 BBs.push_back(cast<BasicBlock>(I->second[BlockInfo[i].second]));
227 }
228 return true;
229 }
230 delete BD.Program; // It didn't crash, revert...
231 BD.Program = M;
232 return false;
233}
234
Chris Lattnerafade922002-11-20 22:28:10 +0000235/// debugCrash - This method is called when some pass crashes on input. It
236/// attempts to prune down the testcase to something reasonable, and figure
237/// out exactly which pass is crashing.
238///
239bool BugDriver::debugCrash() {
Chris Lattneraae33f92003-04-24 22:24:58 +0000240 bool AnyReduction = false;
Chris Lattnerafade922002-11-20 22:28:10 +0000241 std::cout << "\n*** Debugging optimizer crash!\n";
242
Chris Lattner640f22e2003-04-24 17:02:17 +0000243 // Reduce the list of passes which causes the optimizer to crash...
Chris Lattneraae33f92003-04-24 22:24:58 +0000244 unsigned OldSize = PassesToRun.size();
Chris Lattner640f22e2003-04-24 17:02:17 +0000245 DebugCrashes(*this).reduceList(PassesToRun);
Chris Lattner640f22e2003-04-24 17:02:17 +0000246
Chris Lattneraae33f92003-04-24 22:24:58 +0000247 std::cout << "\n*** Found crashing pass"
248 << (PassesToRun.size() == 1 ? ": " : "es: ")
249 << getPassesString(PassesToRun) << "\n";
Chris Lattnerafade922002-11-20 22:28:10 +0000250
Chris Lattner640f22e2003-04-24 17:02:17 +0000251 EmitProgressBytecode("passinput");
Chris Lattner5f73e382003-04-25 00:53:05 +0000252
253 // See if we can get away with nuking all of the global variable initializers
254 // in the program...
255 if (Program->gbegin() != Program->gend()) {
256 Module *M = CloneModule(Program);
257 bool DeletedInit = false;
258 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
259 if (I->hasInitializer()) {
260 I->setInitializer(0);
261 I->setLinkage(GlobalValue::ExternalLinkage);
262 DeletedInit = true;
263 }
264
265 if (!DeletedInit) {
266 delete M; // No change made...
267 } else {
268 // See if the program still causes a crash...
269 std::cout << "\nChecking to see if we can delete global inits: ";
270 std::swap(Program, M);
271 if (runPasses(PassesToRun)) { // Still crashes?
272 AnyReduction = true;
273 delete M;
274 std::cout << "\n*** Able to remove all global initializers!\n";
275 } else { // No longer crashes?
276 delete Program; // Restore program.
277 Program = M;
278 std::cout << " - Removing all global inits hides problem!\n";
279 }
280 }
281 }
Chris Lattneraae33f92003-04-24 22:24:58 +0000282
283 // Now try to reduce the number of functions in the module to something small.
284 std::vector<Function*> Functions;
285 for (Module::iterator I = Program->begin(), E = Program->end(); I != E; ++I)
286 if (!I->isExternal())
287 Functions.push_back(I);
Chris Lattnerafade922002-11-20 22:28:10 +0000288
Chris Lattneraae33f92003-04-24 22:24:58 +0000289 if (Functions.size() > 1) {
290 std::cout << "\n*** Attempting to reduce the number of functions "
291 "in the testcase\n";
Chris Lattnerafade922002-11-20 22:28:10 +0000292
Chris Lattneraae33f92003-04-24 22:24:58 +0000293 OldSize = Functions.size();
294 ReduceCrashingFunctions(*this).reduceList(Functions);
Chris Lattnerafade922002-11-20 22:28:10 +0000295
Chris Lattneraae33f92003-04-24 22:24:58 +0000296 if (Functions.size() < OldSize) {
297 EmitProgressBytecode("reduced-function");
298 AnyReduction = true;
Chris Lattner65207852003-01-23 02:48:33 +0000299 }
Chris Lattnerafade922002-11-20 22:28:10 +0000300 }
301
Chris Lattner286921e2003-04-24 23:51:38 +0000302 // Attempt to delete entire basic blocks at a time to speed up
303 // convergence... this actually works by setting the terminator of the blocks
304 // to a return instruction then running simplifycfg, which can potentially
305 // shrinks the code dramatically quickly
306 //
Chris Lattner47ae4a12003-08-05 15:51:05 +0000307 if (!DisableSimplifyCFG) {
308 std::vector<BasicBlock*> Blocks;
309 for (Module::iterator I = Program->begin(), E = Program->end(); I != E; ++I)
310 for (Function::iterator FI = I->begin(), E = I->end(); FI != E; ++FI)
311 Blocks.push_back(FI);
312 ReduceCrashingBlocks(*this).reduceList(Blocks);
313 }
Chris Lattner218e26e2002-12-23 23:49:59 +0000314
Chris Lattneraae33f92003-04-24 22:24:58 +0000315 // FIXME: This should use the list reducer to converge faster by deleting
316 // larger chunks of instructions at a time!
Chris Lattner65207852003-01-23 02:48:33 +0000317 unsigned Simplification = 4;
318 do {
319 --Simplification;
320 std::cout << "\n*** Attempting to reduce testcase by deleting instruc"
321 << "tions: Simplification Level #" << Simplification << "\n";
322
Misha Brukman5560c9d2003-08-18 14:43:39 +0000323 // Now that we have deleted the functions that are unnecessary for the
324 // program, try to remove instructions that are not necessary to cause the
Chris Lattner65207852003-01-23 02:48:33 +0000325 // crash. To do this, we loop through all of the instructions in the
326 // remaining functions, deleting them (replacing any values produced with
327 // nulls), and then running ADCE and SimplifyCFG. If the transformed input
328 // still triggers failure, keep deleting until we cannot trigger failure
329 // anymore.
330 //
331 TryAgain:
332
333 // Loop over all of the (non-terminator) instructions remaining in the
334 // function, attempting to delete them.
335 for (Module::iterator FI = Program->begin(), E = Program->end();
336 FI != E; ++FI)
337 if (!FI->isExternal()) {
338 for (Function::iterator BI = FI->begin(), E = FI->end(); BI != E; ++BI)
339 for (BasicBlock::iterator I = BI->begin(), E = --BI->end();
340 I != E; ++I) {
341 Module *M = deleteInstructionFromProgram(I, Simplification);
342
343 // Make the function the current program...
344 std::swap(Program, M);
345
346 // Find out if the pass still crashes on this pass...
347 std::cout << "Checking instruction '" << I->getName() << "': ";
Chris Lattneraae33f92003-04-24 22:24:58 +0000348 if (runPasses(PassesToRun)) {
Chris Lattner65207852003-01-23 02:48:33 +0000349 // Yup, it does, we delete the old module, and continue trying to
350 // reduce the testcase...
Chris Lattner65207852003-01-23 02:48:33 +0000351 delete M;
Chris Lattnerf607b792003-04-24 22:54:06 +0000352 AnyReduction = true;
Chris Lattner65207852003-01-23 02:48:33 +0000353 goto TryAgain; // I wish I had a multi-level break here!
354 }
355
356 // This pass didn't crash without this instruction, try the next
357 // one.
358 delete Program;
359 Program = M;
360 }
361 }
362 } while (Simplification);
Chris Lattnerba386d92003-02-28 16:13:20 +0000363
364 // Try to clean up the testcase by running funcresolve and globaldce...
Chris Lattner898e0e42003-06-25 04:13:52 +0000365 std::cout << "\n*** Attempting to perform final cleanups: ";
366 Module *M = performFinalCleanups();
367 std::swap(Program, M);
Chris Lattnerba386d92003-02-28 16:13:20 +0000368
Chris Lattner898e0e42003-06-25 04:13:52 +0000369 // Find out if the pass still crashes on the cleaned up program...
370 if (runPasses(PassesToRun)) {
371 // Yup, it does, keep the reduced version...
372 delete M;
373 AnyReduction = true;
374 } else {
375 delete Program; // Otherwise, restore the original module...
376 Program = M;
Chris Lattnerba386d92003-02-28 16:13:20 +0000377 }
378
Chris Lattnerf607b792003-04-24 22:54:06 +0000379 if (AnyReduction)
Chris Lattner640f22e2003-04-24 17:02:17 +0000380 EmitProgressBytecode("reduced-simplified");
Chris Lattnerba386d92003-02-28 16:13:20 +0000381
Chris Lattnerafade922002-11-20 22:28:10 +0000382 return false;
383}