Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 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 <vector> |
| 13 | #include <string> |
| 14 | class PassInfo; |
| 15 | class Module; |
| 16 | class Function; |
Chris Lattner | 218e26e | 2002-12-23 23:49:59 +0000 | [diff] [blame] | 17 | class AbstractInterpreter; |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 18 | class Instruction; |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 19 | |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame^] | 20 | class ReduceMiscompilingPasses; |
| 21 | class ReduceMiscompilingFunctions; |
| 22 | |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 23 | class BugDriver { |
| 24 | const std::string ToolName; // Name of bugpoint |
| 25 | Module *Program; // The raw program, linked together |
| 26 | std::vector<const PassInfo*> PassesToRun; |
Chris Lattner | 218e26e | 2002-12-23 23:49:59 +0000 | [diff] [blame] | 27 | AbstractInterpreter *Interpreter; // How to run the program |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame^] | 28 | |
| 29 | friend class ReduceMiscompilingPasses; |
| 30 | friend class ReduceMiscompilingFunctions; |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 31 | public: |
Chris Lattner | 218e26e | 2002-12-23 23:49:59 +0000 | [diff] [blame] | 32 | BugDriver(const char *toolname) |
| 33 | : ToolName(toolname), Program(0), Interpreter(0) {} |
| 34 | |
| 35 | const std::string &getToolName() const { return ToolName; } |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 36 | |
| 37 | // Set up methods... these methods are used to copy information about the |
| 38 | // command line arguments into instance variables of BugDriver. |
| 39 | // |
| 40 | bool addSources(const std::vector<std::string> &FileNames); |
| 41 | template<class It> |
| 42 | void addPasses(It I, It E) { PassesToRun.insert(PassesToRun.end(), I, E); } |
| 43 | |
| 44 | /// run - The top level method that is invoked after all of the instance |
| 45 | /// variables are set up from command line arguments. |
| 46 | /// |
| 47 | bool run(); |
| 48 | |
| 49 | /// debugCrash - This method is called when some pass crashes on input. It |
| 50 | /// attempts to prune down the testcase to something reasonable, and figure |
| 51 | /// out exactly which pass is crashing. |
| 52 | /// |
| 53 | bool debugCrash(); |
| 54 | |
| 55 | /// debugPassCrash - This method is called when the specified pass crashes on |
| 56 | /// Program as input. It tries to reduce the testcase to something that still |
| 57 | /// crashes, but it smaller. |
| 58 | /// |
| 59 | bool debugPassCrash(const PassInfo *PI); |
| 60 | |
| 61 | /// debugMiscompilation - This method is used when the passes selected are not |
| 62 | /// crashing, but the generated output is semantically different from the |
| 63 | /// input. |
| 64 | bool debugMiscompilation(); |
| 65 | |
Chris Lattner | 218e26e | 2002-12-23 23:49:59 +0000 | [diff] [blame] | 66 | /// debugPassMiscompilation - This method is called when the specified pass |
| 67 | /// miscompiles Program as input. It tries to reduce the testcase to |
| 68 | /// something that smaller that still miscompiles the program. |
| 69 | /// ReferenceOutput contains the filename of the file containing the output we |
| 70 | /// are to match. |
| 71 | /// |
| 72 | bool debugPassMiscompilation(const PassInfo *ThePass, |
| 73 | const std::string &ReferenceOutput); |
| 74 | |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 75 | private: |
| 76 | /// ParseInputFile - Given a bytecode or assembly input filename, parse and |
| 77 | /// return it, or return null if not possible. |
| 78 | /// |
| 79 | Module *ParseInputFile(const std::string &InputFilename) const; |
| 80 | |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 81 | /// writeProgramToFile - This writes the current "Program" to the named |
| 82 | /// bytecode file. If an error occurs, true is returned. |
| 83 | /// |
Chris Lattner | 218e26e | 2002-12-23 23:49:59 +0000 | [diff] [blame] | 84 | bool writeProgramToFile(const std::string &Filename, Module *M = 0) const; |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 85 | |
| 86 | |
| 87 | /// EmitProgressBytecode - This function is used to output the current Program |
| 88 | /// to a file named "bugpoing-ID.bc". |
| 89 | /// |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame^] | 90 | void EmitProgressBytecode(const std::string &ID, bool NoFlyer = false); |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 91 | |
| 92 | /// runPasses - Run the specified passes on Program, outputting a bytecode |
| 93 | /// file and writting the filename into OutputFile if successful. If the |
| 94 | /// optimizations fail for some reason (optimizer crashes), return true, |
| 95 | /// otherwise return false. If DeleteOutput is set to true, the bytecode is |
| 96 | /// deleted on success, and the filename string is undefined. This prints to |
| 97 | /// cout a single line message indicating whether compilation was successful |
Chris Lattner | 218e26e | 2002-12-23 23:49:59 +0000 | [diff] [blame] | 98 | /// or failed, unless Quiet is set. |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 99 | /// |
| 100 | bool runPasses(const std::vector<const PassInfo*> &PassesToRun, |
Chris Lattner | 218e26e | 2002-12-23 23:49:59 +0000 | [diff] [blame] | 101 | std::string &OutputFilename, bool DeleteOutput = false, |
| 102 | bool Quiet = false) const; |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 103 | |
| 104 | /// runPasses - Just like the method above, but this just returns true or |
| 105 | /// false indicating whether or not the optimizer crashed on the specified |
| 106 | /// input (true = crashed). |
| 107 | /// |
| 108 | bool runPasses(const std::vector<const PassInfo*> &PassesToRun, |
| 109 | bool DeleteOutput = true) const { |
| 110 | std::string Filename; |
| 111 | return runPasses(PassesToRun, Filename, DeleteOutput); |
| 112 | } |
| 113 | |
| 114 | /// runPass - Run only the specified pass on the program. |
Chris Lattner | 218e26e | 2002-12-23 23:49:59 +0000 | [diff] [blame] | 115 | /// |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 116 | bool runPass(const PassInfo *P, bool DeleteOutput = true) const { |
| 117 | return runPasses(std::vector<const PassInfo*>(1, P), DeleteOutput); |
| 118 | } |
| 119 | |
| 120 | /// extractFunctionFromModule - This method is used to extract the specified |
| 121 | /// (non-external) function from the current program, slim down the module, |
| 122 | /// and then return it. This does not modify Program at all, it modifies a |
| 123 | /// copy, which it returns. |
Chris Lattner | 218e26e | 2002-12-23 23:49:59 +0000 | [diff] [blame] | 124 | /// |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 125 | Module *extractFunctionFromModule(Function *F) const; |
| 126 | |
Chris Lattner | 6520785 | 2003-01-23 02:48:33 +0000 | [diff] [blame] | 127 | /// deleteInstructionFromProgram - This method clones the current Program and |
| 128 | /// deletes the specified instruction from the cloned module. It then runs a |
| 129 | /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code |
| 130 | /// which depends on the value. The modified module is then returned. |
| 131 | /// |
| 132 | Module *deleteInstructionFromProgram(Instruction *I, unsigned Simp) const; |
| 133 | |
Chris Lattner | ba386d9 | 2003-02-28 16:13:20 +0000 | [diff] [blame] | 134 | /// performFinalCleanups - This method clones the current Program and performs |
| 135 | /// a series of cleanups intended to get rid of extra cruft on the module |
| 136 | /// before handing it to the user... |
| 137 | /// |
| 138 | Module *performFinalCleanups() const; |
| 139 | |
Chris Lattner | 218e26e | 2002-12-23 23:49:59 +0000 | [diff] [blame] | 140 | /// initializeExecutionEnvironment - This method is used to set up the |
| 141 | /// environment for executing LLVM programs. |
| 142 | /// |
| 143 | bool initializeExecutionEnvironment(); |
| 144 | |
| 145 | /// executeProgram - This method runs "Program", capturing the output of the |
| 146 | /// program to a file, returning the filename of the file. A recommended |
| 147 | /// filename may be optionally specified. |
| 148 | /// |
| 149 | std::string executeProgram(std::string RequestedOutputFilename = "", |
| 150 | std::string Bytecode = ""); |
| 151 | |
| 152 | /// diffProgram - This method executes the specified module and diffs the |
| 153 | /// output against the file specified by ReferenceOutputFile. If the output |
| 154 | /// is different, true is returned. |
| 155 | /// |
| 156 | bool diffProgram(const std::string &ReferenceOutputFile, |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame^] | 157 | const std::string &BytecodeFile = "", |
| 158 | bool RemoveBytecode = false); |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 159 | }; |
| 160 | |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame^] | 161 | /// getPassesString - Turn a list of passes into a string which indicates the |
| 162 | /// command line options that must be passed to add the passes. |
| 163 | /// |
| 164 | std::string getPassesString(const std::vector<const PassInfo*> &Passes); |
| 165 | |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 166 | #endif |