xref: /freebsd/contrib/llvm-project/llvm/tools/bugpoint/BugDriver.h (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
10b57cec5SDimitry Andric //===- BugDriver.h - Top-Level BugPoint class -------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This class contains all of the shared state and information that is used by
100b57cec5SDimitry Andric // the BugPoint tool to track down errors in optimizations.  This class is the
110b57cec5SDimitry Andric // main driver class that invokes all sub-functionality.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #ifndef LLVM_TOOLS_BUGPOINT_BUGDRIVER_H
160b57cec5SDimitry Andric #define LLVM_TOOLS_BUGPOINT_BUGDRIVER_H
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric #include "llvm/IR/ValueMap.h"
190b57cec5SDimitry Andric #include "llvm/Support/Error.h"
200b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
210b57cec5SDimitry Andric #include "llvm/Transforms/Utils/ValueMapper.h"
220b57cec5SDimitry Andric #include <memory>
230b57cec5SDimitry Andric #include <string>
240b57cec5SDimitry Andric #include <vector>
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric namespace llvm {
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric class Module;
290b57cec5SDimitry Andric class GlobalVariable;
300b57cec5SDimitry Andric class Function;
310b57cec5SDimitry Andric class BasicBlock;
320b57cec5SDimitry Andric class AbstractInterpreter;
330b57cec5SDimitry Andric class Instruction;
340b57cec5SDimitry Andric class LLVMContext;
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric class CC;
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric extern bool DisableSimplifyCFG;
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric /// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
410b57cec5SDimitry Andric ///
420b57cec5SDimitry Andric extern bool BugpointIsInterrupted;
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric class BugDriver {
450b57cec5SDimitry Andric   LLVMContext &Context;
460b57cec5SDimitry Andric   const char *ToolName;            // argv[0] of bugpoint
470b57cec5SDimitry Andric   std::string ReferenceOutputFile; // Name of `good' output file
480b57cec5SDimitry Andric   std::unique_ptr<Module> Program; // The raw program, linked together
490b57cec5SDimitry Andric   std::vector<std::string> PassesToRun;
500b57cec5SDimitry Andric   AbstractInterpreter *Interpreter;     // How to run the program
510b57cec5SDimitry Andric   AbstractInterpreter *SafeInterpreter; // To generate reference output, etc.
520b57cec5SDimitry Andric   CC *cc;
530b57cec5SDimitry Andric   bool run_find_bugs;
540b57cec5SDimitry Andric   unsigned Timeout;
550b57cec5SDimitry Andric   unsigned MemoryLimit;
560b57cec5SDimitry Andric   bool UseValgrind;
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric   // FIXME: sort out public/private distinctions...
590b57cec5SDimitry Andric   friend class ReducePassList;
600b57cec5SDimitry Andric   friend class ReduceMisCodegenFunctions;
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric public:
630b57cec5SDimitry Andric   BugDriver(const char *toolname, bool find_bugs, unsigned timeout,
640b57cec5SDimitry Andric             unsigned memlimit, bool use_valgrind, LLVMContext &ctxt);
650b57cec5SDimitry Andric   ~BugDriver();
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric   const char *getToolName() const { return ToolName; }
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric   LLVMContext &getContext() const { return Context; }
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric   // Set up methods... these methods are used to copy information about the
720b57cec5SDimitry Andric   // command line arguments into instance variables of BugDriver.
730b57cec5SDimitry Andric   //
740b57cec5SDimitry Andric   bool addSources(const std::vector<std::string> &FileNames);
750b57cec5SDimitry Andric   void addPass(std::string p) { PassesToRun.push_back(std::move(p)); }
760b57cec5SDimitry Andric   void setPassesToRun(const std::vector<std::string> &PTR) {
770b57cec5SDimitry Andric     PassesToRun = PTR;
780b57cec5SDimitry Andric   }
790b57cec5SDimitry Andric   const std::vector<std::string> &getPassesToRun() const { return PassesToRun; }
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric   /// run - The top level method that is invoked after all of the instance
820b57cec5SDimitry Andric   /// variables are set up from command line arguments. The \p as_child argument
830b57cec5SDimitry Andric   /// indicates whether the driver is to run in parent mode or child mode.
840b57cec5SDimitry Andric   ///
850b57cec5SDimitry Andric   Error run();
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric   /// debugOptimizerCrash - This method is called when some optimizer pass
880b57cec5SDimitry Andric   /// crashes on input.  It attempts to prune down the testcase to something
890b57cec5SDimitry Andric   /// reasonable, and figure out exactly which pass is crashing.
900b57cec5SDimitry Andric   ///
910b57cec5SDimitry Andric   Error debugOptimizerCrash(const std::string &ID = "passes");
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric   /// debugCodeGeneratorCrash - This method is called when the code generator
940b57cec5SDimitry Andric   /// crashes on an input.  It attempts to reduce the input as much as possible
950b57cec5SDimitry Andric   /// while still causing the code generator to crash.
960b57cec5SDimitry Andric   Error debugCodeGeneratorCrash();
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric   /// debugMiscompilation - This method is used when the passes selected are not
990b57cec5SDimitry Andric   /// crashing, but the generated output is semantically different from the
1000b57cec5SDimitry Andric   /// input.
1010b57cec5SDimitry Andric   Error debugMiscompilation();
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric   /// compileSharedObject - This method creates a SharedObject from a given
1040b57cec5SDimitry Andric   /// BitcodeFile for debugging a code generator.
1050b57cec5SDimitry Andric   ///
1060b57cec5SDimitry Andric   Expected<std::string> compileSharedObject(const std::string &BitcodeFile);
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric   /// debugCodeGenerator - This method narrows down a module to a function or
1090b57cec5SDimitry Andric   /// set of functions, using the CBE as a ``safe'' code generator for other
1100b57cec5SDimitry Andric   /// functions that are not under consideration.
1110b57cec5SDimitry Andric   Error debugCodeGenerator();
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric   /// isExecutingJIT - Returns true if bugpoint is currently testing the JIT
1140b57cec5SDimitry Andric   ///
1150b57cec5SDimitry Andric   bool isExecutingJIT();
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric   Module &getProgram() const { return *Program; }
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric   /// Set the current module to the specified module, returning the old one.
1200b57cec5SDimitry Andric   std::unique_ptr<Module> swapProgramIn(std::unique_ptr<Module> M);
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric   AbstractInterpreter *switchToSafeInterpreter() {
1230b57cec5SDimitry Andric     AbstractInterpreter *Old = Interpreter;
1240b57cec5SDimitry Andric     Interpreter = (AbstractInterpreter *)SafeInterpreter;
1250b57cec5SDimitry Andric     return Old;
1260b57cec5SDimitry Andric   }
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric   void switchToInterpreter(AbstractInterpreter *AI) { Interpreter = AI; }
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric   /// If we reduce or update the program somehow, call this method to update
1310b57cec5SDimitry Andric   /// bugdriver with it.  This deletes the old module and sets the specified one
1320b57cec5SDimitry Andric   /// as the current program.
1330b57cec5SDimitry Andric   void setNewProgram(std::unique_ptr<Module> M);
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric   /// Try to compile the specified module. This is used for code generation
1360b57cec5SDimitry Andric   /// crash testing.
1370b57cec5SDimitry Andric   Error compileProgram(Module &M) const;
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric   /// This method runs "Program", capturing the output of the program to a file.
1400b57cec5SDimitry Andric   /// A recommended filename may be optionally specified.
1410b57cec5SDimitry Andric   Expected<std::string> executeProgram(const Module &Program,
1420b57cec5SDimitry Andric                                        std::string OutputFilename,
1430b57cec5SDimitry Andric                                        std::string Bitcode,
1440b57cec5SDimitry Andric                                        const std::string &SharedObjects,
1450b57cec5SDimitry Andric                                        AbstractInterpreter *AI) const;
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric   /// Used to create reference output with the "safe" backend, if reference
1480b57cec5SDimitry Andric   /// output is not provided.  If there is a problem with the code generator
1490b57cec5SDimitry Andric   /// (e.g., llc crashes), this will return false and set Error.
1500b57cec5SDimitry Andric   Expected<std::string>
1510b57cec5SDimitry Andric   executeProgramSafely(const Module &Program,
1520b57cec5SDimitry Andric                        const std::string &OutputFile) const;
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric   /// Calls compileProgram and then records the output into ReferenceOutputFile.
1550b57cec5SDimitry Andric   /// Returns true if reference file created, false otherwise. Note:
1560b57cec5SDimitry Andric   /// initializeExecutionEnvironment should be called BEFORE this function.
1570b57cec5SDimitry Andric   Error createReferenceFile(Module &M, const std::string &Filename =
1580b57cec5SDimitry Andric                                            "bugpoint.reference.out-%%%%%%%");
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric   /// This method executes the specified module and diffs the output against the
1610b57cec5SDimitry Andric   /// file specified by ReferenceOutputFile.  If the output is different, 1 is
1620b57cec5SDimitry Andric   /// returned.  If there is a problem with the code generator (e.g., llc
1630b57cec5SDimitry Andric   /// crashes), this will return -1 and set Error.
1640b57cec5SDimitry Andric   Expected<bool> diffProgram(const Module &Program,
1650b57cec5SDimitry Andric                              const std::string &BitcodeFile = "",
1660b57cec5SDimitry Andric                              const std::string &SharedObj = "",
1670b57cec5SDimitry Andric                              bool RemoveBitcode = false) const;
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric   /// This function is used to output M to a file named "bugpoint-ID.bc".
1700b57cec5SDimitry Andric   void EmitProgressBitcode(const Module &M, const std::string &ID,
1710b57cec5SDimitry Andric                            bool NoFlyer = false) const;
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric   /// This method clones the current Program and deletes the specified
1740b57cec5SDimitry Andric   /// instruction from the cloned module.  It then runs a series of cleanup
1750b57cec5SDimitry Andric   /// passes (ADCE and SimplifyCFG) to eliminate any code which depends on the
1760b57cec5SDimitry Andric   /// value. The modified module is then returned.
1770b57cec5SDimitry Andric   ///
1780b57cec5SDimitry Andric   std::unique_ptr<Module> deleteInstructionFromProgram(const Instruction *I,
1790b57cec5SDimitry Andric                                                        unsigned Simp);
1800b57cec5SDimitry Andric 
1810b57cec5SDimitry Andric   /// This method clones the current Program and performs a series of cleanups
1820b57cec5SDimitry Andric   /// intended to get rid of extra cruft on the module. If the
1830b57cec5SDimitry Andric   /// MayModifySemantics argument is true, then the cleanups is allowed to
1840b57cec5SDimitry Andric   /// modify how the code behaves.
1850b57cec5SDimitry Andric   ///
1860b57cec5SDimitry Andric   std::unique_ptr<Module> performFinalCleanups(std::unique_ptr<Module> M,
1870b57cec5SDimitry Andric                                                bool MayModifySemantics = false);
1880b57cec5SDimitry Andric 
1890b57cec5SDimitry Andric   /// Given a module, extract up to one loop from it into a new function. This
1900b57cec5SDimitry Andric   /// returns null if there are no extractable loops in the program or if the
1910b57cec5SDimitry Andric   /// loop extractor crashes.
1920b57cec5SDimitry Andric   std::unique_ptr<Module> extractLoop(Module *M);
1930b57cec5SDimitry Andric 
1940b57cec5SDimitry Andric   /// Extract all but the specified basic blocks into their own functions. The
1950b57cec5SDimitry Andric   /// only detail is that M is actually a module cloned from the one the BBs are
1960b57cec5SDimitry Andric   /// in, so some mapping needs to be performed. If this operation fails for
1970b57cec5SDimitry Andric   /// some reason (ie the implementation is buggy), this function should return
1980b57cec5SDimitry Andric   /// null, otherwise it returns a new Module.
1990b57cec5SDimitry Andric   std::unique_ptr<Module>
2000b57cec5SDimitry Andric   extractMappedBlocksFromModule(const std::vector<BasicBlock *> &BBs,
2010b57cec5SDimitry Andric                                 Module *M);
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric   /// Carefully run the specified set of pass on the specified/ module,
2040b57cec5SDimitry Andric   /// returning the transformed module on success, or a null pointer on failure.
2050b57cec5SDimitry Andric   std::unique_ptr<Module> runPassesOn(Module *M,
2060b57cec5SDimitry Andric                                       const std::vector<std::string> &Passes,
207*8bcb0991SDimitry Andric                                       ArrayRef<std::string> ExtraArgs = {});
2080b57cec5SDimitry Andric 
2090b57cec5SDimitry Andric   /// runPasses - Run the specified passes on Program, outputting a bitcode
2100b57cec5SDimitry Andric   /// file and writting the filename into OutputFile if successful.  If the
2110b57cec5SDimitry Andric   /// optimizations fail for some reason (optimizer crashes), return true,
2120b57cec5SDimitry Andric   /// otherwise return false.  If DeleteOutput is set to true, the bitcode is
2130b57cec5SDimitry Andric   /// deleted on success, and the filename string is undefined.  This prints to
2140b57cec5SDimitry Andric   /// outs() a single line message indicating whether compilation was successful
2150b57cec5SDimitry Andric   /// or failed, unless Quiet is set.  ExtraArgs specifies additional arguments
2160b57cec5SDimitry Andric   /// to pass to the child bugpoint instance.
2170b57cec5SDimitry Andric   ///
2180b57cec5SDimitry Andric   bool runPasses(Module &Program, const std::vector<std::string> &PassesToRun,
2190b57cec5SDimitry Andric                  std::string &OutputFilename, bool DeleteOutput = false,
220*8bcb0991SDimitry Andric                  bool Quiet = false,
221*8bcb0991SDimitry Andric                  ArrayRef<std::string> ExtraArgs = {}) const;
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric   /// runPasses - Just like the method above, but this just returns true or
2240b57cec5SDimitry Andric   /// false indicating whether or not the optimizer crashed on the specified
2250b57cec5SDimitry Andric   /// input (true = crashed).  Does not produce any output.
2260b57cec5SDimitry Andric   ///
2270b57cec5SDimitry Andric   bool runPasses(Module &M, const std::vector<std::string> &PassesToRun) const {
2280b57cec5SDimitry Andric     std::string Filename;
2290b57cec5SDimitry Andric     return runPasses(M, PassesToRun, Filename, true);
2300b57cec5SDimitry Andric   }
2310b57cec5SDimitry Andric 
2320b57cec5SDimitry Andric   /// Take the specified pass list and create different combinations of passes
2330b57cec5SDimitry Andric   /// to compile the program with. Compile the program with each set and mark
2340b57cec5SDimitry Andric   /// test to see if it compiled correctly. If the passes compiled correctly
2350b57cec5SDimitry Andric   /// output nothing and rearrange the passes into a new order. If the passes
2360b57cec5SDimitry Andric   /// did not compile correctly, output the command required to recreate the
2370b57cec5SDimitry Andric   /// failure.
2380b57cec5SDimitry Andric   Error runManyPasses(const std::vector<std::string> &AllPasses);
2390b57cec5SDimitry Andric 
2400b57cec5SDimitry Andric   /// This writes the current "Program" to the named bitcode file.  If an error
2410b57cec5SDimitry Andric   /// occurs, true is returned.
2420b57cec5SDimitry Andric   bool writeProgramToFile(const std::string &Filename, const Module &M) const;
2430b57cec5SDimitry Andric   bool writeProgramToFile(const std::string &Filename, int FD,
2440b57cec5SDimitry Andric                           const Module &M) const;
2450b57cec5SDimitry Andric   bool writeProgramToFile(int FD, const Module &M) const;
2460b57cec5SDimitry Andric 
2470b57cec5SDimitry Andric private:
2480b57cec5SDimitry Andric   /// initializeExecutionEnvironment - This method is used to set up the
2490b57cec5SDimitry Andric   /// environment for executing LLVM programs.
2500b57cec5SDimitry Andric   ///
2510b57cec5SDimitry Andric   Error initializeExecutionEnvironment();
2520b57cec5SDimitry Andric };
2530b57cec5SDimitry Andric 
2540b57cec5SDimitry Andric struct DiscardTemp {
2550b57cec5SDimitry Andric   sys::fs::TempFile &File;
2560b57cec5SDimitry Andric   ~DiscardTemp();
2570b57cec5SDimitry Andric };
2580b57cec5SDimitry Andric 
2590b57cec5SDimitry Andric ///  Given a bitcode or assembly input filename, parse and return it, or return
2600b57cec5SDimitry Andric ///  null if not possible.
2610b57cec5SDimitry Andric ///
2620b57cec5SDimitry Andric std::unique_ptr<Module> parseInputFile(StringRef InputFilename,
2630b57cec5SDimitry Andric                                        LLVMContext &ctxt);
2640b57cec5SDimitry Andric 
2650b57cec5SDimitry Andric /// getPassesString - Turn a list of passes into a string which indicates the
2660b57cec5SDimitry Andric /// command line options that must be passed to add the passes.
2670b57cec5SDimitry Andric ///
2680b57cec5SDimitry Andric std::string getPassesString(const std::vector<std::string> &Passes);
2690b57cec5SDimitry Andric 
2700b57cec5SDimitry Andric /// PrintFunctionList - prints out list of problematic functions
2710b57cec5SDimitry Andric ///
2720b57cec5SDimitry Andric void PrintFunctionList(const std::vector<Function *> &Funcs);
2730b57cec5SDimitry Andric 
2740b57cec5SDimitry Andric /// PrintGlobalVariableList - prints out list of problematic global variables
2750b57cec5SDimitry Andric ///
2760b57cec5SDimitry Andric void PrintGlobalVariableList(const std::vector<GlobalVariable *> &GVs);
2770b57cec5SDimitry Andric 
2780b57cec5SDimitry Andric // DeleteGlobalInitializer - "Remove" the global variable by deleting its
2790b57cec5SDimitry Andric // initializer, making it external.
2800b57cec5SDimitry Andric //
2810b57cec5SDimitry Andric void DeleteGlobalInitializer(GlobalVariable *GV);
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric // DeleteFunctionBody - "Remove" the function by deleting all of it's basic
2840b57cec5SDimitry Andric // blocks, making it external.
2850b57cec5SDimitry Andric //
2860b57cec5SDimitry Andric void DeleteFunctionBody(Function *F);
2870b57cec5SDimitry Andric 
2880b57cec5SDimitry Andric /// Given a module and a list of functions in the module, split the functions
2890b57cec5SDimitry Andric /// OUT of the specified module, and place them in the new module.
2900b57cec5SDimitry Andric std::unique_ptr<Module>
2910b57cec5SDimitry Andric SplitFunctionsOutOfModule(Module *M, const std::vector<Function *> &F,
2920b57cec5SDimitry Andric                           ValueToValueMapTy &VMap);
2930b57cec5SDimitry Andric 
2940b57cec5SDimitry Andric } // End llvm namespace
2950b57cec5SDimitry Andric 
2960b57cec5SDimitry Andric #endif
297