blob: 19d62b4ea2fca0f72178013949bcc661730bba94 [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 Lattner286921e2003-04-24 23:51:38 +000026#include "llvm/Transforms/Scalar.h"
Chris Lattneraae33f92003-04-24 22:24:58 +000027#include "llvm/Transforms/Utils/Cloning.h"
Misha Brukman3d9cafa2003-08-07 21:42:28 +000028#include "Support/FileUtilities.h"
Chris Lattnerafade922002-11-20 22:28:10 +000029#include <fstream>
Chris Lattneraae33f92003-04-24 22:24:58 +000030#include <set>
Chris Lattnerfa761832004-01-14 03:38:37 +000031using namespace llvm;
Chris Lattnerafade922002-11-20 22:28:10 +000032
Brian Gaeked0fde302003-11-11 22:41:34 +000033namespace llvm {
Chris Lattner06905db2004-02-18 21:24:48 +000034 class ReducePassList : public ListReducer<const PassInfo*> {
Chris Lattnerfa761832004-01-14 03:38:37 +000035 BugDriver &BD;
36 public:
Chris Lattner06905db2004-02-18 21:24:48 +000037 ReducePassList(BugDriver &bd) : BD(bd) {}
Chris Lattnerfa761832004-01-14 03:38:37 +000038
39 // doTest - Return true iff running the "removed" passes succeeds, and
40 // running the "Kept" passes fail when run on the output of the "removed"
41 // passes. If we return true, we update the current module of bugpoint.
42 //
43 virtual TestResult doTest(std::vector<const PassInfo*> &Removed,
44 std::vector<const PassInfo*> &Kept);
45 };
46}
Chris Lattneraae33f92003-04-24 22:24:58 +000047
Chris Lattner06905db2004-02-18 21:24:48 +000048ReducePassList::TestResult
49ReducePassList::doTest(std::vector<const PassInfo*> &Prefix,
50 std::vector<const PassInfo*> &Suffix) {
Chris Lattneraae33f92003-04-24 22:24:58 +000051 std::string PrefixOutput;
Chris Lattnerb417c792003-06-02 04:54:29 +000052 Module *OrigProgram = 0;
Chris Lattneraae33f92003-04-24 22:24:58 +000053 if (!Prefix.empty()) {
54 std::cout << "Checking to see if these passes crash: "
55 << getPassesString(Prefix) << ": ";
56 if (BD.runPasses(Prefix, PrefixOutput))
57 return KeepPrefix;
Chris Lattnerb417c792003-06-02 04:54:29 +000058
59 OrigProgram = BD.Program;
60
61 BD.Program = BD.ParseInputFile(PrefixOutput);
62 if (BD.Program == 0) {
63 std::cerr << BD.getToolName() << ": Error reading bytecode file '"
64 << PrefixOutput << "'!\n";
65 exit(1);
66 }
67 removeFile(PrefixOutput);
Chris Lattneraae33f92003-04-24 22:24:58 +000068 }
69
70 std::cout << "Checking to see if these passes crash: "
71 << getPassesString(Suffix) << ": ";
Chris Lattnerb417c792003-06-02 04:54:29 +000072
Chris Lattneraae33f92003-04-24 22:24:58 +000073 if (BD.runPasses(Suffix)) {
74 delete OrigProgram; // The suffix crashes alone...
75 return KeepSuffix;
76 }
77
78 // Nothing failed, restore state...
Chris Lattnerb417c792003-06-02 04:54:29 +000079 if (OrigProgram) {
80 delete BD.Program;
81 BD.Program = OrigProgram;
82 }
Chris Lattneraae33f92003-04-24 22:24:58 +000083 return NoFailure;
84}
85
Chris Lattnerfa761832004-01-14 03:38:37 +000086namespace llvm {
87 class ReduceCrashingFunctions : public ListReducer<Function*> {
88 BugDriver &BD;
89 public:
90 ReduceCrashingFunctions(BugDriver &bd) : BD(bd) {}
91
92 virtual TestResult doTest(std::vector<Function*> &Prefix,
93 std::vector<Function*> &Kept) {
94 if (!Kept.empty() && TestFuncs(Kept))
95 return KeepSuffix;
96 if (!Prefix.empty() && TestFuncs(Prefix))
97 return KeepPrefix;
98 return NoFailure;
99 }
100
101 bool TestFuncs(std::vector<Function*> &Prefix);
102 };
103}
Chris Lattneraae33f92003-04-24 22:24:58 +0000104
105bool ReduceCrashingFunctions::TestFuncs(std::vector<Function*> &Funcs) {
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000106 // Clone the program to try hacking it apart...
Chris Lattner06905db2004-02-18 21:24:48 +0000107 Module *M = CloneModule(BD.getProgram());
Chris Lattneraae33f92003-04-24 22:24:58 +0000108
109 // Convert list to set for fast lookup...
110 std::set<Function*> Functions;
111 for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
112 Function *CMF = M->getFunction(Funcs[i]->getName(),
113 Funcs[i]->getFunctionType());
114 assert(CMF && "Function not in module?!");
Chris Lattnerf607b792003-04-24 22:54:06 +0000115 Functions.insert(CMF);
Chris Lattneraae33f92003-04-24 22:24:58 +0000116 }
117
118 std::cout << "Checking for crash with only these functions:";
Chris Lattner73b96bd2003-10-27 04:44:59 +0000119 unsigned NumPrint = Funcs.size();
120 if (NumPrint > 10) NumPrint = 10;
121 for (unsigned i = 0; i != NumPrint; ++i)
Chris Lattneraae33f92003-04-24 22:24:58 +0000122 std::cout << " " << Funcs[i]->getName();
Chris Lattner73b96bd2003-10-27 04:44:59 +0000123 if (NumPrint < Funcs.size())
124 std::cout << "... <" << Funcs.size() << " total>";
Chris Lattneraae33f92003-04-24 22:24:58 +0000125 std::cout << ": ";
126
127 // Loop over and delete any functions which we aren't supposed to be playing
128 // with...
129 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerf607b792003-04-24 22:54:06 +0000130 if (!I->isExternal() && !Functions.count(I))
Chris Lattneraae33f92003-04-24 22:24:58 +0000131 DeleteFunctionBody(I);
132
133 // Try running the hacked up program...
Chris Lattner06905db2004-02-18 21:24:48 +0000134 if (BD.runPasses(M)) {
135 BD.setNewProgram(M); // It crashed, keep the trimmed version...
Chris Lattneraae33f92003-04-24 22:24:58 +0000136
137 // Make sure to use function pointers that point into the now-current
138 // module.
139 Funcs.assign(Functions.begin(), Functions.end());
140 return true;
141 }
Chris Lattner06905db2004-02-18 21:24:48 +0000142 delete M;
Chris Lattneraae33f92003-04-24 22:24:58 +0000143 return false;
144}
145
Chris Lattner640f22e2003-04-24 17:02:17 +0000146
Chris Lattnerf913f402004-02-18 21:29:46 +0000147namespace {
Chris Lattnerfa761832004-01-14 03:38:37 +0000148 /// ReduceCrashingBlocks reducer - This works by setting the terminators of
149 /// all terminators except the specified basic blocks to a 'ret' instruction,
150 /// then running the simplify-cfg pass. This has the effect of chopping up
151 /// the CFG really fast which can reduce large functions quickly.
152 ///
153 class ReduceCrashingBlocks : public ListReducer<BasicBlock*> {
154 BugDriver &BD;
155 public:
156 ReduceCrashingBlocks(BugDriver &bd) : BD(bd) {}
Chris Lattner286921e2003-04-24 23:51:38 +0000157
Chris Lattnerfa761832004-01-14 03:38:37 +0000158 virtual TestResult doTest(std::vector<BasicBlock*> &Prefix,
159 std::vector<BasicBlock*> &Kept) {
160 if (!Kept.empty() && TestBlocks(Kept))
161 return KeepSuffix;
162 if (!Prefix.empty() && TestBlocks(Prefix))
163 return KeepPrefix;
164 return NoFailure;
165 }
Chris Lattner286921e2003-04-24 23:51:38 +0000166
Chris Lattnerfa761832004-01-14 03:38:37 +0000167 bool TestBlocks(std::vector<BasicBlock*> &Prefix);
168 };
169}
Chris Lattner286921e2003-04-24 23:51:38 +0000170
171bool ReduceCrashingBlocks::TestBlocks(std::vector<BasicBlock*> &BBs) {
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000172 // Clone the program to try hacking it apart...
Chris Lattner06905db2004-02-18 21:24:48 +0000173 Module *M = CloneModule(BD.getProgram());
Chris Lattner286921e2003-04-24 23:51:38 +0000174
175 // Convert list to set for fast lookup...
176 std::set<BasicBlock*> Blocks;
177 for (unsigned i = 0, e = BBs.size(); i != e; ++i) {
178 // Convert the basic block from the original module to the new module...
179 Function *F = BBs[i]->getParent();
180 Function *CMF = M->getFunction(F->getName(), F->getFunctionType());
181 assert(CMF && "Function not in module?!");
182
183 // Get the mapped basic block...
184 Function::iterator CBI = CMF->begin();
185 std::advance(CBI, std::distance(F->begin(), Function::iterator(BBs[i])));
186 Blocks.insert(CBI);
187 }
188
189 std::cout << "Checking for crash with only these blocks:";
Chris Lattner73b96bd2003-10-27 04:44:59 +0000190 unsigned NumPrint = Blocks.size();
191 if (NumPrint > 10) NumPrint = 10;
192 for (unsigned i = 0, e = NumPrint; i != e; ++i)
Chris Lattner286921e2003-04-24 23:51:38 +0000193 std::cout << " " << BBs[i]->getName();
Chris Lattner73b96bd2003-10-27 04:44:59 +0000194 if (NumPrint < Blocks.size())
195 std::cout << "... <" << Blocks.size() << " total>";
Chris Lattner286921e2003-04-24 23:51:38 +0000196 std::cout << ": ";
197
198 // Loop over and delete any hack up any blocks that are not listed...
199 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
200 for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
Chris Lattner8bc098b2003-11-22 02:10:38 +0000201 if (!Blocks.count(BB) && BB->getTerminator()->getNumSuccessors()) {
Chris Lattner286921e2003-04-24 23:51:38 +0000202 // Loop over all of the successors of this block, deleting any PHI nodes
203 // that might include it.
204 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
205 (*SI)->removePredecessor(BB);
206
Chris Lattner8bc098b2003-11-22 02:10:38 +0000207 if (BB->getTerminator()->getType() != Type::VoidTy)
208 BB->getTerminator()->replaceAllUsesWith(
209 Constant::getNullValue(BB->getTerminator()->getType()));
210
Chris Lattner286921e2003-04-24 23:51:38 +0000211 // Delete the old terminator instruction...
212 BB->getInstList().pop_back();
213
214 // Add a new return instruction of the appropriate type...
215 const Type *RetTy = BB->getParent()->getReturnType();
Chris Lattner8bc098b2003-11-22 02:10:38 +0000216 new ReturnInst(RetTy == Type::VoidTy ? 0 :
217 Constant::getNullValue(RetTy), BB);
Chris Lattner286921e2003-04-24 23:51:38 +0000218 }
219
220 // The CFG Simplifier pass may delete one of the basic blocks we are
221 // interested in. If it does we need to take the block out of the list. Make
222 // a "persistent mapping" by turning basic blocks into <function, name> pairs.
223 // This won't work well if blocks are unnamed, but that is just the risk we
224 // have to take.
225 std::vector<std::pair<Function*, std::string> > BlockInfo;
226
227 for (std::set<BasicBlock*>::iterator I = Blocks.begin(), E = Blocks.end();
228 I != E; ++I)
229 BlockInfo.push_back(std::make_pair((*I)->getParent(), (*I)->getName()));
230
231 // Now run the CFG simplify pass on the function...
232 PassManager Passes;
233 Passes.add(createCFGSimplificationPass());
234 Passes.add(createVerifierPass());
235 Passes.run(*M);
236
237 // Try running on the hacked up program...
Chris Lattner06905db2004-02-18 21:24:48 +0000238 if (BD.runPasses(M)) {
239 BD.setNewProgram(M); // It crashed, keep the trimmed version...
Chris Lattner286921e2003-04-24 23:51:38 +0000240
241 // Make sure to use basic block pointers that point into the now-current
242 // module, and that they don't include any deleted blocks.
243 BBs.clear();
244 for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) {
245 SymbolTable &ST = BlockInfo[i].first->getSymbolTable();
246 SymbolTable::iterator I = ST.find(Type::LabelTy);
247 if (I != ST.end() && I->second.count(BlockInfo[i].second))
248 BBs.push_back(cast<BasicBlock>(I->second[BlockInfo[i].second]));
249 }
250 return true;
251 }
Chris Lattner06905db2004-02-18 21:24:48 +0000252 delete M; // It didn't crash, try something else.
Chris Lattner286921e2003-04-24 23:51:38 +0000253 return false;
254}
255
Chris Lattner06905db2004-02-18 21:24:48 +0000256static bool TestForOptimizerCrash(BugDriver &BD) {
257 return BD.runPasses();
258}
259
260
Chris Lattner02526262004-02-18 21:02:04 +0000261/// debugOptimizerCrash - This method is called when some pass crashes on input.
262/// It attempts to prune down the testcase to something reasonable, and figure
Chris Lattnerafade922002-11-20 22:28:10 +0000263/// out exactly which pass is crashing.
264///
Chris Lattner02526262004-02-18 21:02:04 +0000265bool BugDriver::debugOptimizerCrash() {
Chris Lattneraae33f92003-04-24 22:24:58 +0000266 bool AnyReduction = false;
Chris Lattnerafade922002-11-20 22:28:10 +0000267 std::cout << "\n*** Debugging optimizer crash!\n";
268
Chris Lattner640f22e2003-04-24 17:02:17 +0000269 // Reduce the list of passes which causes the optimizer to crash...
Chris Lattneraae33f92003-04-24 22:24:58 +0000270 unsigned OldSize = PassesToRun.size();
Chris Lattner06905db2004-02-18 21:24:48 +0000271 ReducePassList(*this).reduceList(PassesToRun);
Chris Lattner640f22e2003-04-24 17:02:17 +0000272
Chris Lattneraae33f92003-04-24 22:24:58 +0000273 std::cout << "\n*** Found crashing pass"
274 << (PassesToRun.size() == 1 ? ": " : "es: ")
275 << getPassesString(PassesToRun) << "\n";
Chris Lattnerafade922002-11-20 22:28:10 +0000276
Chris Lattner640f22e2003-04-24 17:02:17 +0000277 EmitProgressBytecode("passinput");
Chris Lattner5f73e382003-04-25 00:53:05 +0000278
279 // See if we can get away with nuking all of the global variable initializers
280 // in the program...
281 if (Program->gbegin() != Program->gend()) {
282 Module *M = CloneModule(Program);
283 bool DeletedInit = false;
284 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
285 if (I->hasInitializer()) {
286 I->setInitializer(0);
287 I->setLinkage(GlobalValue::ExternalLinkage);
288 DeletedInit = true;
289 }
290
291 if (!DeletedInit) {
292 delete M; // No change made...
293 } else {
294 // See if the program still causes a crash...
295 std::cout << "\nChecking to see if we can delete global inits: ";
Chris Lattner06905db2004-02-18 21:24:48 +0000296 if (runPasses(M)) { // Still crashes?
297 setNewProgram(M);
Chris Lattner5f73e382003-04-25 00:53:05 +0000298 AnyReduction = true;
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 }
Chris Lattneraae33f92003-04-24 22:24:58 +0000306
307 // Now try to reduce the number of functions in the module to something small.
308 std::vector<Function*> Functions;
309 for (Module::iterator I = Program->begin(), E = Program->end(); I != E; ++I)
310 if (!I->isExternal())
311 Functions.push_back(I);
Chris Lattnerafade922002-11-20 22:28:10 +0000312
Chris Lattneraae33f92003-04-24 22:24:58 +0000313 if (Functions.size() > 1) {
314 std::cout << "\n*** Attempting to reduce the number of functions "
315 "in the testcase\n";
Chris Lattnerafade922002-11-20 22:28:10 +0000316
Chris Lattneraae33f92003-04-24 22:24:58 +0000317 OldSize = Functions.size();
318 ReduceCrashingFunctions(*this).reduceList(Functions);
Chris Lattnerafade922002-11-20 22:28:10 +0000319
Chris Lattneraae33f92003-04-24 22:24:58 +0000320 if (Functions.size() < OldSize) {
321 EmitProgressBytecode("reduced-function");
322 AnyReduction = true;
Chris Lattner65207852003-01-23 02:48:33 +0000323 }
Chris Lattnerafade922002-11-20 22:28:10 +0000324 }
325
Chris Lattner286921e2003-04-24 23:51:38 +0000326 // Attempt to delete entire basic blocks at a time to speed up
327 // convergence... this actually works by setting the terminator of the blocks
328 // to a return instruction then running simplifycfg, which can potentially
329 // shrinks the code dramatically quickly
330 //
Chris Lattner47ae4a12003-08-05 15:51:05 +0000331 if (!DisableSimplifyCFG) {
332 std::vector<BasicBlock*> Blocks;
333 for (Module::iterator I = Program->begin(), E = Program->end(); I != E; ++I)
334 for (Function::iterator FI = I->begin(), E = I->end(); FI != E; ++FI)
335 Blocks.push_back(FI);
336 ReduceCrashingBlocks(*this).reduceList(Blocks);
337 }
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 Lattner65207852003-01-23 02:48:33 +0000341 unsigned Simplification = 4;
342 do {
343 --Simplification;
344 std::cout << "\n*** Attempting to reduce testcase by deleting instruc"
345 << "tions: Simplification Level #" << Simplification << "\n";
346
Misha Brukman5560c9d2003-08-18 14:43:39 +0000347 // Now that we have deleted the functions that are unnecessary for the
348 // program, try to remove instructions that are not necessary to cause the
Chris Lattner65207852003-01-23 02:48:33 +0000349 // crash. To do this, we loop through all of the instructions in the
350 // remaining functions, deleting them (replacing any values produced with
351 // nulls), and then running ADCE and SimplifyCFG. If the transformed input
352 // still triggers failure, keep deleting until we cannot trigger failure
353 // anymore.
354 //
355 TryAgain:
356
357 // Loop over all of the (non-terminator) instructions remaining in the
358 // function, attempting to delete them.
359 for (Module::iterator FI = Program->begin(), E = Program->end();
360 FI != E; ++FI)
361 if (!FI->isExternal()) {
362 for (Function::iterator BI = FI->begin(), E = FI->end(); BI != E; ++BI)
363 for (BasicBlock::iterator I = BI->begin(), E = --BI->end();
364 I != E; ++I) {
365 Module *M = deleteInstructionFromProgram(I, Simplification);
366
Chris Lattner65207852003-01-23 02:48:33 +0000367 // Find out if the pass still crashes on this pass...
368 std::cout << "Checking instruction '" << I->getName() << "': ";
Chris Lattner06905db2004-02-18 21:24:48 +0000369 if (runPasses(M)) {
Chris Lattner65207852003-01-23 02:48:33 +0000370 // Yup, it does, we delete the old module, and continue trying to
371 // reduce the testcase...
Chris Lattner06905db2004-02-18 21:24:48 +0000372 setNewProgram(M);
Chris Lattnerf607b792003-04-24 22:54:06 +0000373 AnyReduction = true;
Chris Lattner65207852003-01-23 02:48:33 +0000374 goto TryAgain; // I wish I had a multi-level break here!
375 }
376
377 // This pass didn't crash without this instruction, try the next
378 // one.
Chris Lattner06905db2004-02-18 21:24:48 +0000379 delete M;
Chris Lattner65207852003-01-23 02:48:33 +0000380 }
381 }
382 } while (Simplification);
Chris Lattnerba386d92003-02-28 16:13:20 +0000383
384 // Try to clean up the testcase by running funcresolve and globaldce...
Chris Lattner898e0e42003-06-25 04:13:52 +0000385 std::cout << "\n*** Attempting to perform final cleanups: ";
Chris Lattner417477d2003-11-05 21:15:19 +0000386 Module *M = CloneModule(Program);
Chris Lattnerfcb6ec02003-11-05 21:45:35 +0000387 M = performFinalCleanups(M, true);
Chris Lattnerba386d92003-02-28 16:13:20 +0000388
Chris Lattner898e0e42003-06-25 04:13:52 +0000389 // Find out if the pass still crashes on the cleaned up program...
Chris Lattner06905db2004-02-18 21:24:48 +0000390 if (runPasses(M)) {
391 setNewProgram(M); // Yup, it does, keep the reduced version...
Chris Lattner898e0e42003-06-25 04:13:52 +0000392 AnyReduction = true;
393 } else {
Chris Lattner06905db2004-02-18 21:24:48 +0000394 delete M;
Chris Lattnerba386d92003-02-28 16:13:20 +0000395 }
396
Chris Lattnerf607b792003-04-24 22:54:06 +0000397 if (AnyReduction)
Chris Lattner640f22e2003-04-24 17:02:17 +0000398 EmitProgressBytecode("reduced-simplified");
Chris Lattnerba386d92003-02-28 16:13:20 +0000399
Chris Lattnerafade922002-11-20 22:28:10 +0000400 return false;
401}
Brian Gaeked0fde302003-11-11 22:41:34 +0000402
Chris Lattner02526262004-02-18 21:02:04 +0000403
404
405/// debugCodeGeneratorCrash - This method is called when the code generator
406/// crashes on an input. It attempts to reduce the input as much as possible
407/// while still causing the code generator to crash.
408bool BugDriver::debugCodeGeneratorCrash() {
Chris Lattner06905db2004-02-18 21:24:48 +0000409 std::cerr << "*** Debugging code generator crash!\n";
Chris Lattner02526262004-02-18 21:02:04 +0000410
411 return false;
412}