BugDriver.h revision c600f3c337f18c62116ac58b701e4f7ae6d2fb1a
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// This class contains all of the shared state and information that is used by
11// the BugPoint tool to track down errors in optimizations.  This class is the
12// main driver class that invokes all sub-functionality.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef BUGDRIVER_H
17#define BUGDRIVER_H
18
19#include <vector>
20#include <string>
21
22namespace llvm {
23
24class PassInfo;
25class Module;
26class Function;
27class BasicBlock;
28class AbstractInterpreter;
29class Instruction;
30
31class DebugCrashes;
32
33class GCC;
34
35extern bool DisableSimplifyCFG;
36
37/// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
38///
39extern bool BugpointIsInterrupted;
40
41class BugDriver {
42  const std::string ToolName;  // Name of bugpoint
43  std::string ReferenceOutputFile; // Name of `good' output file
44  Module *Program;             // The raw program, linked together
45  std::vector<const PassInfo*> PassesToRun;
46  AbstractInterpreter *Interpreter;   // How to run the program
47  AbstractInterpreter *cbe;
48  GCC *gcc;
49  bool run_as_child;
50  bool run_find_bugs;
51  unsigned Timeout;
52
53  // FIXME: sort out public/private distinctions...
54  friend class ReducePassList;
55  friend class ReduceMisCodegenFunctions;
56
57public:
58  BugDriver(const char *toolname, bool as_child, bool find_bugs,
59            unsigned timeout);
60
61  const std::string &getToolName() const { return ToolName; }
62
63  // Set up methods... these methods are used to copy information about the
64  // command line arguments into instance variables of BugDriver.
65  //
66  bool addSources(const std::vector<std::string> &FileNames);
67  template<class It>
68  void addPasses(It I, It E) { PassesToRun.insert(PassesToRun.end(), I, E); }
69  void setPassesToRun(const std::vector<const PassInfo*> &PTR) {
70    PassesToRun = PTR;
71  }
72  const std::vector<const PassInfo*> &getPassesToRun() const {
73    return PassesToRun;
74  }
75
76  /// run - The top level method that is invoked after all of the instance
77  /// variables are set up from command line arguments. The \p as_child argument
78  /// indicates whether the driver is to run in parent mode or child mode.
79  ///
80  bool run();
81
82  /// debugOptimizerCrash - This method is called when some optimizer pass
83  /// crashes on input.  It attempts to prune down the testcase to something
84  /// reasonable, and figure out exactly which pass is crashing.
85  ///
86  bool debugOptimizerCrash(const std::string &ID = "passes");
87
88  /// debugCodeGeneratorCrash - This method is called when the code generator
89  /// crashes on an input.  It attempts to reduce the input as much as possible
90  /// while still causing the code generator to crash.
91  bool debugCodeGeneratorCrash();
92
93  /// debugMiscompilation - This method is used when the passes selected are not
94  /// crashing, but the generated output is semantically different from the
95  /// input.
96  bool debugMiscompilation();
97
98  /// debugPassMiscompilation - This method is called when the specified pass
99  /// miscompiles Program as input.  It tries to reduce the testcase to
100  /// something that smaller that still miscompiles the program.
101  /// ReferenceOutput contains the filename of the file containing the output we
102  /// are to match.
103  ///
104  bool debugPassMiscompilation(const PassInfo *ThePass,
105                               const std::string &ReferenceOutput);
106
107  /// compileSharedObject - This method creates a SharedObject from a given
108  /// BytecodeFile for debugging a code generator.
109  ///
110  std::string compileSharedObject(const std::string &BytecodeFile);
111
112  /// debugCodeGenerator - This method narrows down a module to a function or
113  /// set of functions, using the CBE as a ``safe'' code generator for other
114  /// functions that are not under consideration.
115  bool debugCodeGenerator();
116
117  /// isExecutingJIT - Returns true if bugpoint is currently testing the JIT
118  ///
119  bool isExecutingJIT();
120
121  /// runPasses - Run all of the passes in the "PassesToRun" list, discard the
122  /// output, and return true if any of the passes crashed.
123  bool runPasses(Module *M = 0) {
124    if (M == 0) M = Program;
125    std::swap(M, Program);
126    bool Result = runPasses(PassesToRun);
127    std::swap(M, Program);
128    return Result;
129  }
130
131  Module *getProgram() const { return Program; }
132
133  /// swapProgramIn - Set the current module to the specified module, returning
134  /// the old one.
135  Module *swapProgramIn(Module *M) {
136    Module *OldProgram = Program;
137    Program = M;
138    return OldProgram;
139  }
140
141  AbstractInterpreter *switchToCBE() {
142    AbstractInterpreter *Old = Interpreter;
143    Interpreter = (AbstractInterpreter*)cbe;
144    return Old;
145  }
146
147  void switchToInterpreter(AbstractInterpreter *AI) {
148    Interpreter = AI;
149  }
150
151  /// setNewProgram - If we reduce or update the program somehow, call this
152  /// method to update bugdriver with it.  This deletes the old module and sets
153  /// the specified one as the current program.
154  void setNewProgram(Module *M);
155
156  /// compileProgram - Try to compile the specified module, throwing an
157  /// exception if an error occurs, or returning normally if not.  This is used
158  /// for code generation crash testing.
159  ///
160  void compileProgram(Module *M);
161
162  /// executeProgram - This method runs "Program", capturing the output of the
163  /// program to a file, returning the filename of the file.  A recommended
164  /// filename may be optionally specified.  If there is a problem with the code
165  /// generator (e.g., llc crashes), this will throw an exception.
166  ///
167  std::string executeProgram(std::string RequestedOutputFilename = "",
168                             std::string Bytecode = "",
169                             const std::string &SharedObjects = "",
170                             AbstractInterpreter *AI = 0,
171                             bool *ProgramExitedNonzero = 0);
172
173  /// executeProgramWithCBE - Used to create reference output with the C
174  /// backend, if reference output is not provided.  If there is a problem with
175  /// the code generator (e.g., llc crashes), this will throw an exception.
176  ///
177  std::string executeProgramWithCBE(std::string OutputFile = "");
178
179  /// createReferenceFile - calls compileProgram and then records the output
180  /// into ReferenceOutputFile. Returns true if reference file created, false
181  /// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
182  /// this function.
183  ///
184  bool createReferenceFile(Module *M, const std::string &Filename
185                                            = "bugpoint.reference.out");
186
187  /// diffProgram - This method executes the specified module and diffs the
188  /// output against the file specified by ReferenceOutputFile.  If the output
189  /// is different, true is returned.  If there is a problem with the code
190  /// generator (e.g., llc crashes), this will throw an exception.
191  ///
192  bool diffProgram(const std::string &BytecodeFile = "",
193                   const std::string &SharedObj = "",
194                   bool RemoveBytecode = false);
195
196  /// EmitProgressBytecode - This function is used to output the current Program
197  /// to a file named "bugpoint-ID.bc".
198  ///
199  void EmitProgressBytecode(const std::string &ID, bool NoFlyer = false);
200
201  /// deleteInstructionFromProgram - This method clones the current Program and
202  /// deletes the specified instruction from the cloned module.  It then runs a
203  /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code
204  /// which depends on the value.  The modified module is then returned.
205  ///
206  Module *deleteInstructionFromProgram(const Instruction *I, unsigned Simp)
207    const;
208
209  /// performFinalCleanups - This method clones the current Program and performs
210  /// a series of cleanups intended to get rid of extra cruft on the module.  If
211  /// the MayModifySemantics argument is true, then the cleanups is allowed to
212  /// modify how the code behaves.
213  ///
214  Module *performFinalCleanups(Module *M, bool MayModifySemantics = false);
215
216  /// ExtractLoop - Given a module, extract up to one loop from it into a new
217  /// function.  This returns null if there are no extractable loops in the
218  /// program or if the loop extractor crashes.
219  Module *ExtractLoop(Module *M);
220
221  /// ExtractMappedBlocksFromModule - Extract all but the specified basic blocks
222  /// into their own functions.  The only detail is that M is actually a module
223  /// cloned from the one the BBs are in, so some mapping needs to be performed.
224  /// If this operation fails for some reason (ie the implementation is buggy),
225  /// this function should return null, otherwise it returns a new Module.
226  Module *ExtractMappedBlocksFromModule(const std::vector<BasicBlock*> &BBs,
227                                        Module *M);
228
229  /// runPassesOn - Carefully run the specified set of pass on the specified
230  /// module, returning the transformed module on success, or a null pointer on
231  /// failure.  If AutoDebugCrashes is set to true, then bugpoint will
232  /// automatically attempt to track down a crashing pass if one exists, and
233  /// this method will never return null.
234  Module *runPassesOn(Module *M, const std::vector<const PassInfo*> &Passes,
235                      bool AutoDebugCrashes = false);
236
237  /// runPasses - Run the specified passes on Program, outputting a bytecode
238  /// file and writting the filename into OutputFile if successful.  If the
239  /// optimizations fail for some reason (optimizer crashes), return true,
240  /// otherwise return false.  If DeleteOutput is set to true, the bytecode is
241  /// deleted on success, and the filename string is undefined.  This prints to
242  /// cout a single line message indicating whether compilation was successful
243  /// or failed, unless Quiet is set.
244  ///
245  bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
246                 std::string &OutputFilename, bool DeleteOutput = false,
247                 bool Quiet = false) const;
248
249  /// runManyPasses - Take the specified pass list and create different
250  /// combinations of passes to compile the program with. Compile the program with
251  /// each set and mark test to see if it compiled correctly. If the passes
252  /// compiled correctly output nothing and rearrange the passes into a new order.
253  /// If the passes did not compile correctly, output the command required to
254  /// recreate the failure. This returns true if a compiler error is found.
255  ///
256  bool runManyPasses(const std::vector<const PassInfo*> &AllPasses);
257
258  /// writeProgramToFile - This writes the current "Program" to the named
259  /// bytecode file.  If an error occurs, true is returned.
260  ///
261  bool writeProgramToFile(const std::string &Filename, Module *M = 0) const;
262
263private:
264  /// runPasses - Just like the method above, but this just returns true or
265  /// false indicating whether or not the optimizer crashed on the specified
266  /// input (true = crashed).
267  ///
268  bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
269                 bool DeleteOutput = true) const {
270    std::string Filename;
271    return runPasses(PassesToRun, Filename, DeleteOutput);
272  }
273
274  /// runAsChild - The actual "runPasses" guts that runs in a child process.
275  int runPassesAsChild(const std::vector<const PassInfo*> &PassesToRun);
276
277  /// initializeExecutionEnvironment - This method is used to set up the
278  /// environment for executing LLVM programs.
279  ///
280  bool initializeExecutionEnvironment();
281};
282
283/// ParseInputFile - Given a bytecode or assembly input filename, parse and
284/// return it, or return null if not possible.
285///
286Module *ParseInputFile(const std::string &InputFilename);
287
288
289/// getPassesString - Turn a list of passes into a string which indicates the
290/// command line options that must be passed to add the passes.
291///
292std::string getPassesString(const std::vector<const PassInfo*> &Passes);
293
294/// PrintFunctionList - prints out list of problematic functions
295///
296void PrintFunctionList(const std::vector<Function*> &Funcs);
297
298// DeleteFunctionBody - "Remove" the function by deleting all of it's basic
299// blocks, making it external.
300//
301void DeleteFunctionBody(Function *F);
302
303/// SplitFunctionsOutOfModule - Given a module and a list of functions in the
304/// module, split the functions OUT of the specified module, and place them in
305/// the new module.
306Module *SplitFunctionsOutOfModule(Module *M, const std::vector<Function*> &F);
307
308} // End llvm namespace
309
310#endif
311