blob: 05d4108824af23ecf04e515601fa19b897e31909 [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>
14class PassInfo;
15class Module;
16class Function;
Chris Lattner218e26e2002-12-23 23:49:59 +000017class AbstractInterpreter;
Chris Lattner65207852003-01-23 02:48:33 +000018class Instruction;
Chris Lattnerafade922002-11-20 22:28:10 +000019
Chris Lattneraae33f92003-04-24 22:24:58 +000020class DebugCrashes;
Chris Lattner640f22e2003-04-24 17:02:17 +000021class ReduceMiscompilingPasses;
22class ReduceMiscompilingFunctions;
Chris Lattneraae33f92003-04-24 22:24:58 +000023class ReduceCrashingFunctions;
Chris Lattner286921e2003-04-24 23:51:38 +000024class ReduceCrashingBlocks;
Chris Lattner640f22e2003-04-24 17:02:17 +000025
Chris Lattnerafade922002-11-20 22:28:10 +000026class BugDriver {
27 const std::string ToolName; // Name of bugpoint
28 Module *Program; // The raw program, linked together
29 std::vector<const PassInfo*> PassesToRun;
Chris Lattner218e26e2002-12-23 23:49:59 +000030 AbstractInterpreter *Interpreter; // How to run the program
Chris Lattner640f22e2003-04-24 17:02:17 +000031
Chris Lattneraae33f92003-04-24 22:24:58 +000032 // FIXME: sort out public/private distinctions...
33 friend class DebugCrashes;
Chris Lattner640f22e2003-04-24 17:02:17 +000034 friend class ReduceMiscompilingPasses;
35 friend class ReduceMiscompilingFunctions;
Chris Lattneraae33f92003-04-24 22:24:58 +000036 friend class ReduceCrashingFunctions;
Chris Lattner286921e2003-04-24 23:51:38 +000037 friend class ReduceCrashingBlocks;
Chris Lattnerafade922002-11-20 22:28:10 +000038public:
Chris Lattner218e26e2002-12-23 23:49:59 +000039 BugDriver(const char *toolname)
40 : ToolName(toolname), Program(0), Interpreter(0) {}
41
42 const std::string &getToolName() const { return ToolName; }
Chris Lattnerafade922002-11-20 22:28:10 +000043
44 // Set up methods... these methods are used to copy information about the
45 // command line arguments into instance variables of BugDriver.
46 //
47 bool addSources(const std::vector<std::string> &FileNames);
48 template<class It>
49 void addPasses(It I, It E) { PassesToRun.insert(PassesToRun.end(), I, E); }
50
51 /// run - The top level method that is invoked after all of the instance
52 /// variables are set up from command line arguments.
53 ///
54 bool run();
55
56 /// debugCrash - This method is called when some pass crashes on input. It
57 /// attempts to prune down the testcase to something reasonable, and figure
58 /// out exactly which pass is crashing.
59 ///
60 bool debugCrash();
61
Chris Lattnerafade922002-11-20 22:28:10 +000062 /// debugMiscompilation - This method is used when the passes selected are not
63 /// crashing, but the generated output is semantically different from the
64 /// input.
65 bool debugMiscompilation();
66
Chris Lattner218e26e2002-12-23 23:49:59 +000067 /// debugPassMiscompilation - This method is called when the specified pass
68 /// miscompiles Program as input. It tries to reduce the testcase to
69 /// something that smaller that still miscompiles the program.
70 /// ReferenceOutput contains the filename of the file containing the output we
71 /// are to match.
72 ///
73 bool debugPassMiscompilation(const PassInfo *ThePass,
74 const std::string &ReferenceOutput);
75
Chris Lattnerafade922002-11-20 22:28:10 +000076private:
77 /// ParseInputFile - Given a bytecode or assembly input filename, parse and
78 /// return it, or return null if not possible.
79 ///
80 Module *ParseInputFile(const std::string &InputFilename) const;
81
Chris Lattnerafade922002-11-20 22:28:10 +000082 /// writeProgramToFile - This writes the current "Program" to the named
83 /// bytecode file. If an error occurs, true is returned.
84 ///
Chris Lattner218e26e2002-12-23 23:49:59 +000085 bool writeProgramToFile(const std::string &Filename, Module *M = 0) const;
Chris Lattnerafade922002-11-20 22:28:10 +000086
87
88 /// EmitProgressBytecode - This function is used to output the current Program
89 /// to a file named "bugpoing-ID.bc".
90 ///
Chris Lattner640f22e2003-04-24 17:02:17 +000091 void EmitProgressBytecode(const std::string &ID, bool NoFlyer = false);
Chris Lattnerafade922002-11-20 22:28:10 +000092
93 /// runPasses - Run the specified passes on Program, outputting a bytecode
94 /// file and writting the filename into OutputFile if successful. If the
95 /// optimizations fail for some reason (optimizer crashes), return true,
96 /// otherwise return false. If DeleteOutput is set to true, the bytecode is
97 /// deleted on success, and the filename string is undefined. This prints to
98 /// cout a single line message indicating whether compilation was successful
Chris Lattner218e26e2002-12-23 23:49:59 +000099 /// or failed, unless Quiet is set.
Chris Lattnerafade922002-11-20 22:28:10 +0000100 ///
101 bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
Chris Lattner218e26e2002-12-23 23:49:59 +0000102 std::string &OutputFilename, bool DeleteOutput = false,
103 bool Quiet = false) const;
Chris Lattnerafade922002-11-20 22:28:10 +0000104
105 /// runPasses - Just like the method above, but this just returns true or
106 /// false indicating whether or not the optimizer crashed on the specified
107 /// input (true = crashed).
108 ///
109 bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
110 bool DeleteOutput = true) const {
111 std::string Filename;
112 return runPasses(PassesToRun, Filename, DeleteOutput);
113 }
114
Chris Lattner65207852003-01-23 02:48:33 +0000115 /// deleteInstructionFromProgram - This method clones the current Program and
116 /// deletes the specified instruction from the cloned module. It then runs a
117 /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code
118 /// which depends on the value. The modified module is then returned.
119 ///
120 Module *deleteInstructionFromProgram(Instruction *I, unsigned Simp) const;
121
Chris Lattnerba386d92003-02-28 16:13:20 +0000122 /// performFinalCleanups - This method clones the current Program and performs
123 /// a series of cleanups intended to get rid of extra cruft on the module
124 /// before handing it to the user...
125 ///
126 Module *performFinalCleanups() const;
127
Chris Lattner218e26e2002-12-23 23:49:59 +0000128 /// initializeExecutionEnvironment - This method is used to set up the
129 /// environment for executing LLVM programs.
130 ///
131 bool initializeExecutionEnvironment();
132
133 /// executeProgram - This method runs "Program", capturing the output of the
134 /// program to a file, returning the filename of the file. A recommended
135 /// filename may be optionally specified.
136 ///
137 std::string executeProgram(std::string RequestedOutputFilename = "",
138 std::string Bytecode = "");
139
140 /// diffProgram - This method executes the specified module and diffs the
141 /// output against the file specified by ReferenceOutputFile. If the output
142 /// is different, true is returned.
143 ///
144 bool diffProgram(const std::string &ReferenceOutputFile,
Chris Lattner640f22e2003-04-24 17:02:17 +0000145 const std::string &BytecodeFile = "",
146 bool RemoveBytecode = false);
Chris Lattnerafade922002-11-20 22:28:10 +0000147};
148
Chris Lattner640f22e2003-04-24 17:02:17 +0000149/// getPassesString - Turn a list of passes into a string which indicates the
150/// command line options that must be passed to add the passes.
151///
152std::string getPassesString(const std::vector<const PassInfo*> &Passes);
153
Chris Lattneraae33f92003-04-24 22:24:58 +0000154// DeleteFunctionBody - "Remove" the function by deleting all of it's basic
155// blocks, making it external.
156//
157void DeleteFunctionBody(Function *F);
158
Chris Lattnerafade922002-11-20 22:28:10 +0000159#endif