BugDriver.h revision 218885
1362181Sdim//===- BugDriver.h - Top-Level BugPoint class -------------------*- C++ -*-===//
2362181Sdim//
3362181Sdim//                     The LLVM Compiler Infrastructure
4362181Sdim//
5362181Sdim// This file is distributed under the University of Illinois Open Source
6362181Sdim// License. See LICENSE.TXT for details.
7362181Sdim//
8362181Sdim//===----------------------------------------------------------------------===//
9251877Speter//
10362181Sdim// This class contains all of the shared state and information that is used by
11251877Speter// the BugPoint tool to track down errors in optimizations.  This class is the
12362181Sdim// main driver class that invokes all sub-functionality.
13362181Sdim//
14362181Sdim//===----------------------------------------------------------------------===//
15362181Sdim
16362181Sdim#ifndef BUGDRIVER_H
17362181Sdim#define BUGDRIVER_H
18362181Sdim
19251877Speter#include "llvm/ADT/ValueMap.h"
20251877Speter#include "llvm/Transforms/Utils/ValueMapper.h"
21251877Speter#include <vector>
22251877Speter#include <string>
23251877Speter
24251877Speternamespace llvm {
25251877Speter
26251877Speterclass Value;
27251877Speterclass PassInfo;
28251877Speterclass Module;
29251877Speterclass GlobalVariable;
30251877Speterclass Function;
31251877Speterclass BasicBlock;
32251877Speterclass AbstractInterpreter;
33251877Speterclass Instruction;
34251877Speterclass LLVMContext;
35253895Speter
36253895Speterclass DebugCrashes;
37253895Speter
38253895Speterclass GCC;
39251877Speter
40253895Speterextern bool DisableSimplifyCFG;
41251877Speter
42251877Speter/// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
43251877Speter///
44251877Speterextern bool BugpointIsInterrupted;
45251877Speter
46251877Speterclass BugDriver {
47251877Speter  LLVMContext& Context;
48251877Speter  const char *ToolName;            // argv[0] of bugpoint
49251877Speter  std::string ReferenceOutputFile; // Name of `good' output file
50251877Speter  Module *Program;             // The raw program, linked together
51251877Speter  std::vector<std::string> PassesToRun;
52251877Speter  AbstractInterpreter *Interpreter;   // How to run the program
53251877Speter  AbstractInterpreter *SafeInterpreter;  // To generate reference output, etc.
54251877Speter  GCC *gcc;
55251877Speter  bool run_find_bugs;
56251877Speter  unsigned Timeout;
57251877Speter  unsigned MemoryLimit;
58251877Speter  bool UseValgrind;
59251877Speter
60251877Speter  // FIXME: sort out public/private distinctions...
61251877Speter  friend class ReducePassList;
62251877Speter  friend class ReduceMisCodegenFunctions;
63251877Speter
64251877Speterpublic:
65251877Speter  BugDriver(const char *toolname, bool find_bugs,
66251877Speter            unsigned timeout, unsigned memlimit, bool use_valgrind,
67251877Speter            LLVMContext& ctxt);
68251877Speter  ~BugDriver();
69251877Speter
70251877Speter  const char *getToolName() const { return ToolName; }
71251877Speter
72251877Speter  LLVMContext& getContext() const { return Context; }
73251877Speter
74251877Speter  // Set up methods... these methods are used to copy information about the
75251877Speter  // command line arguments into instance variables of BugDriver.
76251877Speter  //
77251877Speter  bool addSources(const std::vector<std::string> &FileNames);
78251877Speter  void addPass(std::string p) { PassesToRun.push_back(p); }
79251877Speter  void setPassesToRun(const std::vector<std::string> &PTR) {
80251877Speter    PassesToRun = PTR;
81251877Speter  }
82251877Speter  const std::vector<std::string> &getPassesToRun() const {
83251877Speter    return PassesToRun;
84251877Speter  }
85251877Speter
86251877Speter  /// run - The top level method that is invoked after all of the instance
87251877Speter  /// variables are set up from command line arguments. The \p as_child argument
88251877Speter  /// indicates whether the driver is to run in parent mode or child mode.
89251877Speter  ///
90251877Speter  bool run(std::string &ErrMsg);
91251877Speter
92251877Speter  /// debugOptimizerCrash - This method is called when some optimizer pass
93251877Speter  /// crashes on input.  It attempts to prune down the testcase to something
94251877Speter  /// reasonable, and figure out exactly which pass is crashing.
95251877Speter  ///
96251877Speter  bool debugOptimizerCrash(const std::string &ID = "passes");
97251877Speter
98251877Speter  /// debugCodeGeneratorCrash - This method is called when the code generator
99251877Speter  /// crashes on an input.  It attempts to reduce the input as much as possible
100251877Speter  /// while still causing the code generator to crash.
101251877Speter  bool debugCodeGeneratorCrash(std::string &Error);
102251877Speter
103251877Speter  /// debugMiscompilation - This method is used when the passes selected are not
104262324Speter  /// crashing, but the generated output is semantically different from the
105262324Speter  /// input.
106262324Speter  void debugMiscompilation(std::string *Error);
107251877Speter
108251877Speter  /// debugPassMiscompilation - This method is called when the specified pass
109251877Speter  /// miscompiles Program as input.  It tries to reduce the testcase to
110251877Speter  /// something that smaller that still miscompiles the program.
111251877Speter  /// ReferenceOutput contains the filename of the file containing the output we
112251877Speter  /// are to match.
113251877Speter  ///
114251877Speter  bool debugPassMiscompilation(const PassInfo *ThePass,
115251877Speter                               const std::string &ReferenceOutput);
116251877Speter
117251877Speter  /// compileSharedObject - This method creates a SharedObject from a given
118251877Speter  /// BitcodeFile for debugging a code generator.
119251877Speter  ///
120251877Speter  std::string compileSharedObject(const std::string &BitcodeFile,
121251877Speter                                  std::string &Error);
122262324Speter
123262324Speter  /// debugCodeGenerator - This method narrows down a module to a function or
124251877Speter  /// set of functions, using the CBE as a ``safe'' code generator for other
125262324Speter  /// functions that are not under consideration.
126262324Speter  bool debugCodeGenerator(std::string *Error);
127262324Speter
128251877Speter  /// isExecutingJIT - Returns true if bugpoint is currently testing the JIT
129251877Speter  ///
130262324Speter  bool isExecutingJIT();
131262324Speter
132262324Speter  /// runPasses - Run all of the passes in the "PassesToRun" list, discard the
133251877Speter  /// output, and return true if any of the passes crashed.
134251877Speter  bool runPasses(Module *M) const {
135251877Speter    return runPasses(M, PassesToRun);
136251877Speter  }
137251877Speter
138251877Speter  Module *getProgram() const { return Program; }
139251877Speter
140251877Speter  /// swapProgramIn - Set the current module to the specified module, returning
141251877Speter  /// the old one.
142251877Speter  Module *swapProgramIn(Module *M) {
143251877Speter    Module *OldProgram = Program;
144251877Speter    Program = M;
145251877Speter    return OldProgram;
146251877Speter  }
147251877Speter
148262324Speter  AbstractInterpreter *switchToSafeInterpreter() {
149262324Speter    AbstractInterpreter *Old = Interpreter;
150251877Speter    Interpreter = (AbstractInterpreter*)SafeInterpreter;
151262324Speter    return Old;
152262324Speter  }
153262324Speter
154251877Speter  void switchToInterpreter(AbstractInterpreter *AI) {
155251877Speter    Interpreter = AI;
156262324Speter  }
157251877Speter
158251877Speter  /// setNewProgram - If we reduce or update the program somehow, call this
159251877Speter  /// method to update bugdriver with it.  This deletes the old module and sets
160262324Speter  /// the specified one as the current program.
161262324Speter  void setNewProgram(Module *M);
162262324Speter
163251877Speter  /// compileProgram - Try to compile the specified module, returning false and
164251877Speter  /// setting Error if an error occurs.  This is used for code generation
165251877Speter  /// crash testing.
166251877Speter  ///
167251877Speter  void compileProgram(Module *M, std::string *Error) const;
168251877Speter
169251877Speter  /// executeProgram - This method runs "Program", capturing the output of the
170251877Speter  /// program to a file.  A recommended filename may be optionally specified.
171251877Speter  ///
172251877Speter  std::string executeProgram(const Module *Program,
173251877Speter                             std::string OutputFilename,
174262324Speter                             std::string Bitcode,
175262324Speter                             const std::string &SharedObjects,
176262324Speter                             AbstractInterpreter *AI,
177251877Speter                             std::string *Error) const;
178251877Speter
179251877Speter  /// executeProgramSafely - Used to create reference output with the "safe"
180251877Speter  /// backend, if reference output is not provided.  If there is a problem with
181251877Speter  /// the code generator (e.g., llc crashes), this will return false and set
182251877Speter  /// Error.
183251877Speter  ///
184251877Speter  std::string executeProgramSafely(const Module *Program,
185251877Speter                                   std::string OutputFile,
186251877Speter                                   std::string *Error) const;
187251877Speter
188251877Speter  /// createReferenceFile - calls compileProgram and then records the output
189251877Speter  /// into ReferenceOutputFile. Returns true if reference file created, false
190251877Speter  /// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
191251877Speter  /// this function.
192251877Speter  ///
193251877Speter  bool createReferenceFile(Module *M, const std::string &Filename
194251877Speter                                            = "bugpoint.reference.out");
195251877Speter
196251877Speter  /// diffProgram - This method executes the specified module and diffs the
197251877Speter  /// output against the file specified by ReferenceOutputFile.  If the output
198251877Speter  /// is different, 1 is returned.  If there is a problem with the code
199251877Speter  /// generator (e.g., llc crashes), this will return -1 and set Error.
200251877Speter  ///
201251877Speter  bool diffProgram(const Module *Program,
202251877Speter                   const std::string &BitcodeFile = "",
203251877Speter                   const std::string &SharedObj = "",
204251877Speter                   bool RemoveBitcode = false,
205251877Speter                   std::string *Error = 0) const;
206251877Speter
207251877Speter  /// EmitProgressBitcode - This function is used to output M to a file named
208251877Speter  /// "bugpoint-ID.bc".
209251877Speter  ///
210251877Speter  void EmitProgressBitcode(const Module *M, const std::string &ID,
211251877Speter                           bool NoFlyer = false) const;
212251877Speter
213251877Speter  /// deleteInstructionFromProgram - This method clones the current Program and
214262324Speter  /// deletes the specified instruction from the cloned module.  It then runs a
215262324Speter  /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code
216262324Speter  /// which depends on the value.  The modified module is then returned.
217251877Speter  ///
218251877Speter  Module *deleteInstructionFromProgram(const Instruction *I, unsigned Simp);
219251877Speter
220251877Speter  /// performFinalCleanups - This method clones the current Program and performs
221251877Speter  /// a series of cleanups intended to get rid of extra cruft on the module.  If
222251877Speter  /// the MayModifySemantics argument is true, then the cleanups is allowed to
223251877Speter  /// modify how the code behaves.
224251877Speter  ///
225251877Speter  Module *performFinalCleanups(Module *M, bool MayModifySemantics = false);
226251877Speter
227251877Speter  /// ExtractLoop - Given a module, extract up to one loop from it into a new
228251877Speter  /// function.  This returns null if there are no extractable loops in the
229251877Speter  /// program or if the loop extractor crashes.
230262324Speter  Module *ExtractLoop(Module *M);
231262324Speter
232262324Speter  /// ExtractMappedBlocksFromModule - Extract all but the specified basic blocks
233251877Speter  /// into their own functions.  The only detail is that M is actually a module
234251877Speter  /// cloned from the one the BBs are in, so some mapping needs to be performed.
235251877Speter  /// If this operation fails for some reason (ie the implementation is buggy),
236251877Speter  /// this function should return null, otherwise it returns a new Module.
237251877Speter  Module *ExtractMappedBlocksFromModule(const std::vector<BasicBlock*> &BBs,
238251877Speter                                        Module *M);
239251877Speter
240251877Speter  /// runPassesOn - Carefully run the specified set of pass on the specified
241251877Speter  /// module, returning the transformed module on success, or a null pointer on
242251877Speter  /// failure.  If AutoDebugCrashes is set to true, then bugpoint will
243251877Speter  /// automatically attempt to track down a crashing pass if one exists, and
244251877Speter  /// this method will never return null.
245251877Speter  Module *runPassesOn(Module *M, const std::vector<std::string> &Passes,
246253895Speter                      bool AutoDebugCrashes = false, unsigned NumExtraArgs = 0,
247251877Speter                      const char * const *ExtraArgs = NULL);
248251877Speter
249251877Speter  /// runPasses - Run the specified passes on Program, outputting a bitcode
250251877Speter  /// file and writting the filename into OutputFile if successful.  If the
251251877Speter  /// optimizations fail for some reason (optimizer crashes), return true,
252251877Speter  /// otherwise return false.  If DeleteOutput is set to true, the bitcode is
253251877Speter  /// deleted on success, and the filename string is undefined.  This prints to
254253895Speter  /// outs() a single line message indicating whether compilation was successful
255253895Speter  /// or failed, unless Quiet is set.  ExtraArgs specifies additional arguments
256251877Speter  /// to pass to the child bugpoint instance.
257251877Speter  ///
258251877Speter  bool runPasses(Module *Program,
259251877Speter                 const std::vector<std::string> &PassesToRun,
260251877Speter                 std::string &OutputFilename, bool DeleteOutput = false,
261251877Speter                 bool Quiet = false, unsigned NumExtraArgs = 0,
262251877Speter                 const char * const *ExtraArgs = NULL) const;
263251877Speter
264251877Speter  /// runManyPasses - Take the specified pass list and create different
265251877Speter  /// combinations of passes to compile the program with. Compile the program with
266253895Speter  /// each set and mark test to see if it compiled correctly. If the passes
267253895Speter  /// compiled correctly output nothing and rearrange the passes into a new order.
268253895Speter  /// If the passes did not compile correctly, output the command required to
269253895Speter  /// recreate the failure. This returns true if a compiler error is found.
270253895Speter  ///
271253895Speter  bool runManyPasses(const std::vector<std::string> &AllPasses,
272253895Speter                     std::string &ErrMsg);
273251877Speter
274251877Speter  /// writeProgramToFile - This writes the current "Program" to the named
275251877Speter  /// bitcode file.  If an error occurs, true is returned.
276251877Speter  ///
277251877Speter  bool writeProgramToFile(const std::string &Filename, const Module *M) const;
278251877Speter
279251877Speterprivate:
280251877Speter  /// runPasses - Just like the method above, but this just returns true or
281251877Speter  /// false indicating whether or not the optimizer crashed on the specified
282251877Speter  /// input (true = crashed).
283251877Speter  ///
284251877Speter  bool runPasses(Module *M,
285251877Speter                 const std::vector<std::string> &PassesToRun,
286251877Speter                 bool DeleteOutput = true) const {
287251877Speter    std::string Filename;
288251877Speter    return runPasses(M, PassesToRun, Filename, DeleteOutput);
289251877Speter  }
290251877Speter
291251877Speter  /// initializeExecutionEnvironment - This method is used to set up the
292251877Speter  /// environment for executing LLVM programs.
293251877Speter  ///
294251877Speter  bool initializeExecutionEnvironment();
295251877Speter};
296251877Speter
297251877Speter/// ParseInputFile - Given a bitcode or assembly input filename, parse and
298251877Speter/// return it, or return null if not possible.
299251877Speter///
300251877SpeterModule *ParseInputFile(const std::string &InputFilename,
301251877Speter                       LLVMContext& ctxt);
302251877Speter
303251877Speter
304251877Speter/// getPassesString - Turn a list of passes into a string which indicates the
305251877Speter/// command line options that must be passed to add the passes.
306251877Speter///
307251877Speterstd::string getPassesString(const std::vector<std::string> &Passes);
308251877Speter
309251877Speter/// PrintFunctionList - prints out list of problematic functions
310251877Speter///
311251877Spetervoid PrintFunctionList(const std::vector<Function*> &Funcs);
312251877Speter
313251877Speter/// PrintGlobalVariableList - prints out list of problematic global variables
314251877Speter///
315251877Spetervoid PrintGlobalVariableList(const std::vector<GlobalVariable*> &GVs);
316251877Speter
317251877Speter// DeleteFunctionBody - "Remove" the function by deleting all of it's basic
318251877Speter// blocks, making it external.
319251877Speter//
320253895Spetervoid DeleteFunctionBody(Function *F);
321253895Speter
322253895Speter/// SplitFunctionsOutOfModule - Given a module and a list of functions in the
323251877Speter/// module, split the functions OUT of the specified module, and place them in
324251877Speter/// the new module.
325251877SpeterModule *SplitFunctionsOutOfModule(Module *M, const std::vector<Function*> &F,
326253895Speter                                  ValueToValueMapTy &VMap);
327253895Speter
328253895Speter} // End llvm namespace
329253895Speter
330253895Speter#endif
331251877Speter