xref: /freebsd/contrib/llvm-project/llvm/tools/bugpoint/BugDriver.h (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
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   /// debugPassMiscompilation - This method is called when the specified pass
105   /// miscompiles Program as input.  It tries to reduce the testcase to
106   /// something that smaller that still miscompiles the program.
107   /// ReferenceOutput contains the filename of the file containing the output we
108   /// are to match.
109   ///
110   bool debugPassMiscompilation(const PassInfo *ThePass,
111                                const std::string &ReferenceOutput);
112 
113   /// compileSharedObject - This method creates a SharedObject from a given
114   /// BitcodeFile for debugging a code generator.
115   ///
116   Expected<std::string> compileSharedObject(const std::string &BitcodeFile);
117 
118   /// debugCodeGenerator - This method narrows down a module to a function or
119   /// set of functions, using the CBE as a ``safe'' code generator for other
120   /// functions that are not under consideration.
121   Error debugCodeGenerator();
122 
123   /// isExecutingJIT - Returns true if bugpoint is currently testing the JIT
124   ///
125   bool isExecutingJIT();
126 
127   Module &getProgram() const { return *Program; }
128 
129   /// Set the current module to the specified module, returning the old one.
130   std::unique_ptr<Module> swapProgramIn(std::unique_ptr<Module> M);
131 
132   AbstractInterpreter *switchToSafeInterpreter() {
133     AbstractInterpreter *Old = Interpreter;
134     Interpreter = (AbstractInterpreter *)SafeInterpreter;
135     return Old;
136   }
137 
138   void switchToInterpreter(AbstractInterpreter *AI) { Interpreter = AI; }
139 
140   /// If we reduce or update the program somehow, call this method to update
141   /// bugdriver with it.  This deletes the old module and sets the specified one
142   /// as the current program.
143   void setNewProgram(std::unique_ptr<Module> M);
144 
145   /// Try to compile the specified module. This is used for code generation
146   /// crash testing.
147   Error compileProgram(Module &M) const;
148 
149   /// This method runs "Program", capturing the output of the program to a file.
150   /// A recommended filename may be optionally specified.
151   Expected<std::string> executeProgram(const Module &Program,
152                                        std::string OutputFilename,
153                                        std::string Bitcode,
154                                        const std::string &SharedObjects,
155                                        AbstractInterpreter *AI) const;
156 
157   /// Used to create reference output with the "safe" backend, if reference
158   /// output is not provided.  If there is a problem with the code generator
159   /// (e.g., llc crashes), this will return false and set Error.
160   Expected<std::string>
161   executeProgramSafely(const Module &Program,
162                        const std::string &OutputFile) const;
163 
164   /// Calls compileProgram and then records the output into ReferenceOutputFile.
165   /// Returns true if reference file created, false otherwise. Note:
166   /// initializeExecutionEnvironment should be called BEFORE this function.
167   Error createReferenceFile(Module &M, const std::string &Filename =
168                                            "bugpoint.reference.out-%%%%%%%");
169 
170   /// This method executes the specified module and diffs the output against the
171   /// file specified by ReferenceOutputFile.  If the output is different, 1 is
172   /// returned.  If there is a problem with the code generator (e.g., llc
173   /// crashes), this will return -1 and set Error.
174   Expected<bool> diffProgram(const Module &Program,
175                              const std::string &BitcodeFile = "",
176                              const std::string &SharedObj = "",
177                              bool RemoveBitcode = false) const;
178 
179   /// This function is used to output M to a file named "bugpoint-ID.bc".
180   void EmitProgressBitcode(const Module &M, const std::string &ID,
181                            bool NoFlyer = false) const;
182 
183   /// This method clones the current Program and deletes the specified
184   /// instruction from the cloned module.  It then runs a series of cleanup
185   /// passes (ADCE and SimplifyCFG) to eliminate any code which depends on the
186   /// value. The modified module is then returned.
187   ///
188   std::unique_ptr<Module> deleteInstructionFromProgram(const Instruction *I,
189                                                        unsigned Simp);
190 
191   /// This method clones the current Program and performs a series of cleanups
192   /// intended to get rid of extra cruft on the module. If the
193   /// MayModifySemantics argument is true, then the cleanups is allowed to
194   /// modify how the code behaves.
195   ///
196   std::unique_ptr<Module> performFinalCleanups(std::unique_ptr<Module> M,
197                                                bool MayModifySemantics = false);
198 
199   /// Given a module, extract up to one loop from it into a new function. This
200   /// returns null if there are no extractable loops in the program or if the
201   /// loop extractor crashes.
202   std::unique_ptr<Module> extractLoop(Module *M);
203 
204   /// Extract all but the specified basic blocks into their own functions. The
205   /// only detail is that M is actually a module cloned from the one the BBs are
206   /// in, so some mapping needs to be performed. If this operation fails for
207   /// some reason (ie the implementation is buggy), this function should return
208   /// null, otherwise it returns a new Module.
209   std::unique_ptr<Module>
210   extractMappedBlocksFromModule(const std::vector<BasicBlock *> &BBs,
211                                 Module *M);
212 
213   /// Carefully run the specified set of pass on the specified/ module,
214   /// returning the transformed module on success, or a null pointer on failure.
215   std::unique_ptr<Module> runPassesOn(Module *M,
216                                       const std::vector<std::string> &Passes,
217                                       ArrayRef<std::string> ExtraArgs = {});
218 
219   /// runPasses - Run the specified passes on Program, outputting a bitcode
220   /// file and writting the filename into OutputFile if successful.  If the
221   /// optimizations fail for some reason (optimizer crashes), return true,
222   /// otherwise return false.  If DeleteOutput is set to true, the bitcode is
223   /// deleted on success, and the filename string is undefined.  This prints to
224   /// outs() a single line message indicating whether compilation was successful
225   /// or failed, unless Quiet is set.  ExtraArgs specifies additional arguments
226   /// to pass to the child bugpoint instance.
227   ///
228   bool runPasses(Module &Program, const std::vector<std::string> &PassesToRun,
229                  std::string &OutputFilename, bool DeleteOutput = false,
230                  bool Quiet = false,
231                  ArrayRef<std::string> ExtraArgs = {}) const;
232 
233   /// runPasses - Just like the method above, but this just returns true or
234   /// false indicating whether or not the optimizer crashed on the specified
235   /// input (true = crashed).  Does not produce any output.
236   ///
237   bool runPasses(Module &M, const std::vector<std::string> &PassesToRun) const {
238     std::string Filename;
239     return runPasses(M, PassesToRun, Filename, true);
240   }
241 
242   /// Take the specified pass list and create different combinations of passes
243   /// to compile the program with. Compile the program with each set and mark
244   /// test to see if it compiled correctly. If the passes compiled correctly
245   /// output nothing and rearrange the passes into a new order. If the passes
246   /// did not compile correctly, output the command required to recreate the
247   /// failure.
248   Error runManyPasses(const std::vector<std::string> &AllPasses);
249 
250   /// This writes the current "Program" to the named bitcode file.  If an error
251   /// occurs, true is returned.
252   bool writeProgramToFile(const std::string &Filename, const Module &M) const;
253   bool writeProgramToFile(const std::string &Filename, int FD,
254                           const Module &M) const;
255   bool writeProgramToFile(int FD, const Module &M) const;
256 
257 private:
258   /// initializeExecutionEnvironment - This method is used to set up the
259   /// environment for executing LLVM programs.
260   ///
261   Error initializeExecutionEnvironment();
262 };
263 
264 struct DiscardTemp {
265   sys::fs::TempFile &File;
266   ~DiscardTemp();
267 };
268 
269 ///  Given a bitcode or assembly input filename, parse and return it, or return
270 ///  null if not possible.
271 ///
272 std::unique_ptr<Module> parseInputFile(StringRef InputFilename,
273                                        LLVMContext &ctxt);
274 
275 /// getPassesString - Turn a list of passes into a string which indicates the
276 /// command line options that must be passed to add the passes.
277 ///
278 std::string getPassesString(const std::vector<std::string> &Passes);
279 
280 /// PrintFunctionList - prints out list of problematic functions
281 ///
282 void PrintFunctionList(const std::vector<Function *> &Funcs);
283 
284 /// PrintGlobalVariableList - prints out list of problematic global variables
285 ///
286 void PrintGlobalVariableList(const std::vector<GlobalVariable *> &GVs);
287 
288 // DeleteGlobalInitializer - "Remove" the global variable by deleting its
289 // initializer, making it external.
290 //
291 void DeleteGlobalInitializer(GlobalVariable *GV);
292 
293 // DeleteFunctionBody - "Remove" the function by deleting all of it's basic
294 // blocks, making it external.
295 //
296 void DeleteFunctionBody(Function *F);
297 
298 /// Given a module and a list of functions in the module, split the functions
299 /// OUT of the specified module, and place them in the new module.
300 std::unique_ptr<Module>
301 SplitFunctionsOutOfModule(Module *M, const std::vector<Function *> &F,
302                           ValueToValueMapTy &VMap);
303 
304 } // End llvm namespace
305 
306 #endif
307