Simplify the performFinalCleanups interface
[oota-llvm.git] / tools / bugpoint / BugDriver.h
1 //===- BugDriver.h - Top-Level BugPoint class -------------------*- C++ -*-===//
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 class contains all of the shared state and information that is used by
11 // the BugPoint tool to track down errors in optimizations.  This class is the
12 // main driver class that invokes all sub-functionality.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef BUGDRIVER_H
17 #define BUGDRIVER_H
18
19 #include <vector>
20 #include <string>
21
22 class PassInfo;
23 class Module;
24 class Function;
25 class AbstractInterpreter;
26 class Instruction;
27
28 class DebugCrashes;
29 class ReduceMiscompilingPasses;
30 class ReduceMiscompilingFunctions;
31 class ReduceCrashingFunctions;
32 class ReduceCrashingBlocks;
33
34 class CBE;
35 class GCC;
36
37 extern bool DisableSimplifyCFG;
38
39 class BugDriver {
40   const std::string ToolName;  // Name of bugpoint
41   std::string ReferenceOutputFile; // Name of `good' output file
42   Module *Program;             // The raw program, linked together
43   std::vector<const PassInfo*> PassesToRun;
44   AbstractInterpreter *Interpreter;   // How to run the program
45   CBE *cbe;
46   GCC *gcc;
47
48   // FIXME: sort out public/private distinctions...
49   friend class DebugCrashes;
50   friend class ReduceMiscompilingPasses;
51   friend class ReduceMiscompilingFunctions;
52   friend class ReduceMisCodegenFunctions;
53   friend class ReduceCrashingFunctions;
54   friend class ReduceCrashingBlocks;
55
56 public:
57   BugDriver(const char *toolname);
58
59   const std::string &getToolName() const { return ToolName; }
60
61   // Set up methods... these methods are used to copy information about the
62   // command line arguments into instance variables of BugDriver.
63   //
64   bool addSources(const std::vector<std::string> &FileNames);
65   template<class It>
66   void addPasses(It I, It E) { PassesToRun.insert(PassesToRun.end(), I, E); }
67   void setPassesToRun(const std::vector<const PassInfo*> &PTR) {
68     PassesToRun = PTR;
69   }
70
71   /// run - The top level method that is invoked after all of the instance
72   /// variables are set up from command line arguments.
73   ///
74   bool run();
75
76   /// debugCrash - This method is called when some pass crashes on input.  It
77   /// attempts to prune down the testcase to something reasonable, and figure
78   /// out exactly which pass is crashing.
79   ///
80   bool debugCrash();
81
82   /// debugMiscompilation - This method is used when the passes selected are not
83   /// crashing, but the generated output is semantically different from the
84   /// input.
85   bool debugMiscompilation();
86
87   /// debugPassMiscompilation - This method is called when the specified pass
88   /// miscompiles Program as input.  It tries to reduce the testcase to
89   /// something that smaller that still miscompiles the program.
90   /// ReferenceOutput contains the filename of the file containing the output we
91   /// are to match.
92   ///
93   bool debugPassMiscompilation(const PassInfo *ThePass,
94                                const std::string &ReferenceOutput);
95
96   /// compileSharedObject - This method creates a SharedObject from a given
97   /// BytecodeFile for debugging a code generator.
98   ///
99   std::string compileSharedObject(const std::string &BytecodeFile);
100
101   /// debugCodeGenerator - This method narrows down a module to a function or
102   /// set of functions, using the CBE as a ``safe'' code generator for other
103   /// functions that are not under consideration.
104   bool debugCodeGenerator();
105
106   /// isExecutingJIT - Returns true if bugpoint is currently testing the JIT
107   ///
108   bool isExecutingJIT();
109
110 private:
111   /// ParseInputFile - Given a bytecode or assembly input filename, parse and
112   /// return it, or return null if not possible.
113   ///
114   Module *ParseInputFile(const std::string &InputFilename) const;
115
116   /// writeProgramToFile - This writes the current "Program" to the named
117   /// bytecode file.  If an error occurs, true is returned.
118   ///
119   bool writeProgramToFile(const std::string &Filename, Module *M = 0) const;
120
121
122   /// EmitProgressBytecode - This function is used to output the current Program
123   /// to a file named "bugpoing-ID.bc".
124   ///
125   void EmitProgressBytecode(const std::string &ID, bool NoFlyer = false);
126   
127   /// runPasses - Run the specified passes on Program, outputting a bytecode
128   /// file and writting the filename into OutputFile if successful.  If the
129   /// optimizations fail for some reason (optimizer crashes), return true,
130   /// otherwise return false.  If DeleteOutput is set to true, the bytecode is
131   /// deleted on success, and the filename string is undefined.  This prints to
132   /// cout a single line message indicating whether compilation was successful
133   /// or failed, unless Quiet is set.
134   ///
135   bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
136                  std::string &OutputFilename, bool DeleteOutput = false,
137                  bool Quiet = false) const;
138
139   /// runPasses - Just like the method above, but this just returns true or
140   /// false indicating whether or not the optimizer crashed on the specified
141   /// input (true = crashed).
142   ///
143   bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
144                  bool DeleteOutput = true) const {
145     std::string Filename;
146     return runPasses(PassesToRun, Filename, DeleteOutput);
147   }
148
149   /// PrintFunctionList - prints out list of problematic functions
150   ///
151   static void PrintFunctionList(const std::vector<Function*> &Funcs);
152
153   /// deleteInstructionFromProgram - This method clones the current Program and
154   /// deletes the specified instruction from the cloned module.  It then runs a
155   /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code
156   /// which depends on the value.  The modified module is then returned.
157   ///
158   Module *deleteInstructionFromProgram(Instruction *I, unsigned Simp) const;
159
160   /// performFinalCleanups - This method clones the current Program and performs
161   /// a series of cleanups intended to get rid of extra cruft on the module.  If
162   /// the MayModifySemantics argument is true, then the cleanups is allowed to
163   /// modify how the code behaves.
164   ///
165   void performFinalCleanups(Module *M, bool MayModifySemantics = false) const;
166
167   /// initializeExecutionEnvironment - This method is used to set up the
168   /// environment for executing LLVM programs.
169   ///
170   bool initializeExecutionEnvironment();
171
172   /// executeProgram - This method runs "Program", capturing the output of the
173   /// program to a file, returning the filename of the file.  A recommended
174   /// filename may be optionally specified.
175   ///
176   std::string executeProgram(std::string RequestedOutputFilename = "",
177                              std::string Bytecode = "",
178                              const std::string &SharedObjects = "",
179                              AbstractInterpreter *AI = 0);
180
181   /// executeProgramWithCBE - Used to create reference output with the C
182   /// backend, if reference output is not provided.
183   ///
184   std::string executeProgramWithCBE(std::string OutputFile = "",
185                                     std::string BytecodeFile = "",
186                                     const std::string &SharedObj = "") {
187     return executeProgram(OutputFile, BytecodeFile, SharedObj,
188                           (AbstractInterpreter*)cbe);
189   }
190
191   /// diffProgram - This method executes the specified module and diffs the
192   /// output against the file specified by ReferenceOutputFile.  If the output
193   /// is different, true is returned.
194   ///
195   bool diffProgram(const std::string &BytecodeFile = "",
196                    const std::string &SharedObj = "",
197                    bool RemoveBytecode = false);
198 };
199
200 /// getPassesString - Turn a list of passes into a string which indicates the
201 /// command line options that must be passed to add the passes.
202 ///
203 std::string getPassesString(const std::vector<const PassInfo*> &Passes);
204
205 // DeleteFunctionBody - "Remove" the function by deleting all of it's basic
206 // blocks, making it external.
207 //
208 void DeleteFunctionBody(Function *F);
209
210 #endif