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