When the user hits ctrl-c, bugpoint should attempt to stop reduction as
[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 namespace llvm {
23
24 class PassInfo;
25 class Module;
26 class Function;
27 class BasicBlock;
28 class AbstractInterpreter;
29 class Instruction;
30
31 class DebugCrashes;
32
33 class CBE;
34 class GCC;
35
36 extern bool DisableSimplifyCFG;
37
38 /// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
39 ///
40 extern bool BugpointIsInterrupted;
41
42 class BugDriver {
43   const std::string ToolName;  // Name of bugpoint
44   std::string ReferenceOutputFile; // Name of `good' output file
45   Module *Program;             // The raw program, linked together
46   std::vector<const PassInfo*> PassesToRun;
47   AbstractInterpreter *Interpreter;   // How to run the program
48   CBE *cbe;
49   GCC *gcc;
50
51   // FIXME: sort out public/private distinctions...
52   friend class ReducePassList;
53   friend class ReduceMisCodegenFunctions;
54
55 public:
56   BugDriver(const char *toolname);
57
58   const std::string &getToolName() const { return ToolName; }
59
60   // Set up methods... these methods are used to copy information about the
61   // command line arguments into instance variables of BugDriver.
62   //
63   bool addSources(const std::vector<std::string> &FileNames);
64   template<class It>
65   void addPasses(It I, It E) { PassesToRun.insert(PassesToRun.end(), I, E); }
66   void setPassesToRun(const std::vector<const PassInfo*> &PTR) {
67     PassesToRun = PTR;
68   }
69   const std::vector<const PassInfo*> &getPassesToRun() const {
70     return PassesToRun;
71   }
72
73   /// run - The top level method that is invoked after all of the instance
74   /// variables are set up from command line arguments.
75   ///
76   bool run();
77
78   /// debugOptimizerCrash - This method is called when some optimizer pass
79   /// crashes on input.  It attempts to prune down the testcase to something
80   /// reasonable, and figure out exactly which pass is crashing.
81   ///
82   bool debugOptimizerCrash();
83
84   /// debugCodeGeneratorCrash - This method is called when the code generator
85   /// crashes on an input.  It attempts to reduce the input as much as possible
86   /// while still causing the code generator to crash.
87   bool debugCodeGeneratorCrash();
88
89   /// debugMiscompilation - This method is used when the passes selected are not
90   /// crashing, but the generated output is semantically different from the
91   /// input.
92   bool debugMiscompilation();
93
94   /// debugPassMiscompilation - This method is called when the specified pass
95   /// miscompiles Program as input.  It tries to reduce the testcase to
96   /// something that smaller that still miscompiles the program.
97   /// ReferenceOutput contains the filename of the file containing the output we
98   /// are to match.
99   ///
100   bool debugPassMiscompilation(const PassInfo *ThePass,
101                                const std::string &ReferenceOutput);
102
103   /// compileSharedObject - This method creates a SharedObject from a given
104   /// BytecodeFile for debugging a code generator.
105   ///
106   std::string compileSharedObject(const std::string &BytecodeFile);
107
108   /// debugCodeGenerator - This method narrows down a module to a function or
109   /// set of functions, using the CBE as a ``safe'' code generator for other
110   /// functions that are not under consideration.
111   bool debugCodeGenerator();
112
113   /// isExecutingJIT - Returns true if bugpoint is currently testing the JIT
114   ///
115   bool isExecutingJIT();
116
117   /// runPasses - Run all of the passes in the "PassesToRun" list, discard the
118   /// output, and return true if any of the passes crashed.
119   bool runPasses(Module *M = 0) {
120     if (M == 0) M = Program;
121     std::swap(M, Program);
122     bool Result = runPasses(PassesToRun);
123     std::swap(M, Program);
124     return Result;
125   }
126
127   Module *getProgram() const { return Program; }
128
129   /// swapProgramIn - Set the current module to the specified module, returning
130   /// the old one.
131   Module *swapProgramIn(Module *M) {
132     Module *OldProgram = Program;
133     Program = M;
134     return OldProgram;
135   }
136
137   AbstractInterpreter *switchToCBE() {
138     AbstractInterpreter *Old = Interpreter;
139     Interpreter = (AbstractInterpreter*)cbe;
140     return Old;
141   }
142
143   void switchToInterpreter(AbstractInterpreter *AI) {
144     Interpreter = AI;
145   }
146
147   /// setNewProgram - If we reduce or update the program somehow, call this
148   /// method to update bugdriver with it.  This deletes the old module and sets
149   /// the specified one as the current program.
150   void setNewProgram(Module *M);
151
152   /// compileProgram - Try to compile the specified module, throwing an
153   /// exception if an error occurs, or returning normally if not.  This is used
154   /// for code generation crash testing.
155   ///
156   void compileProgram(Module *M);
157
158   /// executeProgram - This method runs "Program", capturing the output of the
159   /// program to a file, returning the filename of the file.  A recommended
160   /// filename may be optionally specified.  If there is a problem with the code
161   /// generator (e.g., llc crashes), this will throw an exception.
162   ///
163   std::string executeProgram(std::string RequestedOutputFilename = "",
164                              std::string Bytecode = "",
165                              const std::string &SharedObjects = "",
166                              AbstractInterpreter *AI = 0,
167                              bool *ProgramExitedNonzero = 0);
168
169   /// executeProgramWithCBE - Used to create reference output with the C
170   /// backend, if reference output is not provided.  If there is a problem with
171   /// the code generator (e.g., llc crashes), this will throw an exception.
172   ///
173   std::string executeProgramWithCBE(std::string OutputFile = "");
174
175   /// diffProgram - This method executes the specified module and diffs the
176   /// output against the file specified by ReferenceOutputFile.  If the output
177   /// is different, true is returned.  If there is a problem with the code
178   /// generator (e.g., llc crashes), this will throw an exception.
179   ///
180   bool diffProgram(const std::string &BytecodeFile = "",
181                    const std::string &SharedObj = "",
182                    bool RemoveBytecode = false);
183   /// EmitProgressBytecode - This function is used to output the current Program
184   /// to a file named "bugpoint-ID.bc".
185   ///
186   void EmitProgressBytecode(const std::string &ID, bool NoFlyer = false);
187
188   /// deleteInstructionFromProgram - This method clones the current Program and
189   /// deletes the specified instruction from the cloned module.  It then runs a
190   /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code
191   /// which depends on the value.  The modified module is then returned.
192   ///
193   Module *deleteInstructionFromProgram(const Instruction *I, unsigned Simp)
194     const;
195
196   /// performFinalCleanups - This method clones the current Program and performs
197   /// a series of cleanups intended to get rid of extra cruft on the module.  If
198   /// the MayModifySemantics argument is true, then the cleanups is allowed to
199   /// modify how the code behaves.
200   ///
201   Module *performFinalCleanups(Module *M, bool MayModifySemantics = false);
202
203   /// ExtractLoop - Given a module, extract up to one loop from it into a new
204   /// function.  This returns null if there are no extractable loops in the
205   /// program or if the loop extractor crashes.
206   Module *ExtractLoop(Module *M);
207
208   /// ExtractMappedBlocksFromModule - Extract all but the specified basic blocks
209   /// into their own functions.  The only detail is that M is actually a module
210   /// cloned from the one the BBs are in, so some mapping needs to be performed.
211   /// If this operation fails for some reason (ie the implementation is buggy),
212   /// this function should return null, otherwise it returns a new Module.
213   Module *ExtractMappedBlocksFromModule(const std::vector<BasicBlock*> &BBs,
214                                         Module *M);
215
216   /// runPassesOn - Carefully run the specified set of pass on the specified
217   /// module, returning the transformed module on success, or a null pointer on
218   /// failure.  If AutoDebugCrashes is set to true, then bugpoint will
219   /// automatically attempt to track down a crashing pass if one exists, and
220   /// this method will never return null.
221   Module *runPassesOn(Module *M, const std::vector<const PassInfo*> &Passes,
222                       bool AutoDebugCrashes = false);
223
224   /// runPasses - Run the specified passes on Program, outputting a bytecode
225   /// file and writting the filename into OutputFile if successful.  If the
226   /// optimizations fail for some reason (optimizer crashes), return true,
227   /// otherwise return false.  If DeleteOutput is set to true, the bytecode is
228   /// deleted on success, and the filename string is undefined.  This prints to
229   /// cout a single line message indicating whether compilation was successful
230   /// or failed, unless Quiet is set.
231   ///
232   bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
233                  std::string &OutputFilename, bool DeleteOutput = false,
234                  bool Quiet = false) const;
235
236   /// writeProgramToFile - This writes the current "Program" to the named
237   /// bytecode file.  If an error occurs, true is returned.
238   ///
239   bool writeProgramToFile(const std::string &Filename, Module *M = 0) const;
240
241 private:
242   /// runPasses - Just like the method above, but this just returns true or
243   /// false indicating whether or not the optimizer crashed on the specified
244   /// input (true = crashed).
245   ///
246   bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
247                  bool DeleteOutput = true) const {
248     std::string Filename;
249     return runPasses(PassesToRun, Filename, DeleteOutput);
250   }
251
252   /// initializeExecutionEnvironment - This method is used to set up the
253   /// environment for executing LLVM programs.
254   ///
255   bool initializeExecutionEnvironment();
256 };
257
258 /// ParseInputFile - Given a bytecode or assembly input filename, parse and
259 /// return it, or return null if not possible.
260 ///
261 Module *ParseInputFile(const std::string &InputFilename);
262
263
264 /// getPassesString - Turn a list of passes into a string which indicates the
265 /// command line options that must be passed to add the passes.
266 ///
267 std::string getPassesString(const std::vector<const PassInfo*> &Passes);
268
269 /// PrintFunctionList - prints out list of problematic functions
270 ///
271 void PrintFunctionList(const std::vector<Function*> &Funcs);
272
273 // DeleteFunctionBody - "Remove" the function by deleting all of it's basic
274 // blocks, making it external.
275 //
276 void DeleteFunctionBody(Function *F);
277
278 /// SplitFunctionsOutOfModule - Given a module and a list of functions in the
279 /// module, split the functions OUT of the specified module, and place them in
280 /// the new module.
281 Module *SplitFunctionsOutOfModule(Module *M, const std::vector<Function*> &F);
282
283 } // End llvm namespace
284
285 #endif