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;
36 extern bool DisableSimplifyCFG;
38 /// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
40 extern bool BugpointIsInterrupted;
43 const std::string ToolName; // Name of bugpoint
44 std::string ReferenceOutputFile; // Name of `good' output file
45 Module *Program; // The raw program, linked together
46 std::vector<const PassInfo*> PassesToRun;
47 AbstractInterpreter *Interpreter; // How to run the program
51 // FIXME: sort out public/private distinctions...
52 friend class ReducePassList;
53 friend class ReduceMisCodegenFunctions;
56 BugDriver(const char *toolname);
58 const std::string &getToolName() const { return ToolName; }
60 // Set up methods... these methods are used to copy information about the
61 // command line arguments into instance variables of BugDriver.
63 bool addSources(const std::vector<std::string> &FileNames);
65 void addPasses(It I, It E) { PassesToRun.insert(PassesToRun.end(), I, E); }
66 void setPassesToRun(const std::vector<const PassInfo*> &PTR) {
69 const std::vector<const PassInfo*> &getPassesToRun() const {
73 /// run - The top level method that is invoked after all of the instance
74 /// variables are set up from command line arguments.
78 /// debugOptimizerCrash - This method is called when some optimizer pass
79 /// crashes on input. It attempts to prune down the testcase to something
80 /// reasonable, and figure out exactly which pass is crashing.
82 bool debugOptimizerCrash();
84 /// debugCodeGeneratorCrash - This method is called when the code generator
85 /// crashes on an input. It attempts to reduce the input as much as possible
86 /// while still causing the code generator to crash.
87 bool debugCodeGeneratorCrash();
89 /// debugMiscompilation - This method is used when the passes selected are not
90 /// crashing, but the generated output is semantically different from the
92 bool debugMiscompilation();
94 /// debugPassMiscompilation - This method is called when the specified pass
95 /// miscompiles Program as input. It tries to reduce the testcase to
96 /// something that smaller that still miscompiles the program.
97 /// ReferenceOutput contains the filename of the file containing the output we
100 bool debugPassMiscompilation(const PassInfo *ThePass,
101 const std::string &ReferenceOutput);
103 /// compileSharedObject - This method creates a SharedObject from a given
104 /// BytecodeFile for debugging a code generator.
106 std::string compileSharedObject(const std::string &BytecodeFile);
108 /// debugCodeGenerator - This method narrows down a module to a function or
109 /// set of functions, using the CBE as a ``safe'' code generator for other
110 /// functions that are not under consideration.
111 bool debugCodeGenerator();
113 /// isExecutingJIT - Returns true if bugpoint is currently testing the JIT
115 bool isExecutingJIT();
117 /// runPasses - Run all of the passes in the "PassesToRun" list, discard the
118 /// output, and return true if any of the passes crashed.
119 bool runPasses(Module *M = 0) {
120 if (M == 0) M = Program;
121 std::swap(M, Program);
122 bool Result = runPasses(PassesToRun);
123 std::swap(M, Program);
127 Module *getProgram() const { return Program; }
129 /// swapProgramIn - Set the current module to the specified module, returning
131 Module *swapProgramIn(Module *M) {
132 Module *OldProgram = Program;
137 AbstractInterpreter *switchToCBE() {
138 AbstractInterpreter *Old = Interpreter;
139 Interpreter = (AbstractInterpreter*)cbe;
143 void switchToInterpreter(AbstractInterpreter *AI) {
147 /// setNewProgram - If we reduce or update the program somehow, call this
148 /// method to update bugdriver with it. This deletes the old module and sets
149 /// the specified one as the current program.
150 void setNewProgram(Module *M);
152 /// compileProgram - Try to compile the specified module, throwing an
153 /// exception if an error occurs, or returning normally if not. This is used
154 /// for code generation crash testing.
156 void compileProgram(Module *M);
158 /// executeProgram - This method runs "Program", capturing the output of the
159 /// program to a file, returning the filename of the file. A recommended
160 /// filename may be optionally specified. If there is a problem with the code
161 /// generator (e.g., llc crashes), this will throw an exception.
163 std::string executeProgram(std::string RequestedOutputFilename = "",
164 std::string Bytecode = "",
165 const std::string &SharedObjects = "",
166 AbstractInterpreter *AI = 0,
167 bool *ProgramExitedNonzero = 0);
169 /// executeProgramWithCBE - Used to create reference output with the C
170 /// backend, if reference output is not provided. If there is a problem with
171 /// the code generator (e.g., llc crashes), this will throw an exception.
173 std::string executeProgramWithCBE(std::string OutputFile = "");
175 /// diffProgram - This method executes the specified module and diffs the
176 /// output against the file specified by ReferenceOutputFile. If the output
177 /// is different, true is returned. If there is a problem with the code
178 /// generator (e.g., llc crashes), this will throw an exception.
180 bool diffProgram(const std::string &BytecodeFile = "",
181 const std::string &SharedObj = "",
182 bool RemoveBytecode = false);
183 /// EmitProgressBytecode - This function is used to output the current Program
184 /// to a file named "bugpoint-ID.bc".
186 void EmitProgressBytecode(const std::string &ID, bool NoFlyer = false);
188 /// deleteInstructionFromProgram - This method clones the current Program and
189 /// deletes the specified instruction from the cloned module. It then runs a
190 /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code
191 /// which depends on the value. The modified module is then returned.
193 Module *deleteInstructionFromProgram(const Instruction *I, unsigned Simp)
196 /// performFinalCleanups - This method clones the current Program and performs
197 /// a series of cleanups intended to get rid of extra cruft on the module. If
198 /// the MayModifySemantics argument is true, then the cleanups is allowed to
199 /// modify how the code behaves.
201 Module *performFinalCleanups(Module *M, bool MayModifySemantics = false);
203 /// ExtractLoop - Given a module, extract up to one loop from it into a new
204 /// function. This returns null if there are no extractable loops in the
205 /// program or if the loop extractor crashes.
206 Module *ExtractLoop(Module *M);
208 /// ExtractMappedBlocksFromModule - Extract all but the specified basic blocks
209 /// into their own functions. The only detail is that M is actually a module
210 /// cloned from the one the BBs are in, so some mapping needs to be performed.
211 /// If this operation fails for some reason (ie the implementation is buggy),
212 /// this function should return null, otherwise it returns a new Module.
213 Module *ExtractMappedBlocksFromModule(const std::vector<BasicBlock*> &BBs,
216 /// runPassesOn - Carefully run the specified set of pass on the specified
217 /// module, returning the transformed module on success, or a null pointer on
218 /// failure. If AutoDebugCrashes is set to true, then bugpoint will
219 /// automatically attempt to track down a crashing pass if one exists, and
220 /// this method will never return null.
221 Module *runPassesOn(Module *M, const std::vector<const PassInfo*> &Passes,
222 bool AutoDebugCrashes = false);
224 /// runPasses - Run the specified passes on Program, outputting a bytecode
225 /// file and writting the filename into OutputFile if successful. If the
226 /// optimizations fail for some reason (optimizer crashes), return true,
227 /// otherwise return false. If DeleteOutput is set to true, the bytecode is
228 /// deleted on success, and the filename string is undefined. This prints to
229 /// cout a single line message indicating whether compilation was successful
230 /// or failed, unless Quiet is set.
232 bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
233 std::string &OutputFilename, bool DeleteOutput = false,
234 bool Quiet = false) const;
236 /// writeProgramToFile - This writes the current "Program" to the named
237 /// bytecode file. If an error occurs, true is returned.
239 bool writeProgramToFile(const std::string &Filename, Module *M = 0) const;
242 /// runPasses - Just like the method above, but this just returns true or
243 /// false indicating whether or not the optimizer crashed on the specified
244 /// input (true = crashed).
246 bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
247 bool DeleteOutput = true) const {
248 std::string Filename;
249 return runPasses(PassesToRun, Filename, DeleteOutput);
252 /// initializeExecutionEnvironment - This method is used to set up the
253 /// environment for executing LLVM programs.
255 bool initializeExecutionEnvironment();
258 /// ParseInputFile - Given a bytecode or assembly input filename, parse and
259 /// return it, or return null if not possible.
261 Module *ParseInputFile(const std::string &InputFilename);
264 /// getPassesString - Turn a list of passes into a string which indicates the
265 /// command line options that must be passed to add the passes.
267 std::string getPassesString(const std::vector<const PassInfo*> &Passes);
269 /// PrintFunctionList - prints out list of problematic functions
271 void PrintFunctionList(const std::vector<Function*> &Funcs);
273 // DeleteFunctionBody - "Remove" the function by deleting all of it's basic
274 // blocks, making it external.
276 void DeleteFunctionBody(Function *F);
278 /// SplitFunctionsOutOfModule - Given a module and a list of functions in the
279 /// module, split the functions OUT of the specified module, and place them in
281 Module *SplitFunctionsOutOfModule(Module *M, const std::vector<Function*> &F);
283 } // End llvm namespace