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