blob: 8c29ea226a43d645642f7455b378e6bb2bb4548d [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)
198 if (!Blocks.count(BB) && !isa<ReturnInst>(BB->getTerminator())) {
199 // 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
204 // Delete the old terminator instruction...
205 BB->getInstList().pop_back();
206
207 // Add a new return instruction of the appropriate type...
208 const Type *RetTy = BB->getParent()->getReturnType();
209 ReturnInst *RI = new ReturnInst(RetTy == Type::VoidTy ? 0 :
210 Constant::getNullValue(RetTy));
211 BB->getInstList().push_back(RI);
212 }
213
214 // The CFG Simplifier pass may delete one of the basic blocks we are
215 // interested in. If it does we need to take the block out of the list. Make
216 // a "persistent mapping" by turning basic blocks into <function, name> pairs.
217 // This won't work well if blocks are unnamed, but that is just the risk we
218 // have to take.
219 std::vector<std::pair<Function*, std::string> > BlockInfo;
220
221 for (std::set<BasicBlock*>::iterator I = Blocks.begin(), E = Blocks.end();
222 I != E; ++I)
223 BlockInfo.push_back(std::make_pair((*I)->getParent(), (*I)->getName()));
224
225 // Now run the CFG simplify pass on the function...
226 PassManager Passes;
227 Passes.add(createCFGSimplificationPass());
228 Passes.add(createVerifierPass());
229 Passes.run(*M);
230
231 // Try running on the hacked up program...
232 std::swap(BD.Program, M);
233 if (BD.runPasses(BD.PassesToRun)) {
234 delete M; // It crashed, keep the trimmed version...
235
236 // Make sure to use basic block pointers that point into the now-current
237 // module, and that they don't include any deleted blocks.
238 BBs.clear();
239 for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) {
240 SymbolTable &ST = BlockInfo[i].first->getSymbolTable();
241 SymbolTable::iterator I = ST.find(Type::LabelTy);
242 if (I != ST.end() && I->second.count(BlockInfo[i].second))
243 BBs.push_back(cast<BasicBlock>(I->second[BlockInfo[i].second]));
244 }
245 return true;
246 }
247 delete BD.Program; // It didn't crash, revert...
248 BD.Program = M;
249 return false;
250}
251
Chris Lattnerafade922002-11-20 22:28:10 +0000252/// debugCrash - This method is called when some pass crashes on input. It
253/// attempts to prune down the testcase to something reasonable, and figure
254/// out exactly which pass is crashing.
255///
256bool BugDriver::debugCrash() {
Chris Lattneraae33f92003-04-24 22:24:58 +0000257 bool AnyReduction = false;
Chris Lattnerafade922002-11-20 22:28:10 +0000258 std::cout << "\n*** Debugging optimizer crash!\n";
259
Chris Lattner640f22e2003-04-24 17:02:17 +0000260 // Reduce the list of passes which causes the optimizer to crash...
Chris Lattneraae33f92003-04-24 22:24:58 +0000261 unsigned OldSize = PassesToRun.size();
Chris Lattner640f22e2003-04-24 17:02:17 +0000262 DebugCrashes(*this).reduceList(PassesToRun);
Chris Lattner640f22e2003-04-24 17:02:17 +0000263
Chris Lattneraae33f92003-04-24 22:24:58 +0000264 std::cout << "\n*** Found crashing pass"
265 << (PassesToRun.size() == 1 ? ": " : "es: ")
266 << getPassesString(PassesToRun) << "\n";
Chris Lattnerafade922002-11-20 22:28:10 +0000267
Chris Lattner640f22e2003-04-24 17:02:17 +0000268 EmitProgressBytecode("passinput");
Chris Lattner5f73e382003-04-25 00:53:05 +0000269
270 // See if we can get away with nuking all of the global variable initializers
271 // in the program...
272 if (Program->gbegin() != Program->gend()) {
273 Module *M = CloneModule(Program);
274 bool DeletedInit = false;
275 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
276 if (I->hasInitializer()) {
277 I->setInitializer(0);
278 I->setLinkage(GlobalValue::ExternalLinkage);
279 DeletedInit = true;
280 }
281
282 if (!DeletedInit) {
283 delete M; // No change made...
284 } else {
285 // See if the program still causes a crash...
286 std::cout << "\nChecking to see if we can delete global inits: ";
287 std::swap(Program, M);
288 if (runPasses(PassesToRun)) { // Still crashes?
289 AnyReduction = true;
290 delete M;
291 std::cout << "\n*** Able to remove all global initializers!\n";
292 } else { // No longer crashes?
293 delete Program; // Restore program.
294 Program = M;
295 std::cout << " - Removing all global inits hides problem!\n";
296 }
297 }
298 }
Chris Lattneraae33f92003-04-24 22:24:58 +0000299
300 // Now try to reduce the number of functions in the module to something small.
301 std::vector<Function*> Functions;
302 for (Module::iterator I = Program->begin(), E = Program->end(); I != E; ++I)
303 if (!I->isExternal())
304 Functions.push_back(I);
Chris Lattnerafade922002-11-20 22:28:10 +0000305
Chris Lattneraae33f92003-04-24 22:24:58 +0000306 if (Functions.size() > 1) {
307 std::cout << "\n*** Attempting to reduce the number of functions "
308 "in the testcase\n";
Chris Lattnerafade922002-11-20 22:28:10 +0000309
Chris Lattneraae33f92003-04-24 22:24:58 +0000310 OldSize = Functions.size();
311 ReduceCrashingFunctions(*this).reduceList(Functions);
Chris Lattnerafade922002-11-20 22:28:10 +0000312
Chris Lattneraae33f92003-04-24 22:24:58 +0000313 if (Functions.size() < OldSize) {
314 EmitProgressBytecode("reduced-function");
315 AnyReduction = true;
Chris Lattner65207852003-01-23 02:48:33 +0000316 }
Chris Lattnerafade922002-11-20 22:28:10 +0000317 }
318
Chris Lattner286921e2003-04-24 23:51:38 +0000319 // Attempt to delete entire basic blocks at a time to speed up
320 // convergence... this actually works by setting the terminator of the blocks
321 // to a return instruction then running simplifycfg, which can potentially
322 // shrinks the code dramatically quickly
323 //
Chris Lattner47ae4a12003-08-05 15:51:05 +0000324 if (!DisableSimplifyCFG) {
325 std::vector<BasicBlock*> Blocks;
326 for (Module::iterator I = Program->begin(), E = Program->end(); I != E; ++I)
327 for (Function::iterator FI = I->begin(), E = I->end(); FI != E; ++FI)
328 Blocks.push_back(FI);
329 ReduceCrashingBlocks(*this).reduceList(Blocks);
330 }
Chris Lattner218e26e2002-12-23 23:49:59 +0000331
Chris Lattneraae33f92003-04-24 22:24:58 +0000332 // FIXME: This should use the list reducer to converge faster by deleting
333 // larger chunks of instructions at a time!
Chris Lattner65207852003-01-23 02:48:33 +0000334 unsigned Simplification = 4;
335 do {
336 --Simplification;
337 std::cout << "\n*** Attempting to reduce testcase by deleting instruc"
338 << "tions: Simplification Level #" << Simplification << "\n";
339
Misha Brukman5560c9d2003-08-18 14:43:39 +0000340 // Now that we have deleted the functions that are unnecessary for the
341 // program, try to remove instructions that are not necessary to cause the
Chris Lattner65207852003-01-23 02:48:33 +0000342 // crash. To do this, we loop through all of the instructions in the
343 // remaining functions, deleting them (replacing any values produced with
344 // nulls), and then running ADCE and SimplifyCFG. If the transformed input
345 // still triggers failure, keep deleting until we cannot trigger failure
346 // anymore.
347 //
348 TryAgain:
349
350 // Loop over all of the (non-terminator) instructions remaining in the
351 // function, attempting to delete them.
352 for (Module::iterator FI = Program->begin(), E = Program->end();
353 FI != E; ++FI)
354 if (!FI->isExternal()) {
355 for (Function::iterator BI = FI->begin(), E = FI->end(); BI != E; ++BI)
356 for (BasicBlock::iterator I = BI->begin(), E = --BI->end();
357 I != E; ++I) {
358 Module *M = deleteInstructionFromProgram(I, Simplification);
359
360 // Make the function the current program...
361 std::swap(Program, M);
362
363 // Find out if the pass still crashes on this pass...
364 std::cout << "Checking instruction '" << I->getName() << "': ";
Chris Lattneraae33f92003-04-24 22:24:58 +0000365 if (runPasses(PassesToRun)) {
Chris Lattner65207852003-01-23 02:48:33 +0000366 // Yup, it does, we delete the old module, and continue trying to
367 // reduce the testcase...
Chris Lattner65207852003-01-23 02:48:33 +0000368 delete M;
Chris Lattnerf607b792003-04-24 22:54:06 +0000369 AnyReduction = true;
Chris Lattner65207852003-01-23 02:48:33 +0000370 goto TryAgain; // I wish I had a multi-level break here!
371 }
372
373 // This pass didn't crash without this instruction, try the next
374 // one.
375 delete Program;
376 Program = M;
377 }
378 }
379 } while (Simplification);
Chris Lattnerba386d92003-02-28 16:13:20 +0000380
381 // Try to clean up the testcase by running funcresolve and globaldce...
Chris Lattner898e0e42003-06-25 04:13:52 +0000382 std::cout << "\n*** Attempting to perform final cleanups: ";
Chris Lattner417477d2003-11-05 21:15:19 +0000383 Module *M = CloneModule(Program);
Chris Lattnerfcb6ec02003-11-05 21:45:35 +0000384 M = performFinalCleanups(M, true);
Chris Lattner898e0e42003-06-25 04:13:52 +0000385 std::swap(Program, M);
Chris Lattnerba386d92003-02-28 16:13:20 +0000386
Chris Lattner898e0e42003-06-25 04:13:52 +0000387 // Find out if the pass still crashes on the cleaned up program...
388 if (runPasses(PassesToRun)) {
389 // Yup, it does, keep the reduced version...
390 delete M;
391 AnyReduction = true;
392 } else {
393 delete Program; // Otherwise, restore the original module...
394 Program = 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
403} // End llvm namespace