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