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