Initial checkin of bugpoint
[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
18 class BugDriver {
19   const std::string ToolName;  // Name of bugpoint
20   Module *Program;             // The raw program, linked together
21   std::vector<const PassInfo*> PassesToRun;
22 public:
23   BugDriver(const char *toolname) : ToolName(toolname), Program(0) {}
24
25   // Set up methods... these methods are used to copy information about the
26   // command line arguments into instance variables of BugDriver.
27   //
28   bool addSources(const std::vector<std::string> &FileNames);
29   template<class It>
30   void addPasses(It I, It E) { PassesToRun.insert(PassesToRun.end(), I, E); }
31
32   /// run - The top level method that is invoked after all of the instance
33   /// variables are set up from command line arguments.
34   ///
35   bool run();
36
37   /// debugCrash - This method is called when some pass crashes on input.  It
38   /// attempts to prune down the testcase to something reasonable, and figure
39   /// out exactly which pass is crashing.
40   ///
41   bool debugCrash();
42
43   /// debugPassCrash - This method is called when the specified pass crashes on
44   /// Program as input.  It tries to reduce the testcase to something that still
45   /// crashes, but it smaller.
46   ///
47   bool debugPassCrash(const PassInfo *PI);
48
49   /// debugMiscompilation - This method is used when the passes selected are not
50   /// crashing, but the generated output is semantically different from the
51   /// input.
52   bool debugMiscompilation();
53
54 private:
55   /// ParseInputFile - Given a bytecode or assembly input filename, parse and
56   /// return it, or return null if not possible.
57   ///
58   Module *ParseInputFile(const std::string &InputFilename) const;
59
60   /// removeFile - Delete the specified file
61   ///
62   void removeFile(const std::string &Filename) const;
63
64   /// writeProgramToFile - This writes the current "Program" to the named
65   /// bytecode file.  If an error occurs, true is returned.
66   ///
67   bool writeProgramToFile(const std::string &Filename) const;
68
69
70   /// EmitProgressBytecode - This function is used to output the current Program
71   /// to a file named "bugpoing-ID.bc".
72   ///
73   void EmitProgressBytecode(const PassInfo *Pass, const std::string &ID);
74   
75   /// runPasses - Run the specified passes on Program, outputting a bytecode
76   /// file and writting the filename into OutputFile if successful.  If the
77   /// optimizations fail for some reason (optimizer crashes), return true,
78   /// otherwise return false.  If DeleteOutput is set to true, the bytecode is
79   /// deleted on success, and the filename string is undefined.  This prints to
80   /// cout a single line message indicating whether compilation was successful
81   /// or failed.
82   ///
83   bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
84                  std::string &OutputFilename, bool DeleteOutput = false) const;
85
86   /// runPasses - Just like the method above, but this just returns true or
87   /// false indicating whether or not the optimizer crashed on the specified
88   /// input (true = crashed).
89   ///
90   bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
91                  bool DeleteOutput = true) const {
92     std::string Filename;
93     return runPasses(PassesToRun, Filename, DeleteOutput);
94   }
95
96   /// runPass - Run only the specified pass on the program.
97   bool runPass(const PassInfo *P, bool DeleteOutput = true) const {
98     return runPasses(std::vector<const PassInfo*>(1, P), DeleteOutput);
99   }
100   
101   /// extractFunctionFromModule - This method is used to extract the specified
102   /// (non-external) function from the current program, slim down the module,
103   /// and then return it.  This does not modify Program at all, it modifies a
104   /// copy, which it returns.
105   Module *extractFunctionFromModule(Function *F) const;
106
107 };
108
109 #endif