Rename Kept -> Suffix
[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 DebugCrashes;
21 class ReduceMiscompilingPasses;
22 class ReduceMiscompilingFunctions;
23 class ReduceCrashingFunctions;
24 class ReduceCrashingBlocks;
25
26 class BugDriver {
27   const std::string ToolName;  // Name of bugpoint
28   Module *Program;             // The raw program, linked together
29   std::vector<const PassInfo*> PassesToRun;
30   AbstractInterpreter *Interpreter;   // How to run the program
31
32   // FIXME: sort out public/private distinctions...
33   friend class DebugCrashes;
34   friend class ReduceMiscompilingPasses;
35   friend class ReduceMiscompilingFunctions;
36   friend class ReduceCrashingFunctions;
37   friend class ReduceCrashingBlocks;
38 public:
39   BugDriver(const char *toolname)
40     : ToolName(toolname), Program(0), Interpreter(0) {}
41
42   const std::string &getToolName() const { return ToolName; }
43
44   // Set up methods... these methods are used to copy information about the
45   // command line arguments into instance variables of BugDriver.
46   //
47   bool addSources(const std::vector<std::string> &FileNames);
48   template<class It>
49   void addPasses(It I, It E) { PassesToRun.insert(PassesToRun.end(), I, E); }
50
51   /// run - The top level method that is invoked after all of the instance
52   /// variables are set up from command line arguments.
53   ///
54   bool run();
55
56   /// debugCrash - This method is called when some pass crashes on input.  It
57   /// attempts to prune down the testcase to something reasonable, and figure
58   /// out exactly which pass is crashing.
59   ///
60   bool debugCrash();
61
62   /// debugMiscompilation - This method is used when the passes selected are not
63   /// crashing, but the generated output is semantically different from the
64   /// input.
65   bool debugMiscompilation();
66
67   /// debugPassMiscompilation - This method is called when the specified pass
68   /// miscompiles Program as input.  It tries to reduce the testcase to
69   /// something that smaller that still miscompiles the program.
70   /// ReferenceOutput contains the filename of the file containing the output we
71   /// are to match.
72   ///
73   bool debugPassMiscompilation(const PassInfo *ThePass,
74                                const std::string &ReferenceOutput);
75
76 private:
77   /// ParseInputFile - Given a bytecode or assembly input filename, parse and
78   /// return it, or return null if not possible.
79   ///
80   Module *ParseInputFile(const std::string &InputFilename) const;
81
82   /// writeProgramToFile - This writes the current "Program" to the named
83   /// bytecode file.  If an error occurs, true is returned.
84   ///
85   bool writeProgramToFile(const std::string &Filename, Module *M = 0) const;
86
87
88   /// EmitProgressBytecode - This function is used to output the current Program
89   /// to a file named "bugpoing-ID.bc".
90   ///
91   void EmitProgressBytecode(const std::string &ID, bool NoFlyer = false);
92   
93   /// runPasses - Run the specified passes on Program, outputting a bytecode
94   /// file and writting the filename into OutputFile if successful.  If the
95   /// optimizations fail for some reason (optimizer crashes), return true,
96   /// otherwise return false.  If DeleteOutput is set to true, the bytecode is
97   /// deleted on success, and the filename string is undefined.  This prints to
98   /// cout a single line message indicating whether compilation was successful
99   /// or failed, unless Quiet is set.
100   ///
101   bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
102                  std::string &OutputFilename, bool DeleteOutput = false,
103                  bool Quiet = false) const;
104
105   /// runPasses - Just like the method above, but this just returns true or
106   /// false indicating whether or not the optimizer crashed on the specified
107   /// input (true = crashed).
108   ///
109   bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
110                  bool DeleteOutput = true) const {
111     std::string Filename;
112     return runPasses(PassesToRun, Filename, DeleteOutput);
113   }
114
115   /// deleteInstructionFromProgram - This method clones the current Program and
116   /// deletes the specified instruction from the cloned module.  It then runs a
117   /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code
118   /// which depends on the value.  The modified module is then returned.
119   ///
120   Module *deleteInstructionFromProgram(Instruction *I, unsigned Simp) const;
121
122   /// performFinalCleanups - This method clones the current Program and performs
123   /// a series of cleanups intended to get rid of extra cruft on the module
124   /// before handing it to the user...
125   ///
126   Module *performFinalCleanups() 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                    bool RemoveBytecode = false);
147 };
148
149 /// getPassesString - Turn a list of passes into a string which indicates the
150 /// command line options that must be passed to add the passes.
151 ///
152 std::string getPassesString(const std::vector<const PassInfo*> &Passes);
153
154 // DeleteFunctionBody - "Remove" the function by deleting all of it's basic
155 // blocks, making it external.
156 //
157 void DeleteFunctionBody(Function *F);
158
159 #endif