1 //===- BugDriver.h - Top-Level BugPoint class -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
28 class AbstractInterpreter;
35 extern bool DisableSimplifyCFG;
37 /// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
39 extern bool BugpointIsInterrupted;
42 const std::string ToolName; // Name of bugpoint
43 std::string ReferenceOutputFile; // Name of `good' output file
44 Module *Program; // The raw program, linked together
45 std::vector<const PassInfo*> PassesToRun;
46 AbstractInterpreter *Interpreter; // How to run the program
47 AbstractInterpreter *cbe;
53 // FIXME: sort out public/private distinctions...
54 friend class ReducePassList;
55 friend class ReduceMisCodegenFunctions;
58 BugDriver(const char *toolname, bool as_child, bool find_bugs,
61 const std::string &getToolName() const { return ToolName; }
63 // Set up methods... these methods are used to copy information about the
64 // command line arguments into instance variables of BugDriver.
66 bool addSources(const std::vector<std::string> &FileNames);
68 void addPasses(It I, It E) { PassesToRun.insert(PassesToRun.end(), I, E); }
69 void setPassesToRun(const std::vector<const PassInfo*> &PTR) {
72 const std::vector<const PassInfo*> &getPassesToRun() const {
76 /// run - The top level method that is invoked after all of the instance
77 /// variables are set up from command line arguments. The \p as_child argument
78 /// indicates whether the driver is to run in parent mode or child mode.
82 /// debugOptimizerCrash - This method is called when some optimizer pass
83 /// crashes on input. It attempts to prune down the testcase to something
84 /// reasonable, and figure out exactly which pass is crashing.
86 bool debugOptimizerCrash(const std::string &ID = "passes");
88 /// debugCodeGeneratorCrash - This method is called when the code generator
89 /// crashes on an input. It attempts to reduce the input as much as possible
90 /// while still causing the code generator to crash.
91 bool debugCodeGeneratorCrash();
93 /// debugMiscompilation - This method is used when the passes selected are not
94 /// crashing, but the generated output is semantically different from the
96 bool debugMiscompilation();
98 /// debugPassMiscompilation - This method is called when the specified pass
99 /// miscompiles Program as input. It tries to reduce the testcase to
100 /// something that smaller that still miscompiles the program.
101 /// ReferenceOutput contains the filename of the file containing the output we
104 bool debugPassMiscompilation(const PassInfo *ThePass,
105 const std::string &ReferenceOutput);
107 /// compileSharedObject - This method creates a SharedObject from a given
108 /// BytecodeFile for debugging a code generator.
110 std::string compileSharedObject(const std::string &BytecodeFile);
112 /// debugCodeGenerator - This method narrows down a module to a function or
113 /// set of functions, using the CBE as a ``safe'' code generator for other
114 /// functions that are not under consideration.
115 bool debugCodeGenerator();
117 /// isExecutingJIT - Returns true if bugpoint is currently testing the JIT
119 bool isExecutingJIT();
121 /// runPasses - Run all of the passes in the "PassesToRun" list, discard the
122 /// output, and return true if any of the passes crashed.
123 bool runPasses(Module *M = 0) {
124 if (M == 0) M = Program;
125 std::swap(M, Program);
126 bool Result = runPasses(PassesToRun);
127 std::swap(M, Program);
131 Module *getProgram() const { return Program; }
133 /// swapProgramIn - Set the current module to the specified module, returning
135 Module *swapProgramIn(Module *M) {
136 Module *OldProgram = Program;
141 AbstractInterpreter *switchToCBE() {
142 AbstractInterpreter *Old = Interpreter;
143 Interpreter = (AbstractInterpreter*)cbe;
147 void switchToInterpreter(AbstractInterpreter *AI) {
151 /// setNewProgram - If we reduce or update the program somehow, call this
152 /// method to update bugdriver with it. This deletes the old module and sets
153 /// the specified one as the current program.
154 void setNewProgram(Module *M);
156 /// compileProgram - Try to compile the specified module, throwing an
157 /// exception if an error occurs, or returning normally if not. This is used
158 /// for code generation crash testing.
160 void compileProgram(Module *M);
162 /// executeProgram - This method runs "Program", capturing the output of the
163 /// program to a file, returning the filename of the file. A recommended
164 /// filename may be optionally specified. If there is a problem with the code
165 /// generator (e.g., llc crashes), this will throw an exception.
167 std::string executeProgram(std::string RequestedOutputFilename = "",
168 std::string Bytecode = "",
169 const std::string &SharedObjects = "",
170 AbstractInterpreter *AI = 0,
171 bool *ProgramExitedNonzero = 0);
173 /// executeProgramWithCBE - Used to create reference output with the C
174 /// backend, if reference output is not provided. If there is a problem with
175 /// the code generator (e.g., llc crashes), this will throw an exception.
177 std::string executeProgramWithCBE(std::string OutputFile = "");
179 /// createReferenceFile - calls compileProgram and then records the output
180 /// into ReferenceOutputFile. Returns true if reference file created, false
181 /// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
184 bool createReferenceFile(Module *M, const std::string &Filename
185 = "bugpoint.reference.out");
187 /// diffProgram - This method executes the specified module and diffs the
188 /// output against the file specified by ReferenceOutputFile. If the output
189 /// is different, true is returned. If there is a problem with the code
190 /// generator (e.g., llc crashes), this will throw an exception.
192 bool diffProgram(const std::string &BytecodeFile = "",
193 const std::string &SharedObj = "",
194 bool RemoveBytecode = false);
196 /// EmitProgressBytecode - This function is used to output the current Program
197 /// to a file named "bugpoint-ID.bc".
199 void EmitProgressBytecode(const std::string &ID, bool NoFlyer = false);
201 /// deleteInstructionFromProgram - This method clones the current Program and
202 /// deletes the specified instruction from the cloned module. It then runs a
203 /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code
204 /// which depends on the value. The modified module is then returned.
206 Module *deleteInstructionFromProgram(const Instruction *I, unsigned Simp)
209 /// performFinalCleanups - This method clones the current Program and performs
210 /// a series of cleanups intended to get rid of extra cruft on the module. If
211 /// the MayModifySemantics argument is true, then the cleanups is allowed to
212 /// modify how the code behaves.
214 Module *performFinalCleanups(Module *M, bool MayModifySemantics = false);
216 /// ExtractLoop - Given a module, extract up to one loop from it into a new
217 /// function. This returns null if there are no extractable loops in the
218 /// program or if the loop extractor crashes.
219 Module *ExtractLoop(Module *M);
221 /// ExtractMappedBlocksFromModule - Extract all but the specified basic blocks
222 /// into their own functions. The only detail is that M is actually a module
223 /// cloned from the one the BBs are in, so some mapping needs to be performed.
224 /// If this operation fails for some reason (ie the implementation is buggy),
225 /// this function should return null, otherwise it returns a new Module.
226 Module *ExtractMappedBlocksFromModule(const std::vector<BasicBlock*> &BBs,
229 /// runPassesOn - Carefully run the specified set of pass on the specified
230 /// module, returning the transformed module on success, or a null pointer on
231 /// failure. If AutoDebugCrashes is set to true, then bugpoint will
232 /// automatically attempt to track down a crashing pass if one exists, and
233 /// this method will never return null.
234 Module *runPassesOn(Module *M, const std::vector<const PassInfo*> &Passes,
235 bool AutoDebugCrashes = false);
237 /// runPasses - Run the specified passes on Program, outputting a bytecode
238 /// file and writting the filename into OutputFile if successful. If the
239 /// optimizations fail for some reason (optimizer crashes), return true,
240 /// otherwise return false. If DeleteOutput is set to true, the bytecode is
241 /// deleted on success, and the filename string is undefined. This prints to
242 /// cout a single line message indicating whether compilation was successful
243 /// or failed, unless Quiet is set.
245 bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
246 std::string &OutputFilename, bool DeleteOutput = false,
247 bool Quiet = false) const;
249 /// runManyPasses - Take the specified pass list and create different
250 /// combinations of passes to compile the program with. Compile the program with
251 /// each set and mark test to see if it compiled correctly. If the passes
252 /// compiled correctly output nothing and rearrange the passes into a new order.
253 /// If the passes did not compile correctly, output the command required to
254 /// recreate the failure. This returns true if a compiler error is found.
256 bool runManyPasses(const std::vector<const PassInfo*> &AllPasses);
258 /// writeProgramToFile - This writes the current "Program" to the named
259 /// bytecode file. If an error occurs, true is returned.
261 bool writeProgramToFile(const std::string &Filename, Module *M = 0) const;
264 /// runPasses - Just like the method above, but this just returns true or
265 /// false indicating whether or not the optimizer crashed on the specified
266 /// input (true = crashed).
268 bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
269 bool DeleteOutput = true) const {
270 std::string Filename;
271 return runPasses(PassesToRun, Filename, DeleteOutput);
274 /// runAsChild - The actual "runPasses" guts that runs in a child process.
275 int runPassesAsChild(const std::vector<const PassInfo*> &PassesToRun);
277 /// initializeExecutionEnvironment - This method is used to set up the
278 /// environment for executing LLVM programs.
280 bool initializeExecutionEnvironment();
283 /// ParseInputFile - Given a bytecode or assembly input filename, parse and
284 /// return it, or return null if not possible.
286 Module *ParseInputFile(const std::string &InputFilename);
289 /// getPassesString - Turn a list of passes into a string which indicates the
290 /// command line options that must be passed to add the passes.
292 std::string getPassesString(const std::vector<const PassInfo*> &Passes);
294 /// PrintFunctionList - prints out list of problematic functions
296 void PrintFunctionList(const std::vector<Function*> &Funcs);
298 // DeleteFunctionBody - "Remove" the function by deleting all of it's basic
299 // blocks, making it external.
301 void DeleteFunctionBody(Function *F);
303 /// SplitFunctionsOutOfModule - Given a module and a list of functions in the
304 /// module, split the functions OUT of the specified module, and place them in
306 Module *SplitFunctionsOutOfModule(Module *M, const std::vector<Function*> &F);
308 } // End llvm namespace