4d6754aa7cd997fc6b10170ac1304ceb59a95113
[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 AbstractInterpreter;
28 class Instruction;
29
30 class DebugCrashes;
31 class ReduceMiscompilingPasses;
32 class ReduceMiscompilingFunctions;
33 class ReduceCrashingFunctions;
34 class ReduceCrashingBlocks;
35
36 class CBE;
37 class GCC;
38
39 extern bool DisableSimplifyCFG;
40
41 class BugDriver {
42   const std::string ToolName;  // Name of bugpoint
43   std::string ReferenceOutputFile; // Name of `good' output file
44   Module *Program;             // The raw program, linked together
45   std::vector<const PassInfo*> PassesToRun;
46   AbstractInterpreter *Interpreter;   // How to run the program
47   CBE *cbe;
48   GCC *gcc;
49
50   // FIXME: sort out public/private distinctions...
51   friend class DebugCrashes;
52   friend class ReduceMiscompilingPasses;
53   friend class ReduceMiscompilingFunctions;
54   friend class ReduceMisCodegenFunctions;
55   friend class ReduceCrashingFunctions;
56   friend class ReduceCrashingBlocks;
57
58 public:
59   BugDriver(const char *toolname);
60
61   const std::string &getToolName() const { return ToolName; }
62
63   // Set up methods... these methods are used to copy information about the
64   // command line arguments into instance variables of BugDriver.
65   //
66   bool addSources(const std::vector<std::string> &FileNames);
67   template<class It>
68   void addPasses(It I, It E) { PassesToRun.insert(PassesToRun.end(), I, E); }
69   void setPassesToRun(const std::vector<const PassInfo*> &PTR) {
70     PassesToRun = PTR;
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 private:
118   /// ParseInputFile - Given a bytecode or assembly input filename, parse and
119   /// return it, or return null if not possible.
120   ///
121   Module *ParseInputFile(const std::string &InputFilename) const;
122
123   /// writeProgramToFile - This writes the current "Program" to the named
124   /// bytecode file.  If an error occurs, true is returned.
125   ///
126   bool writeProgramToFile(const std::string &Filename, Module *M = 0) const;
127
128
129   /// EmitProgressBytecode - This function is used to output the current Program
130   /// to a file named "bugpoint-ID.bc".
131   ///
132   void EmitProgressBytecode(const std::string &ID, bool NoFlyer = false);
133   
134   /// runPasses - Run the specified passes on Program, outputting a bytecode
135   /// file and writting the filename into OutputFile if successful.  If the
136   /// optimizations fail for some reason (optimizer crashes), return true,
137   /// otherwise return false.  If DeleteOutput is set to true, the bytecode is
138   /// deleted on success, and the filename string is undefined.  This prints to
139   /// cout a single line message indicating whether compilation was successful
140   /// or failed, unless Quiet is set.
141   ///
142   bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
143                  std::string &OutputFilename, bool DeleteOutput = false,
144                  bool Quiet = false) const;
145
146   /// runPasses - Just like the method above, but this just returns true or
147   /// false indicating whether or not the optimizer crashed on the specified
148   /// input (true = crashed).
149   ///
150   bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
151                  bool DeleteOutput = true) const {
152     std::string Filename;
153     return runPasses(PassesToRun, Filename, DeleteOutput);
154   }
155
156   /// PrintFunctionList - prints out list of problematic functions
157   ///
158   static void PrintFunctionList(const std::vector<Function*> &Funcs);
159
160   /// deleteInstructionFromProgram - This method clones the current Program and
161   /// deletes the specified instruction from the cloned module.  It then runs a
162   /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code
163   /// which depends on the value.  The modified module is then returned.
164   ///
165   Module *deleteInstructionFromProgram(Instruction *I, unsigned Simp) const;
166
167   /// performFinalCleanups - This method clones the current Program and performs
168   /// a series of cleanups intended to get rid of extra cruft on the module.  If
169   /// the MayModifySemantics argument is true, then the cleanups is allowed to
170   /// modify how the code behaves.
171   ///
172   Module *performFinalCleanups(Module *M, bool MayModifySemantics = false);
173
174   /// initializeExecutionEnvironment - This method is used to set up the
175   /// environment for executing LLVM programs.
176   ///
177   bool initializeExecutionEnvironment();
178
179   /// executeProgram - This method runs "Program", capturing the output of the
180   /// program to a file, returning the filename of the file.  A recommended
181   /// filename may be optionally specified.  If there is a problem with the code
182   /// generator (e.g., llc crashes), this will throw an exception.
183   ///
184   std::string executeProgram(std::string RequestedOutputFilename = "",
185                              std::string Bytecode = "",
186                              const std::string &SharedObjects = "",
187                              AbstractInterpreter *AI = 0,
188                              bool *ProgramExitedNonzero = 0);
189
190   /// executeProgramWithCBE - Used to create reference output with the C
191   /// backend, if reference output is not provided.  If there is a problem with
192   /// the code generator (e.g., llc crashes), this will throw an exception.
193   ///
194   std::string executeProgramWithCBE(std::string OutputFile = "");
195
196   /// diffProgram - This method executes the specified module and diffs the
197   /// output against the file specified by ReferenceOutputFile.  If the output
198   /// is different, true is returned.  If there is a problem with the code
199   /// generator (e.g., llc crashes), this will throw an exception.
200   ///
201   bool diffProgram(const std::string &BytecodeFile = "",
202                    const std::string &SharedObj = "",
203                    bool RemoveBytecode = false);
204 };
205
206 /// getPassesString - Turn a list of passes into a string which indicates the
207 /// command line options that must be passed to add the passes.
208 ///
209 std::string getPassesString(const std::vector<const PassInfo*> &Passes);
210
211 // DeleteFunctionBody - "Remove" the function by deleting all of it's basic
212 // blocks, making it external.
213 //
214 void DeleteFunctionBody(Function *F);
215
216 } // End llvm namespace
217
218 #endif