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