blob: 0d04b2563fe7b9de1ec57aa9451841da8246fc0d [file] [log] [blame]
Chris Lattnerafade922002-11-20 22:28:10 +00001//===- CrashDebugger.cpp - Debug compilation crashes ----------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// 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.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
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"
Misha Brukman47b14a42004-07-29 17:30:56 +000017#include "llvm/Instructions.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"
Reid Spencer551ccae2004-09-01 22:55:40 +000029#include "llvm/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) {}
Misha Brukman3da94ae2005-04-22 00:00:37 +000039
Chris Lattnerfa761832004-01-14 03:38:37 +000040 // 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) {
Reid Spencer5f767602004-12-16 23:04:20 +000052 sys::Path 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) << ": ";
Reid Spencer5f767602004-12-16 23:04:20 +000057 std::string PfxOutput;
58 if (BD.runPasses(Prefix, PfxOutput))
Chris Lattneraae33f92003-04-24 22:24:58 +000059 return KeepPrefix;
Chris Lattnerb417c792003-06-02 04:54:29 +000060
Reid Spencerdd04df02005-07-07 23:21:43 +000061 PrefixOutput.set(PfxOutput);
Chris Lattnerb417c792003-06-02 04:54:29 +000062 OrigProgram = BD.Program;
63
Reid Spencer5f767602004-12-16 23:04:20 +000064 BD.Program = ParseInputFile(PrefixOutput.toString());
Chris Lattnerb417c792003-06-02 04:54:29 +000065 if (BD.Program == 0) {
66 std::cerr << BD.getToolName() << ": Error reading bytecode file '"
67 << PrefixOutput << "'!\n";
68 exit(1);
69 }
Reid Spencera229c5c2005-07-08 03:08:58 +000070 PrefixOutput.eraseFromDisk();
Chris Lattneraae33f92003-04-24 22:24:58 +000071 }
72
73 std::cout << "Checking to see if these passes crash: "
74 << getPassesString(Suffix) << ": ";
Misha Brukman3da94ae2005-04-22 00:00:37 +000075
Chris Lattneraae33f92003-04-24 22:24:58 +000076 if (BD.runPasses(Suffix)) {
77 delete OrigProgram; // The suffix crashes alone...
78 return KeepSuffix;
79 }
80
81 // Nothing failed, restore state...
Chris Lattnerb417c792003-06-02 04:54:29 +000082 if (OrigProgram) {
83 delete BD.Program;
84 BD.Program = OrigProgram;
85 }
Chris Lattneraae33f92003-04-24 22:24:58 +000086 return NoFailure;
87}
88
Chris Lattnerfa761832004-01-14 03:38:37 +000089namespace llvm {
Chris Lattnerefdc0b52004-03-14 20:50:42 +000090 class ReduceCrashingFunctions : public ListReducer<Function*> {
Chris Lattnerfa761832004-01-14 03:38:37 +000091 BugDriver &BD;
Chris Lattner8b189272004-02-18 23:26:28 +000092 bool (*TestFn)(BugDriver &, Module *);
Chris Lattnerfa761832004-01-14 03:38:37 +000093 public:
Chris Lattner8b189272004-02-18 23:26:28 +000094 ReduceCrashingFunctions(BugDriver &bd,
95 bool (*testFn)(BugDriver &, Module *))
96 : BD(bd), TestFn(testFn) {}
Misha Brukman3da94ae2005-04-22 00:00:37 +000097
Chris Lattnerefdc0b52004-03-14 20:50:42 +000098 virtual TestResult doTest(std::vector<Function*> &Prefix,
99 std::vector<Function*> &Kept) {
Chris Lattnerfa761832004-01-14 03:38:37 +0000100 if (!Kept.empty() && TestFuncs(Kept))
101 return KeepSuffix;
102 if (!Prefix.empty() && TestFuncs(Prefix))
103 return KeepPrefix;
104 return NoFailure;
105 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000106
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000107 bool TestFuncs(std::vector<Function*> &Prefix);
Chris Lattnerfa761832004-01-14 03:38:37 +0000108 };
109}
Chris Lattneraae33f92003-04-24 22:24:58 +0000110
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000111bool ReduceCrashingFunctions::TestFuncs(std::vector<Function*> &Funcs) {
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000112 // Clone the program to try hacking it apart...
Chris Lattner06905db2004-02-18 21:24:48 +0000113 Module *M = CloneModule(BD.getProgram());
Misha Brukman3da94ae2005-04-22 00:00:37 +0000114
Chris Lattneraae33f92003-04-24 22:24:58 +0000115 // Convert list to set for fast lookup...
116 std::set<Function*> Functions;
117 for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
Misha Brukman3da94ae2005-04-22 00:00:37 +0000118 Function *CMF = M->getFunction(Funcs[i]->getName(),
Chris Lattneraae33f92003-04-24 22:24:58 +0000119 Funcs[i]->getFunctionType());
120 assert(CMF && "Function not in module?!");
Chris Lattnerf607b792003-04-24 22:54:06 +0000121 Functions.insert(CMF);
Chris Lattneraae33f92003-04-24 22:24:58 +0000122 }
123
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000124 std::cout << "Checking for crash with only these functions: ";
125 PrintFunctionList(Funcs);
Chris Lattneraae33f92003-04-24 22:24:58 +0000126 std::cout << ": ";
127
128 // Loop over and delete any functions which we aren't supposed to be playing
129 // with...
130 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerf607b792003-04-24 22:54:06 +0000131 if (!I->isExternal() && !Functions.count(I))
Chris Lattneraae33f92003-04-24 22:24:58 +0000132 DeleteFunctionBody(I);
133
134 // Try running the hacked up program...
Chris Lattner8b189272004-02-18 23:26:28 +0000135 if (TestFn(BD, M)) {
Chris Lattner06905db2004-02-18 21:24:48 +0000136 BD.setNewProgram(M); // It crashed, keep the trimmed version...
Chris Lattneraae33f92003-04-24 22:24:58 +0000137
138 // Make sure to use function pointers that point into the now-current
139 // module.
140 Funcs.assign(Functions.begin(), Functions.end());
141 return true;
142 }
Chris Lattner06905db2004-02-18 21:24:48 +0000143 delete M;
Chris Lattneraae33f92003-04-24 22:24:58 +0000144 return false;
145}
146
Chris Lattner640f22e2003-04-24 17:02:17 +0000147
Chris Lattnerf913f402004-02-18 21:29:46 +0000148namespace {
Chris Lattnerfa761832004-01-14 03:38:37 +0000149 /// ReduceCrashingBlocks reducer - This works by setting the terminators of
150 /// all terminators except the specified basic blocks to a 'ret' instruction,
151 /// then running the simplify-cfg pass. This has the effect of chopping up
152 /// the CFG really fast which can reduce large functions quickly.
153 ///
Chris Lattner8b189272004-02-18 23:26:28 +0000154 class ReduceCrashingBlocks : public ListReducer<const BasicBlock*> {
Chris Lattnerfa761832004-01-14 03:38:37 +0000155 BugDriver &BD;
Chris Lattner8b189272004-02-18 23:26:28 +0000156 bool (*TestFn)(BugDriver &, Module *);
Chris Lattnerfa761832004-01-14 03:38:37 +0000157 public:
Chris Lattner8b189272004-02-18 23:26:28 +0000158 ReduceCrashingBlocks(BugDriver &bd, bool (*testFn)(BugDriver &, Module *))
159 : BD(bd), TestFn(testFn) {}
Misha Brukman3da94ae2005-04-22 00:00:37 +0000160
Chris Lattner8b189272004-02-18 23:26:28 +0000161 virtual TestResult doTest(std::vector<const BasicBlock*> &Prefix,
162 std::vector<const BasicBlock*> &Kept) {
Chris Lattnerfa761832004-01-14 03:38:37 +0000163 if (!Kept.empty() && TestBlocks(Kept))
164 return KeepSuffix;
165 if (!Prefix.empty() && TestBlocks(Prefix))
166 return KeepPrefix;
167 return NoFailure;
168 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000169
Chris Lattner8b189272004-02-18 23:26:28 +0000170 bool TestBlocks(std::vector<const BasicBlock*> &Prefix);
Chris Lattnerfa761832004-01-14 03:38:37 +0000171 };
172}
Chris Lattner286921e2003-04-24 23:51:38 +0000173
Chris Lattner8b189272004-02-18 23:26:28 +0000174bool ReduceCrashingBlocks::TestBlocks(std::vector<const BasicBlock*> &BBs) {
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000175 // Clone the program to try hacking it apart...
Chris Lattner06905db2004-02-18 21:24:48 +0000176 Module *M = CloneModule(BD.getProgram());
Misha Brukman3da94ae2005-04-22 00:00:37 +0000177
Chris Lattner286921e2003-04-24 23:51:38 +0000178 // Convert list to set for fast lookup...
179 std::set<BasicBlock*> Blocks;
180 for (unsigned i = 0, e = BBs.size(); i != e; ++i) {
181 // Convert the basic block from the original module to the new module...
Chris Lattner8b189272004-02-18 23:26:28 +0000182 const Function *F = BBs[i]->getParent();
Chris Lattner286921e2003-04-24 23:51:38 +0000183 Function *CMF = M->getFunction(F->getName(), F->getFunctionType());
184 assert(CMF && "Function not in module?!");
185
186 // Get the mapped basic block...
187 Function::iterator CBI = CMF->begin();
Chris Lattner8b189272004-02-18 23:26:28 +0000188 std::advance(CBI, std::distance(F->begin(),
189 Function::const_iterator(BBs[i])));
Chris Lattner286921e2003-04-24 23:51:38 +0000190 Blocks.insert(CBI);
191 }
192
193 std::cout << "Checking for crash with only these blocks:";
Chris Lattner73b96bd2003-10-27 04:44:59 +0000194 unsigned NumPrint = Blocks.size();
195 if (NumPrint > 10) NumPrint = 10;
196 for (unsigned i = 0, e = NumPrint; i != e; ++i)
Chris Lattner286921e2003-04-24 23:51:38 +0000197 std::cout << " " << BBs[i]->getName();
Chris Lattner73b96bd2003-10-27 04:44:59 +0000198 if (NumPrint < Blocks.size())
199 std::cout << "... <" << Blocks.size() << " total>";
Chris Lattner286921e2003-04-24 23:51:38 +0000200 std::cout << ": ";
201
202 // Loop over and delete any hack up any blocks that are not listed...
203 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
204 for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
Chris Lattner8bc098b2003-11-22 02:10:38 +0000205 if (!Blocks.count(BB) && BB->getTerminator()->getNumSuccessors()) {
Chris Lattner286921e2003-04-24 23:51:38 +0000206 // Loop over all of the successors of this block, deleting any PHI nodes
207 // that might include it.
208 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
209 (*SI)->removePredecessor(BB);
210
Chris Lattner8bc098b2003-11-22 02:10:38 +0000211 if (BB->getTerminator()->getType() != Type::VoidTy)
212 BB->getTerminator()->replaceAllUsesWith(
213 Constant::getNullValue(BB->getTerminator()->getType()));
214
Chris Lattner286921e2003-04-24 23:51:38 +0000215 // Delete the old terminator instruction...
216 BB->getInstList().pop_back();
Misha Brukman3da94ae2005-04-22 00:00:37 +0000217
Chris Lattner286921e2003-04-24 23:51:38 +0000218 // Add a new return instruction of the appropriate type...
219 const Type *RetTy = BB->getParent()->getReturnType();
Chris Lattner8bc098b2003-11-22 02:10:38 +0000220 new ReturnInst(RetTy == Type::VoidTy ? 0 :
221 Constant::getNullValue(RetTy), BB);
Chris Lattner286921e2003-04-24 23:51:38 +0000222 }
223
224 // The CFG Simplifier pass may delete one of the basic blocks we are
225 // interested in. If it does we need to take the block out of the list. Make
226 // a "persistent mapping" by turning basic blocks into <function, name> pairs.
227 // This won't work well if blocks are unnamed, but that is just the risk we
228 // have to take.
229 std::vector<std::pair<Function*, std::string> > BlockInfo;
230
231 for (std::set<BasicBlock*>::iterator I = Blocks.begin(), E = Blocks.end();
232 I != E; ++I)
233 BlockInfo.push_back(std::make_pair((*I)->getParent(), (*I)->getName()));
234
235 // Now run the CFG simplify pass on the function...
236 PassManager Passes;
237 Passes.add(createCFGSimplificationPass());
238 Passes.add(createVerifierPass());
239 Passes.run(*M);
240
241 // Try running on the hacked up program...
Chris Lattner8b189272004-02-18 23:26:28 +0000242 if (TestFn(BD, M)) {
Chris Lattner06905db2004-02-18 21:24:48 +0000243 BD.setNewProgram(M); // It crashed, keep the trimmed version...
Chris Lattner286921e2003-04-24 23:51:38 +0000244
245 // Make sure to use basic block pointers that point into the now-current
246 // module, and that they don't include any deleted blocks.
247 BBs.clear();
248 for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) {
249 SymbolTable &ST = BlockInfo[i].first->getSymbolTable();
Reid Spencer9231ac82004-05-25 08:53:40 +0000250 SymbolTable::plane_iterator PI = ST.find(Type::LabelTy);
251 if (PI != ST.plane_end() && PI->second.count(BlockInfo[i].second))
252 BBs.push_back(cast<BasicBlock>(PI->second[BlockInfo[i].second]));
Chris Lattner286921e2003-04-24 23:51:38 +0000253 }
254 return true;
255 }
Chris Lattner06905db2004-02-18 21:24:48 +0000256 delete M; // It didn't crash, try something else.
Chris Lattner286921e2003-04-24 23:51:38 +0000257 return false;
258}
259
Chris Lattner8b189272004-02-18 23:26:28 +0000260/// DebugACrash - Given a predicate that determines whether a component crashes
261/// on a program, try to destructively reduce the program while still keeping
262/// the predicate true.
263static bool DebugACrash(BugDriver &BD, bool (*TestFn)(BugDriver &, Module *)) {
Chris Lattner5f73e382003-04-25 00:53:05 +0000264 // See if we can get away with nuking all of the global variable initializers
265 // in the program...
Chris Lattner852b4d42005-03-15 15:48:06 +0000266 if (BD.getProgram()->global_begin() != BD.getProgram()->global_end()) {
Chris Lattner8b189272004-02-18 23:26:28 +0000267 Module *M = CloneModule(BD.getProgram());
Chris Lattner5f73e382003-04-25 00:53:05 +0000268 bool DeletedInit = false;
Chris Lattner852b4d42005-03-15 15:48:06 +0000269 for (Module::global_iterator I = M->global_begin(), E = M->global_end(); I != E; ++I)
Chris Lattner5f73e382003-04-25 00:53:05 +0000270 if (I->hasInitializer()) {
271 I->setInitializer(0);
272 I->setLinkage(GlobalValue::ExternalLinkage);
273 DeletedInit = true;
274 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000275
Chris Lattner5f73e382003-04-25 00:53:05 +0000276 if (!DeletedInit) {
277 delete M; // No change made...
278 } else {
279 // See if the program still causes a crash...
280 std::cout << "\nChecking to see if we can delete global inits: ";
Chris Lattner8b189272004-02-18 23:26:28 +0000281 if (TestFn(BD, M)) { // Still crashes?
282 BD.setNewProgram(M);
Chris Lattner5f73e382003-04-25 00:53:05 +0000283 std::cout << "\n*** Able to remove all global initializers!\n";
284 } else { // No longer crashes?
Chris Lattner5f73e382003-04-25 00:53:05 +0000285 std::cout << " - Removing all global inits hides problem!\n";
Chris Lattner06905db2004-02-18 21:24:48 +0000286 delete M;
Chris Lattner5f73e382003-04-25 00:53:05 +0000287 }
288 }
289 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000290
Chris Lattneraae33f92003-04-24 22:24:58 +0000291 // Now try to reduce the number of functions in the module to something small.
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000292 std::vector<Function*> Functions;
293 for (Module::iterator I = BD.getProgram()->begin(),
Chris Lattner8b189272004-02-18 23:26:28 +0000294 E = BD.getProgram()->end(); I != E; ++I)
Chris Lattneraae33f92003-04-24 22:24:58 +0000295 if (!I->isExternal())
296 Functions.push_back(I);
Chris Lattnerafade922002-11-20 22:28:10 +0000297
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000298 if (Functions.size() > 1 && !BugpointIsInterrupted) {
Chris Lattneraae33f92003-04-24 22:24:58 +0000299 std::cout << "\n*** Attempting to reduce the number of functions "
300 "in the testcase\n";
Chris Lattnerafade922002-11-20 22:28:10 +0000301
Chris Lattner8b189272004-02-18 23:26:28 +0000302 unsigned OldSize = Functions.size();
303 ReduceCrashingFunctions(BD, TestFn).reduceList(Functions);
Chris Lattnerafade922002-11-20 22:28:10 +0000304
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000305 if (Functions.size() < OldSize)
Chris Lattner8b189272004-02-18 23:26:28 +0000306 BD.EmitProgressBytecode("reduced-function");
Chris Lattnerafade922002-11-20 22:28:10 +0000307 }
308
Chris Lattner286921e2003-04-24 23:51:38 +0000309 // Attempt to delete entire basic blocks at a time to speed up
310 // convergence... this actually works by setting the terminator of the blocks
311 // to a return instruction then running simplifycfg, which can potentially
312 // shrinks the code dramatically quickly
313 //
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000314 if (!DisableSimplifyCFG && !BugpointIsInterrupted) {
Chris Lattner8b189272004-02-18 23:26:28 +0000315 std::vector<const BasicBlock*> Blocks;
316 for (Module::const_iterator I = BD.getProgram()->begin(),
317 E = BD.getProgram()->end(); I != E; ++I)
318 for (Function::const_iterator FI = I->begin(), E = I->end(); FI !=E; ++FI)
Chris Lattner47ae4a12003-08-05 15:51:05 +0000319 Blocks.push_back(FI);
Chris Lattner8b189272004-02-18 23:26:28 +0000320 ReduceCrashingBlocks(BD, TestFn).reduceList(Blocks);
Chris Lattner47ae4a12003-08-05 15:51:05 +0000321 }
Chris Lattner218e26e2002-12-23 23:49:59 +0000322
Chris Lattneraae33f92003-04-24 22:24:58 +0000323 // FIXME: This should use the list reducer to converge faster by deleting
324 // larger chunks of instructions at a time!
Chris Lattnerb2c180f2004-03-13 19:35:54 +0000325 unsigned Simplification = 2;
Chris Lattner65207852003-01-23 02:48:33 +0000326 do {
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000327 if (BugpointIsInterrupted) break;
Chris Lattner65207852003-01-23 02:48:33 +0000328 --Simplification;
329 std::cout << "\n*** Attempting to reduce testcase by deleting instruc"
Misha Brukmaneed80e22004-07-23 01:30:49 +0000330 << "tions: Simplification Level #" << Simplification << '\n';
Chris Lattner65207852003-01-23 02:48:33 +0000331
Misha Brukman5560c9d2003-08-18 14:43:39 +0000332 // Now that we have deleted the functions that are unnecessary for the
333 // program, try to remove instructions that are not necessary to cause the
Chris Lattner65207852003-01-23 02:48:33 +0000334 // crash. To do this, we loop through all of the instructions in the
335 // remaining functions, deleting them (replacing any values produced with
336 // nulls), and then running ADCE and SimplifyCFG. If the transformed input
337 // still triggers failure, keep deleting until we cannot trigger failure
338 // anymore.
339 //
Chris Lattnerf66d9062004-02-18 23:59:11 +0000340 unsigned InstructionsToSkipBeforeDeleting = 0;
Chris Lattner65207852003-01-23 02:48:33 +0000341 TryAgain:
Misha Brukman3da94ae2005-04-22 00:00:37 +0000342
Chris Lattner65207852003-01-23 02:48:33 +0000343 // Loop over all of the (non-terminator) instructions remaining in the
344 // function, attempting to delete them.
Chris Lattnerf66d9062004-02-18 23:59:11 +0000345 unsigned CurInstructionNum = 0;
Chris Lattner8b189272004-02-18 23:26:28 +0000346 for (Module::const_iterator FI = BD.getProgram()->begin(),
347 E = BD.getProgram()->end(); FI != E; ++FI)
Chris Lattnerf66d9062004-02-18 23:59:11 +0000348 if (!FI->isExternal())
Chris Lattner8b189272004-02-18 23:26:28 +0000349 for (Function::const_iterator BI = FI->begin(), E = FI->end(); BI != E;
350 ++BI)
351 for (BasicBlock::const_iterator I = BI->begin(), E = --BI->end();
Chris Lattnerf66d9062004-02-18 23:59:11 +0000352 I != E; ++I, ++CurInstructionNum)
353 if (InstructionsToSkipBeforeDeleting) {
354 --InstructionsToSkipBeforeDeleting;
355 } else {
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000356 if (BugpointIsInterrupted) goto ExitLoops;
357
Chris Lattnerf66d9062004-02-18 23:59:11 +0000358 std::cout << "Checking instruction '" << I->getName() << "': ";
359 Module *M = BD.deleteInstructionFromProgram(I, Simplification);
Misha Brukman3da94ae2005-04-22 00:00:37 +0000360
Chris Lattnerf66d9062004-02-18 23:59:11 +0000361 // Find out if the pass still crashes on this pass...
362 if (TestFn(BD, M)) {
363 // Yup, it does, we delete the old module, and continue trying
364 // to reduce the testcase...
365 BD.setNewProgram(M);
Chris Lattnerf66d9062004-02-18 23:59:11 +0000366 InstructionsToSkipBeforeDeleting = CurInstructionNum;
367 goto TryAgain; // I wish I had a multi-level break here!
368 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000369
Chris Lattnerf66d9062004-02-18 23:59:11 +0000370 // This pass didn't crash without this instruction, try the next
371 // one.
372 delete M;
Chris Lattner65207852003-01-23 02:48:33 +0000373 }
Chris Lattnerf66d9062004-02-18 23:59:11 +0000374
375 if (InstructionsToSkipBeforeDeleting) {
376 InstructionsToSkipBeforeDeleting = 0;
377 goto TryAgain;
378 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000379
Chris Lattner65207852003-01-23 02:48:33 +0000380 } while (Simplification);
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000381ExitLoops:
Chris Lattnerba386d92003-02-28 16:13:20 +0000382
383 // Try to clean up the testcase by running funcresolve and globaldce...
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000384 if (!BugpointIsInterrupted) {
385 std::cout << "\n*** Attempting to perform final cleanups: ";
386 Module *M = CloneModule(BD.getProgram());
387 M = BD.performFinalCleanups(M, true);
Misha Brukman3da94ae2005-04-22 00:00:37 +0000388
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000389 // Find out if the pass still crashes on the cleaned up program...
390 if (TestFn(BD, M)) {
391 BD.setNewProgram(M); // Yup, it does, keep the reduced version...
392 } else {
393 delete M;
394 }
Chris Lattnerba386d92003-02-28 16:13:20 +0000395 }
396
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000397 BD.EmitProgressBytecode("reduced-simplified");
Chris Lattnerba386d92003-02-28 16:13:20 +0000398
Misha Brukman3da94ae2005-04-22 00:00:37 +0000399 return false;
Chris Lattnerafade922002-11-20 22:28:10 +0000400}
Brian Gaeked0fde302003-11-11 22:41:34 +0000401
Chris Lattner8b189272004-02-18 23:26:28 +0000402static bool TestForOptimizerCrash(BugDriver &BD, Module *M) {
403 return BD.runPasses(M);
404}
Chris Lattner02526262004-02-18 21:02:04 +0000405
Chris Lattner8b189272004-02-18 23:26:28 +0000406/// debugOptimizerCrash - This method is called when some pass crashes on input.
407/// It attempts to prune down the testcase to something reasonable, and figure
408/// out exactly which pass is crashing.
409///
410bool BugDriver::debugOptimizerCrash() {
411 std::cout << "\n*** Debugging optimizer crash!\n";
412
413 // Reduce the list of passes which causes the optimizer to crash...
414 unsigned OldSize = PassesToRun.size();
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000415 if (!BugpointIsInterrupted)
416 ReducePassList(*this).reduceList(PassesToRun);
Chris Lattner8b189272004-02-18 23:26:28 +0000417
418 std::cout << "\n*** Found crashing pass"
419 << (PassesToRun.size() == 1 ? ": " : "es: ")
Misha Brukmaneed80e22004-07-23 01:30:49 +0000420 << getPassesString(PassesToRun) << '\n';
Chris Lattner8b189272004-02-18 23:26:28 +0000421
422 EmitProgressBytecode("passinput");
423
424 return DebugACrash(*this, TestForOptimizerCrash);
425}
426
427static bool TestForCodeGenCrash(BugDriver &BD, Module *M) {
428 try {
Misha Brukmaneed80e22004-07-23 01:30:49 +0000429 std::cerr << '\n';
Chris Lattner8b189272004-02-18 23:26:28 +0000430 BD.compileProgram(M);
Misha Brukmaneed80e22004-07-23 01:30:49 +0000431 std::cerr << '\n';
Chris Lattner8b189272004-02-18 23:26:28 +0000432 return false;
Jeff Cohen83881952005-01-22 16:30:58 +0000433 } catch (ToolExecutionError &) {
Chris Lattner8b189272004-02-18 23:26:28 +0000434 std::cerr << "<crash>\n";
435 return true; // Tool is still crashing.
436 }
437}
Chris Lattner02526262004-02-18 21:02:04 +0000438
439/// debugCodeGeneratorCrash - This method is called when the code generator
440/// crashes on an input. It attempts to reduce the input as much as possible
441/// while still causing the code generator to crash.
442bool BugDriver::debugCodeGeneratorCrash() {
Chris Lattner06905db2004-02-18 21:24:48 +0000443 std::cerr << "*** Debugging code generator crash!\n";
Chris Lattner02526262004-02-18 21:02:04 +0000444
Chris Lattner8b189272004-02-18 23:26:28 +0000445 return DebugACrash(*this, TestForCodeGenCrash);
Chris Lattner02526262004-02-18 21:02:04 +0000446}