BugDriver.h revision 218e26ef3583cc3270f5f2a2b9cb1025e5b05ebe
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>
14class PassInfo;
15class Module;
16class Function;
17class AbstractInterpreter;
18
19class BugDriver {
20  const std::string ToolName;  // Name of bugpoint
21  Module *Program;             // The raw program, linked together
22  std::vector<const PassInfo*> PassesToRun;
23  AbstractInterpreter *Interpreter;   // How to run the program
24public:
25  BugDriver(const char *toolname)
26    : ToolName(toolname), Program(0), Interpreter(0) {}
27
28  const std::string &getToolName() const { return ToolName; }
29
30  // Set up methods... these methods are used to copy information about the
31  // command line arguments into instance variables of BugDriver.
32  //
33  bool addSources(const std::vector<std::string> &FileNames);
34  template<class It>
35  void addPasses(It I, It E) { PassesToRun.insert(PassesToRun.end(), I, E); }
36
37  /// run - The top level method that is invoked after all of the instance
38  /// variables are set up from command line arguments.
39  ///
40  bool run();
41
42  /// debugCrash - This method is called when some pass crashes on input.  It
43  /// attempts to prune down the testcase to something reasonable, and figure
44  /// out exactly which pass is crashing.
45  ///
46  bool debugCrash();
47
48  /// debugPassCrash - This method is called when the specified pass crashes on
49  /// Program as input.  It tries to reduce the testcase to something that still
50  /// crashes, but it smaller.
51  ///
52  bool debugPassCrash(const PassInfo *PI);
53
54  /// debugMiscompilation - This method is used when the passes selected are not
55  /// crashing, but the generated output is semantically different from the
56  /// input.
57  bool debugMiscompilation();
58
59  /// debugPassMiscompilation - This method is called when the specified pass
60  /// miscompiles Program as input.  It tries to reduce the testcase to
61  /// something that smaller that still miscompiles the program.
62  /// ReferenceOutput contains the filename of the file containing the output we
63  /// are to match.
64  ///
65  bool debugPassMiscompilation(const PassInfo *ThePass,
66			       const std::string &ReferenceOutput);
67
68private:
69  /// ParseInputFile - Given a bytecode or assembly input filename, parse and
70  /// return it, or return null if not possible.
71  ///
72  Module *ParseInputFile(const std::string &InputFilename) const;
73
74  /// writeProgramToFile - This writes the current "Program" to the named
75  /// bytecode file.  If an error occurs, true is returned.
76  ///
77  bool writeProgramToFile(const std::string &Filename, Module *M = 0) const;
78
79
80  /// EmitProgressBytecode - This function is used to output the current Program
81  /// to a file named "bugpoing-ID.bc".
82  ///
83  void EmitProgressBytecode(const PassInfo *Pass, const std::string &ID);
84
85  /// runPasses - Run the specified passes on Program, outputting a bytecode
86  /// file and writting the filename into OutputFile if successful.  If the
87  /// optimizations fail for some reason (optimizer crashes), return true,
88  /// otherwise return false.  If DeleteOutput is set to true, the bytecode is
89  /// deleted on success, and the filename string is undefined.  This prints to
90  /// cout a single line message indicating whether compilation was successful
91  /// or failed, unless Quiet is set.
92  ///
93  bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
94                 std::string &OutputFilename, bool DeleteOutput = false,
95		 bool Quiet = false) const;
96
97  /// runPasses - Just like the method above, but this just returns true or
98  /// false indicating whether or not the optimizer crashed on the specified
99  /// input (true = crashed).
100  ///
101  bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
102                 bool DeleteOutput = true) const {
103    std::string Filename;
104    return runPasses(PassesToRun, Filename, DeleteOutput);
105  }
106
107  /// runPass - Run only the specified pass on the program.
108  ///
109  bool runPass(const PassInfo *P, bool DeleteOutput = true) const {
110    return runPasses(std::vector<const PassInfo*>(1, P), DeleteOutput);
111  }
112
113  /// extractFunctionFromModule - This method is used to extract the specified
114  /// (non-external) function from the current program, slim down the module,
115  /// and then return it.  This does not modify Program at all, it modifies a
116  /// copy, which it returns.
117  ///
118  Module *extractFunctionFromModule(Function *F) const;
119
120  /// initializeExecutionEnvironment - This method is used to set up the
121  /// environment for executing LLVM programs.
122  ///
123  bool initializeExecutionEnvironment();
124
125  /// executeProgram - This method runs "Program", capturing the output of the
126  /// program to a file, returning the filename of the file.  A recommended
127  /// filename may be optionally specified.
128  ///
129  std::string executeProgram(std::string RequestedOutputFilename = "",
130			     std::string Bytecode = "");
131
132  /// diffProgram - This method executes the specified module and diffs the
133  /// output against the file specified by ReferenceOutputFile.  If the output
134  /// is different, true is returned.
135  ///
136  bool diffProgram(const std::string &ReferenceOutputFile,
137		   const std::string &BytecodeFile = "");
138};
139
140#endif
141