Home | History | Annotate | Download | only in bugpoint
      1 //===- BugDriver.h - Top-Level BugPoint class -------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      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 LLVM_TOOLS_BUGPOINT_BUGDRIVER_H
     17 #define LLVM_TOOLS_BUGPOINT_BUGDRIVER_H
     18 
     19 #include "llvm/IR/ValueMap.h"
     20 #include "llvm/Transforms/Utils/ValueMapper.h"
     21 #include <memory>
     22 #include <string>
     23 #include <vector>
     24 
     25 namespace llvm {
     26 
     27 class Value;
     28 class PassInfo;
     29 class Module;
     30 class GlobalVariable;
     31 class Function;
     32 class BasicBlock;
     33 class AbstractInterpreter;
     34 class Instruction;
     35 class LLVMContext;
     36 
     37 class DebugCrashes;
     38 
     39 class CC;
     40 
     41 extern bool DisableSimplifyCFG;
     42 
     43 /// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
     44 ///
     45 extern bool BugpointIsInterrupted;
     46 
     47 class BugDriver {
     48   LLVMContext& Context;
     49   const char *ToolName;            // argv[0] of bugpoint
     50   std::string ReferenceOutputFile; // Name of `good' output file
     51   Module *Program;             // The raw program, linked together
     52   std::vector<std::string> PassesToRun;
     53   AbstractInterpreter *Interpreter;   // How to run the program
     54   AbstractInterpreter *SafeInterpreter;  // To generate reference output, etc.
     55   CC *cc;
     56   bool run_find_bugs;
     57   unsigned Timeout;
     58   unsigned MemoryLimit;
     59   bool UseValgrind;
     60 
     61   // FIXME: sort out public/private distinctions...
     62   friend class ReducePassList;
     63   friend class ReduceMisCodegenFunctions;
     64 
     65 public:
     66   BugDriver(const char *toolname, bool find_bugs,
     67             unsigned timeout, unsigned memlimit, bool use_valgrind,
     68             LLVMContext& ctxt);
     69   ~BugDriver();
     70 
     71   const char *getToolName() const { return ToolName; }
     72 
     73   LLVMContext& getContext() const { return Context; }
     74 
     75   // Set up methods... these methods are used to copy information about the
     76   // command line arguments into instance variables of BugDriver.
     77   //
     78   bool addSources(const std::vector<std::string> &FileNames);
     79   void addPass(std::string p) { PassesToRun.push_back(std::move(p)); }
     80   void setPassesToRun(const std::vector<std::string> &PTR) {
     81     PassesToRun = PTR;
     82   }
     83   const std::vector<std::string> &getPassesToRun() const {
     84     return PassesToRun;
     85   }
     86 
     87   /// run - The top level method that is invoked after all of the instance
     88   /// variables are set up from command line arguments. The \p as_child argument
     89   /// indicates whether the driver is to run in parent mode or child mode.
     90   ///
     91   bool run(std::string &ErrMsg);
     92 
     93   /// debugOptimizerCrash - This method is called when some optimizer pass
     94   /// crashes on input.  It attempts to prune down the testcase to something
     95   /// reasonable, and figure out exactly which pass is crashing.
     96   ///
     97   bool debugOptimizerCrash(const std::string &ID = "passes");
     98 
     99   /// debugCodeGeneratorCrash - This method is called when the code generator
    100   /// crashes on an input.  It attempts to reduce the input as much as possible
    101   /// while still causing the code generator to crash.
    102   bool debugCodeGeneratorCrash(std::string &Error);
    103 
    104   /// debugMiscompilation - This method is used when the passes selected are not
    105   /// crashing, but the generated output is semantically different from the
    106   /// input.
    107   void debugMiscompilation(std::string *Error);
    108 
    109   /// debugPassMiscompilation - This method is called when the specified pass
    110   /// miscompiles Program as input.  It tries to reduce the testcase to
    111   /// something that smaller that still miscompiles the program.
    112   /// ReferenceOutput contains the filename of the file containing the output we
    113   /// are to match.
    114   ///
    115   bool debugPassMiscompilation(const PassInfo *ThePass,
    116                                const std::string &ReferenceOutput);
    117 
    118   /// compileSharedObject - This method creates a SharedObject from a given
    119   /// BitcodeFile for debugging a code generator.
    120   ///
    121   std::string compileSharedObject(const std::string &BitcodeFile,
    122                                   std::string &Error);
    123 
    124   /// debugCodeGenerator - This method narrows down a module to a function or
    125   /// set of functions, using the CBE as a ``safe'' code generator for other
    126   /// functions that are not under consideration.
    127   bool debugCodeGenerator(std::string *Error);
    128 
    129   /// isExecutingJIT - Returns true if bugpoint is currently testing the JIT
    130   ///
    131   bool isExecutingJIT();
    132 
    133   Module *getProgram() const { return Program; }
    134 
    135   /// swapProgramIn - Set the current module to the specified module, returning
    136   /// the old one.
    137   Module *swapProgramIn(Module *M) {
    138     Module *OldProgram = Program;
    139     Program = M;
    140     return OldProgram;
    141   }
    142 
    143   AbstractInterpreter *switchToSafeInterpreter() {
    144     AbstractInterpreter *Old = Interpreter;
    145     Interpreter = (AbstractInterpreter*)SafeInterpreter;
    146     return Old;
    147   }
    148 
    149   void switchToInterpreter(AbstractInterpreter *AI) {
    150     Interpreter = AI;
    151   }
    152 
    153   /// setNewProgram - If we reduce or update the program somehow, call this
    154   /// method to update bugdriver with it.  This deletes the old module and sets
    155   /// the specified one as the current program.
    156   void setNewProgram(Module *M);
    157 
    158   /// compileProgram - Try to compile the specified module, returning false and
    159   /// setting Error if an error occurs.  This is used for code generation
    160   /// crash testing.
    161   ///
    162   void compileProgram(Module *M, std::string *Error) const;
    163 
    164   /// executeProgram - This method runs "Program", capturing the output of the
    165   /// program to a file.  A recommended filename may be optionally specified.
    166   ///
    167   std::string executeProgram(const Module *Program,
    168                              std::string OutputFilename,
    169                              std::string Bitcode,
    170                              const std::string &SharedObjects,
    171                              AbstractInterpreter *AI,
    172                              std::string *Error) const;
    173 
    174   /// executeProgramSafely - Used to create reference output with the "safe"
    175   /// backend, if reference output is not provided.  If there is a problem with
    176   /// the code generator (e.g., llc crashes), this will return false and set
    177   /// Error.
    178   ///
    179   std::string executeProgramSafely(const Module *Program,
    180                                    const std::string &OutputFile,
    181                                    std::string *Error) const;
    182 
    183   /// createReferenceFile - calls compileProgram and then records the output
    184   /// into ReferenceOutputFile. Returns true if reference file created, false
    185   /// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
    186   /// this function.
    187   ///
    188   bool createReferenceFile(Module *M, const std::string &Filename
    189                                             = "bugpoint.reference.out-%%%%%%%");
    190 
    191   /// diffProgram - This method executes the specified module and diffs the
    192   /// output against the file specified by ReferenceOutputFile.  If the output
    193   /// is different, 1 is returned.  If there is a problem with the code
    194   /// generator (e.g., llc crashes), this will return -1 and set Error.
    195   ///
    196   bool diffProgram(const Module *Program,
    197                    const std::string &BitcodeFile = "",
    198                    const std::string &SharedObj = "",
    199                    bool RemoveBitcode = false,
    200                    std::string *Error = nullptr) const;
    201 
    202   /// EmitProgressBitcode - This function is used to output M to a file named
    203   /// "bugpoint-ID.bc".
    204   ///
    205   void EmitProgressBitcode(const Module *M, const std::string &ID,
    206                            bool NoFlyer = false) const;
    207 
    208   /// This method clones the current Program and deletes the specified
    209   /// instruction from the cloned module.  It then runs a series of cleanup
    210   /// passes (ADCE and SimplifyCFG) to eliminate any code which depends on the
    211   /// value. The modified module is then returned.
    212   ///
    213   std::unique_ptr<Module> deleteInstructionFromProgram(const Instruction *I,
    214                                                        unsigned Simp);
    215 
    216   /// This method clones the current Program and performs a series of cleanups
    217   /// intended to get rid of extra cruft on the module. If the
    218   /// MayModifySemantics argument is true, then the cleanups is allowed to
    219   /// modify how the code behaves.
    220   ///
    221   std::unique_ptr<Module> performFinalCleanups(Module *M,
    222                                                bool MayModifySemantics = false);
    223 
    224   /// Given a module, extract up to one loop from it into a new function. This
    225   /// returns null if there are no extractable loops in the program or if the
    226   /// loop extractor crashes.
    227   std::unique_ptr<Module> extractLoop(Module *M);
    228 
    229   /// Extract all but the specified basic blocks into their own functions. The
    230   /// only detail is that M is actually a module cloned from the one the BBs are
    231   /// in, so some mapping needs to be performed. If this operation fails for
    232   /// some reason (ie the implementation is buggy), this function should return
    233   /// null, otherwise it returns a new Module.
    234   std::unique_ptr<Module>
    235   extractMappedBlocksFromModule(const std::vector<BasicBlock *> &BBs,
    236                                 Module *M);
    237 
    238   /// Carefully run the specified set of pass on the specified/ module,
    239   /// returning the transformed module on success, or a null pointer on failure.
    240   std::unique_ptr<Module> runPassesOn(Module *M,
    241                                       const std::vector<std::string> &Passes,
    242                                       unsigned NumExtraArgs = 0,
    243                                       const char *const *ExtraArgs = nullptr);
    244 
    245   /// runPasses - Run the specified passes on Program, outputting a bitcode
    246   /// file and writting the filename into OutputFile if successful.  If the
    247   /// optimizations fail for some reason (optimizer crashes), return true,
    248   /// otherwise return false.  If DeleteOutput is set to true, the bitcode is
    249   /// deleted on success, and the filename string is undefined.  This prints to
    250   /// outs() a single line message indicating whether compilation was successful
    251   /// or failed, unless Quiet is set.  ExtraArgs specifies additional arguments
    252   /// to pass to the child bugpoint instance.
    253   ///
    254   bool runPasses(Module *Program,
    255                  const std::vector<std::string> &PassesToRun,
    256                  std::string &OutputFilename, bool DeleteOutput = false,
    257                  bool Quiet = false, unsigned NumExtraArgs = 0,
    258                  const char * const *ExtraArgs = nullptr) const;
    259 
    260   /// runPasses - Just like the method above, but this just returns true or
    261   /// false indicating whether or not the optimizer crashed on the specified
    262   /// input (true = crashed).  Does not produce any output.
    263   ///
    264   bool runPasses(Module *M,
    265                  const std::vector<std::string> &PassesToRun) const {
    266     std::string Filename;
    267     return runPasses(M, PassesToRun, Filename, true);
    268   }
    269 
    270   /// runManyPasses - Take the specified pass list and create different
    271   /// combinations of passes to compile the program with. Compile the program with
    272   /// each set and mark test to see if it compiled correctly. If the passes
    273   /// compiled correctly output nothing and rearrange the passes into a new order.
    274   /// If the passes did not compile correctly, output the command required to
    275   /// recreate the failure. This returns true if a compiler error is found.
    276   ///
    277   bool runManyPasses(const std::vector<std::string> &AllPasses,
    278                      std::string &ErrMsg);
    279 
    280   /// writeProgramToFile - This writes the current "Program" to the named
    281   /// bitcode file.  If an error occurs, true is returned.
    282   ///
    283   bool writeProgramToFile(const std::string &Filename, const Module *M) const;
    284   bool writeProgramToFile(const std::string &Filename, int FD,
    285                           const Module *M) const;
    286 
    287 private:
    288   /// initializeExecutionEnvironment - This method is used to set up the
    289   /// environment for executing LLVM programs.
    290   ///
    291   bool initializeExecutionEnvironment();
    292 };
    293 
    294 ///  Given a bitcode or assembly input filename, parse and return it, or return
    295 ///  null if not possible.
    296 ///
    297 std::unique_ptr<Module> parseInputFile(StringRef InputFilename,
    298                                        LLVMContext &ctxt);
    299 
    300 /// getPassesString - Turn a list of passes into a string which indicates the
    301 /// command line options that must be passed to add the passes.
    302 ///
    303 std::string getPassesString(const std::vector<std::string> &Passes);
    304 
    305 /// PrintFunctionList - prints out list of problematic functions
    306 ///
    307 void PrintFunctionList(const std::vector<Function*> &Funcs);
    308 
    309 /// PrintGlobalVariableList - prints out list of problematic global variables
    310 ///
    311 void PrintGlobalVariableList(const std::vector<GlobalVariable*> &GVs);
    312 
    313 // DeleteGlobalInitializer - "Remove" the global variable by deleting its
    314 // initializer, making it external.
    315 //
    316 void DeleteGlobalInitializer(GlobalVariable *GV);
    317 
    318 // DeleteFunctionBody - "Remove" the function by deleting all of it's basic
    319 // blocks, making it external.
    320 //
    321 void DeleteFunctionBody(Function *F);
    322 
    323 /// Given a module and a list of functions in the module, split the functions
    324 /// OUT of the specified module, and place them in the new module.
    325 std::unique_ptr<Module>
    326 SplitFunctionsOutOfModule(Module *M, const std::vector<Function *> &F,
    327                           ValueToValueMapTy &VMap);
    328 
    329 } // End llvm namespace
    330 
    331 #endif
    332