blob: af64d7a9af6c751c58857180f51c7e4a2c53c32d [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 Lattnerafade922002-11-20 22:28:10 +000031
Brian Gaeked0fde302003-11-11 22:41:34 +000032namespace llvm {
33
Chris Lattner640f22e2003-04-24 17:02:17 +000034class DebugCrashes : public ListReducer<const PassInfo*> {
35 BugDriver &BD;
36public:
37 DebugCrashes(BugDriver &bd) : BD(bd) {}
38
39 // doTest - Return true iff running the "removed" passes succeeds, and running
40 // the "Kept" passes fail when run on the output of the "removed" passes. If
41 // we return true, we update the current module of bugpoint.
42 //
Chris Lattneraae33f92003-04-24 22:24:58 +000043 virtual TestResult doTest(std::vector<const PassInfo*> &Removed,
44 std::vector<const PassInfo*> &Kept);
Chris Lattner640f22e2003-04-24 17:02:17 +000045};
Chris Lattneraae33f92003-04-24 22:24:58 +000046
47DebugCrashes::TestResult
48DebugCrashes::doTest(std::vector<const PassInfo*> &Prefix,
49 std::vector<const PassInfo*> &Suffix) {
50 std::string PrefixOutput;
Chris Lattnerb417c792003-06-02 04:54:29 +000051 Module *OrigProgram = 0;
Chris Lattneraae33f92003-04-24 22:24:58 +000052 if (!Prefix.empty()) {
53 std::cout << "Checking to see if these passes crash: "
54 << getPassesString(Prefix) << ": ";
55 if (BD.runPasses(Prefix, PrefixOutput))
56 return KeepPrefix;
Chris Lattnerb417c792003-06-02 04:54:29 +000057
58 OrigProgram = BD.Program;
59
60 BD.Program = BD.ParseInputFile(PrefixOutput);
61 if (BD.Program == 0) {
62 std::cerr << BD.getToolName() << ": Error reading bytecode file '"
63 << PrefixOutput << "'!\n";
64 exit(1);
65 }
66 removeFile(PrefixOutput);
Chris Lattneraae33f92003-04-24 22:24:58 +000067 }
68
69 std::cout << "Checking to see if these passes crash: "
70 << getPassesString(Suffix) << ": ";
Chris Lattnerb417c792003-06-02 04:54:29 +000071
Chris Lattneraae33f92003-04-24 22:24:58 +000072 if (BD.runPasses(Suffix)) {
73 delete OrigProgram; // The suffix crashes alone...
74 return KeepSuffix;
75 }
76
77 // Nothing failed, restore state...
Chris Lattnerb417c792003-06-02 04:54:29 +000078 if (OrigProgram) {
79 delete BD.Program;
80 BD.Program = OrigProgram;
81 }
Chris Lattneraae33f92003-04-24 22:24:58 +000082 return NoFailure;
83}
84
85class ReduceCrashingFunctions : public ListReducer<Function*> {
86 BugDriver &BD;
87public:
88 ReduceCrashingFunctions(BugDriver &bd) : BD(bd) {}
89
90 virtual TestResult doTest(std::vector<Function*> &Prefix,
91 std::vector<Function*> &Kept) {
Misha Brukmana493ae32003-08-05 15:26:21 +000092 if (!Kept.empty() && TestFuncs(Kept))
Chris Lattneraae33f92003-04-24 22:24:58 +000093 return KeepSuffix;
94 if (!Prefix.empty() && TestFuncs(Prefix))
95 return KeepPrefix;
96 return NoFailure;
97 }
98
99 bool TestFuncs(std::vector<Function*> &Prefix);
100};
101
102bool ReduceCrashingFunctions::TestFuncs(std::vector<Function*> &Funcs) {
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000103 // Clone the program to try hacking it apart...
Chris Lattneraae33f92003-04-24 22:24:58 +0000104 Module *M = CloneModule(BD.Program);
105
106 // Convert list to set for fast lookup...
107 std::set<Function*> Functions;
108 for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
109 Function *CMF = M->getFunction(Funcs[i]->getName(),
110 Funcs[i]->getFunctionType());
111 assert(CMF && "Function not in module?!");
Chris Lattnerf607b792003-04-24 22:54:06 +0000112 Functions.insert(CMF);
Chris Lattneraae33f92003-04-24 22:24:58 +0000113 }
114
115 std::cout << "Checking for crash with only these functions:";
Chris Lattner73b96bd2003-10-27 04:44:59 +0000116 unsigned NumPrint = Funcs.size();
117 if (NumPrint > 10) NumPrint = 10;
118 for (unsigned i = 0; i != NumPrint; ++i)
Chris Lattneraae33f92003-04-24 22:24:58 +0000119 std::cout << " " << Funcs[i]->getName();
Chris Lattner73b96bd2003-10-27 04:44:59 +0000120 if (NumPrint < Funcs.size())
121 std::cout << "... <" << Funcs.size() << " total>";
Chris Lattneraae33f92003-04-24 22:24:58 +0000122 std::cout << ": ";
123
124 // Loop over and delete any functions which we aren't supposed to be playing
125 // with...
126 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerf607b792003-04-24 22:54:06 +0000127 if (!I->isExternal() && !Functions.count(I))
Chris Lattneraae33f92003-04-24 22:24:58 +0000128 DeleteFunctionBody(I);
129
130 // Try running the hacked up program...
131 std::swap(BD.Program, M);
132 if (BD.runPasses(BD.PassesToRun)) {
133 delete M; // It crashed, keep the trimmed version...
134
135 // Make sure to use function pointers that point into the now-current
136 // module.
137 Funcs.assign(Functions.begin(), Functions.end());
138 return true;
139 }
140 delete BD.Program; // It didn't crash, revert...
141 BD.Program = M;
142 return false;
143}
144
Chris Lattner640f22e2003-04-24 17:02:17 +0000145
Chris Lattner286921e2003-04-24 23:51:38 +0000146/// ReduceCrashingBlocks reducer - This works by setting the terminators of all
147/// terminators except the specified basic blocks to a 'ret' instruction, then
148/// running the simplify-cfg pass. This has the effect of chopping up the CFG
149/// really fast which can reduce large functions quickly.
150///
151class ReduceCrashingBlocks : public ListReducer<BasicBlock*> {
152 BugDriver &BD;
153public:
154 ReduceCrashingBlocks(BugDriver &bd) : BD(bd) {}
155
156 virtual TestResult doTest(std::vector<BasicBlock*> &Prefix,
157 std::vector<BasicBlock*> &Kept) {
John Criswell5cfff252003-08-04 18:24:31 +0000158 if (!Kept.empty() && TestBlocks(Kept))
Chris Lattner286921e2003-04-24 23:51:38 +0000159 return KeepSuffix;
160 if (!Prefix.empty() && TestBlocks(Prefix))
161 return KeepPrefix;
162 return NoFailure;
163 }
164
165 bool TestBlocks(std::vector<BasicBlock*> &Prefix);
166};
167
168bool ReduceCrashingBlocks::TestBlocks(std::vector<BasicBlock*> &BBs) {
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000169 // Clone the program to try hacking it apart...
Chris Lattner286921e2003-04-24 23:51:38 +0000170 Module *M = CloneModule(BD.Program);
171
172 // Convert list to set for fast lookup...
173 std::set<BasicBlock*> Blocks;
174 for (unsigned i = 0, e = BBs.size(); i != e; ++i) {
175 // Convert the basic block from the original module to the new module...
176 Function *F = BBs[i]->getParent();
177 Function *CMF = M->getFunction(F->getName(), F->getFunctionType());
178 assert(CMF && "Function not in module?!");
179
180 // Get the mapped basic block...
181 Function::iterator CBI = CMF->begin();
182 std::advance(CBI, std::distance(F->begin(), Function::iterator(BBs[i])));
183 Blocks.insert(CBI);
184 }
185
186 std::cout << "Checking for crash with only these blocks:";
Chris Lattner73b96bd2003-10-27 04:44:59 +0000187 unsigned NumPrint = Blocks.size();
188 if (NumPrint > 10) NumPrint = 10;
189 for (unsigned i = 0, e = NumPrint; i != e; ++i)
Chris Lattner286921e2003-04-24 23:51:38 +0000190 std::cout << " " << BBs[i]->getName();
Chris Lattner73b96bd2003-10-27 04:44:59 +0000191 if (NumPrint < Blocks.size())
192 std::cout << "... <" << Blocks.size() << " total>";
Chris Lattner286921e2003-04-24 23:51:38 +0000193 std::cout << ": ";
194
195 // Loop over and delete any hack up any blocks that are not listed...
196 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
197 for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
Chris Lattner8bc098b2003-11-22 02:10:38 +0000198 if (!Blocks.count(BB) && BB->getTerminator()->getNumSuccessors()) {
Chris Lattner286921e2003-04-24 23:51:38 +0000199 // Loop over all of the successors of this block, deleting any PHI nodes
200 // that might include it.
201 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
202 (*SI)->removePredecessor(BB);
203
Chris Lattner8bc098b2003-11-22 02:10:38 +0000204 if (BB->getTerminator()->getType() != Type::VoidTy)
205 BB->getTerminator()->replaceAllUsesWith(
206 Constant::getNullValue(BB->getTerminator()->getType()));
207
Chris Lattner286921e2003-04-24 23:51:38 +0000208 // Delete the old terminator instruction...
209 BB->getInstList().pop_back();
210
211 // Add a new return instruction of the appropriate type...
212 const Type *RetTy = BB->getParent()->getReturnType();
Chris Lattner8bc098b2003-11-22 02:10:38 +0000213 new ReturnInst(RetTy == Type::VoidTy ? 0 :
214 Constant::getNullValue(RetTy), BB);
Chris Lattner286921e2003-04-24 23:51:38 +0000215 }
216
217 // The CFG Simplifier pass may delete one of the basic blocks we are
218 // interested in. If it does we need to take the block out of the list. Make
219 // a "persistent mapping" by turning basic blocks into <function, name> pairs.
220 // This won't work well if blocks are unnamed, but that is just the risk we
221 // have to take.
222 std::vector<std::pair<Function*, std::string> > BlockInfo;
223
224 for (std::set<BasicBlock*>::iterator I = Blocks.begin(), E = Blocks.end();
225 I != E; ++I)
226 BlockInfo.push_back(std::make_pair((*I)->getParent(), (*I)->getName()));
227
228 // Now run the CFG simplify pass on the function...
229 PassManager Passes;
230 Passes.add(createCFGSimplificationPass());
231 Passes.add(createVerifierPass());
232 Passes.run(*M);
233
234 // Try running on the hacked up program...
235 std::swap(BD.Program, M);
236 if (BD.runPasses(BD.PassesToRun)) {
237 delete M; // It crashed, keep the trimmed version...
238
239 // Make sure to use basic block pointers that point into the now-current
240 // module, and that they don't include any deleted blocks.
241 BBs.clear();
242 for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) {
243 SymbolTable &ST = BlockInfo[i].first->getSymbolTable();
244 SymbolTable::iterator I = ST.find(Type::LabelTy);
245 if (I != ST.end() && I->second.count(BlockInfo[i].second))
246 BBs.push_back(cast<BasicBlock>(I->second[BlockInfo[i].second]));
247 }
248 return true;
249 }
250 delete BD.Program; // It didn't crash, revert...
251 BD.Program = M;
252 return false;
253}
254
Chris Lattnerafade922002-11-20 22:28:10 +0000255/// debugCrash - This method is called when some pass crashes on input. It
256/// attempts to prune down the testcase to something reasonable, and figure
257/// out exactly which pass is crashing.
258///
259bool BugDriver::debugCrash() {
Chris Lattneraae33f92003-04-24 22:24:58 +0000260 bool AnyReduction = false;
Chris Lattnerafade922002-11-20 22:28:10 +0000261 std::cout << "\n*** Debugging optimizer crash!\n";
262
Chris Lattner640f22e2003-04-24 17:02:17 +0000263 // Reduce the list of passes which causes the optimizer to crash...
Chris Lattneraae33f92003-04-24 22:24:58 +0000264 unsigned OldSize = PassesToRun.size();
Chris Lattner640f22e2003-04-24 17:02:17 +0000265 DebugCrashes(*this).reduceList(PassesToRun);
Chris Lattner640f22e2003-04-24 17:02:17 +0000266
Chris Lattneraae33f92003-04-24 22:24:58 +0000267 std::cout << "\n*** Found crashing pass"
268 << (PassesToRun.size() == 1 ? ": " : "es: ")
269 << getPassesString(PassesToRun) << "\n";
Chris Lattnerafade922002-11-20 22:28:10 +0000270
Chris Lattner640f22e2003-04-24 17:02:17 +0000271 EmitProgressBytecode("passinput");
Chris Lattner5f73e382003-04-25 00:53:05 +0000272
273 // See if we can get away with nuking all of the global variable initializers
274 // in the program...
275 if (Program->gbegin() != Program->gend()) {
276 Module *M = CloneModule(Program);
277 bool DeletedInit = false;
278 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
279 if (I->hasInitializer()) {
280 I->setInitializer(0);
281 I->setLinkage(GlobalValue::ExternalLinkage);
282 DeletedInit = true;
283 }
284
285 if (!DeletedInit) {
286 delete M; // No change made...
287 } else {
288 // See if the program still causes a crash...
289 std::cout << "\nChecking to see if we can delete global inits: ";
290 std::swap(Program, M);
291 if (runPasses(PassesToRun)) { // Still crashes?
292 AnyReduction = true;
293 delete M;
294 std::cout << "\n*** Able to remove all global initializers!\n";
295 } else { // No longer crashes?
296 delete Program; // Restore program.
297 Program = M;
298 std::cout << " - Removing all global inits hides problem!\n";
299 }
300 }
301 }
Chris Lattneraae33f92003-04-24 22:24:58 +0000302
303 // Now try to reduce the number of functions in the module to something small.
304 std::vector<Function*> Functions;
305 for (Module::iterator I = Program->begin(), E = Program->end(); I != E; ++I)
306 if (!I->isExternal())
307 Functions.push_back(I);
Chris Lattnerafade922002-11-20 22:28:10 +0000308
Chris Lattneraae33f92003-04-24 22:24:58 +0000309 if (Functions.size() > 1) {
310 std::cout << "\n*** Attempting to reduce the number of functions "
311 "in the testcase\n";
Chris Lattnerafade922002-11-20 22:28:10 +0000312
Chris Lattneraae33f92003-04-24 22:24:58 +0000313 OldSize = Functions.size();
314 ReduceCrashingFunctions(*this).reduceList(Functions);
Chris Lattnerafade922002-11-20 22:28:10 +0000315
Chris Lattneraae33f92003-04-24 22:24:58 +0000316 if (Functions.size() < OldSize) {
317 EmitProgressBytecode("reduced-function");
318 AnyReduction = true;
Chris Lattner65207852003-01-23 02:48:33 +0000319 }
Chris Lattnerafade922002-11-20 22:28:10 +0000320 }
321
Chris Lattner286921e2003-04-24 23:51:38 +0000322 // Attempt to delete entire basic blocks at a time to speed up
323 // convergence... this actually works by setting the terminator of the blocks
324 // to a return instruction then running simplifycfg, which can potentially
325 // shrinks the code dramatically quickly
326 //
Chris Lattner47ae4a12003-08-05 15:51:05 +0000327 if (!DisableSimplifyCFG) {
328 std::vector<BasicBlock*> Blocks;
329 for (Module::iterator I = Program->begin(), E = Program->end(); I != E; ++I)
330 for (Function::iterator FI = I->begin(), E = I->end(); FI != E; ++FI)
331 Blocks.push_back(FI);
332 ReduceCrashingBlocks(*this).reduceList(Blocks);
333 }
Chris Lattner218e26e2002-12-23 23:49:59 +0000334
Chris Lattneraae33f92003-04-24 22:24:58 +0000335 // FIXME: This should use the list reducer to converge faster by deleting
336 // larger chunks of instructions at a time!
Chris Lattner65207852003-01-23 02:48:33 +0000337 unsigned Simplification = 4;
338 do {
339 --Simplification;
340 std::cout << "\n*** Attempting to reduce testcase by deleting instruc"
341 << "tions: Simplification Level #" << Simplification << "\n";
342
Misha Brukman5560c9d2003-08-18 14:43:39 +0000343 // Now that we have deleted the functions that are unnecessary for the
344 // program, try to remove instructions that are not necessary to cause the
Chris Lattner65207852003-01-23 02:48:33 +0000345 // crash. To do this, we loop through all of the instructions in the
346 // remaining functions, deleting them (replacing any values produced with
347 // nulls), and then running ADCE and SimplifyCFG. If the transformed input
348 // still triggers failure, keep deleting until we cannot trigger failure
349 // anymore.
350 //
351 TryAgain:
352
353 // Loop over all of the (non-terminator) instructions remaining in the
354 // function, attempting to delete them.
355 for (Module::iterator FI = Program->begin(), E = Program->end();
356 FI != E; ++FI)
357 if (!FI->isExternal()) {
358 for (Function::iterator BI = FI->begin(), E = FI->end(); BI != E; ++BI)
359 for (BasicBlock::iterator I = BI->begin(), E = --BI->end();
360 I != E; ++I) {
361 Module *M = deleteInstructionFromProgram(I, Simplification);
362
363 // Make the function the current program...
364 std::swap(Program, M);
365
366 // Find out if the pass still crashes on this pass...
367 std::cout << "Checking instruction '" << I->getName() << "': ";
Chris Lattneraae33f92003-04-24 22:24:58 +0000368 if (runPasses(PassesToRun)) {
Chris Lattner65207852003-01-23 02:48:33 +0000369 // Yup, it does, we delete the old module, and continue trying to
370 // reduce the testcase...
Chris Lattner65207852003-01-23 02:48:33 +0000371 delete M;
Chris Lattnerf607b792003-04-24 22:54:06 +0000372 AnyReduction = true;
Chris Lattner65207852003-01-23 02:48:33 +0000373 goto TryAgain; // I wish I had a multi-level break here!
374 }
375
376 // This pass didn't crash without this instruction, try the next
377 // one.
378 delete Program;
379 Program = M;
380 }
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 Lattner898e0e42003-06-25 04:13:52 +0000388 std::swap(Program, M);
Chris Lattnerba386d92003-02-28 16:13:20 +0000389
Chris Lattner898e0e42003-06-25 04:13:52 +0000390 // Find out if the pass still crashes on the cleaned up program...
391 if (runPasses(PassesToRun)) {
392 // Yup, it does, keep the reduced version...
393 delete M;
394 AnyReduction = true;
395 } else {
396 delete Program; // Otherwise, restore the original module...
397 Program = M;
Chris Lattnerba386d92003-02-28 16:13:20 +0000398 }
399
Chris Lattnerf607b792003-04-24 22:54:06 +0000400 if (AnyReduction)
Chris Lattner640f22e2003-04-24 17:02:17 +0000401 EmitProgressBytecode("reduced-simplified");
Chris Lattnerba386d92003-02-28 16:13:20 +0000402
Chris Lattnerafade922002-11-20 22:28:10 +0000403 return false;
404}
Brian Gaeked0fde302003-11-11 22:41:34 +0000405
406} // End llvm namespace