blob: cf2ae58fcdd5ad1e9c856b55f018051cbca4f881 [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 Lattnerafade922002-11-20 22:28:10 +000030class BugDriver {
31 const std::string ToolName; // Name of bugpoint
Misha Brukmana259c9b2003-07-24 21:59:10 +000032 std::string ReferenceOutputFile; // Name of `good' output file
Chris Lattnerafade922002-11-20 22:28:10 +000033 Module *Program; // The raw program, linked together
34 std::vector<const PassInfo*> PassesToRun;
Chris Lattner218e26e2002-12-23 23:49:59 +000035 AbstractInterpreter *Interpreter; // How to run the program
Misha Brukmana259c9b2003-07-24 21:59:10 +000036 CBE *cbe;
37 GCC *gcc;
Chris Lattner640f22e2003-04-24 17:02:17 +000038
Chris Lattneraae33f92003-04-24 22:24:58 +000039 // FIXME: sort out public/private distinctions...
40 friend class DebugCrashes;
Chris Lattner640f22e2003-04-24 17:02:17 +000041 friend class ReduceMiscompilingPasses;
42 friend class ReduceMiscompilingFunctions;
Misha Brukman50733362003-07-24 18:17:43 +000043 friend class ReduceMisCodegenFunctions;
Chris Lattneraae33f92003-04-24 22:24:58 +000044 friend class ReduceCrashingFunctions;
Chris Lattner286921e2003-04-24 23:51:38 +000045 friend class ReduceCrashingBlocks;
Misha Brukman50733362003-07-24 18:17:43 +000046
Chris Lattnerafade922002-11-20 22:28:10 +000047public:
Misha Brukman50733362003-07-24 18:17:43 +000048 BugDriver(const char *toolname);
Chris Lattner218e26e2002-12-23 23:49:59 +000049
50 const std::string &getToolName() const { return ToolName; }
Chris Lattnerafade922002-11-20 22:28:10 +000051
52 // Set up methods... these methods are used to copy information about the
53 // command line arguments into instance variables of BugDriver.
54 //
55 bool addSources(const std::vector<std::string> &FileNames);
56 template<class It>
57 void addPasses(It I, It E) { PassesToRun.insert(PassesToRun.end(), I, E); }
58
59 /// run - The top level method that is invoked after all of the instance
60 /// variables are set up from command line arguments.
61 ///
62 bool run();
63
64 /// debugCrash - This method is called when some pass crashes on input. It
65 /// attempts to prune down the testcase to something reasonable, and figure
66 /// out exactly which pass is crashing.
67 ///
68 bool debugCrash();
69
Chris Lattnerafade922002-11-20 22:28:10 +000070 /// debugMiscompilation - This method is used when the passes selected are not
71 /// crashing, but the generated output is semantically different from the
72 /// input.
73 bool debugMiscompilation();
74
Chris Lattner218e26e2002-12-23 23:49:59 +000075 /// debugPassMiscompilation - This method is called when the specified pass
76 /// miscompiles Program as input. It tries to reduce the testcase to
77 /// something that smaller that still miscompiles the program.
78 /// ReferenceOutput contains the filename of the file containing the output we
79 /// are to match.
80 ///
81 bool debugPassMiscompilation(const PassInfo *ThePass,
82 const std::string &ReferenceOutput);
83
Misha Brukman50733362003-07-24 18:17:43 +000084 /// compileSharedObject - This method creates a SharedObject from a given
85 /// BytecodeFile for debugging a code generator.
86 int compileSharedObject(const std::string &BytecodeFile,
87 std::string &SharedObject);
88
89 /// debugCodeGenerator - This method narrows down a module to a function or
90 /// set of functions, using the CBE as a ``safe'' code generator for other
91 /// functions that are not under consideration.
92 bool debugCodeGenerator();
93
Misha Brukman91eabc12003-07-28 19:16:14 +000094 bool isExecutingJIT();
95
Chris Lattnerafade922002-11-20 22:28:10 +000096private:
97 /// ParseInputFile - Given a bytecode or assembly input filename, parse and
98 /// return it, or return null if not possible.
99 ///
100 Module *ParseInputFile(const std::string &InputFilename) const;
101
Chris Lattnerafade922002-11-20 22:28:10 +0000102 /// writeProgramToFile - This writes the current "Program" to the named
103 /// bytecode file. If an error occurs, true is returned.
104 ///
Chris Lattner218e26e2002-12-23 23:49:59 +0000105 bool writeProgramToFile(const std::string &Filename, Module *M = 0) const;
Chris Lattnerafade922002-11-20 22:28:10 +0000106
107
108 /// EmitProgressBytecode - This function is used to output the current Program
109 /// to a file named "bugpoing-ID.bc".
110 ///
Chris Lattner640f22e2003-04-24 17:02:17 +0000111 void EmitProgressBytecode(const std::string &ID, bool NoFlyer = false);
Chris Lattnerafade922002-11-20 22:28:10 +0000112
113 /// runPasses - Run the specified passes on Program, outputting a bytecode
114 /// file and writting the filename into OutputFile if successful. If the
115 /// optimizations fail for some reason (optimizer crashes), return true,
116 /// otherwise return false. If DeleteOutput is set to true, the bytecode is
117 /// deleted on success, and the filename string is undefined. This prints to
118 /// cout a single line message indicating whether compilation was successful
Chris Lattner218e26e2002-12-23 23:49:59 +0000119 /// or failed, unless Quiet is set.
Chris Lattnerafade922002-11-20 22:28:10 +0000120 ///
121 bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
Chris Lattner218e26e2002-12-23 23:49:59 +0000122 std::string &OutputFilename, bool DeleteOutput = false,
123 bool Quiet = false) const;
Chris Lattnerafade922002-11-20 22:28:10 +0000124
125 /// runPasses - Just like the method above, but this just returns true or
126 /// false indicating whether or not the optimizer crashed on the specified
127 /// input (true = crashed).
128 ///
129 bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
130 bool DeleteOutput = true) const {
131 std::string Filename;
132 return runPasses(PassesToRun, Filename, DeleteOutput);
133 }
134
Misha Brukman50733362003-07-24 18:17:43 +0000135 /// PrintFunctionList - prints out list of problematic functions
Misha Brukmana259c9b2003-07-24 21:59:10 +0000136 ///
Misha Brukman50733362003-07-24 18:17:43 +0000137 static void PrintFunctionList(const std::vector<Function*> &Funcs);
138
Chris Lattner65207852003-01-23 02:48:33 +0000139 /// deleteInstructionFromProgram - This method clones the current Program and
140 /// deletes the specified instruction from the cloned module. It then runs a
141 /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code
142 /// which depends on the value. The modified module is then returned.
143 ///
144 Module *deleteInstructionFromProgram(Instruction *I, unsigned Simp) const;
145
Chris Lattnerba386d92003-02-28 16:13:20 +0000146 /// performFinalCleanups - This method clones the current Program and performs
147 /// a series of cleanups intended to get rid of extra cruft on the module
148 /// before handing it to the user...
149 ///
150 Module *performFinalCleanups() const;
151
Chris Lattner218e26e2002-12-23 23:49:59 +0000152 /// initializeExecutionEnvironment - This method is used to set up the
153 /// environment for executing LLVM programs.
154 ///
155 bool initializeExecutionEnvironment();
156
157 /// executeProgram - This method runs "Program", capturing the output of the
158 /// program to a file, returning the filename of the file. A recommended
159 /// filename may be optionally specified.
160 ///
161 std::string executeProgram(std::string RequestedOutputFilename = "",
Misha Brukman50733362003-07-24 18:17:43 +0000162 std::string Bytecode = "",
163 std::string SharedObject = "",
164 AbstractInterpreter *AI = 0);
165
166 /// executeProgramWithCBE - Used to create reference output with the C
167 /// backend, if reference output is not provided.
168 std::string executeProgramWithCBE(std::string RequestedOutputFilename = "",
169 std::string Bytecode = "",
170 std::string SharedObject = "");
Chris Lattner218e26e2002-12-23 23:49:59 +0000171
172 /// diffProgram - This method executes the specified module and diffs the
173 /// output against the file specified by ReferenceOutputFile. If the output
174 /// is different, true is returned.
175 ///
Misha Brukman50733362003-07-24 18:17:43 +0000176 bool diffProgram(const std::string &BytecodeFile = "",
177 const std::string &SharedObject = "",
Chris Lattner640f22e2003-04-24 17:02:17 +0000178 bool RemoveBytecode = false);
Chris Lattnerafade922002-11-20 22:28:10 +0000179};
180
Chris Lattner640f22e2003-04-24 17:02:17 +0000181/// getPassesString - Turn a list of passes into a string which indicates the
182/// command line options that must be passed to add the passes.
183///
184std::string getPassesString(const std::vector<const PassInfo*> &Passes);
185
Chris Lattneraae33f92003-04-24 22:24:58 +0000186// DeleteFunctionBody - "Remove" the function by deleting all of it's basic
187// blocks, making it external.
188//
189void DeleteFunctionBody(Function *F);
190
Chris Lattnerafade922002-11-20 22:28:10 +0000191#endif