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