blob: 43edf5c264f38a5a85e771ce2eef0d3ae5829865 [file] [log] [blame]
Chris Lattnerafade922002-11-20 22:28:10 +00001//===- BugDriver.h - Top-Level BugPoint class -------------------*- C++ -*-===//
2//
3// This class contains all of the shared state and information that is used by
4// the BugPoint tool to track down errors in optimizations. This class is the
5// main driver class that invokes all sub-functionality.
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef BUGDRIVER_H
10#define BUGDRIVER_H
11
12#include <vector>
13#include <string>
Misha Brukman50733362003-07-24 18:17:43 +000014
Chris Lattnerafade922002-11-20 22:28:10 +000015class PassInfo;
16class Module;
17class Function;
Chris Lattner218e26e2002-12-23 23:49:59 +000018class AbstractInterpreter;
Chris Lattner65207852003-01-23 02:48:33 +000019class Instruction;
Chris Lattnerafade922002-11-20 22:28:10 +000020
Chris Lattneraae33f92003-04-24 22:24:58 +000021class DebugCrashes;
Chris Lattner640f22e2003-04-24 17:02:17 +000022class ReduceMiscompilingPasses;
23class ReduceMiscompilingFunctions;
Chris Lattneraae33f92003-04-24 22:24:58 +000024class ReduceCrashingFunctions;
Chris Lattner286921e2003-04-24 23:51:38 +000025class ReduceCrashingBlocks;
Chris Lattner640f22e2003-04-24 17:02:17 +000026
Misha Brukmana259c9b2003-07-24 21:59:10 +000027class CBE;
28class GCC;
29
Chris Lattner47ae4a12003-08-05 15:51:05 +000030extern bool DisableSimplifyCFG;
31
Chris Lattnerafade922002-11-20 22:28:10 +000032class BugDriver {
33 const std::string ToolName; // Name of bugpoint
Misha Brukmana259c9b2003-07-24 21:59:10 +000034 std::string ReferenceOutputFile; // Name of `good' output file
Chris Lattnerafade922002-11-20 22:28:10 +000035 Module *Program; // The raw program, linked together
36 std::vector<const PassInfo*> PassesToRun;
Chris Lattner218e26e2002-12-23 23:49:59 +000037 AbstractInterpreter *Interpreter; // How to run the program
Misha Brukmana259c9b2003-07-24 21:59:10 +000038 CBE *cbe;
39 GCC *gcc;
Chris Lattner640f22e2003-04-24 17:02:17 +000040
Chris Lattneraae33f92003-04-24 22:24:58 +000041 // FIXME: sort out public/private distinctions...
42 friend class DebugCrashes;
Chris Lattner640f22e2003-04-24 17:02:17 +000043 friend class ReduceMiscompilingPasses;
44 friend class ReduceMiscompilingFunctions;
Misha Brukman50733362003-07-24 18:17:43 +000045 friend class ReduceMisCodegenFunctions;
Chris Lattneraae33f92003-04-24 22:24:58 +000046 friend class ReduceCrashingFunctions;
Chris Lattner286921e2003-04-24 23:51:38 +000047 friend class ReduceCrashingBlocks;
Misha Brukman50733362003-07-24 18:17:43 +000048
Chris Lattnerafade922002-11-20 22:28:10 +000049public:
Misha Brukman50733362003-07-24 18:17:43 +000050 BugDriver(const char *toolname);
Chris Lattner218e26e2002-12-23 23:49:59 +000051
52 const std::string &getToolName() const { return ToolName; }
Chris Lattnerafade922002-11-20 22:28:10 +000053
54 // Set up methods... these methods are used to copy information about the
55 // command line arguments into instance variables of BugDriver.
56 //
57 bool addSources(const std::vector<std::string> &FileNames);
58 template<class It>
59 void addPasses(It I, It E) { PassesToRun.insert(PassesToRun.end(), I, E); }
Chris Lattner9c6cfe12003-10-17 23:03:16 +000060 void setPassesToRun(const std::vector<const PassInfo*> &PTR) {
61 PassesToRun = PTR;
62 }
Chris Lattnerafade922002-11-20 22:28:10 +000063
64 /// run - The top level method that is invoked after all of the instance
65 /// variables are set up from command line arguments.
66 ///
67 bool run();
68
69 /// debugCrash - This method is called when some pass crashes on input. It
70 /// attempts to prune down the testcase to something reasonable, and figure
71 /// out exactly which pass is crashing.
72 ///
73 bool debugCrash();
74
Chris Lattnerafade922002-11-20 22:28:10 +000075 /// debugMiscompilation - This method is used when the passes selected are not
76 /// crashing, but the generated output is semantically different from the
77 /// input.
78 bool debugMiscompilation();
79
Chris Lattner218e26e2002-12-23 23:49:59 +000080 /// debugPassMiscompilation - This method is called when the specified pass
81 /// miscompiles Program as input. It tries to reduce the testcase to
82 /// something that smaller that still miscompiles the program.
83 /// ReferenceOutput contains the filename of the file containing the output we
84 /// are to match.
85 ///
86 bool debugPassMiscompilation(const PassInfo *ThePass,
87 const std::string &ReferenceOutput);
88
Misha Brukman50733362003-07-24 18:17:43 +000089 /// compileSharedObject - This method creates a SharedObject from a given
90 /// BytecodeFile for debugging a code generator.
Chris Lattnera0f5b152003-10-14 21:09:11 +000091 ///
92 std::string compileSharedObject(const std::string &BytecodeFile);
Misha Brukman50733362003-07-24 18:17:43 +000093
94 /// debugCodeGenerator - This method narrows down a module to a function or
95 /// set of functions, using the CBE as a ``safe'' code generator for other
96 /// functions that are not under consideration.
97 bool debugCodeGenerator();
98
Misha Brukmanfe04db82003-07-28 20:59:16 +000099 /// isExecutingJIT - Returns true if bugpoint is currently testing the JIT
100 ///
Misha Brukman91eabc12003-07-28 19:16:14 +0000101 bool isExecutingJIT();
102
Chris Lattnerafade922002-11-20 22:28:10 +0000103private:
104 /// ParseInputFile - Given a bytecode or assembly input filename, parse and
105 /// return it, or return null if not possible.
106 ///
107 Module *ParseInputFile(const std::string &InputFilename) const;
108
Chris Lattnerafade922002-11-20 22:28:10 +0000109 /// writeProgramToFile - This writes the current "Program" to the named
110 /// bytecode file. If an error occurs, true is returned.
111 ///
Chris Lattner218e26e2002-12-23 23:49:59 +0000112 bool writeProgramToFile(const std::string &Filename, Module *M = 0) const;
Chris Lattnerafade922002-11-20 22:28:10 +0000113
114
115 /// EmitProgressBytecode - This function is used to output the current Program
116 /// to a file named "bugpoing-ID.bc".
117 ///
Chris Lattner640f22e2003-04-24 17:02:17 +0000118 void EmitProgressBytecode(const std::string &ID, bool NoFlyer = false);
Chris Lattnerafade922002-11-20 22:28:10 +0000119
120 /// runPasses - Run the specified passes on Program, outputting a bytecode
121 /// file and writting the filename into OutputFile if successful. If the
122 /// optimizations fail for some reason (optimizer crashes), return true,
123 /// otherwise return false. If DeleteOutput is set to true, the bytecode is
124 /// deleted on success, and the filename string is undefined. This prints to
125 /// cout a single line message indicating whether compilation was successful
Chris Lattner218e26e2002-12-23 23:49:59 +0000126 /// or failed, unless Quiet is set.
Chris Lattnerafade922002-11-20 22:28:10 +0000127 ///
128 bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
Chris Lattner218e26e2002-12-23 23:49:59 +0000129 std::string &OutputFilename, bool DeleteOutput = false,
130 bool Quiet = false) const;
Chris Lattnerafade922002-11-20 22:28:10 +0000131
132 /// runPasses - Just like the method above, but this just returns true or
133 /// false indicating whether or not the optimizer crashed on the specified
134 /// input (true = crashed).
135 ///
136 bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
137 bool DeleteOutput = true) const {
138 std::string Filename;
139 return runPasses(PassesToRun, Filename, DeleteOutput);
140 }
141
Misha Brukman50733362003-07-24 18:17:43 +0000142 /// PrintFunctionList - prints out list of problematic functions
Misha Brukmana259c9b2003-07-24 21:59:10 +0000143 ///
Misha Brukman50733362003-07-24 18:17:43 +0000144 static void PrintFunctionList(const std::vector<Function*> &Funcs);
145
Chris Lattner65207852003-01-23 02:48:33 +0000146 /// deleteInstructionFromProgram - This method clones the current Program and
147 /// deletes the specified instruction from the cloned module. It then runs a
148 /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code
149 /// which depends on the value. The modified module is then returned.
150 ///
151 Module *deleteInstructionFromProgram(Instruction *I, unsigned Simp) const;
152
Chris Lattnerba386d92003-02-28 16:13:20 +0000153 /// performFinalCleanups - This method clones the current Program and performs
154 /// a series of cleanups intended to get rid of extra cruft on the module
Chris Lattner587a6ce2003-08-01 16:13:49 +0000155 /// before handing it to the user... if the module parameter is specified, it
156 /// operates directly on the specified Module, modifying it in place.
Chris Lattnerba386d92003-02-28 16:13:20 +0000157 ///
Chris Lattner587a6ce2003-08-01 16:13:49 +0000158 Module *performFinalCleanups(Module *M = 0) const;
Chris Lattnerba386d92003-02-28 16:13:20 +0000159
Chris Lattner218e26e2002-12-23 23:49:59 +0000160 /// initializeExecutionEnvironment - This method is used to set up the
161 /// environment for executing LLVM programs.
162 ///
163 bool initializeExecutionEnvironment();
164
165 /// executeProgram - This method runs "Program", capturing the output of the
166 /// program to a file, returning the filename of the file. A recommended
167 /// filename may be optionally specified.
168 ///
169 std::string executeProgram(std::string RequestedOutputFilename = "",
Misha Brukman50733362003-07-24 18:17:43 +0000170 std::string Bytecode = "",
Chris Lattner769f1fe2003-10-14 21:59:36 +0000171 const std::string &SharedObjects = "",
Misha Brukman50733362003-07-24 18:17:43 +0000172 AbstractInterpreter *AI = 0);
173
174 /// executeProgramWithCBE - Used to create reference output with the C
175 /// backend, if reference output is not provided.
Chris Lattner769f1fe2003-10-14 21:59:36 +0000176 ///
177 std::string executeProgramWithCBE(std::string OutputFile = "",
178 std::string BytecodeFile = "",
179 const std::string &SharedObj = "") {
180 return executeProgram(OutputFile, BytecodeFile, SharedObj,
181 (AbstractInterpreter*)cbe);
182 }
Chris Lattner218e26e2002-12-23 23:49:59 +0000183
184 /// diffProgram - This method executes the specified module and diffs the
185 /// output against the file specified by ReferenceOutputFile. If the output
186 /// is different, true is returned.
187 ///
Misha Brukman50733362003-07-24 18:17:43 +0000188 bool diffProgram(const std::string &BytecodeFile = "",
Chris Lattner769f1fe2003-10-14 21:59:36 +0000189 const std::string &SharedObj = "",
Chris Lattner640f22e2003-04-24 17:02:17 +0000190 bool RemoveBytecode = false);
Chris Lattnerafade922002-11-20 22:28:10 +0000191};
192
Chris Lattner640f22e2003-04-24 17:02:17 +0000193/// getPassesString - Turn a list of passes into a string which indicates the
194/// command line options that must be passed to add the passes.
195///
196std::string getPassesString(const std::vector<const PassInfo*> &Passes);
197
Chris Lattneraae33f92003-04-24 22:24:58 +0000198// DeleteFunctionBody - "Remove" the function by deleting all of it's basic
199// blocks, making it external.
200//
201void DeleteFunctionBody(Function *F);
202
Chris Lattnerafade922002-11-20 22:28:10 +0000203#endif