blob: f88d29ffb7730139da2882262dde4c34de628433 [file] [log] [blame]
Chris Lattnerafade922002-11-20 22:28:10 +00001//===- 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;
Chris Lattner218e26e2002-12-23 23:49:59 +000017class AbstractInterpreter;
Chris Lattner65207852003-01-23 02:48:33 +000018class Instruction;
Chris Lattnerafade922002-11-20 22:28:10 +000019
20class BugDriver {
21 const std::string ToolName; // Name of bugpoint
22 Module *Program; // The raw program, linked together
23 std::vector<const PassInfo*> PassesToRun;
Chris Lattner218e26e2002-12-23 23:49:59 +000024 AbstractInterpreter *Interpreter; // How to run the program
Chris Lattnerafade922002-11-20 22:28:10 +000025public:
Chris Lattner218e26e2002-12-23 23:49:59 +000026 BugDriver(const char *toolname)
27 : ToolName(toolname), Program(0), Interpreter(0) {}
28
29 const std::string &getToolName() const { return ToolName; }
Chris Lattnerafade922002-11-20 22:28:10 +000030
31 // Set up methods... these methods are used to copy information about the
32 // command line arguments into instance variables of BugDriver.
33 //
34 bool addSources(const std::vector<std::string> &FileNames);
35 template<class It>
36 void addPasses(It I, It E) { PassesToRun.insert(PassesToRun.end(), I, E); }
37
38 /// run - The top level method that is invoked after all of the instance
39 /// variables are set up from command line arguments.
40 ///
41 bool run();
42
43 /// debugCrash - This method is called when some pass crashes on input. It
44 /// attempts to prune down the testcase to something reasonable, and figure
45 /// out exactly which pass is crashing.
46 ///
47 bool debugCrash();
48
49 /// debugPassCrash - This method is called when the specified pass crashes on
50 /// Program as input. It tries to reduce the testcase to something that still
51 /// crashes, but it smaller.
52 ///
53 bool debugPassCrash(const PassInfo *PI);
54
55 /// debugMiscompilation - This method is used when the passes selected are not
56 /// crashing, but the generated output is semantically different from the
57 /// input.
58 bool debugMiscompilation();
59
Chris Lattner218e26e2002-12-23 23:49:59 +000060 /// debugPassMiscompilation - This method is called when the specified pass
61 /// miscompiles Program as input. It tries to reduce the testcase to
62 /// something that smaller that still miscompiles the program.
63 /// ReferenceOutput contains the filename of the file containing the output we
64 /// are to match.
65 ///
66 bool debugPassMiscompilation(const PassInfo *ThePass,
67 const std::string &ReferenceOutput);
68
Chris Lattnerafade922002-11-20 22:28:10 +000069private:
70 /// ParseInputFile - Given a bytecode or assembly input filename, parse and
71 /// return it, or return null if not possible.
72 ///
73 Module *ParseInputFile(const std::string &InputFilename) const;
74
Chris Lattnerafade922002-11-20 22:28:10 +000075 /// writeProgramToFile - This writes the current "Program" to the named
76 /// bytecode file. If an error occurs, true is returned.
77 ///
Chris Lattner218e26e2002-12-23 23:49:59 +000078 bool writeProgramToFile(const std::string &Filename, Module *M = 0) const;
Chris Lattnerafade922002-11-20 22:28:10 +000079
80
81 /// EmitProgressBytecode - This function is used to output the current Program
82 /// to a file named "bugpoing-ID.bc".
83 ///
84 void EmitProgressBytecode(const PassInfo *Pass, const std::string &ID);
85
86 /// runPasses - Run the specified passes on Program, outputting a bytecode
87 /// file and writting the filename into OutputFile if successful. If the
88 /// optimizations fail for some reason (optimizer crashes), return true,
89 /// otherwise return false. If DeleteOutput is set to true, the bytecode is
90 /// deleted on success, and the filename string is undefined. This prints to
91 /// cout a single line message indicating whether compilation was successful
Chris Lattner218e26e2002-12-23 23:49:59 +000092 /// or failed, unless Quiet is set.
Chris Lattnerafade922002-11-20 22:28:10 +000093 ///
94 bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
Chris Lattner218e26e2002-12-23 23:49:59 +000095 std::string &OutputFilename, bool DeleteOutput = false,
96 bool Quiet = false) const;
Chris Lattnerafade922002-11-20 22:28:10 +000097
98 /// runPasses - Just like the method above, but this just returns true or
99 /// false indicating whether or not the optimizer crashed on the specified
100 /// input (true = crashed).
101 ///
102 bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
103 bool DeleteOutput = true) const {
104 std::string Filename;
105 return runPasses(PassesToRun, Filename, DeleteOutput);
106 }
107
108 /// runPass - Run only the specified pass on the program.
Chris Lattner218e26e2002-12-23 23:49:59 +0000109 ///
Chris Lattnerafade922002-11-20 22:28:10 +0000110 bool runPass(const PassInfo *P, bool DeleteOutput = true) const {
111 return runPasses(std::vector<const PassInfo*>(1, P), DeleteOutput);
112 }
113
114 /// extractFunctionFromModule - This method is used to extract the specified
115 /// (non-external) function from the current program, slim down the module,
116 /// and then return it. This does not modify Program at all, it modifies a
117 /// copy, which it returns.
Chris Lattner218e26e2002-12-23 23:49:59 +0000118 ///
Chris Lattnerafade922002-11-20 22:28:10 +0000119 Module *extractFunctionFromModule(Function *F) const;
120
Chris Lattner65207852003-01-23 02:48:33 +0000121 /// deleteInstructionFromProgram - This method clones the current Program and
122 /// deletes the specified instruction from the cloned module. It then runs a
123 /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code
124 /// which depends on the value. The modified module is then returned.
125 ///
126 Module *deleteInstructionFromProgram(Instruction *I, unsigned Simp) const;
127
Chris Lattnerba386d92003-02-28 16:13:20 +0000128 /// performFinalCleanups - This method clones the current Program and performs
129 /// a series of cleanups intended to get rid of extra cruft on the module
130 /// before handing it to the user...
131 ///
132 Module *performFinalCleanups() const;
133
Chris Lattner218e26e2002-12-23 23:49:59 +0000134 /// initializeExecutionEnvironment - This method is used to set up the
135 /// environment for executing LLVM programs.
136 ///
137 bool initializeExecutionEnvironment();
138
139 /// executeProgram - This method runs "Program", capturing the output of the
140 /// program to a file, returning the filename of the file. A recommended
141 /// filename may be optionally specified.
142 ///
143 std::string executeProgram(std::string RequestedOutputFilename = "",
144 std::string Bytecode = "");
145
146 /// diffProgram - This method executes the specified module and diffs the
147 /// output against the file specified by ReferenceOutputFile. If the output
148 /// is different, true is returned.
149 ///
150 bool diffProgram(const std::string &ReferenceOutputFile,
151 const std::string &BytecodeFile = "");
Chris Lattnerafade922002-11-20 22:28:10 +0000152};
153
154#endif