blob: 2029e2da029926c6d4dd5bf005be1283433d71c0 [file] [log] [blame]
Chris Lattnerafade922002-11-20 22:28:10 +00001//===- BugDriver.h - Top-Level BugPoint class -------------------*- C++ -*-===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
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.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerafade922002-11-20 22:28:10 +00009//
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.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef BUGDRIVER_H
17#define BUGDRIVER_H
18
19#include <vector>
20#include <string>
Misha Brukman50733362003-07-24 18:17:43 +000021
Brian Gaeked0fde302003-11-11 22:41:34 +000022namespace llvm {
23
Chris Lattnerafade922002-11-20 22:28:10 +000024class PassInfo;
25class Module;
26class Function;
Chris Lattner5e783ab2004-05-11 21:54:13 +000027class BasicBlock;
Chris Lattner218e26e2002-12-23 23:49:59 +000028class AbstractInterpreter;
Chris Lattner65207852003-01-23 02:48:33 +000029class Instruction;
Chris Lattnerafade922002-11-20 22:28:10 +000030
Chris Lattneraae33f92003-04-24 22:24:58 +000031class DebugCrashes;
Chris Lattner640f22e2003-04-24 17:02:17 +000032
Misha Brukmana259c9b2003-07-24 21:59:10 +000033class GCC;
34
Chris Lattner47ae4a12003-08-05 15:51:05 +000035extern bool DisableSimplifyCFG;
36
Chris Lattnerf9aaae02005-08-02 02:16:17 +000037/// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
38///
39extern bool BugpointIsInterrupted;
40
Chris Lattnerafade922002-11-20 22:28:10 +000041class BugDriver {
42 const std::string ToolName; // Name of bugpoint
Misha Brukmana259c9b2003-07-24 21:59:10 +000043 std::string ReferenceOutputFile; // Name of `good' output file
Chris Lattnerafade922002-11-20 22:28:10 +000044 Module *Program; // The raw program, linked together
45 std::vector<const PassInfo*> PassesToRun;
Chris Lattner218e26e2002-12-23 23:49:59 +000046 AbstractInterpreter *Interpreter; // How to run the program
Chris Lattnerc600f3c2006-09-15 21:29:15 +000047 AbstractInterpreter *cbe;
Misha Brukmana259c9b2003-07-24 21:59:10 +000048 GCC *gcc;
Reid Spencerc4bb0522005-12-22 20:02:55 +000049 bool run_as_child;
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +000050 bool run_find_bugs;
Chris Lattner9686ae72006-06-13 03:10:48 +000051 unsigned Timeout;
Chris Lattner640f22e2003-04-24 17:02:17 +000052
Chris Lattneraae33f92003-04-24 22:24:58 +000053 // FIXME: sort out public/private distinctions...
Chris Lattner06905db2004-02-18 21:24:48 +000054 friend class ReducePassList;
Misha Brukman50733362003-07-24 18:17:43 +000055 friend class ReduceMisCodegenFunctions;
Misha Brukman50733362003-07-24 18:17:43 +000056
Chris Lattnerafade922002-11-20 22:28:10 +000057public:
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +000058 BugDriver(const char *toolname, bool as_child, bool find_bugs,
59 unsigned timeout);
Chris Lattner218e26e2002-12-23 23:49:59 +000060
61 const std::string &getToolName() const { return ToolName; }
Chris Lattnerafade922002-11-20 22:28:10 +000062
63 // Set up methods... these methods are used to copy information about the
64 // command line arguments into instance variables of BugDriver.
65 //
66 bool addSources(const std::vector<std::string> &FileNames);
67 template<class It>
68 void addPasses(It I, It E) { PassesToRun.insert(PassesToRun.end(), I, E); }
Chris Lattner9c6cfe12003-10-17 23:03:16 +000069 void setPassesToRun(const std::vector<const PassInfo*> &PTR) {
70 PassesToRun = PTR;
71 }
Chris Lattnerefdc0b52004-03-14 20:50:42 +000072 const std::vector<const PassInfo*> &getPassesToRun() const {
73 return PassesToRun;
74 }
Chris Lattnerafade922002-11-20 22:28:10 +000075
76 /// run - The top level method that is invoked after all of the instance
Reid Spencerc4bb0522005-12-22 20:02:55 +000077 /// 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.
Chris Lattnerafade922002-11-20 22:28:10 +000079 ///
80 bool run();
81
Chris Lattner02526262004-02-18 21:02:04 +000082 /// 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.
Chris Lattnerafade922002-11-20 22:28:10 +000085 ///
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +000086 bool debugOptimizerCrash(const std::string &ID = "passes");
Misha Brukman3da94ae2005-04-22 00:00:37 +000087
Chris Lattner02526262004-02-18 21:02:04 +000088 /// 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();
Chris Lattnerafade922002-11-20 22:28:10 +000092
Chris Lattnerafade922002-11-20 22:28:10 +000093 /// debugMiscompilation - This method is used when the passes selected are not
94 /// crashing, but the generated output is semantically different from the
95 /// input.
96 bool debugMiscompilation();
97
Chris Lattner218e26e2002-12-23 23:49:59 +000098 /// 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
102 /// are to match.
103 ///
104 bool debugPassMiscompilation(const PassInfo *ThePass,
Jeff Cohenea3e5e52005-04-22 04:13:13 +0000105 const std::string &ReferenceOutput);
Chris Lattner218e26e2002-12-23 23:49:59 +0000106
Misha Brukman50733362003-07-24 18:17:43 +0000107 /// compileSharedObject - This method creates a SharedObject from a given
108 /// BytecodeFile for debugging a code generator.
Chris Lattnera0f5b152003-10-14 21:09:11 +0000109 ///
110 std::string compileSharedObject(const std::string &BytecodeFile);
Misha Brukman50733362003-07-24 18:17:43 +0000111
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();
116
Misha Brukmanfe04db82003-07-28 20:59:16 +0000117 /// isExecutingJIT - Returns true if bugpoint is currently testing the JIT
118 ///
Misha Brukman91eabc12003-07-28 19:16:14 +0000119 bool isExecutingJIT();
120
Chris Lattner06905db2004-02-18 21:24:48 +0000121 /// 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);
128 return Result;
129 }
130
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000131 Module *getProgram() const { return Program; }
132
133 /// swapProgramIn - Set the current module to the specified module, returning
134 /// the old one.
135 Module *swapProgramIn(Module *M) {
136 Module *OldProgram = Program;
137 Program = M;
138 return OldProgram;
139 }
Chris Lattner06905db2004-02-18 21:24:48 +0000140
Chris Lattnera57d86b2004-04-05 22:58:16 +0000141 AbstractInterpreter *switchToCBE() {
142 AbstractInterpreter *Old = Interpreter;
143 Interpreter = (AbstractInterpreter*)cbe;
144 return Old;
145 }
146
147 void switchToInterpreter(AbstractInterpreter *AI) {
148 Interpreter = AI;
149 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000150
Chris Lattner06905db2004-02-18 21:24:48 +0000151 /// 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);
155
Chris Lattnerea9212c2004-02-18 23:25:22 +0000156 /// 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.
159 ///
160 void compileProgram(Module *M);
161
Chris Lattnera36ec882004-02-18 21:35:28 +0000162 /// 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.
166 ///
167 std::string executeProgram(std::string RequestedOutputFilename = "",
168 std::string Bytecode = "",
169 const std::string &SharedObjects = "",
170 AbstractInterpreter *AI = 0,
171 bool *ProgramExitedNonzero = 0);
172
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.
176 ///
177 std::string executeProgramWithCBE(std::string OutputFile = "");
178
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000179 /// 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
182 /// this function.
183 ///
Chris Lattnerc0431fe2006-08-17 18:51:28 +0000184 bool createReferenceFile(Module *M, const std::string &Filename
185 = "bugpoint.reference.out");
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000186
Chris Lattnera36ec882004-02-18 21:35:28 +0000187 /// 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.
191 ///
192 bool diffProgram(const std::string &BytecodeFile = "",
193 const std::string &SharedObj = "",
194 bool RemoveBytecode = false);
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000195
Chris Lattner0cc88072004-02-18 21:50:26 +0000196 /// EmitProgressBytecode - This function is used to output the current Program
197 /// to a file named "bugpoint-ID.bc".
198 ///
199 void EmitProgressBytecode(const std::string &ID, bool NoFlyer = false);
200
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.
205 ///
206 Module *deleteInstructionFromProgram(const Instruction *I, unsigned Simp)
207 const;
208
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.
213 ///
214 Module *performFinalCleanups(Module *M, bool MayModifySemantics = false);
215
Chris Lattner7546c382004-03-14 20:02:07 +0000216 /// 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);
220
Chris Lattner5e783ab2004-05-11 21:54:13 +0000221 /// 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,
227 Module *M);
228
Chris Lattner3b6441e2004-03-14 21:17:03 +0000229 /// runPassesOn - Carefully run the specified set of pass on the specified
230 /// module, returning the transformed module on success, or a null pointer on
Chris Lattner0a002562004-03-14 21:21:57 +0000231 /// 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);
Chris Lattner3b6441e2004-03-14 21:17:03 +0000236
Chris Lattnerafade922002-11-20 22:28:10 +0000237 /// 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
Chris Lattner218e26e2002-12-23 23:49:59 +0000243 /// or failed, unless Quiet is set.
Chris Lattnerafade922002-11-20 22:28:10 +0000244 ///
245 bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
Chris Lattner218e26e2002-12-23 23:49:59 +0000246 std::string &OutputFilename, bool DeleteOutput = false,
Jeff Cohenea3e5e52005-04-22 04:13:13 +0000247 bool Quiet = false) const;
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000248
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.
255 ///
256 bool runManyPasses(const std::vector<const PassInfo*> &AllPasses);
Chris Lattner11b8cd12004-04-05 22:01:48 +0000257
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000258 /// writeProgramToFile - This writes the current "Program" to the named
259 /// bytecode file. If an error occurs, true is returned.
260 ///
261 bool writeProgramToFile(const std::string &Filename, Module *M = 0) const;
Chris Lattnerafade922002-11-20 22:28:10 +0000262
Chris Lattner11b8cd12004-04-05 22:01:48 +0000263private:
Chris Lattnerafade922002-11-20 22:28:10 +0000264 /// 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).
267 ///
268 bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
269 bool DeleteOutput = true) const {
270 std::string Filename;
271 return runPasses(PassesToRun, Filename, DeleteOutput);
272 }
273
Reid Spencerc4bb0522005-12-22 20:02:55 +0000274 /// runAsChild - The actual "runPasses" guts that runs in a child process.
275 int runPassesAsChild(const std::vector<const PassInfo*> &PassesToRun);
276
Chris Lattner218e26e2002-12-23 23:49:59 +0000277 /// initializeExecutionEnvironment - This method is used to set up the
278 /// environment for executing LLVM programs.
279 ///
280 bool initializeExecutionEnvironment();
Chris Lattnerafade922002-11-20 22:28:10 +0000281};
282
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000283/// ParseInputFile - Given a bytecode or assembly input filename, parse and
284/// return it, or return null if not possible.
285///
286Module *ParseInputFile(const std::string &InputFilename);
287
288
Chris Lattner640f22e2003-04-24 17:02:17 +0000289/// getPassesString - Turn a list of passes into a string which indicates the
290/// command line options that must be passed to add the passes.
291///
292std::string getPassesString(const std::vector<const PassInfo*> &Passes);
293
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000294/// PrintFunctionList - prints out list of problematic functions
295///
296void PrintFunctionList(const std::vector<Function*> &Funcs);
297
Chris Lattneraae33f92003-04-24 22:24:58 +0000298// DeleteFunctionBody - "Remove" the function by deleting all of it's basic
299// blocks, making it external.
300//
301void DeleteFunctionBody(Function *F);
302
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000303/// 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
305/// the new module.
306Module *SplitFunctionsOutOfModule(Module *M, const std::vector<Function*> &F);
307
Brian Gaeked0fde302003-11-11 22:41:34 +0000308} // End llvm namespace
309
Chris Lattnerafade922002-11-20 22:28:10 +0000310#endif