blob: 53b15800aedc02a1c6424e68705c88776f9f24cb [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"
Andrew Lenharth7c0a9372006-03-05 22:21:36 +000030#include "llvm/Support/CommandLine.h"
Chris Lattnerafade922002-11-20 22:28:10 +000031#include <fstream>
Chris Lattneraae33f92003-04-24 22:24:58 +000032#include <set>
Chris Lattnerfa761832004-01-14 03:38:37 +000033using namespace llvm;
Chris Lattnerafade922002-11-20 22:28:10 +000034
Andrew Lenharth7c0a9372006-03-05 22:21:36 +000035namespace {
36 cl::opt<bool>
37 KeepMain("keep-main",
38 cl::desc("Force function reduction to keep main"),
39 cl::init(false));
40}
41
Brian Gaeked0fde302003-11-11 22:41:34 +000042namespace llvm {
Chris Lattner06905db2004-02-18 21:24:48 +000043 class ReducePassList : public ListReducer<const PassInfo*> {
Chris Lattnerfa761832004-01-14 03:38:37 +000044 BugDriver &BD;
45 public:
Chris Lattner06905db2004-02-18 21:24:48 +000046 ReducePassList(BugDriver &bd) : BD(bd) {}
Misha Brukman3da94ae2005-04-22 00:00:37 +000047
Chris Lattnerfa761832004-01-14 03:38:37 +000048 // doTest - Return true iff running the "removed" passes succeeds, and
49 // running the "Kept" passes fail when run on the output of the "removed"
50 // passes. If we return true, we update the current module of bugpoint.
51 //
52 virtual TestResult doTest(std::vector<const PassInfo*> &Removed,
53 std::vector<const PassInfo*> &Kept);
54 };
55}
Chris Lattneraae33f92003-04-24 22:24:58 +000056
Chris Lattner06905db2004-02-18 21:24:48 +000057ReducePassList::TestResult
58ReducePassList::doTest(std::vector<const PassInfo*> &Prefix,
59 std::vector<const PassInfo*> &Suffix) {
Reid Spencer5f767602004-12-16 23:04:20 +000060 sys::Path PrefixOutput;
Chris Lattnerb417c792003-06-02 04:54:29 +000061 Module *OrigProgram = 0;
Chris Lattneraae33f92003-04-24 22:24:58 +000062 if (!Prefix.empty()) {
63 std::cout << "Checking to see if these passes crash: "
64 << getPassesString(Prefix) << ": ";
Reid Spencer5f767602004-12-16 23:04:20 +000065 std::string PfxOutput;
66 if (BD.runPasses(Prefix, PfxOutput))
Chris Lattneraae33f92003-04-24 22:24:58 +000067 return KeepPrefix;
Chris Lattnerb417c792003-06-02 04:54:29 +000068
Reid Spencerdd04df02005-07-07 23:21:43 +000069 PrefixOutput.set(PfxOutput);
Chris Lattnerb417c792003-06-02 04:54:29 +000070 OrigProgram = BD.Program;
71
Reid Spencer5f767602004-12-16 23:04:20 +000072 BD.Program = ParseInputFile(PrefixOutput.toString());
Chris Lattnerb417c792003-06-02 04:54:29 +000073 if (BD.Program == 0) {
74 std::cerr << BD.getToolName() << ": Error reading bytecode file '"
75 << PrefixOutput << "'!\n";
76 exit(1);
77 }
Reid Spencera229c5c2005-07-08 03:08:58 +000078 PrefixOutput.eraseFromDisk();
Chris Lattneraae33f92003-04-24 22:24:58 +000079 }
80
81 std::cout << "Checking to see if these passes crash: "
82 << getPassesString(Suffix) << ": ";
Misha Brukman3da94ae2005-04-22 00:00:37 +000083
Chris Lattneraae33f92003-04-24 22:24:58 +000084 if (BD.runPasses(Suffix)) {
85 delete OrigProgram; // The suffix crashes alone...
86 return KeepSuffix;
87 }
88
89 // Nothing failed, restore state...
Chris Lattnerb417c792003-06-02 04:54:29 +000090 if (OrigProgram) {
91 delete BD.Program;
92 BD.Program = OrigProgram;
93 }
Chris Lattneraae33f92003-04-24 22:24:58 +000094 return NoFailure;
95}
96
Chris Lattnerfa761832004-01-14 03:38:37 +000097namespace llvm {
Chris Lattnerefdc0b52004-03-14 20:50:42 +000098 class ReduceCrashingFunctions : public ListReducer<Function*> {
Chris Lattnerfa761832004-01-14 03:38:37 +000099 BugDriver &BD;
Chris Lattner8b189272004-02-18 23:26:28 +0000100 bool (*TestFn)(BugDriver &, Module *);
Chris Lattnerfa761832004-01-14 03:38:37 +0000101 public:
Chris Lattner8b189272004-02-18 23:26:28 +0000102 ReduceCrashingFunctions(BugDriver &bd,
103 bool (*testFn)(BugDriver &, Module *))
104 : BD(bd), TestFn(testFn) {}
Misha Brukman3da94ae2005-04-22 00:00:37 +0000105
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000106 virtual TestResult doTest(std::vector<Function*> &Prefix,
107 std::vector<Function*> &Kept) {
Chris Lattnerfa761832004-01-14 03:38:37 +0000108 if (!Kept.empty() && TestFuncs(Kept))
109 return KeepSuffix;
110 if (!Prefix.empty() && TestFuncs(Prefix))
111 return KeepPrefix;
112 return NoFailure;
113 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000114
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000115 bool TestFuncs(std::vector<Function*> &Prefix);
Chris Lattnerfa761832004-01-14 03:38:37 +0000116 };
117}
Chris Lattneraae33f92003-04-24 22:24:58 +0000118
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000119bool ReduceCrashingFunctions::TestFuncs(std::vector<Function*> &Funcs) {
Andrew Lenharth7c0a9372006-03-05 22:21:36 +0000120
121 //if main isn't present, claim there is no problem
122 if (KeepMain && find(Funcs.begin(), Funcs.end(), BD.getProgram()->getMainFunction()) == Funcs.end())
123 return false;
124
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000125 // Clone the program to try hacking it apart...
Chris Lattner06905db2004-02-18 21:24:48 +0000126 Module *M = CloneModule(BD.getProgram());
Misha Brukman3da94ae2005-04-22 00:00:37 +0000127
Chris Lattneraae33f92003-04-24 22:24:58 +0000128 // Convert list to set for fast lookup...
129 std::set<Function*> Functions;
130 for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
Chris Lattnercc540eb2006-03-16 23:16:17 +0000131 // FIXME: bugpoint should add names to all stripped symbols.
132 assert(!Funcs[i]->getName().empty() &&
133 "Bugpoint doesn't work on stripped modules yet PR718!");
Misha Brukman3da94ae2005-04-22 00:00:37 +0000134 Function *CMF = M->getFunction(Funcs[i]->getName(),
Chris Lattneraae33f92003-04-24 22:24:58 +0000135 Funcs[i]->getFunctionType());
136 assert(CMF && "Function not in module?!");
Chris Lattnerf607b792003-04-24 22:54:06 +0000137 Functions.insert(CMF);
Chris Lattneraae33f92003-04-24 22:24:58 +0000138 }
139
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000140 std::cout << "Checking for crash with only these functions: ";
141 PrintFunctionList(Funcs);
Chris Lattneraae33f92003-04-24 22:24:58 +0000142 std::cout << ": ";
143
144 // Loop over and delete any functions which we aren't supposed to be playing
145 // with...
146 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerf607b792003-04-24 22:54:06 +0000147 if (!I->isExternal() && !Functions.count(I))
Chris Lattneraae33f92003-04-24 22:24:58 +0000148 DeleteFunctionBody(I);
149
150 // Try running the hacked up program...
Chris Lattner8b189272004-02-18 23:26:28 +0000151 if (TestFn(BD, M)) {
Chris Lattner06905db2004-02-18 21:24:48 +0000152 BD.setNewProgram(M); // It crashed, keep the trimmed version...
Chris Lattneraae33f92003-04-24 22:24:58 +0000153
154 // Make sure to use function pointers that point into the now-current
155 // module.
156 Funcs.assign(Functions.begin(), Functions.end());
157 return true;
158 }
Chris Lattner06905db2004-02-18 21:24:48 +0000159 delete M;
Chris Lattneraae33f92003-04-24 22:24:58 +0000160 return false;
161}
162
Chris Lattner640f22e2003-04-24 17:02:17 +0000163
Chris Lattnerf913f402004-02-18 21:29:46 +0000164namespace {
Chris Lattnerfa761832004-01-14 03:38:37 +0000165 /// ReduceCrashingBlocks reducer - This works by setting the terminators of
166 /// all terminators except the specified basic blocks to a 'ret' instruction,
167 /// then running the simplify-cfg pass. This has the effect of chopping up
168 /// the CFG really fast which can reduce large functions quickly.
169 ///
Chris Lattner8b189272004-02-18 23:26:28 +0000170 class ReduceCrashingBlocks : public ListReducer<const BasicBlock*> {
Chris Lattnerfa761832004-01-14 03:38:37 +0000171 BugDriver &BD;
Chris Lattner8b189272004-02-18 23:26:28 +0000172 bool (*TestFn)(BugDriver &, Module *);
Chris Lattnerfa761832004-01-14 03:38:37 +0000173 public:
Chris Lattner8b189272004-02-18 23:26:28 +0000174 ReduceCrashingBlocks(BugDriver &bd, bool (*testFn)(BugDriver &, Module *))
175 : BD(bd), TestFn(testFn) {}
Misha Brukman3da94ae2005-04-22 00:00:37 +0000176
Chris Lattner8b189272004-02-18 23:26:28 +0000177 virtual TestResult doTest(std::vector<const BasicBlock*> &Prefix,
178 std::vector<const BasicBlock*> &Kept) {
Chris Lattnerfa761832004-01-14 03:38:37 +0000179 if (!Kept.empty() && TestBlocks(Kept))
180 return KeepSuffix;
181 if (!Prefix.empty() && TestBlocks(Prefix))
182 return KeepPrefix;
183 return NoFailure;
184 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000185
Chris Lattner8b189272004-02-18 23:26:28 +0000186 bool TestBlocks(std::vector<const BasicBlock*> &Prefix);
Chris Lattnerfa761832004-01-14 03:38:37 +0000187 };
188}
Chris Lattner286921e2003-04-24 23:51:38 +0000189
Chris Lattner8b189272004-02-18 23:26:28 +0000190bool ReduceCrashingBlocks::TestBlocks(std::vector<const BasicBlock*> &BBs) {
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000191 // Clone the program to try hacking it apart...
Chris Lattner06905db2004-02-18 21:24:48 +0000192 Module *M = CloneModule(BD.getProgram());
Misha Brukman3da94ae2005-04-22 00:00:37 +0000193
Chris Lattner286921e2003-04-24 23:51:38 +0000194 // Convert list to set for fast lookup...
195 std::set<BasicBlock*> Blocks;
196 for (unsigned i = 0, e = BBs.size(); i != e; ++i) {
197 // Convert the basic block from the original module to the new module...
Chris Lattner8b189272004-02-18 23:26:28 +0000198 const Function *F = BBs[i]->getParent();
Chris Lattner286921e2003-04-24 23:51:38 +0000199 Function *CMF = M->getFunction(F->getName(), F->getFunctionType());
200 assert(CMF && "Function not in module?!");
201
202 // Get the mapped basic block...
203 Function::iterator CBI = CMF->begin();
Chris Lattner8b189272004-02-18 23:26:28 +0000204 std::advance(CBI, std::distance(F->begin(),
205 Function::const_iterator(BBs[i])));
Chris Lattner286921e2003-04-24 23:51:38 +0000206 Blocks.insert(CBI);
207 }
208
209 std::cout << "Checking for crash with only these blocks:";
Chris Lattner73b96bd2003-10-27 04:44:59 +0000210 unsigned NumPrint = Blocks.size();
211 if (NumPrint > 10) NumPrint = 10;
212 for (unsigned i = 0, e = NumPrint; i != e; ++i)
Chris Lattner286921e2003-04-24 23:51:38 +0000213 std::cout << " " << BBs[i]->getName();
Chris Lattner73b96bd2003-10-27 04:44:59 +0000214 if (NumPrint < Blocks.size())
215 std::cout << "... <" << Blocks.size() << " total>";
Chris Lattner286921e2003-04-24 23:51:38 +0000216 std::cout << ": ";
217
218 // Loop over and delete any hack up any blocks that are not listed...
219 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
220 for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
Chris Lattner8bc098b2003-11-22 02:10:38 +0000221 if (!Blocks.count(BB) && BB->getTerminator()->getNumSuccessors()) {
Chris Lattner286921e2003-04-24 23:51:38 +0000222 // Loop over all of the successors of this block, deleting any PHI nodes
223 // that might include it.
224 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
225 (*SI)->removePredecessor(BB);
226
Chris Lattner8bc098b2003-11-22 02:10:38 +0000227 if (BB->getTerminator()->getType() != Type::VoidTy)
228 BB->getTerminator()->replaceAllUsesWith(
229 Constant::getNullValue(BB->getTerminator()->getType()));
230
Chris Lattner286921e2003-04-24 23:51:38 +0000231 // Delete the old terminator instruction...
232 BB->getInstList().pop_back();
Misha Brukman3da94ae2005-04-22 00:00:37 +0000233
Chris Lattner286921e2003-04-24 23:51:38 +0000234 // Add a new return instruction of the appropriate type...
235 const Type *RetTy = BB->getParent()->getReturnType();
Chris Lattner8bc098b2003-11-22 02:10:38 +0000236 new ReturnInst(RetTy == Type::VoidTy ? 0 :
237 Constant::getNullValue(RetTy), BB);
Chris Lattner286921e2003-04-24 23:51:38 +0000238 }
239
240 // The CFG Simplifier pass may delete one of the basic blocks we are
241 // interested in. If it does we need to take the block out of the list. Make
242 // a "persistent mapping" by turning basic blocks into <function, name> pairs.
243 // This won't work well if blocks are unnamed, but that is just the risk we
244 // have to take.
245 std::vector<std::pair<Function*, std::string> > BlockInfo;
246
247 for (std::set<BasicBlock*>::iterator I = Blocks.begin(), E = Blocks.end();
248 I != E; ++I)
249 BlockInfo.push_back(std::make_pair((*I)->getParent(), (*I)->getName()));
250
251 // Now run the CFG simplify pass on the function...
252 PassManager Passes;
253 Passes.add(createCFGSimplificationPass());
254 Passes.add(createVerifierPass());
255 Passes.run(*M);
256
257 // Try running on the hacked up program...
Chris Lattner8b189272004-02-18 23:26:28 +0000258 if (TestFn(BD, M)) {
Chris Lattner06905db2004-02-18 21:24:48 +0000259 BD.setNewProgram(M); // It crashed, keep the trimmed version...
Chris Lattner286921e2003-04-24 23:51:38 +0000260
261 // Make sure to use basic block pointers that point into the now-current
262 // module, and that they don't include any deleted blocks.
263 BBs.clear();
264 for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) {
265 SymbolTable &ST = BlockInfo[i].first->getSymbolTable();
Reid Spencer9231ac82004-05-25 08:53:40 +0000266 SymbolTable::plane_iterator PI = ST.find(Type::LabelTy);
267 if (PI != ST.plane_end() && PI->second.count(BlockInfo[i].second))
268 BBs.push_back(cast<BasicBlock>(PI->second[BlockInfo[i].second]));
Chris Lattner286921e2003-04-24 23:51:38 +0000269 }
270 return true;
271 }
Chris Lattner06905db2004-02-18 21:24:48 +0000272 delete M; // It didn't crash, try something else.
Chris Lattner286921e2003-04-24 23:51:38 +0000273 return false;
274}
275
Chris Lattner8b189272004-02-18 23:26:28 +0000276/// DebugACrash - Given a predicate that determines whether a component crashes
277/// on a program, try to destructively reduce the program while still keeping
278/// the predicate true.
279static bool DebugACrash(BugDriver &BD, bool (*TestFn)(BugDriver &, Module *)) {
Chris Lattner5f73e382003-04-25 00:53:05 +0000280 // See if we can get away with nuking all of the global variable initializers
281 // in the program...
Chris Lattner852b4d42005-03-15 15:48:06 +0000282 if (BD.getProgram()->global_begin() != BD.getProgram()->global_end()) {
Chris Lattner8b189272004-02-18 23:26:28 +0000283 Module *M = CloneModule(BD.getProgram());
Chris Lattner5f73e382003-04-25 00:53:05 +0000284 bool DeletedInit = false;
Chris Lattner852b4d42005-03-15 15:48:06 +0000285 for (Module::global_iterator I = M->global_begin(), E = M->global_end(); I != E; ++I)
Chris Lattner5f73e382003-04-25 00:53:05 +0000286 if (I->hasInitializer()) {
287 I->setInitializer(0);
288 I->setLinkage(GlobalValue::ExternalLinkage);
289 DeletedInit = true;
290 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000291
Chris Lattner5f73e382003-04-25 00:53:05 +0000292 if (!DeletedInit) {
293 delete M; // No change made...
294 } else {
295 // See if the program still causes a crash...
296 std::cout << "\nChecking to see if we can delete global inits: ";
Chris Lattner8b189272004-02-18 23:26:28 +0000297 if (TestFn(BD, M)) { // Still crashes?
298 BD.setNewProgram(M);
Chris Lattner5f73e382003-04-25 00:53:05 +0000299 std::cout << "\n*** Able to remove all global initializers!\n";
300 } else { // No longer crashes?
Chris Lattner5f73e382003-04-25 00:53:05 +0000301 std::cout << " - Removing all global inits hides problem!\n";
Chris Lattner06905db2004-02-18 21:24:48 +0000302 delete M;
Chris Lattner5f73e382003-04-25 00:53:05 +0000303 }
304 }
305 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000306
Chris Lattneraae33f92003-04-24 22:24:58 +0000307 // Now try to reduce the number of functions in the module to something small.
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000308 std::vector<Function*> Functions;
309 for (Module::iterator I = BD.getProgram()->begin(),
Chris Lattner8b189272004-02-18 23:26:28 +0000310 E = BD.getProgram()->end(); I != E; ++I)
Chris Lattneraae33f92003-04-24 22:24:58 +0000311 if (!I->isExternal())
312 Functions.push_back(I);
Chris Lattnerafade922002-11-20 22:28:10 +0000313
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000314 if (Functions.size() > 1 && !BugpointIsInterrupted) {
Chris Lattneraae33f92003-04-24 22:24:58 +0000315 std::cout << "\n*** Attempting to reduce the number of functions "
316 "in the testcase\n";
Chris Lattnerafade922002-11-20 22:28:10 +0000317
Chris Lattner8b189272004-02-18 23:26:28 +0000318 unsigned OldSize = Functions.size();
319 ReduceCrashingFunctions(BD, TestFn).reduceList(Functions);
Chris Lattnerafade922002-11-20 22:28:10 +0000320
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000321 if (Functions.size() < OldSize)
Chris Lattner8b189272004-02-18 23:26:28 +0000322 BD.EmitProgressBytecode("reduced-function");
Chris Lattnerafade922002-11-20 22:28:10 +0000323 }
324
Chris Lattner286921e2003-04-24 23:51:38 +0000325 // Attempt to delete entire basic blocks at a time to speed up
326 // convergence... this actually works by setting the terminator of the blocks
327 // to a return instruction then running simplifycfg, which can potentially
328 // shrinks the code dramatically quickly
329 //
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000330 if (!DisableSimplifyCFG && !BugpointIsInterrupted) {
Chris Lattner8b189272004-02-18 23:26:28 +0000331 std::vector<const BasicBlock*> Blocks;
332 for (Module::const_iterator I = BD.getProgram()->begin(),
333 E = BD.getProgram()->end(); I != E; ++I)
334 for (Function::const_iterator FI = I->begin(), E = I->end(); FI !=E; ++FI)
Chris Lattner47ae4a12003-08-05 15:51:05 +0000335 Blocks.push_back(FI);
Chris Lattner8b189272004-02-18 23:26:28 +0000336 ReduceCrashingBlocks(BD, TestFn).reduceList(Blocks);
Chris Lattner47ae4a12003-08-05 15:51:05 +0000337 }
Chris Lattner218e26e2002-12-23 23:49:59 +0000338
Chris Lattneraae33f92003-04-24 22:24:58 +0000339 // FIXME: This should use the list reducer to converge faster by deleting
340 // larger chunks of instructions at a time!
Chris Lattnerb2c180f2004-03-13 19:35:54 +0000341 unsigned Simplification = 2;
Chris Lattner65207852003-01-23 02:48:33 +0000342 do {
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000343 if (BugpointIsInterrupted) break;
Chris Lattner65207852003-01-23 02:48:33 +0000344 --Simplification;
345 std::cout << "\n*** Attempting to reduce testcase by deleting instruc"
Misha Brukmaneed80e22004-07-23 01:30:49 +0000346 << "tions: Simplification Level #" << Simplification << '\n';
Chris Lattner65207852003-01-23 02:48:33 +0000347
Misha Brukman5560c9d2003-08-18 14:43:39 +0000348 // Now that we have deleted the functions that are unnecessary for the
349 // program, try to remove instructions that are not necessary to cause the
Chris Lattner65207852003-01-23 02:48:33 +0000350 // crash. To do this, we loop through all of the instructions in the
351 // remaining functions, deleting them (replacing any values produced with
352 // nulls), and then running ADCE and SimplifyCFG. If the transformed input
353 // still triggers failure, keep deleting until we cannot trigger failure
354 // anymore.
355 //
Chris Lattnerf66d9062004-02-18 23:59:11 +0000356 unsigned InstructionsToSkipBeforeDeleting = 0;
Chris Lattner65207852003-01-23 02:48:33 +0000357 TryAgain:
Misha Brukman3da94ae2005-04-22 00:00:37 +0000358
Chris Lattner65207852003-01-23 02:48:33 +0000359 // Loop over all of the (non-terminator) instructions remaining in the
360 // function, attempting to delete them.
Chris Lattnerf66d9062004-02-18 23:59:11 +0000361 unsigned CurInstructionNum = 0;
Chris Lattner8b189272004-02-18 23:26:28 +0000362 for (Module::const_iterator FI = BD.getProgram()->begin(),
363 E = BD.getProgram()->end(); FI != E; ++FI)
Chris Lattnerf66d9062004-02-18 23:59:11 +0000364 if (!FI->isExternal())
Chris Lattner8b189272004-02-18 23:26:28 +0000365 for (Function::const_iterator BI = FI->begin(), E = FI->end(); BI != E;
366 ++BI)
367 for (BasicBlock::const_iterator I = BI->begin(), E = --BI->end();
Chris Lattnerf66d9062004-02-18 23:59:11 +0000368 I != E; ++I, ++CurInstructionNum)
369 if (InstructionsToSkipBeforeDeleting) {
370 --InstructionsToSkipBeforeDeleting;
371 } else {
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000372 if (BugpointIsInterrupted) goto ExitLoops;
373
Chris Lattnerf66d9062004-02-18 23:59:11 +0000374 std::cout << "Checking instruction '" << I->getName() << "': ";
375 Module *M = BD.deleteInstructionFromProgram(I, Simplification);
Misha Brukman3da94ae2005-04-22 00:00:37 +0000376
Chris Lattnerf66d9062004-02-18 23:59:11 +0000377 // Find out if the pass still crashes on this pass...
378 if (TestFn(BD, M)) {
379 // Yup, it does, we delete the old module, and continue trying
380 // to reduce the testcase...
381 BD.setNewProgram(M);
Chris Lattnerf66d9062004-02-18 23:59:11 +0000382 InstructionsToSkipBeforeDeleting = CurInstructionNum;
383 goto TryAgain; // I wish I had a multi-level break here!
384 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000385
Chris Lattnerf66d9062004-02-18 23:59:11 +0000386 // This pass didn't crash without this instruction, try the next
387 // one.
388 delete M;
Chris Lattner65207852003-01-23 02:48:33 +0000389 }
Chris Lattnerf66d9062004-02-18 23:59:11 +0000390
391 if (InstructionsToSkipBeforeDeleting) {
392 InstructionsToSkipBeforeDeleting = 0;
393 goto TryAgain;
394 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000395
Chris Lattner65207852003-01-23 02:48:33 +0000396 } while (Simplification);
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000397ExitLoops:
Chris Lattnerba386d92003-02-28 16:13:20 +0000398
399 // Try to clean up the testcase by running funcresolve and globaldce...
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000400 if (!BugpointIsInterrupted) {
401 std::cout << "\n*** Attempting to perform final cleanups: ";
402 Module *M = CloneModule(BD.getProgram());
403 M = BD.performFinalCleanups(M, true);
Misha Brukman3da94ae2005-04-22 00:00:37 +0000404
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000405 // Find out if the pass still crashes on the cleaned up program...
406 if (TestFn(BD, M)) {
407 BD.setNewProgram(M); // Yup, it does, keep the reduced version...
408 } else {
409 delete M;
410 }
Chris Lattnerba386d92003-02-28 16:13:20 +0000411 }
412
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000413 BD.EmitProgressBytecode("reduced-simplified");
Chris Lattnerba386d92003-02-28 16:13:20 +0000414
Misha Brukman3da94ae2005-04-22 00:00:37 +0000415 return false;
Chris Lattnerafade922002-11-20 22:28:10 +0000416}
Brian Gaeked0fde302003-11-11 22:41:34 +0000417
Chris Lattner8b189272004-02-18 23:26:28 +0000418static bool TestForOptimizerCrash(BugDriver &BD, Module *M) {
419 return BD.runPasses(M);
420}
Chris Lattner02526262004-02-18 21:02:04 +0000421
Chris Lattner8b189272004-02-18 23:26:28 +0000422/// debugOptimizerCrash - This method is called when some pass crashes on input.
423/// It attempts to prune down the testcase to something reasonable, and figure
424/// out exactly which pass is crashing.
425///
426bool BugDriver::debugOptimizerCrash() {
427 std::cout << "\n*** Debugging optimizer crash!\n";
428
429 // Reduce the list of passes which causes the optimizer to crash...
430 unsigned OldSize = PassesToRun.size();
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000431 if (!BugpointIsInterrupted)
432 ReducePassList(*this).reduceList(PassesToRun);
Chris Lattner8b189272004-02-18 23:26:28 +0000433
434 std::cout << "\n*** Found crashing pass"
435 << (PassesToRun.size() == 1 ? ": " : "es: ")
Misha Brukmaneed80e22004-07-23 01:30:49 +0000436 << getPassesString(PassesToRun) << '\n';
Chris Lattner8b189272004-02-18 23:26:28 +0000437
438 EmitProgressBytecode("passinput");
439
440 return DebugACrash(*this, TestForOptimizerCrash);
441}
442
443static bool TestForCodeGenCrash(BugDriver &BD, Module *M) {
444 try {
Misha Brukmaneed80e22004-07-23 01:30:49 +0000445 std::cerr << '\n';
Chris Lattner8b189272004-02-18 23:26:28 +0000446 BD.compileProgram(M);
Misha Brukmaneed80e22004-07-23 01:30:49 +0000447 std::cerr << '\n';
Chris Lattner8b189272004-02-18 23:26:28 +0000448 return false;
Jeff Cohen83881952005-01-22 16:30:58 +0000449 } catch (ToolExecutionError &) {
Chris Lattner8b189272004-02-18 23:26:28 +0000450 std::cerr << "<crash>\n";
451 return true; // Tool is still crashing.
452 }
453}
Chris Lattner02526262004-02-18 21:02:04 +0000454
455/// debugCodeGeneratorCrash - This method is called when the code generator
456/// crashes on an input. It attempts to reduce the input as much as possible
457/// while still causing the code generator to crash.
458bool BugDriver::debugCodeGeneratorCrash() {
Chris Lattner06905db2004-02-18 21:24:48 +0000459 std::cerr << "*** Debugging code generator crash!\n";
Chris Lattner02526262004-02-18 21:02:04 +0000460
Chris Lattner8b189272004-02-18 23:26:28 +0000461 return DebugACrash(*this, TestForCodeGenCrash);
Chris Lattner02526262004-02-18 21:02:04 +0000462}