BugDriver.h revision 769f1fe6728ffb5627ae0cedc392576d6e701a5a
17765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org//===- BugDriver.h - Top-Level BugPoint class -------------------*- C++ -*-===// 27765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org// 37765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org// This class contains all of the shared state and information that is used by 47765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org// the BugPoint tool to track down errors in optimizations. This class is the 57765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org// main driver class that invokes all sub-functionality. 67765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org// 77765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org//===----------------------------------------------------------------------===// 87765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org 97765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org#ifndef BUGDRIVER_H 107765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org#define BUGDRIVER_H 117765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org 127765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org#include <vector> 137765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org#include <string> 147765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org 157765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.orgclass PassInfo; 167765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.orgclass Module; 177765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.orgclass Function; 187765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.orgclass AbstractInterpreter; 197765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.orgclass Instruction; 207765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org 217765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.orgclass DebugCrashes; 227765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.orgclass ReduceMiscompilingPasses; 237765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.orgclass ReduceMiscompilingFunctions; 247765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.orgclass ReduceCrashingFunctions; 257765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.orgclass ReduceCrashingBlocks; 267765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org 277765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.orgclass CBE; 287765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.orgclass GCC; 297765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org 307765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.orgextern bool DisableSimplifyCFG; 317765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org 327765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.orgclass BugDriver { 337765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org const std::string ToolName; // Name of bugpoint 347765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org std::string ReferenceOutputFile; // Name of `good' output file 357765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org Module *Program; // The raw program, linked together 367765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org std::vector<const PassInfo*> PassesToRun; 377765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org AbstractInterpreter *Interpreter; // How to run the program 387765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org CBE *cbe; 397765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org GCC *gcc; 407765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org 417765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org // FIXME: sort out public/private distinctions... 427765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org friend class DebugCrashes; 437765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org friend class ReduceMiscompilingPasses; 447765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org friend class ReduceMiscompilingFunctions; 457765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org friend class ReduceMisCodegenFunctions; 467765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org friend class ReduceCrashingFunctions; 477765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org friend class ReduceCrashingBlocks; 487765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org 497765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.orgpublic: 507765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org BugDriver(const char *toolname); 517765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org 527765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org const std::string &getToolName() const { return ToolName; } 537765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org 547765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org // Set up methods... these methods are used to copy information about the 557765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org // command line arguments into instance variables of BugDriver. 567765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org // 577765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org bool addSources(const std::vector<std::string> &FileNames); 587765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org template<class It> 597765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org void addPasses(It I, It E) { PassesToRun.insert(PassesToRun.end(), I, E); } 607765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org 617765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org /// run - The top level method that is invoked after all of the instance 627765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org /// variables are set up from command line arguments. 637765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org /// 647765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org bool run(); 657765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org 667765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org /// debugCrash - This method is called when some pass crashes on input. It 677765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org /// attempts to prune down the testcase to something reasonable, and figure 687765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org /// out exactly which pass is crashing. 697765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org /// 707765c078fa920ba6c949c15f16b6cc979d8bb95bjohannkoenig@chromium.org bool debugCrash(); 71 72 /// debugMiscompilation - This method is used when the passes selected are not 73 /// crashing, but the generated output is semantically different from the 74 /// input. 75 bool debugMiscompilation(); 76 77 /// debugPassMiscompilation - This method is called when the specified pass 78 /// miscompiles Program as input. It tries to reduce the testcase to 79 /// something that smaller that still miscompiles the program. 80 /// ReferenceOutput contains the filename of the file containing the output we 81 /// are to match. 82 /// 83 bool debugPassMiscompilation(const PassInfo *ThePass, 84 const std::string &ReferenceOutput); 85 86 /// compileSharedObject - This method creates a SharedObject from a given 87 /// BytecodeFile for debugging a code generator. 88 /// 89 std::string compileSharedObject(const std::string &BytecodeFile); 90 91 /// debugCodeGenerator - This method narrows down a module to a function or 92 /// set of functions, using the CBE as a ``safe'' code generator for other 93 /// functions that are not under consideration. 94 bool debugCodeGenerator(); 95 96 /// isExecutingJIT - Returns true if bugpoint is currently testing the JIT 97 /// 98 bool isExecutingJIT(); 99 100private: 101 /// ParseInputFile - Given a bytecode or assembly input filename, parse and 102 /// return it, or return null if not possible. 103 /// 104 Module *ParseInputFile(const std::string &InputFilename) const; 105 106 /// writeProgramToFile - This writes the current "Program" to the named 107 /// bytecode file. If an error occurs, true is returned. 108 /// 109 bool writeProgramToFile(const std::string &Filename, Module *M = 0) const; 110 111 112 /// EmitProgressBytecode - This function is used to output the current Program 113 /// to a file named "bugpoing-ID.bc". 114 /// 115 void EmitProgressBytecode(const std::string &ID, bool NoFlyer = false); 116 117 /// runPasses - Run the specified passes on Program, outputting a bytecode 118 /// file and writting the filename into OutputFile if successful. If the 119 /// optimizations fail for some reason (optimizer crashes), return true, 120 /// otherwise return false. If DeleteOutput is set to true, the bytecode is 121 /// deleted on success, and the filename string is undefined. This prints to 122 /// cout a single line message indicating whether compilation was successful 123 /// or failed, unless Quiet is set. 124 /// 125 bool runPasses(const std::vector<const PassInfo*> &PassesToRun, 126 std::string &OutputFilename, bool DeleteOutput = false, 127 bool Quiet = false) const; 128 129 /// runPasses - Just like the method above, but this just returns true or 130 /// false indicating whether or not the optimizer crashed on the specified 131 /// input (true = crashed). 132 /// 133 bool runPasses(const std::vector<const PassInfo*> &PassesToRun, 134 bool DeleteOutput = true) const { 135 std::string Filename; 136 return runPasses(PassesToRun, Filename, DeleteOutput); 137 } 138 139 /// PrintFunctionList - prints out list of problematic functions 140 /// 141 static void PrintFunctionList(const std::vector<Function*> &Funcs); 142 143 /// deleteInstructionFromProgram - This method clones the current Program and 144 /// deletes the specified instruction from the cloned module. It then runs a 145 /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code 146 /// which depends on the value. The modified module is then returned. 147 /// 148 Module *deleteInstructionFromProgram(Instruction *I, unsigned Simp) const; 149 150 /// performFinalCleanups - This method clones the current Program and performs 151 /// a series of cleanups intended to get rid of extra cruft on the module 152 /// before handing it to the user... if the module parameter is specified, it 153 /// operates directly on the specified Module, modifying it in place. 154 /// 155 Module *performFinalCleanups(Module *M = 0) const; 156 157 /// initializeExecutionEnvironment - This method is used to set up the 158 /// environment for executing LLVM programs. 159 /// 160 bool initializeExecutionEnvironment(); 161 162 /// executeProgram - This method runs "Program", capturing the output of the 163 /// program to a file, returning the filename of the file. A recommended 164 /// filename may be optionally specified. 165 /// 166 std::string executeProgram(std::string RequestedOutputFilename = "", 167 std::string Bytecode = "", 168 const std::string &SharedObjects = "", 169 AbstractInterpreter *AI = 0); 170 171 /// executeProgramWithCBE - Used to create reference output with the C 172 /// backend, if reference output is not provided. 173 /// 174 std::string executeProgramWithCBE(std::string OutputFile = "", 175 std::string BytecodeFile = "", 176 const std::string &SharedObj = "") { 177 return executeProgram(OutputFile, BytecodeFile, SharedObj, 178 (AbstractInterpreter*)cbe); 179 } 180 181 /// diffProgram - This method executes the specified module and diffs the 182 /// output against the file specified by ReferenceOutputFile. If the output 183 /// is different, true is returned. 184 /// 185 bool diffProgram(const std::string &BytecodeFile = "", 186 const std::string &SharedObj = "", 187 bool RemoveBytecode = false); 188}; 189 190/// getPassesString - Turn a list of passes into a string which indicates the 191/// command line options that must be passed to add the passes. 192/// 193std::string getPassesString(const std::vector<const PassInfo*> &Passes); 194 195// DeleteFunctionBody - "Remove" the function by deleting all of it's basic 196// blocks, making it external. 197// 198void DeleteFunctionBody(Function *F); 199 200#endif 201