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