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