BugDriver.h revision 286921e8d21d4f0655905ed278d0e140829c7d1f
1afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner//===- BugDriver.h - Top-Level BugPoint class -------------------*- C++ -*-===//
2afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner//
3afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner// This class contains all of the shared state and information that is used by
4afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner// the BugPoint tool to track down errors in optimizations.  This class is the
5afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner// main driver class that invokes all sub-functionality.
6afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner//
7afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner//===----------------------------------------------------------------------===//
8afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
9afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#ifndef BUGDRIVER_H
10afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#define BUGDRIVER_H
11afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
12afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include <vector>
13afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include <string>
14afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattnerclass PassInfo;
15afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattnerclass Module;
16afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattnerclass Function;
17218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattnerclass AbstractInterpreter;
186520785dcd22012535934098942d57c07c7631c2Chris Lattnerclass Instruction;
19afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
20aae33f9137e4a64394d8f9fe66611ae53a0ef4e8Chris Lattnerclass DebugCrashes;
21640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattnerclass ReduceMiscompilingPasses;
22640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattnerclass ReduceMiscompilingFunctions;
23aae33f9137e4a64394d8f9fe66611ae53a0ef4e8Chris Lattnerclass ReduceCrashingFunctions;
24286921e8d21d4f0655905ed278d0e140829c7d1fChris Lattnerclass ReduceCrashingBlocks;
25640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
26afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattnerclass BugDriver {
27afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  const std::string ToolName;  // Name of bugpoint
28afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  Module *Program;             // The raw program, linked together
29afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  std::vector<const PassInfo*> PassesToRun;
30218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  AbstractInterpreter *Interpreter;   // How to run the program
31640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
32aae33f9137e4a64394d8f9fe66611ae53a0ef4e8Chris Lattner  // FIXME: sort out public/private distinctions...
33aae33f9137e4a64394d8f9fe66611ae53a0ef4e8Chris Lattner  friend class DebugCrashes;
34640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  friend class ReduceMiscompilingPasses;
35640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  friend class ReduceMiscompilingFunctions;
36aae33f9137e4a64394d8f9fe66611ae53a0ef4e8Chris Lattner  friend class ReduceCrashingFunctions;
37286921e8d21d4f0655905ed278d0e140829c7d1fChris Lattner  friend class ReduceCrashingBlocks;
38afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattnerpublic:
39218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  BugDriver(const char *toolname)
40218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner    : ToolName(toolname), Program(0), Interpreter(0) {}
41218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner
42218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  const std::string &getToolName() const { return ToolName; }
43afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
44afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  // Set up methods... these methods are used to copy information about the
45afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  // command line arguments into instance variables of BugDriver.
46afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  //
47afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  bool addSources(const std::vector<std::string> &FileNames);
48afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  template<class It>
49afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  void addPasses(It I, It E) { PassesToRun.insert(PassesToRun.end(), I, E); }
50afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
51afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// run - The top level method that is invoked after all of the instance
52afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// variables are set up from command line arguments.
53afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  ///
54afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  bool run();
55afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
56afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// debugCrash - This method is called when some pass crashes on input.  It
57afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// attempts to prune down the testcase to something reasonable, and figure
58afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// out exactly which pass is crashing.
59afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  ///
60afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  bool debugCrash();
61afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
62afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// debugMiscompilation - This method is used when the passes selected are not
63afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// crashing, but the generated output is semantically different from the
64afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// input.
65afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  bool debugMiscompilation();
66afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
67218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// debugPassMiscompilation - This method is called when the specified pass
68218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// miscompiles Program as input.  It tries to reduce the testcase to
69218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// something that smaller that still miscompiles the program.
70218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// ReferenceOutput contains the filename of the file containing the output we
71218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// are to match.
72218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  ///
73218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  bool debugPassMiscompilation(const PassInfo *ThePass,
74218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner			       const std::string &ReferenceOutput);
75218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner
76afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattnerprivate:
77afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// ParseInputFile - Given a bytecode or assembly input filename, parse and
78afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// return it, or return null if not possible.
79afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  ///
80afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  Module *ParseInputFile(const std::string &InputFilename) const;
81afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
82afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// writeProgramToFile - This writes the current "Program" to the named
83afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// bytecode file.  If an error occurs, true is returned.
84afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  ///
85218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  bool writeProgramToFile(const std::string &Filename, Module *M = 0) const;
86afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
87afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
88afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// EmitProgressBytecode - This function is used to output the current Program
89afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// to a file named "bugpoing-ID.bc".
90afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  ///
91640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner  void EmitProgressBytecode(const std::string &ID, bool NoFlyer = false);
92afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
93afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// runPasses - Run the specified passes on Program, outputting a bytecode
94afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// file and writting the filename into OutputFile if successful.  If the
95afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// optimizations fail for some reason (optimizer crashes), return true,
96afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// otherwise return false.  If DeleteOutput is set to true, the bytecode is
97afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// deleted on success, and the filename string is undefined.  This prints to
98afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// cout a single line message indicating whether compilation was successful
99218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// or failed, unless Quiet is set.
100afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  ///
101afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
102218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner                 std::string &OutputFilename, bool DeleteOutput = false,
103218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner		 bool Quiet = false) const;
104afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
105afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// runPasses - Just like the method above, but this just returns true or
106afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// false indicating whether or not the optimizer crashed on the specified
107afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  /// input (true = crashed).
108afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  ///
109afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
110afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner                 bool DeleteOutput = true) const {
111afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner    std::string Filename;
112afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner    return runPasses(PassesToRun, Filename, DeleteOutput);
113afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner  }
114afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
1156520785dcd22012535934098942d57c07c7631c2Chris Lattner  /// deleteInstructionFromProgram - This method clones the current Program and
1166520785dcd22012535934098942d57c07c7631c2Chris Lattner  /// deletes the specified instruction from the cloned module.  It then runs a
1176520785dcd22012535934098942d57c07c7631c2Chris Lattner  /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code
1186520785dcd22012535934098942d57c07c7631c2Chris Lattner  /// which depends on the value.  The modified module is then returned.
1196520785dcd22012535934098942d57c07c7631c2Chris Lattner  ///
1206520785dcd22012535934098942d57c07c7631c2Chris Lattner  Module *deleteInstructionFromProgram(Instruction *I, unsigned Simp) const;
1216520785dcd22012535934098942d57c07c7631c2Chris Lattner
122ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner  /// performFinalCleanups - This method clones the current Program and performs
123ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner  /// a series of cleanups intended to get rid of extra cruft on the module
124ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner  /// before handing it to the user...
125ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner  ///
126ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner  Module *performFinalCleanups() const;
127ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner
128218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// initializeExecutionEnvironment - This method is used to set up the
129218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// environment for executing LLVM programs.
130218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  ///
131218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  bool initializeExecutionEnvironment();
132218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner
133218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// executeProgram - This method runs "Program", capturing the output of the
134218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// program to a file, returning the filename of the file.  A recommended
135218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// filename may be optionally specified.
136218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  ///
137218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  std::string executeProgram(std::string RequestedOutputFilename = "",
138218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner			     std::string Bytecode = "");
139218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner
140218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// diffProgram - This method executes the specified module and diffs the
141218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// output against the file specified by ReferenceOutputFile.  If the output
142218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  /// is different, true is returned.
143218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  ///
144218e26ef3583cc3270f5f2a2b9cb1025e5b05ebeChris Lattner  bool diffProgram(const std::string &ReferenceOutputFile,
145640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner		   const std::string &BytecodeFile = "",
146640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner                   bool RemoveBytecode = false);
147afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner};
148afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
149640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner/// getPassesString - Turn a list of passes into a string which indicates the
150640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner/// command line options that must be passed to add the passes.
151640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner///
152640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattnerstd::string getPassesString(const std::vector<const PassInfo*> &Passes);
153640f22e66d90439857a97a83896ee68c4f7128c9Chris Lattner
154aae33f9137e4a64394d8f9fe66611ae53a0ef4e8Chris Lattner// DeleteFunctionBody - "Remove" the function by deleting all of it's basic
155aae33f9137e4a64394d8f9fe66611ae53a0ef4e8Chris Lattner// blocks, making it external.
156aae33f9137e4a64394d8f9fe66611ae53a0ef4e8Chris Lattner//
157aae33f9137e4a64394d8f9fe66611ae53a0ef4e8Chris Lattnervoid DeleteFunctionBody(Function *F);
158aae33f9137e4a64394d8f9fe66611ae53a0ef4e8Chris Lattner
159afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#endif
160