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