Replace more a*'s with arg_*'s, thanks to Gabor Greif!
[oota-llvm.git] / tools / bugpoint / CrashDebugger.cpp
1 //===- CrashDebugger.cpp - Debug compilation crashes ----------------------===//
2 // 
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 //===----------------------------------------------------------------------===//
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 "llvm/Constant.h"
17 #include "llvm/Instructions.h"
18 #include "llvm/Module.h"
19 #include "llvm/Pass.h"
20 #include "llvm/PassManager.h"
21 #include "llvm/SymbolTable.h"
22 #include "llvm/Type.h"
23 #include "llvm/Analysis/Verifier.h"
24 #include "llvm/Bytecode/Writer.h"
25 #include "llvm/Support/CFG.h"
26 #include "llvm/Support/ToolRunner.h"
27 #include "llvm/Transforms/Scalar.h"
28 #include "llvm/Transforms/Utils/Cloning.h"
29 #include "llvm/Support/FileUtilities.h"
30 #include <fstream>
31 #include <set>
32 using namespace llvm;
33
34 namespace llvm {
35   class ReducePassList : public ListReducer<const PassInfo*> {
36     BugDriver &BD;
37   public:
38     ReducePassList(BugDriver &bd) : BD(bd) {}
39     
40     // doTest - Return true iff running the "removed" passes succeeds, and
41     // running the "Kept" passes fail when run on the output of the "removed"
42     // passes.  If we return true, we update the current module of bugpoint.
43     //
44     virtual TestResult doTest(std::vector<const PassInfo*> &Removed,
45                               std::vector<const PassInfo*> &Kept);
46   };
47 }
48
49 ReducePassList::TestResult
50 ReducePassList::doTest(std::vector<const PassInfo*> &Prefix,
51                        std::vector<const PassInfo*> &Suffix) {
52   sys::Path PrefixOutput;
53   Module *OrigProgram = 0;
54   if (!Prefix.empty()) {
55     std::cout << "Checking to see if these passes crash: "
56               << getPassesString(Prefix) << ": ";
57     std::string PfxOutput;
58     if (BD.runPasses(Prefix, PfxOutput))
59       return KeepPrefix;
60
61     PrefixOutput.setFile(PfxOutput);
62     OrigProgram = BD.Program;
63
64     BD.Program = ParseInputFile(PrefixOutput.toString());
65     if (BD.Program == 0) {
66       std::cerr << BD.getToolName() << ": Error reading bytecode file '"
67                 << PrefixOutput << "'!\n";
68       exit(1);
69     }
70     PrefixOutput.destroyFile();
71   }
72
73   std::cout << "Checking to see if these passes crash: "
74             << getPassesString(Suffix) << ": ";
75   
76   if (BD.runPasses(Suffix)) {
77     delete OrigProgram;            // The suffix crashes alone...
78     return KeepSuffix;
79   }
80
81   // Nothing failed, restore state...
82   if (OrigProgram) {
83     delete BD.Program;
84     BD.Program = OrigProgram;
85   }
86   return NoFailure;
87 }
88
89 namespace llvm {
90   class ReduceCrashingFunctions : public ListReducer<Function*> {
91     BugDriver &BD;
92     bool (*TestFn)(BugDriver &, Module *);
93   public:
94     ReduceCrashingFunctions(BugDriver &bd,
95                             bool (*testFn)(BugDriver &, Module *))
96       : BD(bd), TestFn(testFn) {}
97     
98     virtual TestResult doTest(std::vector<Function*> &Prefix,
99                               std::vector<Function*> &Kept) {
100       if (!Kept.empty() && TestFuncs(Kept))
101         return KeepSuffix;
102       if (!Prefix.empty() && TestFuncs(Prefix))
103         return KeepPrefix;
104       return NoFailure;
105     }
106     
107     bool TestFuncs(std::vector<Function*> &Prefix);
108   };
109 }
110
111 bool ReduceCrashingFunctions::TestFuncs(std::vector<Function*> &Funcs) {
112   // Clone the program to try hacking it apart...
113   Module *M = CloneModule(BD.getProgram());
114   
115   // Convert list to set for fast lookup...
116   std::set<Function*> Functions;
117   for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
118     Function *CMF = M->getFunction(Funcs[i]->getName(), 
119                                    Funcs[i]->getFunctionType());
120     assert(CMF && "Function not in module?!");
121     Functions.insert(CMF);
122   }
123
124   std::cout << "Checking for crash with only these functions: ";
125   PrintFunctionList(Funcs);
126   std::cout << ": ";
127
128   // Loop over and delete any functions which we aren't supposed to be playing
129   // with...
130   for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
131     if (!I->isExternal() && !Functions.count(I))
132       DeleteFunctionBody(I);
133
134   // Try running the hacked up program...
135   if (TestFn(BD, M)) {
136     BD.setNewProgram(M);        // It crashed, keep the trimmed version...
137
138     // Make sure to use function pointers that point into the now-current
139     // module.
140     Funcs.assign(Functions.begin(), Functions.end());
141     return true;
142   }
143   delete M;
144   return false;
145 }
146
147
148 namespace {
149   /// ReduceCrashingBlocks reducer - This works by setting the terminators of
150   /// all terminators except the specified basic blocks to a 'ret' instruction,
151   /// then running the simplify-cfg pass.  This has the effect of chopping up
152   /// the CFG really fast which can reduce large functions quickly.
153   ///
154   class ReduceCrashingBlocks : public ListReducer<const BasicBlock*> {
155     BugDriver &BD;
156     bool (*TestFn)(BugDriver &, Module *);
157   public:
158     ReduceCrashingBlocks(BugDriver &bd, bool (*testFn)(BugDriver &, Module *))
159       : BD(bd), TestFn(testFn) {}
160     
161     virtual TestResult doTest(std::vector<const BasicBlock*> &Prefix,
162                               std::vector<const BasicBlock*> &Kept) {
163       if (!Kept.empty() && TestBlocks(Kept))
164         return KeepSuffix;
165       if (!Prefix.empty() && TestBlocks(Prefix))
166         return KeepPrefix;
167       return NoFailure;
168     }
169     
170     bool TestBlocks(std::vector<const BasicBlock*> &Prefix);
171   };
172 }
173
174 bool ReduceCrashingBlocks::TestBlocks(std::vector<const BasicBlock*> &BBs) {
175   // Clone the program to try hacking it apart...
176   Module *M = CloneModule(BD.getProgram());
177   
178   // Convert list to set for fast lookup...
179   std::set<BasicBlock*> Blocks;
180   for (unsigned i = 0, e = BBs.size(); i != e; ++i) {
181     // Convert the basic block from the original module to the new module...
182     const Function *F = BBs[i]->getParent();
183     Function *CMF = M->getFunction(F->getName(), F->getFunctionType());
184     assert(CMF && "Function not in module?!");
185
186     // Get the mapped basic block...
187     Function::iterator CBI = CMF->begin();
188     std::advance(CBI, std::distance(F->begin(),
189                                     Function::const_iterator(BBs[i])));
190     Blocks.insert(CBI);
191   }
192
193   std::cout << "Checking for crash with only these blocks:";
194   unsigned NumPrint = Blocks.size();
195   if (NumPrint > 10) NumPrint = 10;
196   for (unsigned i = 0, e = NumPrint; i != e; ++i)
197     std::cout << " " << BBs[i]->getName();
198   if (NumPrint < Blocks.size())
199     std::cout << "... <" << Blocks.size() << " total>";
200   std::cout << ": ";
201
202   // Loop over and delete any hack up any blocks that are not listed...
203   for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
204     for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
205       if (!Blocks.count(BB) && BB->getTerminator()->getNumSuccessors()) {
206         // Loop over all of the successors of this block, deleting any PHI nodes
207         // that might include it.
208         for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
209           (*SI)->removePredecessor(BB);
210
211         if (BB->getTerminator()->getType() != Type::VoidTy)
212           BB->getTerminator()->replaceAllUsesWith(
213                       Constant::getNullValue(BB->getTerminator()->getType()));
214
215         // Delete the old terminator instruction...
216         BB->getInstList().pop_back();
217         
218         // Add a new return instruction of the appropriate type...
219         const Type *RetTy = BB->getParent()->getReturnType();
220         new ReturnInst(RetTy == Type::VoidTy ? 0 :
221                        Constant::getNullValue(RetTy), BB);
222       }
223
224   // The CFG Simplifier pass may delete one of the basic blocks we are
225   // interested in.  If it does we need to take the block out of the list.  Make
226   // a "persistent mapping" by turning basic blocks into <function, name> pairs.
227   // This won't work well if blocks are unnamed, but that is just the risk we
228   // have to take.
229   std::vector<std::pair<Function*, std::string> > BlockInfo;
230
231   for (std::set<BasicBlock*>::iterator I = Blocks.begin(), E = Blocks.end();
232        I != E; ++I)
233     BlockInfo.push_back(std::make_pair((*I)->getParent(), (*I)->getName()));
234
235   // Now run the CFG simplify pass on the function...
236   PassManager Passes;
237   Passes.add(createCFGSimplificationPass());
238   Passes.add(createVerifierPass());
239   Passes.run(*M);
240
241   // Try running on the hacked up program...
242   if (TestFn(BD, M)) {
243     BD.setNewProgram(M);      // It crashed, keep the trimmed version...
244
245     // Make sure to use basic block pointers that point into the now-current
246     // module, and that they don't include any deleted blocks.
247     BBs.clear();
248     for (unsigned i = 0, e = BlockInfo.size(); i != e; ++i) {
249       SymbolTable &ST = BlockInfo[i].first->getSymbolTable();
250       SymbolTable::plane_iterator PI = ST.find(Type::LabelTy);
251       if (PI != ST.plane_end() && PI->second.count(BlockInfo[i].second))
252         BBs.push_back(cast<BasicBlock>(PI->second[BlockInfo[i].second]));
253     }
254     return true;
255   }
256   delete M;  // It didn't crash, try something else.
257   return false;
258 }
259
260 /// DebugACrash - Given a predicate that determines whether a component crashes
261 /// on a program, try to destructively reduce the program while still keeping
262 /// the predicate true.
263 static bool DebugACrash(BugDriver &BD,  bool (*TestFn)(BugDriver &, Module *)) {
264   bool AnyReduction = false;
265
266   // See if we can get away with nuking all of the global variable initializers
267   // in the program...
268   if (BD.getProgram()->global_begin() != BD.getProgram()->global_end()) {
269     Module *M = CloneModule(BD.getProgram());
270     bool DeletedInit = false;
271     for (Module::global_iterator I = M->global_begin(), E = M->global_end(); I != E; ++I)
272       if (I->hasInitializer()) {
273         I->setInitializer(0);
274         I->setLinkage(GlobalValue::ExternalLinkage);
275         DeletedInit = true;
276       }
277     
278     if (!DeletedInit) {
279       delete M;  // No change made...
280     } else {
281       // See if the program still causes a crash...
282       std::cout << "\nChecking to see if we can delete global inits: ";
283       if (TestFn(BD, M)) {  // Still crashes?
284         BD.setNewProgram(M);
285         AnyReduction = true;
286         std::cout << "\n*** Able to remove all global initializers!\n";
287       } else {                       // No longer crashes?
288         std::cout << "  - Removing all global inits hides problem!\n";
289         delete M;
290       }
291     }
292   }
293   
294   // Now try to reduce the number of functions in the module to something small.
295   std::vector<Function*> Functions;
296   for (Module::iterator I = BD.getProgram()->begin(),
297          E = BD.getProgram()->end(); I != E; ++I)
298     if (!I->isExternal())
299       Functions.push_back(I);
300
301   if (Functions.size() > 1) {
302     std::cout << "\n*** Attempting to reduce the number of functions "
303       "in the testcase\n";
304
305     unsigned OldSize = Functions.size();
306     ReduceCrashingFunctions(BD, TestFn).reduceList(Functions);
307
308     if (Functions.size() < OldSize) {
309       BD.EmitProgressBytecode("reduced-function");
310       AnyReduction = true;
311     }
312   }
313
314   // Attempt to delete entire basic blocks at a time to speed up
315   // convergence... this actually works by setting the terminator of the blocks
316   // to a return instruction then running simplifycfg, which can potentially
317   // shrinks the code dramatically quickly
318   //
319   if (!DisableSimplifyCFG) {
320     std::vector<const BasicBlock*> Blocks;
321     for (Module::const_iterator I = BD.getProgram()->begin(),
322            E = BD.getProgram()->end(); I != E; ++I)
323       for (Function::const_iterator FI = I->begin(), E = I->end(); FI !=E; ++FI)
324         Blocks.push_back(FI);
325     ReduceCrashingBlocks(BD, TestFn).reduceList(Blocks);
326   }
327
328   // FIXME: This should use the list reducer to converge faster by deleting
329   // larger chunks of instructions at a time!
330   unsigned Simplification = 2;
331   do {
332     --Simplification;
333     std::cout << "\n*** Attempting to reduce testcase by deleting instruc"
334               << "tions: Simplification Level #" << Simplification << '\n';
335
336     // Now that we have deleted the functions that are unnecessary for the
337     // program, try to remove instructions that are not necessary to cause the
338     // crash.  To do this, we loop through all of the instructions in the
339     // remaining functions, deleting them (replacing any values produced with
340     // nulls), and then running ADCE and SimplifyCFG.  If the transformed input
341     // still triggers failure, keep deleting until we cannot trigger failure
342     // anymore.
343     //
344     unsigned InstructionsToSkipBeforeDeleting = 0;
345   TryAgain:
346     
347     // Loop over all of the (non-terminator) instructions remaining in the
348     // function, attempting to delete them.
349     unsigned CurInstructionNum = 0;
350     for (Module::const_iterator FI = BD.getProgram()->begin(),
351            E = BD.getProgram()->end(); FI != E; ++FI)
352       if (!FI->isExternal())
353         for (Function::const_iterator BI = FI->begin(), E = FI->end(); BI != E;
354              ++BI)
355           for (BasicBlock::const_iterator I = BI->begin(), E = --BI->end();
356                I != E; ++I, ++CurInstructionNum)
357             if (InstructionsToSkipBeforeDeleting) {
358               --InstructionsToSkipBeforeDeleting;
359             } else {
360               std::cout << "Checking instruction '" << I->getName() << "': ";
361               Module *M = BD.deleteInstructionFromProgram(I, Simplification);
362               
363               // Find out if the pass still crashes on this pass...
364               if (TestFn(BD, M)) {
365                 // Yup, it does, we delete the old module, and continue trying
366                 // to reduce the testcase...
367                 BD.setNewProgram(M);
368                 AnyReduction = true;
369                 InstructionsToSkipBeforeDeleting = CurInstructionNum;
370                 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 M;
376             }
377
378     if (InstructionsToSkipBeforeDeleting) {
379       InstructionsToSkipBeforeDeleting = 0;
380       goto TryAgain;
381     }
382       
383   } while (Simplification);
384
385   // Try to clean up the testcase by running funcresolve and globaldce...
386   std::cout << "\n*** Attempting to perform final cleanups: ";
387   Module *M = CloneModule(BD.getProgram());
388   M = BD.performFinalCleanups(M, true);
389             
390   // Find out if the pass still crashes on the cleaned up program...
391   if (TestFn(BD, M)) {
392     BD.setNewProgram(M);     // Yup, it does, keep the reduced version...
393     AnyReduction = true;
394   } else {
395     delete M;
396   }
397
398   if (AnyReduction)
399     BD.EmitProgressBytecode("reduced-simplified");
400
401   return false;  
402 }
403
404 static bool TestForOptimizerCrash(BugDriver &BD, Module *M) {
405   return BD.runPasses(M);
406 }
407
408 /// debugOptimizerCrash - This method is called when some pass crashes on input.
409 /// It attempts to prune down the testcase to something reasonable, and figure
410 /// out exactly which pass is crashing.
411 ///
412 bool BugDriver::debugOptimizerCrash() {
413   std::cout << "\n*** Debugging optimizer crash!\n";
414
415   // Reduce the list of passes which causes the optimizer to crash...
416   unsigned OldSize = PassesToRun.size();
417   ReducePassList(*this).reduceList(PassesToRun);
418
419   std::cout << "\n*** Found crashing pass"
420             << (PassesToRun.size() == 1 ? ": " : "es: ")
421             << getPassesString(PassesToRun) << '\n';
422
423   EmitProgressBytecode("passinput");
424
425   return DebugACrash(*this, TestForOptimizerCrash);
426 }
427
428 static bool TestForCodeGenCrash(BugDriver &BD, Module *M) {
429   try {
430     std::cerr << '\n';
431     BD.compileProgram(M);
432     std::cerr << '\n';
433     return false;
434   } catch (ToolExecutionError &) {
435     std::cerr << "<crash>\n";
436     return true;  // Tool is still crashing.
437   }
438 }
439
440 /// debugCodeGeneratorCrash - This method is called when the code generator
441 /// crashes on an input.  It attempts to reduce the input as much as possible
442 /// while still causing the code generator to crash.
443 bool BugDriver::debugCodeGeneratorCrash() {
444   std::cerr << "*** Debugging code generator crash!\n";
445
446   return DebugACrash(*this, TestForCodeGenCrash);
447 }