Don't use PathV1.h in ToolRunner.h.
[oota-llvm.git] / tools / bugpoint / CrashDebugger.cpp
1 //===- CrashDebugger.cpp - Debug compilation crashes ----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the bugpoint internals that narrow down compilation crashes
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "BugDriver.h"
15 #include "ListReducer.h"
16 #include "ToolRunner.h"
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/Analysis/Verifier.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/ValueSymbolTable.h"
24 #include "llvm/Pass.h"
25 #include "llvm/PassManager.h"
26 #include "llvm/Support/CFG.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/FileUtilities.h"
29 #include "llvm/Support/PathV1.h"
30 #include "llvm/Transforms/Scalar.h"
31 #include "llvm/Transforms/Utils/Cloning.h"
32 #include <set>
33 using namespace llvm;
34
35 namespace {
36   cl::opt<bool>
37   KeepMain("keep-main",
38            cl::desc("Force function reduction to keep main"),
39            cl::init(false));
40   cl::opt<bool>
41   NoGlobalRM ("disable-global-remove",
42          cl::desc("Do not remove global variables"),
43          cl::init(false));
44 }
45
46 namespace llvm {
47   class ReducePassList : public ListReducer<std::string> {
48     BugDriver &BD;
49   public:
50     ReducePassList(BugDriver &bd) : BD(bd) {}
51
52     // doTest - Return true iff running the "removed" passes succeeds, and
53     // running the "Kept" passes fail when run on the output of the "removed"
54     // passes.  If we return true, we update the current module of bugpoint.
55     //
56     virtual TestResult doTest(std::vector<std::string> &Removed,
57                               std::vector<std::string> &Kept,
58                               std::string &Error);
59   };
60 }
61
62 ReducePassList::TestResult
63 ReducePassList::doTest(std::vector<std::string> &Prefix,
64                        std::vector<std::string> &Suffix,
65                        std::string &Error) {
66   sys::Path PrefixOutput;
67   Module *OrigProgram = 0;
68   if (!Prefix.empty()) {
69     outs() << "Checking to see if these passes crash: "
70            << getPassesString(Prefix) << ": ";
71     std::string PfxOutput;
72     if (BD.runPasses(BD.getProgram(), Prefix, PfxOutput))
73       return KeepPrefix;
74
75     PrefixOutput.set(PfxOutput);
76     OrigProgram = BD.Program;
77
78     BD.Program = ParseInputFile(PrefixOutput.str(), BD.getContext());
79     if (BD.Program == 0) {
80       errs() << BD.getToolName() << ": Error reading bitcode file '"
81              << PrefixOutput.str() << "'!\n";
82       exit(1);
83     }
84     PrefixOutput.eraseFromDisk();
85   }
86
87   outs() << "Checking to see if these passes crash: "
88          << getPassesString(Suffix) << ": ";
89
90   if (BD.runPasses(BD.getProgram(), Suffix)) {
91     delete OrigProgram;            // The suffix crashes alone...
92     return KeepSuffix;
93   }
94
95   // Nothing failed, restore state...
96   if (OrigProgram) {
97     delete BD.Program;
98     BD.Program = OrigProgram;
99   }
100   return NoFailure;
101 }
102
103 namespace {
104   /// ReduceCrashingGlobalVariables - This works by removing the global
105   /// variable's initializer and seeing if the program still crashes. If it
106   /// does, then we keep that program and try again.
107   ///
108   class ReduceCrashingGlobalVariables : public ListReducer<GlobalVariable*> {
109     BugDriver &BD;
110     bool (*TestFn)(const BugDriver &, Module *);
111   public:
112     ReduceCrashingGlobalVariables(BugDriver &bd,
113                                   bool (*testFn)(const BugDriver &, Module *))
114       : BD(bd), TestFn(testFn) {}
115
116     virtual TestResult doTest(std::vector<GlobalVariable*> &Prefix,
117                               std::vector<GlobalVariable*> &Kept,
118                               std::string &Error) {
119       if (!Kept.empty() && TestGlobalVariables(Kept))
120         return KeepSuffix;
121       if (!Prefix.empty() && TestGlobalVariables(Prefix))
122         return KeepPrefix;
123       return NoFailure;
124     }
125
126     bool TestGlobalVariables(std::vector<GlobalVariable*> &GVs);
127   };
128 }
129
130 bool
131 ReduceCrashingGlobalVariables::TestGlobalVariables(
132                               std::vector<GlobalVariable*> &GVs) {
133   // Clone the program to try hacking it apart...
134   ValueToValueMapTy VMap;
135   Module *M = CloneModule(BD.getProgram(), VMap);
136
137   // Convert list to set for fast lookup...
138   std::set<GlobalVariable*> GVSet;
139
140   for (unsigned i = 0, e = GVs.size(); i != e; ++i) {
141     GlobalVariable* CMGV = cast<GlobalVariable>(VMap[GVs[i]]);
142     assert(CMGV && "Global Variable not in module?!");
143     GVSet.insert(CMGV);
144   }
145
146   outs() << "Checking for crash with only these global variables: ";
147   PrintGlobalVariableList(GVs);
148   outs() << ": ";
149
150   // Loop over and delete any global variables which we aren't supposed to be
151   // playing with...
152   for (Module::global_iterator I = M->global_begin(), E = M->global_end();
153        I != E; ++I)
154     if (I->hasInitializer() && !GVSet.count(I)) {
155       I->setInitializer(0);
156       I->setLinkage(GlobalValue::ExternalLinkage);
157     }
158
159   // Try running the hacked up program...
160   if (TestFn(BD, M)) {
161     BD.setNewProgram(M);        // It crashed, keep the trimmed version...
162
163     // Make sure to use global variable pointers that point into the now-current
164     // module.
165     GVs.assign(GVSet.begin(), GVSet.end());
166     return true;
167   }
168
169   delete M;
170   return false;
171 }
172
173 namespace {
174   /// ReduceCrashingFunctions reducer - This works by removing functions and
175   /// seeing if the program still crashes. If it does, then keep the newer,
176   /// smaller program.
177   ///
178   class ReduceCrashingFunctions : public ListReducer<Function*> {
179     BugDriver &BD;
180     bool (*TestFn)(const BugDriver &, Module *);
181   public:
182     ReduceCrashingFunctions(BugDriver &bd,
183                             bool (*testFn)(const BugDriver &, Module *))
184       : BD(bd), TestFn(testFn) {}
185
186     virtual TestResult doTest(std::vector<Function*> &Prefix,
187                               std::vector<Function*> &Kept,
188                               std::string &Error) {
189       if (!Kept.empty() && TestFuncs(Kept))
190         return KeepSuffix;
191       if (!Prefix.empty() && TestFuncs(Prefix))
192         return KeepPrefix;
193       return NoFailure;
194     }
195
196     bool TestFuncs(std::vector<Function*> &Prefix);
197   };
198 }
199
200 bool ReduceCrashingFunctions::TestFuncs(std::vector<Function*> &Funcs) {
201
202   //if main isn't present, claim there is no problem
203   if (KeepMain && find(Funcs.begin(), Funcs.end(),
204                        BD.getProgram()->getFunction("main")) == Funcs.end())
205     return false;
206
207   // Clone the program to try hacking it apart...
208   ValueToValueMapTy VMap;
209   Module *M = CloneModule(BD.getProgram(), VMap);
210
211   // Convert list to set for fast lookup...
212   std::set<Function*> Functions;
213   for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
214     Function *CMF = cast<Function>(VMap[Funcs[i]]);
215     assert(CMF && "Function not in module?!");
216     assert(CMF->getFunctionType() == Funcs[i]->getFunctionType() && "wrong ty");
217     assert(CMF->getName() == Funcs[i]->getName() && "wrong name");
218     Functions.insert(CMF);
219   }
220
221   outs() << "Checking for crash with only these functions: ";
222   PrintFunctionList(Funcs);
223   outs() << ": ";
224
225   // Loop over and delete any functions which we aren't supposed to be playing
226   // with...
227   for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
228     if (!I->isDeclaration() && !Functions.count(I))
229       DeleteFunctionBody(I);
230
231   // Try running the hacked up program...
232   if (TestFn(BD, M)) {
233     BD.setNewProgram(M);        // It crashed, keep the trimmed version...
234
235     // Make sure to use function pointers that point into the now-current
236     // module.
237     Funcs.assign(Functions.begin(), Functions.end());
238     return true;
239   }
240   delete M;
241   return false;
242 }
243
244
245 namespace {
246   /// ReduceCrashingBlocks reducer - This works by setting the terminators of
247   /// all terminators except the specified basic blocks to a 'ret' instruction,
248   /// then running the simplify-cfg pass.  This has the effect of chopping up
249   /// the CFG really fast which can reduce large functions quickly.
250   ///
251   class ReduceCrashingBlocks : public ListReducer<const BasicBlock*> {
252     BugDriver &BD;
253     bool (*TestFn)(const BugDriver &, Module *);
254   public:
255     ReduceCrashingBlocks(BugDriver &bd,
256                          bool (*testFn)(const BugDriver &, Module *))
257       : BD(bd), TestFn(testFn) {}
258
259     virtual TestResult doTest(std::vector<const BasicBlock*> &Prefix,
260                               std::vector<const BasicBlock*> &Kept,
261                               std::string &Error) {
262       if (!Kept.empty() && TestBlocks(Kept))
263         return KeepSuffix;
264       if (!Prefix.empty() && TestBlocks(Prefix))
265         return KeepPrefix;
266       return NoFailure;
267     }
268
269     bool TestBlocks(std::vector<const BasicBlock*> &Prefix);
270   };
271 }
272
273 bool ReduceCrashingBlocks::TestBlocks(std::vector<const BasicBlock*> &BBs) {
274   // Clone the program to try hacking it apart...
275   ValueToValueMapTy VMap;
276   Module *M = CloneModule(BD.getProgram(), VMap);
277
278   // Convert list to set for fast lookup...
279   SmallPtrSet<BasicBlock*, 8> Blocks;
280   for (unsigned i = 0, e = BBs.size(); i != e; ++i)
281     Blocks.insert(cast<BasicBlock>(VMap[BBs[i]]));
282
283   outs() << "Checking for crash with only these blocks:";
284   unsigned NumPrint = Blocks.size();
285   if (NumPrint > 10) NumPrint = 10;
286   for (unsigned i = 0, e = NumPrint; i != e; ++i)
287     outs() << " " << BBs[i]->getName();
288   if (NumPrint < Blocks.size())
289     outs() << "... <" << Blocks.size() << " total>";
290   outs() << ": ";
291
292   // Loop over and delete any hack up any blocks that are not listed...
293   for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
294     for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
295       if (!Blocks.count(BB) && BB->getTerminator()->getNumSuccessors()) {
296         // Loop over all of the successors of this block, deleting any PHI nodes
297         // that might include it.
298         for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
299           (*SI)->removePredecessor(BB);
300
301         TerminatorInst *BBTerm = BB->getTerminator();
302         
303         if (!BB->getTerminator()->getType()->isVoidTy())
304           BBTerm->replaceAllUsesWith(Constant::getNullValue(BBTerm->getType()));
305
306         // Replace the old terminator instruction.
307         BB->getInstList().pop_back();
308         new UnreachableInst(BB->getContext(), BB);
309       }
310
311   // The CFG Simplifier pass may delete one of the basic blocks we are
312   // interested in.  If it does we need to take the block out of the list.  Make
313   // a "persistent mapping" by turning basic blocks into <function, name> pairs.
314   // This won't work well if blocks are unnamed, but that is just the risk we
315   // have to take.
316   std::vector<std::pair<std::string, std::string> > BlockInfo;
317
318   for (SmallPtrSet<BasicBlock*, 8>::iterator I = Blocks.begin(),
319          E = Blocks.end(); I != E; ++I)
320     BlockInfo.push_back(std::make_pair((*I)->getParent()->getName(),
321                                        (*I)->getName()));
322
323   // Now run the CFG simplify pass on the function...
324   std::vector<std::string> Passes;
325   Passes.push_back("simplifycfg");
326   Passes.push_back("verify");
327   Module *New = BD.runPassesOn(M, Passes);
328   delete M;
329   if (!New) {
330     errs() << "simplifycfg failed!\n";
331     exit(1);
332   }
333   M = New;
334
335   // Try running on the hacked up program...
336   if (TestFn(BD, M)) {
337     BD.setNewProgram(M);      // It crashed, keep the trimmed version...
338
339     // Make sure to use basic block pointers that point into the now-current
340     // module, and that they don't include any deleted blocks.
341     BBs.clear();
342     const ValueSymbolTable &GST = M->getValueSymbolTable();
343     for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) {
344       Function *F = cast<Function>(GST.lookup(BlockInfo[i].first));
345       ValueSymbolTable &ST = F->getValueSymbolTable();
346       Value* V = ST.lookup(BlockInfo[i].second);
347       if (V && V->getType() == Type::getLabelTy(V->getContext()))
348         BBs.push_back(cast<BasicBlock>(V));
349     }
350     return true;
351   }
352   delete M;  // It didn't crash, try something else.
353   return false;
354 }
355
356 namespace {
357   /// ReduceCrashingInstructions reducer - This works by removing the specified
358   /// non-terminator instructions and replacing them with undef.
359   ///
360   class ReduceCrashingInstructions : public ListReducer<const Instruction*> {
361     BugDriver &BD;
362     bool (*TestFn)(const BugDriver &, Module *);
363   public:
364     ReduceCrashingInstructions(BugDriver &bd,
365                                bool (*testFn)(const BugDriver &, Module *))
366       : BD(bd), TestFn(testFn) {}
367
368     virtual TestResult doTest(std::vector<const Instruction*> &Prefix,
369                               std::vector<const Instruction*> &Kept,
370                               std::string &Error) {
371       if (!Kept.empty() && TestInsts(Kept))
372         return KeepSuffix;
373       if (!Prefix.empty() && TestInsts(Prefix))
374         return KeepPrefix;
375       return NoFailure;
376     }
377
378     bool TestInsts(std::vector<const Instruction*> &Prefix);
379   };
380 }
381
382 bool ReduceCrashingInstructions::TestInsts(std::vector<const Instruction*>
383                                            &Insts) {
384   // Clone the program to try hacking it apart...
385   ValueToValueMapTy VMap;
386   Module *M = CloneModule(BD.getProgram(), VMap);
387
388   // Convert list to set for fast lookup...
389   SmallPtrSet<Instruction*, 64> Instructions;
390   for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
391     assert(!isa<TerminatorInst>(Insts[i]));
392     Instructions.insert(cast<Instruction>(VMap[Insts[i]]));
393   }
394
395   outs() << "Checking for crash with only " << Instructions.size();
396   if (Instructions.size() == 1)
397     outs() << " instruction: ";
398   else
399     outs() << " instructions: ";
400
401   for (Module::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI)
402     for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE; ++FI)
403       for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E;) {
404         Instruction *Inst = I++;
405         if (!Instructions.count(Inst) && !isa<TerminatorInst>(Inst) &&
406             !isa<LandingPadInst>(Inst)) {
407           if (!Inst->getType()->isVoidTy())
408             Inst->replaceAllUsesWith(UndefValue::get(Inst->getType()));
409           Inst->eraseFromParent();
410         }
411       }
412
413   // Verify that this is still valid.
414   PassManager Passes;
415   Passes.add(createVerifierPass());
416   Passes.run(*M);
417
418   // Try running on the hacked up program...
419   if (TestFn(BD, M)) {
420     BD.setNewProgram(M);      // It crashed, keep the trimmed version...
421
422     // Make sure to use instruction pointers that point into the now-current
423     // module, and that they don't include any deleted blocks.
424     Insts.clear();
425     for (SmallPtrSet<Instruction*, 64>::const_iterator I = Instructions.begin(),
426              E = Instructions.end(); I != E; ++I)
427       Insts.push_back(*I);
428     return true;
429   }
430   delete M;  // It didn't crash, try something else.
431   return false;
432 }
433
434 /// DebugACrash - Given a predicate that determines whether a component crashes
435 /// on a program, try to destructively reduce the program while still keeping
436 /// the predicate true.
437 static bool DebugACrash(BugDriver &BD,
438                         bool (*TestFn)(const BugDriver &, Module *),
439                         std::string &Error) {
440   // See if we can get away with nuking some of the global variable initializers
441   // in the program...
442   if (!NoGlobalRM &&
443       BD.getProgram()->global_begin() != BD.getProgram()->global_end()) {
444     // Now try to reduce the number of global variable initializers in the
445     // module to something small.
446     Module *M = CloneModule(BD.getProgram());
447     bool DeletedInit = false;
448
449     for (Module::global_iterator I = M->global_begin(), E = M->global_end();
450          I != E; ++I)
451       if (I->hasInitializer()) {
452         I->setInitializer(0);
453         I->setLinkage(GlobalValue::ExternalLinkage);
454         DeletedInit = true;
455       }
456
457     if (!DeletedInit) {
458       delete M;  // No change made...
459     } else {
460       // See if the program still causes a crash...
461       outs() << "\nChecking to see if we can delete global inits: ";
462
463       if (TestFn(BD, M)) {      // Still crashes?
464         BD.setNewProgram(M);
465         outs() << "\n*** Able to remove all global initializers!\n";
466       } else {                  // No longer crashes?
467         outs() << "  - Removing all global inits hides problem!\n";
468         delete M;
469
470         std::vector<GlobalVariable*> GVs;
471
472         for (Module::global_iterator I = BD.getProgram()->global_begin(),
473                E = BD.getProgram()->global_end(); I != E; ++I)
474           if (I->hasInitializer())
475             GVs.push_back(I);
476
477         if (GVs.size() > 1 && !BugpointIsInterrupted) {
478           outs() << "\n*** Attempting to reduce the number of global "
479                     << "variables in the testcase\n";
480
481           unsigned OldSize = GVs.size();
482           ReduceCrashingGlobalVariables(BD, TestFn).reduceList(GVs, Error);
483           if (!Error.empty())
484             return true;
485
486           if (GVs.size() < OldSize)
487             BD.EmitProgressBitcode(BD.getProgram(), "reduced-global-variables");
488         }
489       }
490     }
491   }
492
493   // Now try to reduce the number of functions in the module to something small.
494   std::vector<Function*> Functions;
495   for (Module::iterator I = BD.getProgram()->begin(),
496          E = BD.getProgram()->end(); I != E; ++I)
497     if (!I->isDeclaration())
498       Functions.push_back(I);
499
500   if (Functions.size() > 1 && !BugpointIsInterrupted) {
501     outs() << "\n*** Attempting to reduce the number of functions "
502       "in the testcase\n";
503
504     unsigned OldSize = Functions.size();
505     ReduceCrashingFunctions(BD, TestFn).reduceList(Functions, Error);
506
507     if (Functions.size() < OldSize)
508       BD.EmitProgressBitcode(BD.getProgram(), "reduced-function");
509   }
510
511   // Attempt to delete entire basic blocks at a time to speed up
512   // convergence... this actually works by setting the terminator of the blocks
513   // to a return instruction then running simplifycfg, which can potentially
514   // shrinks the code dramatically quickly
515   //
516   if (!DisableSimplifyCFG && !BugpointIsInterrupted) {
517     std::vector<const BasicBlock*> Blocks;
518     for (Module::const_iterator I = BD.getProgram()->begin(),
519            E = BD.getProgram()->end(); I != E; ++I)
520       for (Function::const_iterator FI = I->begin(), E = I->end(); FI !=E; ++FI)
521         Blocks.push_back(FI);
522     unsigned OldSize = Blocks.size();
523     ReduceCrashingBlocks(BD, TestFn).reduceList(Blocks, Error);
524     if (Blocks.size() < OldSize)
525       BD.EmitProgressBitcode(BD.getProgram(), "reduced-blocks");
526   }
527
528   // Attempt to delete instructions using bisection. This should help out nasty
529   // cases with large basic blocks where the problem is at one end.
530   if (!BugpointIsInterrupted) {
531     std::vector<const Instruction*> Insts;
532     for (Module::const_iterator MI = BD.getProgram()->begin(),
533            ME = BD.getProgram()->end(); MI != ME; ++MI)
534       for (Function::const_iterator FI = MI->begin(), FE = MI->end(); FI != FE;
535            ++FI)
536         for (BasicBlock::const_iterator I = FI->begin(), E = FI->end();
537              I != E; ++I)
538           if (!isa<TerminatorInst>(I))
539             Insts.push_back(I);
540
541     ReduceCrashingInstructions(BD, TestFn).reduceList(Insts, Error);
542   }
543
544   // FIXME: This should use the list reducer to converge faster by deleting
545   // larger chunks of instructions at a time!
546   unsigned Simplification = 2;
547   do {
548     if (BugpointIsInterrupted) break;
549     --Simplification;
550     outs() << "\n*** Attempting to reduce testcase by deleting instruc"
551            << "tions: Simplification Level #" << Simplification << '\n';
552
553     // Now that we have deleted the functions that are unnecessary for the
554     // program, try to remove instructions that are not necessary to cause the
555     // crash.  To do this, we loop through all of the instructions in the
556     // remaining functions, deleting them (replacing any values produced with
557     // nulls), and then running ADCE and SimplifyCFG.  If the transformed input
558     // still triggers failure, keep deleting until we cannot trigger failure
559     // anymore.
560     //
561     unsigned InstructionsToSkipBeforeDeleting = 0;
562   TryAgain:
563
564     // Loop over all of the (non-terminator) instructions remaining in the
565     // function, attempting to delete them.
566     unsigned CurInstructionNum = 0;
567     for (Module::const_iterator FI = BD.getProgram()->begin(),
568            E = BD.getProgram()->end(); FI != E; ++FI)
569       if (!FI->isDeclaration())
570         for (Function::const_iterator BI = FI->begin(), E = FI->end(); BI != E;
571              ++BI)
572           for (BasicBlock::const_iterator I = BI->begin(), E = --BI->end();
573                I != E; ++I, ++CurInstructionNum) {
574             if (InstructionsToSkipBeforeDeleting) {
575               --InstructionsToSkipBeforeDeleting;
576             } else {
577               if (BugpointIsInterrupted) goto ExitLoops;
578
579               if (isa<LandingPadInst>(I))
580                 continue;
581
582               outs() << "Checking instruction: " << *I;
583               Module *M = BD.deleteInstructionFromProgram(I, Simplification);
584
585               // Find out if the pass still crashes on this pass...
586               if (TestFn(BD, M)) {
587                 // Yup, it does, we delete the old module, and continue trying
588                 // to reduce the testcase...
589                 BD.setNewProgram(M);
590                 InstructionsToSkipBeforeDeleting = CurInstructionNum;
591                 goto TryAgain;  // I wish I had a multi-level break here!
592               }
593
594               // This pass didn't crash without this instruction, try the next
595               // one.
596               delete M;
597             }
598           }
599
600     if (InstructionsToSkipBeforeDeleting) {
601       InstructionsToSkipBeforeDeleting = 0;
602       goto TryAgain;
603     }
604
605   } while (Simplification);
606 ExitLoops:
607
608   // Try to clean up the testcase by running funcresolve and globaldce...
609   if (!BugpointIsInterrupted) {
610     outs() << "\n*** Attempting to perform final cleanups: ";
611     Module *M = CloneModule(BD.getProgram());
612     M = BD.performFinalCleanups(M, true);
613
614     // Find out if the pass still crashes on the cleaned up program...
615     if (TestFn(BD, M)) {
616       BD.setNewProgram(M);     // Yup, it does, keep the reduced version...
617     } else {
618       delete M;
619     }
620   }
621
622   BD.EmitProgressBitcode(BD.getProgram(), "reduced-simplified");
623
624   return false;
625 }
626
627 static bool TestForOptimizerCrash(const BugDriver &BD, Module *M) {
628   return BD.runPasses(M);
629 }
630
631 /// debugOptimizerCrash - This method is called when some pass crashes on input.
632 /// It attempts to prune down the testcase to something reasonable, and figure
633 /// out exactly which pass is crashing.
634 ///
635 bool BugDriver::debugOptimizerCrash(const std::string &ID) {
636   outs() << "\n*** Debugging optimizer crash!\n";
637
638   std::string Error;
639   // Reduce the list of passes which causes the optimizer to crash...
640   if (!BugpointIsInterrupted)
641     ReducePassList(*this).reduceList(PassesToRun, Error);
642   assert(Error.empty());
643
644   outs() << "\n*** Found crashing pass"
645          << (PassesToRun.size() == 1 ? ": " : "es: ")
646          << getPassesString(PassesToRun) << '\n';
647
648   EmitProgressBitcode(Program, ID);
649
650   bool Success = DebugACrash(*this, TestForOptimizerCrash, Error);
651   assert(Error.empty());
652   return Success;
653 }
654
655 static bool TestForCodeGenCrash(const BugDriver &BD, Module *M) {
656   std::string Error;
657   BD.compileProgram(M, &Error);
658   if (!Error.empty()) {
659     errs() << "<crash>\n";
660     return true;  // Tool is still crashing.
661   }
662   errs() << '\n';
663   return false;
664 }
665
666 /// debugCodeGeneratorCrash - This method is called when the code generator
667 /// crashes on an input.  It attempts to reduce the input as much as possible
668 /// while still causing the code generator to crash.
669 bool BugDriver::debugCodeGeneratorCrash(std::string &Error) {
670   errs() << "*** Debugging code generator crash!\n";
671
672   return DebugACrash(*this, TestForCodeGenCrash, Error);
673 }