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 //===----------------------------------------------------------------------===//
27 class AbstractInterpreter;
31 class ReduceMiscompilingPasses;
32 class ReduceMiscompilingFunctions;
33 class ReduceCrashingFunctions;
34 class ReduceCrashingBlocks;
39 extern bool DisableSimplifyCFG;
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
50 // FIXME: sort out public/private distinctions...
51 friend class ReducePassList;
52 friend class ReduceMiscompilingPasses;
53 friend class ReduceMiscompilingFunctions;
54 friend class ReduceMisCodegenFunctions;
57 BugDriver(const char *toolname);
59 const std::string &getToolName() const { return ToolName; }
61 // Set up methods... these methods are used to copy information about the
62 // command line arguments into instance variables of BugDriver.
64 bool addSources(const std::vector<std::string> &FileNames);
66 void addPasses(It I, It E) { PassesToRun.insert(PassesToRun.end(), I, E); }
67 void setPassesToRun(const std::vector<const PassInfo*> &PTR) {
71 /// run - The top level method that is invoked after all of the instance
72 /// variables are set up from command line arguments.
76 /// debugOptimizerCrash - This method is called when some optimizer pass
77 /// crashes on input. It attempts to prune down the testcase to something
78 /// reasonable, and figure out exactly which pass is crashing.
80 bool debugOptimizerCrash();
82 /// debugCodeGeneratorCrash - This method is called when the code generator
83 /// crashes on an input. It attempts to reduce the input as much as possible
84 /// while still causing the code generator to crash.
85 bool debugCodeGeneratorCrash();
87 /// debugMiscompilation - This method is used when the passes selected are not
88 /// crashing, but the generated output is semantically different from the
90 bool debugMiscompilation();
92 /// debugPassMiscompilation - This method is called when the specified pass
93 /// miscompiles Program as input. It tries to reduce the testcase to
94 /// something that smaller that still miscompiles the program.
95 /// ReferenceOutput contains the filename of the file containing the output we
98 bool debugPassMiscompilation(const PassInfo *ThePass,
99 const std::string &ReferenceOutput);
101 /// compileSharedObject - This method creates a SharedObject from a given
102 /// BytecodeFile for debugging a code generator.
104 std::string compileSharedObject(const std::string &BytecodeFile);
106 /// debugCodeGenerator - This method narrows down a module to a function or
107 /// set of functions, using the CBE as a ``safe'' code generator for other
108 /// functions that are not under consideration.
109 bool debugCodeGenerator();
111 /// isExecutingJIT - Returns true if bugpoint is currently testing the JIT
113 bool isExecutingJIT();
115 /// runPasses - Run all of the passes in the "PassesToRun" list, discard the
116 /// output, and return true if any of the passes crashed.
117 bool runPasses(Module *M = 0) {
118 if (M == 0) M = Program;
119 std::swap(M, Program);
120 bool Result = runPasses(PassesToRun);
121 std::swap(M, Program);
125 const Module *getProgram() const { return Program; }
127 /// setNewProgram - If we reduce or update the program somehow, call this
128 /// method to update bugdriver with it. This deletes the old module and sets
129 /// the specified one as the current program.
130 void setNewProgram(Module *M);
133 /// ParseInputFile - Given a bytecode or assembly input filename, parse and
134 /// return it, or return null if not possible.
136 Module *ParseInputFile(const std::string &InputFilename) const;
138 /// writeProgramToFile - This writes the current "Program" to the named
139 /// bytecode file. If an error occurs, true is returned.
141 bool writeProgramToFile(const std::string &Filename, Module *M = 0) const;
144 /// EmitProgressBytecode - This function is used to output the current Program
145 /// to a file named "bugpoint-ID.bc".
147 void EmitProgressBytecode(const std::string &ID, bool NoFlyer = false);
149 /// runPasses - Run the specified passes on Program, outputting a bytecode
150 /// file and writting the filename into OutputFile if successful. If the
151 /// optimizations fail for some reason (optimizer crashes), return true,
152 /// otherwise return false. If DeleteOutput is set to true, the bytecode is
153 /// deleted on success, and the filename string is undefined. This prints to
154 /// cout a single line message indicating whether compilation was successful
155 /// or failed, unless Quiet is set.
157 bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
158 std::string &OutputFilename, bool DeleteOutput = false,
159 bool Quiet = false) const;
161 /// runPasses - Just like the method above, but this just returns true or
162 /// false indicating whether or not the optimizer crashed on the specified
163 /// input (true = crashed).
165 bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
166 bool DeleteOutput = true) const {
167 std::string Filename;
168 return runPasses(PassesToRun, Filename, DeleteOutput);
171 /// PrintFunctionList - prints out list of problematic functions
173 static void PrintFunctionList(const std::vector<Function*> &Funcs);
175 /// deleteInstructionFromProgram - This method clones the current Program and
176 /// deletes the specified instruction from the cloned module. It then runs a
177 /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code
178 /// which depends on the value. The modified module is then returned.
180 Module *deleteInstructionFromProgram(Instruction *I, unsigned Simp) const;
182 /// performFinalCleanups - This method clones the current Program and performs
183 /// a series of cleanups intended to get rid of extra cruft on the module. If
184 /// the MayModifySemantics argument is true, then the cleanups is allowed to
185 /// modify how the code behaves.
187 Module *performFinalCleanups(Module *M, bool MayModifySemantics = false);
189 /// initializeExecutionEnvironment - This method is used to set up the
190 /// environment for executing LLVM programs.
192 bool initializeExecutionEnvironment();
194 /// executeProgram - This method runs "Program", capturing the output of the
195 /// program to a file, returning the filename of the file. A recommended
196 /// filename may be optionally specified. If there is a problem with the code
197 /// generator (e.g., llc crashes), this will throw an exception.
199 std::string executeProgram(std::string RequestedOutputFilename = "",
200 std::string Bytecode = "",
201 const std::string &SharedObjects = "",
202 AbstractInterpreter *AI = 0,
203 bool *ProgramExitedNonzero = 0);
205 /// executeProgramWithCBE - Used to create reference output with the C
206 /// backend, if reference output is not provided. If there is a problem with
207 /// the code generator (e.g., llc crashes), this will throw an exception.
209 std::string executeProgramWithCBE(std::string OutputFile = "");
211 /// diffProgram - This method executes the specified module and diffs the
212 /// output against the file specified by ReferenceOutputFile. If the output
213 /// is different, true is returned. If there is a problem with the code
214 /// generator (e.g., llc crashes), this will throw an exception.
216 bool diffProgram(const std::string &BytecodeFile = "",
217 const std::string &SharedObj = "",
218 bool RemoveBytecode = false);
221 /// getPassesString - Turn a list of passes into a string which indicates the
222 /// command line options that must be passed to add the passes.
224 std::string getPassesString(const std::vector<const PassInfo*> &Passes);
226 // DeleteFunctionBody - "Remove" the function by deleting all of it's basic
227 // blocks, making it external.
229 void DeleteFunctionBody(Function *F);
231 } // End llvm namespace