1217309Snwhitehorn//===- BugDriver.h - Top-Level BugPoint class -------------------*- C++ -*-===// 2217309Snwhitehorn// 3217309Snwhitehorn// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4217309Snwhitehorn// See https://llvm.org/LICENSE.txt for license information. 5224014Snwhitehorn// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6217309Snwhitehorn// 7224014Snwhitehorn//===----------------------------------------------------------------------===// 8217309Snwhitehorn// 9217309Snwhitehorn// This class contains all of the shared state and information that is used by 10217309Snwhitehorn// the BugPoint tool to track down errors in optimizations. This class is the 11217309Snwhitehorn// main driver class that invokes all sub-functionality. 12217309Snwhitehorn// 13217309Snwhitehorn//===----------------------------------------------------------------------===// 14217309Snwhitehorn 15217309Snwhitehorn#ifndef LLVM_TOOLS_BUGPOINT_BUGDRIVER_H 16217309Snwhitehorn#define LLVM_TOOLS_BUGPOINT_BUGDRIVER_H 17217309Snwhitehorn 18217309Snwhitehorn#include "llvm/IR/ValueMap.h" 19217309Snwhitehorn#include "llvm/Support/Error.h" 20217309Snwhitehorn#include "llvm/Support/FileSystem.h" 21217309Snwhitehorn#include "llvm/Transforms/Utils/ValueMapper.h" 22217309Snwhitehorn#include <memory> 23217309Snwhitehorn#include <string> 24217309Snwhitehorn#include <vector> 25217309Snwhitehorn 26217309Snwhitehornnamespace llvm { 27217309Snwhitehorn 28217309Snwhitehornclass Module; 29217309Snwhitehornclass GlobalVariable; 30217309Snwhitehornclass Function; 31217309Snwhitehornclass BasicBlock; 32217309Snwhitehornclass AbstractInterpreter; 33217309Snwhitehornclass Instruction; 34217309Snwhitehornclass LLVMContext; 35217309Snwhitehorn 36217309Snwhitehornclass CC; 37217309Snwhitehorn 38217309Snwhitehornextern bool DisableSimplifyCFG; 39217309Snwhitehorn 40217309Snwhitehorn/// BugpointIsInterrupted - Set to true when the user presses ctrl-c. 41217309Snwhitehorn/// 42217309Snwhitehornextern bool BugpointIsInterrupted; 43217309Snwhitehorn 44217309Snwhitehornclass BugDriver { 45217309Snwhitehorn LLVMContext &Context; 46217309Snwhitehorn const char *ToolName; // argv[0] of bugpoint 47217309Snwhitehorn std::string ReferenceOutputFile; // Name of `good' output file 48217309Snwhitehorn std::unique_ptr<Module> Program; // The raw program, linked together 49217309Snwhitehorn std::vector<std::string> PassesToRun; 50217309Snwhitehorn AbstractInterpreter *Interpreter; // How to run the program 51217309Snwhitehorn AbstractInterpreter *SafeInterpreter; // To generate reference output, etc. 52217309Snwhitehorn CC *cc; 53217309Snwhitehorn bool run_find_bugs; 54217309Snwhitehorn unsigned Timeout; 55217309Snwhitehorn unsigned MemoryLimit; 56217309Snwhitehorn bool UseValgrind; 57217309Snwhitehorn 58217309Snwhitehorn // FIXME: sort out public/private distinctions... 59217309Snwhitehorn friend class ReducePassList; 60217309Snwhitehorn friend class ReduceMisCodegenFunctions; 61217309Snwhitehorn 62217309Snwhitehornpublic: 63217309Snwhitehorn BugDriver(const char *toolname, bool find_bugs, unsigned timeout, 64217309Snwhitehorn unsigned memlimit, bool use_valgrind, LLVMContext &ctxt); 65217309Snwhitehorn ~BugDriver(); 66217309Snwhitehorn 67217309Snwhitehorn const char *getToolName() const { return ToolName; } 68217309Snwhitehorn 69217309Snwhitehorn LLVMContext &getContext() const { return Context; } 70217309Snwhitehorn 71217309Snwhitehorn // Set up methods... these methods are used to copy information about the 72217309Snwhitehorn // command line arguments into instance variables of BugDriver. 73217309Snwhitehorn // 74217309Snwhitehorn bool addSources(const std::vector<std::string> &FileNames); 75217309Snwhitehorn void addPass(std::string p) { PassesToRun.push_back(std::move(p)); } 76217309Snwhitehorn void setPassesToRun(const std::vector<std::string> &PTR) { 77217309Snwhitehorn PassesToRun = PTR; 78217309Snwhitehorn } 79224014Snwhitehorn const std::vector<std::string> &getPassesToRun() const { return PassesToRun; } 80224014Snwhitehorn 81217309Snwhitehorn /// run - The top level method that is invoked after all of the instance 82217309Snwhitehorn /// variables are set up from command line arguments. The \p as_child argument 83217309Snwhitehorn /// indicates whether the driver is to run in parent mode or child mode. 84217309Snwhitehorn /// 85217309Snwhitehorn Error run(); 86217309Snwhitehorn 87217309Snwhitehorn /// debugOptimizerCrash - This method is called when some optimizer pass 88217309Snwhitehorn /// crashes on input. It attempts to prune down the testcase to something 89217309Snwhitehorn /// reasonable, and figure out exactly which pass is crashing. 90217309Snwhitehorn /// 91217309Snwhitehorn Error debugOptimizerCrash(const std::string &ID = "passes"); 92217309Snwhitehorn 93217309Snwhitehorn /// debugCodeGeneratorCrash - This method is called when the code generator 94217309Snwhitehorn /// crashes on an input. It attempts to reduce the input as much as possible 95217309Snwhitehorn /// while still causing the code generator to crash. 96217309Snwhitehorn Error debugCodeGeneratorCrash(); 97217309Snwhitehorn 98217309Snwhitehorn /// debugMiscompilation - This method is used when the passes selected are not 99217309Snwhitehorn /// crashing, but the generated output is semantically different from the 100217309Snwhitehorn /// input. 101217309Snwhitehorn Error debugMiscompilation(); 102217309Snwhitehorn 103217309Snwhitehorn /// compileSharedObject - This method creates a SharedObject from a given 104217309Snwhitehorn /// BitcodeFile for debugging a code generator. 105217309Snwhitehorn /// 106217309Snwhitehorn Expected<std::string> compileSharedObject(const std::string &BitcodeFile); 107217309Snwhitehorn 108217309Snwhitehorn /// debugCodeGenerator - This method narrows down a module to a function or 109217309Snwhitehorn /// set of functions, using the CBE as a ``safe'' code generator for other 110217309Snwhitehorn /// functions that are not under consideration. 111217309Snwhitehorn Error debugCodeGenerator(); 112217309Snwhitehorn 113217309Snwhitehorn /// isExecutingJIT - Returns true if bugpoint is currently testing the JIT 114217309Snwhitehorn /// 115217309Snwhitehorn bool isExecutingJIT(); 116217309Snwhitehorn 117217309Snwhitehorn Module &getProgram() const { return *Program; } 118217309Snwhitehorn 119217309Snwhitehorn /// Set the current module to the specified module, returning the old one. 120217309Snwhitehorn std::unique_ptr<Module> swapProgramIn(std::unique_ptr<Module> M); 121217309Snwhitehorn 122217309Snwhitehorn AbstractInterpreter *switchToSafeInterpreter() { 123217309Snwhitehorn AbstractInterpreter *Old = Interpreter; 124217309Snwhitehorn Interpreter = (AbstractInterpreter *)SafeInterpreter; 125217309Snwhitehorn return Old; 126217309Snwhitehorn } 127220749Snwhitehorn 128220749Snwhitehorn void switchToInterpreter(AbstractInterpreter *AI) { Interpreter = AI; } 129220749Snwhitehorn 130217309Snwhitehorn /// If we reduce or update the program somehow, call this method to update 131217309Snwhitehorn /// bugdriver with it. This deletes the old module and sets the specified one 132217309Snwhitehorn /// as the current program. 133217309Snwhitehorn void setNewProgram(std::unique_ptr<Module> M); 134217309Snwhitehorn 135217309Snwhitehorn /// Try to compile the specified module. This is used for code generation 136217309Snwhitehorn /// crash testing. 137217309Snwhitehorn Error compileProgram(Module &M) const; 138217309Snwhitehorn 139217309Snwhitehorn /// This method runs "Program", capturing the output of the program to a file. 140217309Snwhitehorn /// A recommended filename may be optionally specified. 141217309Snwhitehorn Expected<std::string> executeProgram(const Module &Program, 142217309Snwhitehorn std::string OutputFilename, 143217309Snwhitehorn std::string Bitcode, 144217309Snwhitehorn const std::string &SharedObjects, 145217309Snwhitehorn AbstractInterpreter *AI) const; 146217309Snwhitehorn 147217309Snwhitehorn /// Used to create reference output with the "safe" backend, if reference 148217309Snwhitehorn /// output is not provided. If there is a problem with the code generator 149217309Snwhitehorn /// (e.g., llc crashes), this will return false and set Error. 150217309Snwhitehorn Expected<std::string> 151217309Snwhitehorn executeProgramSafely(const Module &Program, 152217309Snwhitehorn const std::string &OutputFile) const; 153217309Snwhitehorn 154217309Snwhitehorn /// Calls compileProgram and then records the output into ReferenceOutputFile. 155217309Snwhitehorn /// Returns true if reference file created, false otherwise. Note: 156217309Snwhitehorn /// initializeExecutionEnvironment should be called BEFORE this function. 157217309Snwhitehorn Error createReferenceFile(Module &M, const std::string &Filename = 158217309Snwhitehorn "bugpoint.reference.out-%%%%%%%"); 159217309Snwhitehorn 160217309Snwhitehorn /// This method executes the specified module and diffs the output against the 161220749Snwhitehorn /// file specified by ReferenceOutputFile. If the output is different, 1 is 162220749Snwhitehorn /// returned. If there is a problem with the code generator (e.g., llc 163217309Snwhitehorn /// crashes), this will return -1 and set Error. 164217309Snwhitehorn Expected<bool> diffProgram(const Module &Program, 165217309Snwhitehorn const std::string &BitcodeFile = "", 166217309Snwhitehorn const std::string &SharedObj = "", 167217309Snwhitehorn bool RemoveBitcode = false) const; 168217309Snwhitehorn 169217309Snwhitehorn /// This function is used to output M to a file named "bugpoint-ID.bc". 170217309Snwhitehorn void EmitProgressBitcode(const Module &M, const std::string &ID, 171217309Snwhitehorn bool NoFlyer = false) const; 172217309Snwhitehorn 173217309Snwhitehorn /// This method clones the current Program and deletes the specified 174217309Snwhitehorn /// instruction from the cloned module. It then runs a series of cleanup 175217309Snwhitehorn /// passes (ADCE and SimplifyCFG) to eliminate any code which depends on the 176217309Snwhitehorn /// value. The modified module is then returned. 177217309Snwhitehorn /// 178224014Snwhitehorn std::unique_ptr<Module> deleteInstructionFromProgram(const Instruction *I, 179224014Snwhitehorn unsigned Simp); 180217309Snwhitehorn 181224014Snwhitehorn /// This method clones the current Program and performs a series of cleanups 182217309Snwhitehorn /// intended to get rid of extra cruft on the module. If the 183217309Snwhitehorn /// MayModifySemantics argument is true, then the cleanups is allowed to 184217309Snwhitehorn /// modify how the code behaves. 185217309Snwhitehorn /// 186217309Snwhitehorn std::unique_ptr<Module> performFinalCleanups(std::unique_ptr<Module> M, 187217309Snwhitehorn bool MayModifySemantics = false); 188217309Snwhitehorn 189217309Snwhitehorn /// Given a module, extract up to one loop from it into a new function. This 190217309Snwhitehorn /// returns null if there are no extractable loops in the program or if the 191217309Snwhitehorn /// loop extractor crashes. 192217309Snwhitehorn std::unique_ptr<Module> extractLoop(Module *M); 193217309Snwhitehorn 194217309Snwhitehorn /// Extract all but the specified basic blocks into their own functions. The 195217309Snwhitehorn /// only detail is that M is actually a module cloned from the one the BBs are 196217309Snwhitehorn /// in, so some mapping needs to be performed. If this operation fails for 197217309Snwhitehorn /// some reason (ie the implementation is buggy), this function should return 198217309Snwhitehorn /// null, otherwise it returns a new Module. 199217309Snwhitehorn std::unique_ptr<Module> 200217309Snwhitehorn extractMappedBlocksFromModule(const std::vector<BasicBlock *> &BBs, 201217309Snwhitehorn Module *M); 202217309Snwhitehorn 203217309Snwhitehorn /// Carefully run the specified set of pass on the specified/ module, 204217309Snwhitehorn /// returning the transformed module on success, or a null pointer on failure. 205217309Snwhitehorn std::unique_ptr<Module> runPassesOn(Module *M, 206217309Snwhitehorn const std::vector<std::string> &Passes, 207217309Snwhitehorn ArrayRef<std::string> ExtraArgs = {}); 208217309Snwhitehorn 209217309Snwhitehorn /// runPasses - Run the specified passes on Program, outputting a bitcode 210217309Snwhitehorn /// file and writting the filename into OutputFile if successful. If the 211217309Snwhitehorn /// optimizations fail for some reason (optimizer crashes), return true, 212217309Snwhitehorn /// otherwise return false. If DeleteOutput is set to true, the bitcode is 213217309Snwhitehorn /// deleted on success, and the filename string is undefined. This prints to 214217309Snwhitehorn /// outs() a single line message indicating whether compilation was successful 215217309Snwhitehorn /// or failed, unless Quiet is set. ExtraArgs specifies additional arguments 216217309Snwhitehorn /// to pass to the child bugpoint instance. 217217309Snwhitehorn /// 218217309Snwhitehorn bool runPasses(Module &Program, const std::vector<std::string> &PassesToRun, 219217309Snwhitehorn std::string &OutputFilename, bool DeleteOutput = false, 220217309Snwhitehorn bool Quiet = false, 221217309Snwhitehorn ArrayRef<std::string> ExtraArgs = {}) const; 222217309Snwhitehorn 223217309Snwhitehorn /// runPasses - Just like the method above, but this just returns true or 224217309Snwhitehorn /// false indicating whether or not the optimizer crashed on the specified 225217309Snwhitehorn /// input (true = crashed). Does not produce any output. 226217309Snwhitehorn /// 227217309Snwhitehorn bool runPasses(Module &M, const std::vector<std::string> &PassesToRun) const { 228217309Snwhitehorn std::string Filename; 229217309Snwhitehorn return runPasses(M, PassesToRun, Filename, true); 230217309Snwhitehorn } 231217309Snwhitehorn 232217309Snwhitehorn /// Take the specified pass list and create different combinations of passes 233217309Snwhitehorn /// to compile the program with. Compile the program with each set and mark 234217309Snwhitehorn /// test to see if it compiled correctly. If the passes compiled correctly 235217309Snwhitehorn /// output nothing and rearrange the passes into a new order. If the passes 236217309Snwhitehorn /// did not compile correctly, output the command required to recreate the 237217309Snwhitehorn /// failure. 238217309Snwhitehorn Error runManyPasses(const std::vector<std::string> &AllPasses); 239217309Snwhitehorn 240217309Snwhitehorn /// This writes the current "Program" to the named bitcode file. If an error 241217309Snwhitehorn /// occurs, true is returned. 242217309Snwhitehorn bool writeProgramToFile(const std::string &Filename, const Module &M) const; 243217309Snwhitehorn bool writeProgramToFile(const std::string &Filename, int FD, 244217309Snwhitehorn const Module &M) const; 245217309Snwhitehorn bool writeProgramToFile(int FD, const Module &M) const; 246217309Snwhitehorn 247217309Snwhitehornprivate: 248217309Snwhitehorn /// initializeExecutionEnvironment - This method is used to set up the 249217309Snwhitehorn /// environment for executing LLVM programs. 250217309Snwhitehorn /// 251217309Snwhitehorn Error initializeExecutionEnvironment(); 252217309Snwhitehorn}; 253217309Snwhitehorn 254217309Snwhitehornstruct DiscardTemp { 255217309Snwhitehorn sys::fs::TempFile &File; 256217309Snwhitehorn ~DiscardTemp(); 257217309Snwhitehorn}; 258217309Snwhitehorn 259217309Snwhitehorn/// Given a bitcode or assembly input filename, parse and return it, or return 260217309Snwhitehorn/// null if not possible. 261217309Snwhitehorn/// 262217309Snwhitehornstd::unique_ptr<Module> parseInputFile(StringRef InputFilename, 263217309Snwhitehorn LLVMContext &ctxt); 264217309Snwhitehorn 265217309Snwhitehorn/// getPassesString - Turn a list of passes into a string which indicates the 266217309Snwhitehorn/// command line options that must be passed to add the passes. 267217309Snwhitehorn/// 268217309Snwhitehornstd::string getPassesString(const std::vector<std::string> &Passes); 269217309Snwhitehorn 270217309Snwhitehorn/// PrintFunctionList - prints out list of problematic functions 271217309Snwhitehorn/// 272217309Snwhitehornvoid PrintFunctionList(const std::vector<Function *> &Funcs); 273217309Snwhitehorn 274217309Snwhitehorn/// PrintGlobalVariableList - prints out list of problematic global variables 275217309Snwhitehorn/// 276217309Snwhitehornvoid PrintGlobalVariableList(const std::vector<GlobalVariable *> &GVs); 277217309Snwhitehorn 278217309Snwhitehorn// DeleteGlobalInitializer - "Remove" the global variable by deleting its 279217309Snwhitehorn// initializer, making it external. 280217309Snwhitehorn// 281217309Snwhitehornvoid DeleteGlobalInitializer(GlobalVariable *GV); 282217309Snwhitehorn 283217309Snwhitehorn// DeleteFunctionBody - "Remove" the function by deleting all of it's basic 284217309Snwhitehorn// blocks, making it external. 285217309Snwhitehorn// 286224014Snwhitehornvoid DeleteFunctionBody(Function *F); 287217309Snwhitehorn 288217309Snwhitehorn/// Given a module and a list of functions in the module, split the functions 289224014Snwhitehorn/// OUT of the specified module, and place them in the new module. 290217309Snwhitehornstd::unique_ptr<Module> 291217309SnwhitehornSplitFunctionsOutOfModule(Module *M, const std::vector<Function *> &F, 292224014Snwhitehorn ValueToValueMapTy &VMap); 293217309Snwhitehorn 294217309Snwhitehorn} // End llvm namespace 295217309Snwhitehorn 296217309Snwhitehorn#endif 297217309Snwhitehorn