blob: 192ed527178e667c776b1e90f41c19d32c4291f9 [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 CBE;
34class GCC;
35
Chris Lattner47ae4a12003-08-05 15:51:05 +000036extern bool DisableSimplifyCFG;
37
Chris Lattnerf9aaae02005-08-02 02:16:17 +000038/// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
39///
40extern bool BugpointIsInterrupted;
41
Chris Lattnerafade922002-11-20 22:28:10 +000042class BugDriver {
43 const std::string ToolName; // Name of bugpoint
Misha Brukmana259c9b2003-07-24 21:59:10 +000044 std::string ReferenceOutputFile; // Name of `good' output file
Chris Lattnerafade922002-11-20 22:28:10 +000045 Module *Program; // The raw program, linked together
46 std::vector<const PassInfo*> PassesToRun;
Chris Lattner218e26e2002-12-23 23:49:59 +000047 AbstractInterpreter *Interpreter; // How to run the program
Misha Brukmana259c9b2003-07-24 21:59:10 +000048 CBE *cbe;
49 GCC *gcc;
Chris Lattner640f22e2003-04-24 17:02:17 +000050
Chris Lattneraae33f92003-04-24 22:24:58 +000051 // FIXME: sort out public/private distinctions...
Chris Lattner06905db2004-02-18 21:24:48 +000052 friend class ReducePassList;
Misha Brukman50733362003-07-24 18:17:43 +000053 friend class ReduceMisCodegenFunctions;
Misha Brukman50733362003-07-24 18:17:43 +000054
Chris Lattnerafade922002-11-20 22:28:10 +000055public:
Misha Brukman50733362003-07-24 18:17:43 +000056 BugDriver(const char *toolname);
Chris Lattner218e26e2002-12-23 23:49:59 +000057
58 const std::string &getToolName() const { return ToolName; }
Chris Lattnerafade922002-11-20 22:28:10 +000059
60 // Set up methods... these methods are used to copy information about the
61 // command line arguments into instance variables of BugDriver.
62 //
63 bool addSources(const std::vector<std::string> &FileNames);
64 template<class It>
65 void addPasses(It I, It E) { PassesToRun.insert(PassesToRun.end(), I, E); }
Chris Lattner9c6cfe12003-10-17 23:03:16 +000066 void setPassesToRun(const std::vector<const PassInfo*> &PTR) {
67 PassesToRun = PTR;
68 }
Chris Lattnerefdc0b52004-03-14 20:50:42 +000069 const std::vector<const PassInfo*> &getPassesToRun() const {
70 return PassesToRun;
71 }
Chris Lattnerafade922002-11-20 22:28:10 +000072
73 /// run - The top level method that is invoked after all of the instance
74 /// variables are set up from command line arguments.
75 ///
76 bool run();
77
Chris Lattner02526262004-02-18 21:02:04 +000078 /// 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.
Chris Lattnerafade922002-11-20 22:28:10 +000081 ///
Chris Lattner02526262004-02-18 21:02:04 +000082 bool debugOptimizerCrash();
Misha Brukman3da94ae2005-04-22 00:00:37 +000083
Chris Lattner02526262004-02-18 21:02:04 +000084 /// 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();
Chris Lattnerafade922002-11-20 22:28:10 +000088
Chris Lattnerafade922002-11-20 22:28:10 +000089 /// debugMiscompilation - This method is used when the passes selected are not
90 /// crashing, but the generated output is semantically different from the
91 /// input.
92 bool debugMiscompilation();
93
Chris Lattner218e26e2002-12-23 23:49:59 +000094 /// 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
98 /// are to match.
99 ///
100 bool debugPassMiscompilation(const PassInfo *ThePass,
Jeff Cohenea3e5e52005-04-22 04:13:13 +0000101 const std::string &ReferenceOutput);
Chris Lattner218e26e2002-12-23 23:49:59 +0000102
Misha Brukman50733362003-07-24 18:17:43 +0000103 /// compileSharedObject - This method creates a SharedObject from a given
104 /// BytecodeFile for debugging a code generator.
Chris Lattnera0f5b152003-10-14 21:09:11 +0000105 ///
106 std::string compileSharedObject(const std::string &BytecodeFile);
Misha Brukman50733362003-07-24 18:17:43 +0000107
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();
112
Misha Brukmanfe04db82003-07-28 20:59:16 +0000113 /// isExecutingJIT - Returns true if bugpoint is currently testing the JIT
114 ///
Misha Brukman91eabc12003-07-28 19:16:14 +0000115 bool isExecutingJIT();
116
Chris Lattner06905db2004-02-18 21:24:48 +0000117 /// 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);
124 return Result;
125 }
126
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000127 Module *getProgram() const { return Program; }
128
129 /// swapProgramIn - Set the current module to the specified module, returning
130 /// the old one.
131 Module *swapProgramIn(Module *M) {
132 Module *OldProgram = Program;
133 Program = M;
134 return OldProgram;
135 }
Chris Lattner06905db2004-02-18 21:24:48 +0000136
Chris Lattnera57d86b2004-04-05 22:58:16 +0000137 AbstractInterpreter *switchToCBE() {
138 AbstractInterpreter *Old = Interpreter;
139 Interpreter = (AbstractInterpreter*)cbe;
140 return Old;
141 }
142
143 void switchToInterpreter(AbstractInterpreter *AI) {
144 Interpreter = AI;
145 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000146
Chris Lattner06905db2004-02-18 21:24:48 +0000147 /// 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);
151
Chris Lattnerea9212c2004-02-18 23:25:22 +0000152 /// 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.
155 ///
156 void compileProgram(Module *M);
157
Chris Lattnera36ec882004-02-18 21:35:28 +0000158 /// 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.
162 ///
163 std::string executeProgram(std::string RequestedOutputFilename = "",
164 std::string Bytecode = "",
165 const std::string &SharedObjects = "",
166 AbstractInterpreter *AI = 0,
167 bool *ProgramExitedNonzero = 0);
168
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.
172 ///
173 std::string executeProgramWithCBE(std::string OutputFile = "");
174
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.
179 ///
180 bool diffProgram(const std::string &BytecodeFile = "",
181 const std::string &SharedObj = "",
182 bool RemoveBytecode = false);
Chris Lattner0cc88072004-02-18 21:50:26 +0000183 /// EmitProgressBytecode - This function is used to output the current Program
184 /// to a file named "bugpoint-ID.bc".
185 ///
186 void EmitProgressBytecode(const std::string &ID, bool NoFlyer = false);
187
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.
192 ///
193 Module *deleteInstructionFromProgram(const Instruction *I, unsigned Simp)
194 const;
195
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.
200 ///
201 Module *performFinalCleanups(Module *M, bool MayModifySemantics = false);
202
Chris Lattner7546c382004-03-14 20:02:07 +0000203 /// 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);
207
Chris Lattner5e783ab2004-05-11 21:54:13 +0000208 /// 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,
214 Module *M);
215
Chris Lattner3b6441e2004-03-14 21:17:03 +0000216 /// runPassesOn - Carefully run the specified set of pass on the specified
217 /// module, returning the transformed module on success, or a null pointer on
Chris Lattner0a002562004-03-14 21:21:57 +0000218 /// 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);
Chris Lattner3b6441e2004-03-14 21:17:03 +0000223
Chris Lattnerafade922002-11-20 22:28:10 +0000224 /// 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
Chris Lattner218e26e2002-12-23 23:49:59 +0000230 /// or failed, unless Quiet is set.
Chris Lattnerafade922002-11-20 22:28:10 +0000231 ///
232 bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
Chris Lattner218e26e2002-12-23 23:49:59 +0000233 std::string &OutputFilename, bool DeleteOutput = false,
Jeff Cohenea3e5e52005-04-22 04:13:13 +0000234 bool Quiet = false) const;
Chris Lattner11b8cd12004-04-05 22:01:48 +0000235
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000236 /// writeProgramToFile - This writes the current "Program" to the named
237 /// bytecode file. If an error occurs, true is returned.
238 ///
239 bool writeProgramToFile(const std::string &Filename, Module *M = 0) const;
Chris Lattnerafade922002-11-20 22:28:10 +0000240
Chris Lattner11b8cd12004-04-05 22:01:48 +0000241private:
Chris Lattnerafade922002-11-20 22:28:10 +0000242 /// 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).
245 ///
246 bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
247 bool DeleteOutput = true) const {
248 std::string Filename;
249 return runPasses(PassesToRun, Filename, DeleteOutput);
250 }
251
Chris Lattner218e26e2002-12-23 23:49:59 +0000252 /// initializeExecutionEnvironment - This method is used to set up the
253 /// environment for executing LLVM programs.
254 ///
255 bool initializeExecutionEnvironment();
Chris Lattnerafade922002-11-20 22:28:10 +0000256};
257
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000258/// ParseInputFile - Given a bytecode or assembly input filename, parse and
259/// return it, or return null if not possible.
260///
261Module *ParseInputFile(const std::string &InputFilename);
262
263
Chris Lattner640f22e2003-04-24 17:02:17 +0000264/// getPassesString - Turn a list of passes into a string which indicates the
265/// command line options that must be passed to add the passes.
266///
267std::string getPassesString(const std::vector<const PassInfo*> &Passes);
268
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000269/// PrintFunctionList - prints out list of problematic functions
270///
271void PrintFunctionList(const std::vector<Function*> &Funcs);
272
Chris Lattneraae33f92003-04-24 22:24:58 +0000273// DeleteFunctionBody - "Remove" the function by deleting all of it's basic
274// blocks, making it external.
275//
276void DeleteFunctionBody(Function *F);
277
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000278/// 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
280/// the new module.
281Module *SplitFunctionsOutOfModule(Module *M, const std::vector<Function*> &F);
282
Brian Gaeked0fde302003-11-11 22:41:34 +0000283} // End llvm namespace
284
Chris Lattnerafade922002-11-20 22:28:10 +0000285#endif