Make bugpoint *much* more powerful, giving it the capability to delete instructions
[oota-llvm.git] / tools / bugpoint / BugDriver.h
1 //===- BugDriver.h - Top-Level BugPoint class -------------------*- C++ -*-===//
2 //
3 // This class contains all of the shared state and information that is used by
4 // the BugPoint tool to track down errors in optimizations.  This class is the
5 // main driver class that invokes all sub-functionality.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef BUGDRIVER_H
10 #define BUGDRIVER_H
11
12 #include <vector>
13 #include <string>
14 class PassInfo;
15 class Module;
16 class Function;
17 class AbstractInterpreter;
18 class Instruction;
19
20 class BugDriver {
21   const std::string ToolName;  // Name of bugpoint
22   Module *Program;             // The raw program, linked together
23   std::vector<const PassInfo*> PassesToRun;
24   AbstractInterpreter *Interpreter;   // How to run the program
25 public:
26   BugDriver(const char *toolname)
27     : ToolName(toolname), Program(0), Interpreter(0) {}
28
29   const std::string &getToolName() const { return ToolName; }
30
31   // Set up methods... these methods are used to copy information about the
32   // command line arguments into instance variables of BugDriver.
33   //
34   bool addSources(const std::vector<std::string> &FileNames);
35   template<class It>
36   void addPasses(It I, It E) { PassesToRun.insert(PassesToRun.end(), I, E); }
37
38   /// run - The top level method that is invoked after all of the instance
39   /// variables are set up from command line arguments.
40   ///
41   bool run();
42
43   /// debugCrash - This method is called when some pass crashes on input.  It
44   /// attempts to prune down the testcase to something reasonable, and figure
45   /// out exactly which pass is crashing.
46   ///
47   bool debugCrash();
48
49   /// debugPassCrash - This method is called when the specified pass crashes on
50   /// Program as input.  It tries to reduce the testcase to something that still
51   /// crashes, but it smaller.
52   ///
53   bool debugPassCrash(const PassInfo *PI);
54
55   /// debugMiscompilation - This method is used when the passes selected are not
56   /// crashing, but the generated output is semantically different from the
57   /// input.
58   bool debugMiscompilation();
59
60   /// debugPassMiscompilation - This method is called when the specified pass
61   /// miscompiles Program as input.  It tries to reduce the testcase to
62   /// something that smaller that still miscompiles the program.
63   /// ReferenceOutput contains the filename of the file containing the output we
64   /// are to match.
65   ///
66   bool debugPassMiscompilation(const PassInfo *ThePass,
67                                const std::string &ReferenceOutput);
68
69 private:
70   /// ParseInputFile - Given a bytecode or assembly input filename, parse and
71   /// return it, or return null if not possible.
72   ///
73   Module *ParseInputFile(const std::string &InputFilename) const;
74
75   /// writeProgramToFile - This writes the current "Program" to the named
76   /// bytecode file.  If an error occurs, true is returned.
77   ///
78   bool writeProgramToFile(const std::string &Filename, Module *M = 0) const;
79
80
81   /// EmitProgressBytecode - This function is used to output the current Program
82   /// to a file named "bugpoing-ID.bc".
83   ///
84   void EmitProgressBytecode(const PassInfo *Pass, const std::string &ID);
85   
86   /// runPasses - Run the specified passes on Program, outputting a bytecode
87   /// file and writting the filename into OutputFile if successful.  If the
88   /// optimizations fail for some reason (optimizer crashes), return true,
89   /// otherwise return false.  If DeleteOutput is set to true, the bytecode is
90   /// deleted on success, and the filename string is undefined.  This prints to
91   /// cout a single line message indicating whether compilation was successful
92   /// or failed, unless Quiet is set.
93   ///
94   bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
95                  std::string &OutputFilename, bool DeleteOutput = false,
96                  bool Quiet = false) const;
97
98   /// runPasses - Just like the method above, but this just returns true or
99   /// false indicating whether or not the optimizer crashed on the specified
100   /// input (true = crashed).
101   ///
102   bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
103                  bool DeleteOutput = true) const {
104     std::string Filename;
105     return runPasses(PassesToRun, Filename, DeleteOutput);
106   }
107
108   /// runPass - Run only the specified pass on the program.
109   ///
110   bool runPass(const PassInfo *P, bool DeleteOutput = true) const {
111     return runPasses(std::vector<const PassInfo*>(1, P), DeleteOutput);
112   }
113   
114   /// extractFunctionFromModule - This method is used to extract the specified
115   /// (non-external) function from the current program, slim down the module,
116   /// and then return it.  This does not modify Program at all, it modifies a
117   /// copy, which it returns.
118   ///
119   Module *extractFunctionFromModule(Function *F) const;
120
121   /// deleteInstructionFromProgram - This method clones the current Program and
122   /// deletes the specified instruction from the cloned module.  It then runs a
123   /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code
124   /// which depends on the value.  The modified module is then returned.
125   ///
126   Module *deleteInstructionFromProgram(Instruction *I, unsigned Simp) const;
127
128   /// initializeExecutionEnvironment - This method is used to set up the
129   /// environment for executing LLVM programs.
130   ///
131   bool initializeExecutionEnvironment();
132
133   /// executeProgram - This method runs "Program", capturing the output of the
134   /// program to a file, returning the filename of the file.  A recommended
135   /// filename may be optionally specified.
136   ///
137   std::string executeProgram(std::string RequestedOutputFilename = "",
138                              std::string Bytecode = "");
139
140   /// diffProgram - This method executes the specified module and diffs the
141   /// output against the file specified by ReferenceOutputFile.  If the output
142   /// is different, true is returned.
143   ///
144   bool diffProgram(const std::string &ReferenceOutputFile,
145                    const std::string &BytecodeFile = "");
146 };
147
148 #endif