blob: d9d52dccc01c82bd25cda6bce4de2ac22b728087 [file] [log] [blame]
Chris Lattnerafade922002-11-20 22:28:10 +00001//===- CrashDebugger.cpp - Debug compilation crashes ----------------------===//
John Criswell7c0e0222003-10-20 17:47:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnerafade922002-11-20 22:28:10 +00009//
10// This file defines the bugpoint internals that narrow down compilation crashes
11//
12//===----------------------------------------------------------------------===//
13
14#include "BugDriver.h"
Chris Lattneraae33f92003-04-24 22:24:58 +000015#include "ListReducer.h"
Chris Lattner286921e2003-04-24 23:51:38 +000016#include "llvm/Constant.h"
17#include "llvm/iTerminators.h"
Misha Brukmane49603d2003-08-07 21:19:30 +000018#include "llvm/Module.h"
19#include "llvm/Pass.h"
20#include "llvm/PassManager.h"
Chris Lattner286921e2003-04-24 23:51:38 +000021#include "llvm/SymbolTable.h"
Misha Brukmane49603d2003-08-07 21:19:30 +000022#include "llvm/Type.h"
Chris Lattner286921e2003-04-24 23:51:38 +000023#include "llvm/Analysis/Verifier.h"
Misha Brukmane49603d2003-08-07 21:19:30 +000024#include "llvm/Bytecode/Writer.h"
25#include "llvm/Support/CFG.h"
Chris Lattner8b189272004-02-18 23:26:28 +000026#include "llvm/Support/ToolRunner.h"
Chris Lattner286921e2003-04-24 23:51:38 +000027#include "llvm/Transforms/Scalar.h"
Chris Lattneraae33f92003-04-24 22:24:58 +000028#include "llvm/Transforms/Utils/Cloning.h"
Misha Brukman3d9cafa2003-08-07 21:42:28 +000029#include "Support/FileUtilities.h"
Chris Lattnerafade922002-11-20 22:28:10 +000030#include <fstream>
Chris Lattneraae33f92003-04-24 22:24:58 +000031#include <set>
Chris Lattnerfa761832004-01-14 03:38:37 +000032using namespace llvm;
Chris Lattnerafade922002-11-20 22:28:10 +000033
Brian Gaeked0fde302003-11-11 22:41:34 +000034namespace llvm {
Chris Lattner06905db2004-02-18 21:24:48 +000035 class ReducePassList : public ListReducer<const PassInfo*> {
Chris Lattnerfa761832004-01-14 03:38:37 +000036 BugDriver &BD;
37 public:
Chris Lattner06905db2004-02-18 21:24:48 +000038 ReducePassList(BugDriver &bd) : BD(bd) {}
Chris Lattnerfa761832004-01-14 03:38:37 +000039
40 // doTest - Return true iff running the "removed" passes succeeds, and
41 // running the "Kept" passes fail when run on the output of the "removed"
42 // passes. If we return true, we update the current module of bugpoint.
43 //
44 virtual TestResult doTest(std::vector<const PassInfo*> &Removed,
45 std::vector<const PassInfo*> &Kept);
46 };
47}
Chris Lattneraae33f92003-04-24 22:24:58 +000048
Chris Lattner06905db2004-02-18 21:24:48 +000049ReducePassList::TestResult
50ReducePassList::doTest(std::vector<const PassInfo*> &Prefix,
51 std::vector<const PassInfo*> &Suffix) {
Chris Lattneraae33f92003-04-24 22:24:58 +000052 std::string PrefixOutput;
Chris Lattnerb417c792003-06-02 04:54:29 +000053 Module *OrigProgram = 0;
Chris Lattneraae33f92003-04-24 22:24:58 +000054 if (!Prefix.empty()) {
55 std::cout << "Checking to see if these passes crash: "
56 << getPassesString(Prefix) << ": ";
57 if (BD.runPasses(Prefix, PrefixOutput))
58 return KeepPrefix;
Chris Lattnerb417c792003-06-02 04:54:29 +000059
60 OrigProgram = BD.Program;
61
62 BD.Program = BD.ParseInputFile(PrefixOutput);
63 if (BD.Program == 0) {
64 std::cerr << BD.getToolName() << ": Error reading bytecode file '"
65 << PrefixOutput << "'!\n";
66 exit(1);
67 }
68 removeFile(PrefixOutput);
Chris Lattneraae33f92003-04-24 22:24:58 +000069 }
70
71 std::cout << "Checking to see if these passes crash: "
72 << getPassesString(Suffix) << ": ";
Chris Lattnerb417c792003-06-02 04:54:29 +000073
Chris Lattneraae33f92003-04-24 22:24:58 +000074 if (BD.runPasses(Suffix)) {
75 delete OrigProgram; // The suffix crashes alone...
76 return KeepSuffix;
77 }
78
79 // Nothing failed, restore state...
Chris Lattnerb417c792003-06-02 04:54:29 +000080 if (OrigProgram) {
81 delete BD.Program;
82 BD.Program = OrigProgram;
83 }
Chris Lattneraae33f92003-04-24 22:24:58 +000084 return NoFailure;
85}
86
Chris Lattnerfa761832004-01-14 03:38:37 +000087namespace llvm {
Chris Lattner8b189272004-02-18 23:26:28 +000088 class ReduceCrashingFunctions : public ListReducer<const Function*> {
Chris Lattnerfa761832004-01-14 03:38:37 +000089 BugDriver &BD;
Chris Lattner8b189272004-02-18 23:26:28 +000090 bool (*TestFn)(BugDriver &, Module *);
Chris Lattnerfa761832004-01-14 03:38:37 +000091 public:
Chris Lattner8b189272004-02-18 23:26:28 +000092 ReduceCrashingFunctions(BugDriver &bd,
93 bool (*testFn)(BugDriver &, Module *))
94 : BD(bd), TestFn(testFn) {}
Chris Lattnerfa761832004-01-14 03:38:37 +000095
Chris Lattner8b189272004-02-18 23:26:28 +000096 virtual TestResult doTest(std::vector<const Function*> &Prefix,
97 std::vector<const Function*> &Kept) {
Chris Lattnerfa761832004-01-14 03:38:37 +000098 if (!Kept.empty() && TestFuncs(Kept))
99 return KeepSuffix;
100 if (!Prefix.empty() && TestFuncs(Prefix))
101 return KeepPrefix;
102 return NoFailure;
103 }
104
Chris Lattner8b189272004-02-18 23:26:28 +0000105 bool TestFuncs(std::vector<const Function*> &Prefix);
Chris Lattnerfa761832004-01-14 03:38:37 +0000106 };
107}
Chris Lattneraae33f92003-04-24 22:24:58 +0000108
Chris Lattner8b189272004-02-18 23:26:28 +0000109bool ReduceCrashingFunctions::TestFuncs(std::vector<const Function*> &Funcs) {
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000110 // Clone the program to try hacking it apart...
Chris Lattner06905db2004-02-18 21:24:48 +0000111 Module *M = CloneModule(BD.getProgram());
Chris Lattneraae33f92003-04-24 22:24:58 +0000112
113 // Convert list to set for fast lookup...
114 std::set<Function*> Functions;
115 for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
116 Function *CMF = M->getFunction(Funcs[i]->getName(),
117 Funcs[i]->getFunctionType());
118 assert(CMF && "Function not in module?!");
Chris Lattnerf607b792003-04-24 22:54:06 +0000119 Functions.insert(CMF);
Chris Lattneraae33f92003-04-24 22:24:58 +0000120 }
121
122 std::cout << "Checking for crash with only these functions:";
Chris Lattner73b96bd2003-10-27 04:44:59 +0000123 unsigned NumPrint = Funcs.size();
124 if (NumPrint > 10) NumPrint = 10;
125 for (unsigned i = 0; i != NumPrint; ++i)
Chris Lattneraae33f92003-04-24 22:24:58 +0000126 std::cout << " " << Funcs[i]->getName();
Chris Lattner73b96bd2003-10-27 04:44:59 +0000127 if (NumPrint < Funcs.size())
128 std::cout << "... <" << Funcs.size() << " total>";
Chris Lattneraae33f92003-04-24 22:24:58 +0000129 std::cout << ": ";
130
131 // Loop over and delete any functions which we aren't supposed to be playing
132 // with...
133 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerf607b792003-04-24 22:54:06 +0000134 if (!I->isExternal() && !Functions.count(I))
Chris Lattneraae33f92003-04-24 22:24:58 +0000135 DeleteFunctionBody(I);
136
137 // Try running the hacked up program...
Chris Lattner8b189272004-02-18 23:26:28 +0000138 if (TestFn(BD, M)) {
Chris Lattner06905db2004-02-18 21:24:48 +0000139 BD.setNewProgram(M); // It crashed, keep the trimmed version...
Chris Lattneraae33f92003-04-24 22:24:58 +0000140
141 // Make sure to use function pointers that point into the now-current
142 // module.
143 Funcs.assign(Functions.begin(), Functions.end());
144 return true;
145 }
Chris Lattner06905db2004-02-18 21:24:48 +0000146 delete M;
Chris Lattneraae33f92003-04-24 22:24:58 +0000147 return false;
148}
149
Chris Lattner640f22e2003-04-24 17:02:17 +0000150
Chris Lattnerf913f402004-02-18 21:29:46 +0000151namespace {
Chris Lattnerfa761832004-01-14 03:38:37 +0000152 /// ReduceCrashingBlocks reducer - This works by setting the terminators of
153 /// all terminators except the specified basic blocks to a 'ret' instruction,
154 /// then running the simplify-cfg pass. This has the effect of chopping up
155 /// the CFG really fast which can reduce large functions quickly.
156 ///
Chris Lattner8b189272004-02-18 23:26:28 +0000157 class ReduceCrashingBlocks : public ListReducer<const BasicBlock*> {
Chris Lattnerfa761832004-01-14 03:38:37 +0000158 BugDriver &BD;
Chris Lattner8b189272004-02-18 23:26:28 +0000159 bool (*TestFn)(BugDriver &, Module *);
Chris Lattnerfa761832004-01-14 03:38:37 +0000160 public:
Chris Lattner8b189272004-02-18 23:26:28 +0000161 ReduceCrashingBlocks(BugDriver &bd, bool (*testFn)(BugDriver &, Module *))
162 : BD(bd), TestFn(testFn) {}
Chris Lattner286921e2003-04-24 23:51:38 +0000163
Chris Lattner8b189272004-02-18 23:26:28 +0000164 virtual TestResult doTest(std::vector<const BasicBlock*> &Prefix,
165 std::vector<const BasicBlock*> &Kept) {
Chris Lattnerfa761832004-01-14 03:38:37 +0000166 if (!Kept.empty() && TestBlocks(Kept))
167 return KeepSuffix;
168 if (!Prefix.empty() && TestBlocks(Prefix))
169 return KeepPrefix;
170 return NoFailure;
171 }
Chris Lattner286921e2003-04-24 23:51:38 +0000172
Chris Lattner8b189272004-02-18 23:26:28 +0000173 bool TestBlocks(std::vector<const BasicBlock*> &Prefix);
Chris Lattnerfa761832004-01-14 03:38:37 +0000174 };
175}
Chris Lattner286921e2003-04-24 23:51:38 +0000176
Chris Lattner8b189272004-02-18 23:26:28 +0000177bool ReduceCrashingBlocks::TestBlocks(std::vector<const BasicBlock*> &BBs) {
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000178 // Clone the program to try hacking it apart...
Chris Lattner06905db2004-02-18 21:24:48 +0000179 Module *M = CloneModule(BD.getProgram());
Chris Lattner286921e2003-04-24 23:51:38 +0000180
181 // Convert list to set for fast lookup...
182 std::set<BasicBlock*> Blocks;
183 for (unsigned i = 0, e = BBs.size(); i != e; ++i) {
184 // Convert the basic block from the original module to the new module...
Chris Lattner8b189272004-02-18 23:26:28 +0000185 const Function *F = BBs[i]->getParent();
Chris Lattner286921e2003-04-24 23:51:38 +0000186 Function *CMF = M->getFunction(F->getName(), F->getFunctionType());
187 assert(CMF && "Function not in module?!");
188
189 // Get the mapped basic block...
190 Function::iterator CBI = CMF->begin();
Chris Lattner8b189272004-02-18 23:26:28 +0000191 std::advance(CBI, std::distance(F->begin(),
192 Function::const_iterator(BBs[i])));
Chris Lattner286921e2003-04-24 23:51:38 +0000193 Blocks.insert(CBI);
194 }
195
196 std::cout << "Checking for crash with only these blocks:";
Chris Lattner73b96bd2003-10-27 04:44:59 +0000197 unsigned NumPrint = Blocks.size();
198 if (NumPrint > 10) NumPrint = 10;
199 for (unsigned i = 0, e = NumPrint; i != e; ++i)
Chris Lattner286921e2003-04-24 23:51:38 +0000200 std::cout << " " << BBs[i]->getName();
Chris Lattner73b96bd2003-10-27 04:44:59 +0000201 if (NumPrint < Blocks.size())
202 std::cout << "... <" << Blocks.size() << " total>";
Chris Lattner286921e2003-04-24 23:51:38 +0000203 std::cout << ": ";
204
205 // Loop over and delete any hack up any blocks that are not listed...
206 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
207 for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
Chris Lattner8bc098b2003-11-22 02:10:38 +0000208 if (!Blocks.count(BB) && BB->getTerminator()->getNumSuccessors()) {
Chris Lattner286921e2003-04-24 23:51:38 +0000209 // Loop over all of the successors of this block, deleting any PHI nodes
210 // that might include it.
211 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
212 (*SI)->removePredecessor(BB);
213
Chris Lattner8bc098b2003-11-22 02:10:38 +0000214 if (BB->getTerminator()->getType() != Type::VoidTy)
215 BB->getTerminator()->replaceAllUsesWith(
216 Constant::getNullValue(BB->getTerminator()->getType()));
217
Chris Lattner286921e2003-04-24 23:51:38 +0000218 // Delete the old terminator instruction...
219 BB->getInstList().pop_back();
220
221 // Add a new return instruction of the appropriate type...
222 const Type *RetTy = BB->getParent()->getReturnType();
Chris Lattner8bc098b2003-11-22 02:10:38 +0000223 new ReturnInst(RetTy == Type::VoidTy ? 0 :
224 Constant::getNullValue(RetTy), BB);
Chris Lattner286921e2003-04-24 23:51:38 +0000225 }
226
227 // The CFG Simplifier pass may delete one of the basic blocks we are
228 // interested in. If it does we need to take the block out of the list. Make
229 // a "persistent mapping" by turning basic blocks into <function, name> pairs.
230 // This won't work well if blocks are unnamed, but that is just the risk we
231 // have to take.
232 std::vector<std::pair<Function*, std::string> > BlockInfo;
233
234 for (std::set<BasicBlock*>::iterator I = Blocks.begin(), E = Blocks.end();
235 I != E; ++I)
236 BlockInfo.push_back(std::make_pair((*I)->getParent(), (*I)->getName()));
237
238 // Now run the CFG simplify pass on the function...
239 PassManager Passes;
240 Passes.add(createCFGSimplificationPass());
241 Passes.add(createVerifierPass());
242 Passes.run(*M);
243
244 // Try running on the hacked up program...
Chris Lattner8b189272004-02-18 23:26:28 +0000245 if (TestFn(BD, M)) {
Chris Lattner06905db2004-02-18 21:24:48 +0000246 BD.setNewProgram(M); // It crashed, keep the trimmed version...
Chris Lattner286921e2003-04-24 23:51:38 +0000247
248 // Make sure to use basic block pointers that point into the now-current
249 // module, and that they don't include any deleted blocks.
250 BBs.clear();
251 for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) {
252 SymbolTable &ST = BlockInfo[i].first->getSymbolTable();
253 SymbolTable::iterator I = ST.find(Type::LabelTy);
254 if (I != ST.end() && I->second.count(BlockInfo[i].second))
255 BBs.push_back(cast<BasicBlock>(I->second[BlockInfo[i].second]));
256 }
257 return true;
258 }
Chris Lattner06905db2004-02-18 21:24:48 +0000259 delete M; // It didn't crash, try something else.
Chris Lattner286921e2003-04-24 23:51:38 +0000260 return false;
261}
262
Chris Lattner8b189272004-02-18 23:26:28 +0000263/// DebugACrash - Given a predicate that determines whether a component crashes
264/// on a program, try to destructively reduce the program while still keeping
265/// the predicate true.
266static bool DebugACrash(BugDriver &BD, bool (*TestFn)(BugDriver &, Module *)) {
Chris Lattneraae33f92003-04-24 22:24:58 +0000267 bool AnyReduction = false;
Chris Lattner5f73e382003-04-25 00:53:05 +0000268
269 // See if we can get away with nuking all of the global variable initializers
270 // in the program...
Chris Lattner8b189272004-02-18 23:26:28 +0000271 if (BD.getProgram()->gbegin() != BD.getProgram()->gend()) {
272 Module *M = CloneModule(BD.getProgram());
Chris Lattner5f73e382003-04-25 00:53:05 +0000273 bool DeletedInit = false;
274 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
275 if (I->hasInitializer()) {
276 I->setInitializer(0);
277 I->setLinkage(GlobalValue::ExternalLinkage);
278 DeletedInit = true;
279 }
280
281 if (!DeletedInit) {
282 delete M; // No change made...
283 } else {
284 // See if the program still causes a crash...
285 std::cout << "\nChecking to see if we can delete global inits: ";
Chris Lattner8b189272004-02-18 23:26:28 +0000286 if (TestFn(BD, M)) { // Still crashes?
287 BD.setNewProgram(M);
Chris Lattner5f73e382003-04-25 00:53:05 +0000288 AnyReduction = true;
Chris Lattner5f73e382003-04-25 00:53:05 +0000289 std::cout << "\n*** Able to remove all global initializers!\n";
290 } else { // No longer crashes?
Chris Lattner5f73e382003-04-25 00:53:05 +0000291 std::cout << " - Removing all global inits hides problem!\n";
Chris Lattner06905db2004-02-18 21:24:48 +0000292 delete M;
Chris Lattner5f73e382003-04-25 00:53:05 +0000293 }
294 }
295 }
Chris Lattneraae33f92003-04-24 22:24:58 +0000296
297 // Now try to reduce the number of functions in the module to something small.
Chris Lattner8b189272004-02-18 23:26:28 +0000298 std::vector<const Function*> Functions;
299 for (Module::const_iterator I = BD.getProgram()->begin(),
300 E = BD.getProgram()->end(); I != E; ++I)
Chris Lattneraae33f92003-04-24 22:24:58 +0000301 if (!I->isExternal())
302 Functions.push_back(I);
Chris Lattnerafade922002-11-20 22:28:10 +0000303
Chris Lattneraae33f92003-04-24 22:24:58 +0000304 if (Functions.size() > 1) {
305 std::cout << "\n*** Attempting to reduce the number of functions "
306 "in the testcase\n";
Chris Lattnerafade922002-11-20 22:28:10 +0000307
Chris Lattner8b189272004-02-18 23:26:28 +0000308 unsigned OldSize = Functions.size();
309 ReduceCrashingFunctions(BD, TestFn).reduceList(Functions);
Chris Lattnerafade922002-11-20 22:28:10 +0000310
Chris Lattneraae33f92003-04-24 22:24:58 +0000311 if (Functions.size() < OldSize) {
Chris Lattner8b189272004-02-18 23:26:28 +0000312 BD.EmitProgressBytecode("reduced-function");
Chris Lattneraae33f92003-04-24 22:24:58 +0000313 AnyReduction = true;
Chris Lattner65207852003-01-23 02:48:33 +0000314 }
Chris Lattnerafade922002-11-20 22:28:10 +0000315 }
316
Chris Lattner286921e2003-04-24 23:51:38 +0000317 // Attempt to delete entire basic blocks at a time to speed up
318 // convergence... this actually works by setting the terminator of the blocks
319 // to a return instruction then running simplifycfg, which can potentially
320 // shrinks the code dramatically quickly
321 //
Chris Lattner47ae4a12003-08-05 15:51:05 +0000322 if (!DisableSimplifyCFG) {
Chris Lattner8b189272004-02-18 23:26:28 +0000323 std::vector<const BasicBlock*> Blocks;
324 for (Module::const_iterator I = BD.getProgram()->begin(),
325 E = BD.getProgram()->end(); I != E; ++I)
326 for (Function::const_iterator FI = I->begin(), E = I->end(); FI !=E; ++FI)
Chris Lattner47ae4a12003-08-05 15:51:05 +0000327 Blocks.push_back(FI);
Chris Lattner8b189272004-02-18 23:26:28 +0000328 ReduceCrashingBlocks(BD, TestFn).reduceList(Blocks);
Chris Lattner47ae4a12003-08-05 15:51:05 +0000329 }
Chris Lattner218e26e2002-12-23 23:49:59 +0000330
Chris Lattneraae33f92003-04-24 22:24:58 +0000331 // FIXME: This should use the list reducer to converge faster by deleting
332 // larger chunks of instructions at a time!
Chris Lattner65207852003-01-23 02:48:33 +0000333 unsigned Simplification = 4;
334 do {
335 --Simplification;
336 std::cout << "\n*** Attempting to reduce testcase by deleting instruc"
337 << "tions: Simplification Level #" << Simplification << "\n";
338
Misha Brukman5560c9d2003-08-18 14:43:39 +0000339 // Now that we have deleted the functions that are unnecessary for the
340 // program, try to remove instructions that are not necessary to cause the
Chris Lattner65207852003-01-23 02:48:33 +0000341 // crash. To do this, we loop through all of the instructions in the
342 // remaining functions, deleting them (replacing any values produced with
343 // nulls), and then running ADCE and SimplifyCFG. If the transformed input
344 // still triggers failure, keep deleting until we cannot trigger failure
345 // anymore.
346 //
347 TryAgain:
348
349 // Loop over all of the (non-terminator) instructions remaining in the
350 // function, attempting to delete them.
Chris Lattner8b189272004-02-18 23:26:28 +0000351 for (Module::const_iterator FI = BD.getProgram()->begin(),
352 E = BD.getProgram()->end(); FI != E; ++FI)
Chris Lattner65207852003-01-23 02:48:33 +0000353 if (!FI->isExternal()) {
Chris Lattner8b189272004-02-18 23:26:28 +0000354 for (Function::const_iterator BI = FI->begin(), E = FI->end(); BI != E;
355 ++BI)
356 for (BasicBlock::const_iterator I = BI->begin(), E = --BI->end();
Chris Lattner65207852003-01-23 02:48:33 +0000357 I != E; ++I) {
Chris Lattner8b189272004-02-18 23:26:28 +0000358 Module *M = BD.deleteInstructionFromProgram(I, Simplification);
Chris Lattner65207852003-01-23 02:48:33 +0000359
Chris Lattner65207852003-01-23 02:48:33 +0000360 // Find out if the pass still crashes on this pass...
361 std::cout << "Checking instruction '" << I->getName() << "': ";
Chris Lattner8b189272004-02-18 23:26:28 +0000362 if (TestFn(BD, M)) {
Chris Lattner65207852003-01-23 02:48:33 +0000363 // Yup, it does, we delete the old module, and continue trying to
364 // reduce the testcase...
Chris Lattner8b189272004-02-18 23:26:28 +0000365 BD.setNewProgram(M);
Chris Lattnerf607b792003-04-24 22:54:06 +0000366 AnyReduction = true;
Chris Lattner65207852003-01-23 02:48:33 +0000367 goto TryAgain; // I wish I had a multi-level break here!
368 }
369
370 // This pass didn't crash without this instruction, try the next
371 // one.
Chris Lattner06905db2004-02-18 21:24:48 +0000372 delete M;
Chris Lattner65207852003-01-23 02:48:33 +0000373 }
374 }
375 } while (Simplification);
Chris Lattnerba386d92003-02-28 16:13:20 +0000376
377 // Try to clean up the testcase by running funcresolve and globaldce...
Chris Lattner898e0e42003-06-25 04:13:52 +0000378 std::cout << "\n*** Attempting to perform final cleanups: ";
Chris Lattner8b189272004-02-18 23:26:28 +0000379 Module *M = CloneModule(BD.getProgram());
380 M = BD.performFinalCleanups(M, true);
Chris Lattnerba386d92003-02-28 16:13:20 +0000381
Chris Lattner898e0e42003-06-25 04:13:52 +0000382 // Find out if the pass still crashes on the cleaned up program...
Chris Lattner8b189272004-02-18 23:26:28 +0000383 if (TestFn(BD, M)) {
384 BD.setNewProgram(M); // Yup, it does, keep the reduced version...
Chris Lattner898e0e42003-06-25 04:13:52 +0000385 AnyReduction = true;
386 } else {
Chris Lattner06905db2004-02-18 21:24:48 +0000387 delete M;
Chris Lattnerba386d92003-02-28 16:13:20 +0000388 }
389
Chris Lattnerf607b792003-04-24 22:54:06 +0000390 if (AnyReduction)
Chris Lattner8b189272004-02-18 23:26:28 +0000391 BD.EmitProgressBytecode("reduced-simplified");
Chris Lattnerba386d92003-02-28 16:13:20 +0000392
Chris Lattner8b189272004-02-18 23:26:28 +0000393 return false;
Chris Lattnerafade922002-11-20 22:28:10 +0000394}
Brian Gaeked0fde302003-11-11 22:41:34 +0000395
Chris Lattner8b189272004-02-18 23:26:28 +0000396static bool TestForOptimizerCrash(BugDriver &BD, Module *M) {
397 return BD.runPasses(M);
398}
Chris Lattner02526262004-02-18 21:02:04 +0000399
Chris Lattner8b189272004-02-18 23:26:28 +0000400/// debugOptimizerCrash - This method is called when some pass crashes on input.
401/// It attempts to prune down the testcase to something reasonable, and figure
402/// out exactly which pass is crashing.
403///
404bool BugDriver::debugOptimizerCrash() {
405 std::cout << "\n*** Debugging optimizer crash!\n";
406
407 // Reduce the list of passes which causes the optimizer to crash...
408 unsigned OldSize = PassesToRun.size();
409 ReducePassList(*this).reduceList(PassesToRun);
410
411 std::cout << "\n*** Found crashing pass"
412 << (PassesToRun.size() == 1 ? ": " : "es: ")
413 << getPassesString(PassesToRun) << "\n";
414
415 EmitProgressBytecode("passinput");
416
417 return DebugACrash(*this, TestForOptimizerCrash);
418}
419
420static bool TestForCodeGenCrash(BugDriver &BD, Module *M) {
421 try {
422 std::cerr << "\n";
423 BD.compileProgram(M);
424 return false;
425 } catch (ToolExecutionError &TEE) {
426 std::cerr << "<crash>\n";
427 return true; // Tool is still crashing.
428 }
429}
Chris Lattner02526262004-02-18 21:02:04 +0000430
431/// debugCodeGeneratorCrash - This method is called when the code generator
432/// crashes on an input. It attempts to reduce the input as much as possible
433/// while still causing the code generator to crash.
434bool BugDriver::debugCodeGeneratorCrash() {
Chris Lattner06905db2004-02-18 21:24:48 +0000435 std::cerr << "*** Debugging code generator crash!\n";
Chris Lattner02526262004-02-18 21:02:04 +0000436
Chris Lattner8b189272004-02-18 23:26:28 +0000437 return DebugACrash(*this, TestForCodeGenCrash);
Chris Lattner02526262004-02-18 21:02:04 +0000438}