blob: 0c461d1e1722650dfc7ad68b2e34dd42f6e10b18 [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
Misha Brukman50733362003-07-24 18:17:43 +000012#include "Support/CommandLine.h"
Chris Lattnerafade922002-11-20 22:28:10 +000013#include <vector>
14#include <string>
Misha Brukman50733362003-07-24 18:17:43 +000015
Chris Lattnerafade922002-11-20 22:28:10 +000016class PassInfo;
17class Module;
18class Function;
Chris Lattner218e26e2002-12-23 23:49:59 +000019class AbstractInterpreter;
Chris Lattner65207852003-01-23 02:48:33 +000020class Instruction;
Chris Lattnerafade922002-11-20 22:28:10 +000021
Chris Lattneraae33f92003-04-24 22:24:58 +000022class DebugCrashes;
Chris Lattner640f22e2003-04-24 17:02:17 +000023class ReduceMiscompilingPasses;
24class ReduceMiscompilingFunctions;
Chris Lattneraae33f92003-04-24 22:24:58 +000025class ReduceCrashingFunctions;
Chris Lattner286921e2003-04-24 23:51:38 +000026class ReduceCrashingBlocks;
Chris Lattner640f22e2003-04-24 17:02:17 +000027
Chris Lattnerafade922002-11-20 22:28:10 +000028class BugDriver {
29 const std::string ToolName; // Name of bugpoint
Misha Brukman50733362003-07-24 18:17:43 +000030 cl::opt<std::string> ReferenceOutputFile; // Name of `good' output file
Chris Lattnerafade922002-11-20 22:28:10 +000031 Module *Program; // The raw program, linked together
32 std::vector<const PassInfo*> PassesToRun;
Chris Lattner218e26e2002-12-23 23:49:59 +000033 AbstractInterpreter *Interpreter; // How to run the program
Chris Lattner640f22e2003-04-24 17:02:17 +000034
Chris Lattneraae33f92003-04-24 22:24:58 +000035 // FIXME: sort out public/private distinctions...
36 friend class DebugCrashes;
Chris Lattner640f22e2003-04-24 17:02:17 +000037 friend class ReduceMiscompilingPasses;
38 friend class ReduceMiscompilingFunctions;
Misha Brukman50733362003-07-24 18:17:43 +000039 friend class ReduceMisCodegenFunctions;
Chris Lattneraae33f92003-04-24 22:24:58 +000040 friend class ReduceCrashingFunctions;
Chris Lattner286921e2003-04-24 23:51:38 +000041 friend class ReduceCrashingBlocks;
Misha Brukman50733362003-07-24 18:17:43 +000042
Chris Lattnerafade922002-11-20 22:28:10 +000043public:
Misha Brukman50733362003-07-24 18:17:43 +000044 BugDriver(const char *toolname);
Chris Lattner218e26e2002-12-23 23:49:59 +000045
46 const std::string &getToolName() const { return ToolName; }
Chris Lattnerafade922002-11-20 22:28:10 +000047
48 // Set up methods... these methods are used to copy information about the
49 // command line arguments into instance variables of BugDriver.
50 //
51 bool addSources(const std::vector<std::string> &FileNames);
52 template<class It>
53 void addPasses(It I, It E) { PassesToRun.insert(PassesToRun.end(), I, E); }
54
55 /// run - The top level method that is invoked after all of the instance
56 /// variables are set up from command line arguments.
57 ///
58 bool run();
59
60 /// debugCrash - This method is called when some pass crashes on input. It
61 /// attempts to prune down the testcase to something reasonable, and figure
62 /// out exactly which pass is crashing.
63 ///
64 bool debugCrash();
65
Chris Lattnerafade922002-11-20 22:28:10 +000066 /// debugMiscompilation - This method is used when the passes selected are not
67 /// crashing, but the generated output is semantically different from the
68 /// input.
69 bool debugMiscompilation();
70
Chris Lattner218e26e2002-12-23 23:49:59 +000071 /// debugPassMiscompilation - This method is called when the specified pass
72 /// miscompiles Program as input. It tries to reduce the testcase to
73 /// something that smaller that still miscompiles the program.
74 /// ReferenceOutput contains the filename of the file containing the output we
75 /// are to match.
76 ///
77 bool debugPassMiscompilation(const PassInfo *ThePass,
78 const std::string &ReferenceOutput);
79
Misha Brukman50733362003-07-24 18:17:43 +000080
81 /// compileSharedObject - This method creates a SharedObject from a given
82 /// BytecodeFile for debugging a code generator.
83 int compileSharedObject(const std::string &BytecodeFile,
84 std::string &SharedObject);
85
86 /// debugCodeGenerator - This method narrows down a module to a function or
87 /// set of functions, using the CBE as a ``safe'' code generator for other
88 /// functions that are not under consideration.
89 bool debugCodeGenerator();
90
Chris Lattnerafade922002-11-20 22:28:10 +000091private:
92 /// ParseInputFile - Given a bytecode or assembly input filename, parse and
93 /// return it, or return null if not possible.
94 ///
95 Module *ParseInputFile(const std::string &InputFilename) const;
96
Chris Lattnerafade922002-11-20 22:28:10 +000097 /// writeProgramToFile - This writes the current "Program" to the named
98 /// bytecode file. If an error occurs, true is returned.
99 ///
Chris Lattner218e26e2002-12-23 23:49:59 +0000100 bool writeProgramToFile(const std::string &Filename, Module *M = 0) const;
Chris Lattnerafade922002-11-20 22:28:10 +0000101
102
103 /// EmitProgressBytecode - This function is used to output the current Program
104 /// to a file named "bugpoing-ID.bc".
105 ///
Chris Lattner640f22e2003-04-24 17:02:17 +0000106 void EmitProgressBytecode(const std::string &ID, bool NoFlyer = false);
Chris Lattnerafade922002-11-20 22:28:10 +0000107
108 /// runPasses - Run the specified passes on Program, outputting a bytecode
109 /// file and writting the filename into OutputFile if successful. If the
110 /// optimizations fail for some reason (optimizer crashes), return true,
111 /// otherwise return false. If DeleteOutput is set to true, the bytecode is
112 /// deleted on success, and the filename string is undefined. This prints to
113 /// cout a single line message indicating whether compilation was successful
Chris Lattner218e26e2002-12-23 23:49:59 +0000114 /// or failed, unless Quiet is set.
Chris Lattnerafade922002-11-20 22:28:10 +0000115 ///
116 bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
Chris Lattner218e26e2002-12-23 23:49:59 +0000117 std::string &OutputFilename, bool DeleteOutput = false,
118 bool Quiet = false) const;
Chris Lattnerafade922002-11-20 22:28:10 +0000119
120 /// runPasses - Just like the method above, but this just returns true or
121 /// false indicating whether or not the optimizer crashed on the specified
122 /// input (true = crashed).
123 ///
124 bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
125 bool DeleteOutput = true) const {
126 std::string Filename;
127 return runPasses(PassesToRun, Filename, DeleteOutput);
128 }
129
Misha Brukman50733362003-07-24 18:17:43 +0000130 /// PrintFunctionList - prints out list of problematic functions
131 static void PrintFunctionList(const std::vector<Function*> &Funcs);
132
Chris Lattner65207852003-01-23 02:48:33 +0000133 /// deleteInstructionFromProgram - This method clones the current Program and
134 /// deletes the specified instruction from the cloned module. It then runs a
135 /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code
136 /// which depends on the value. The modified module is then returned.
137 ///
138 Module *deleteInstructionFromProgram(Instruction *I, unsigned Simp) const;
139
Chris Lattnerba386d92003-02-28 16:13:20 +0000140 /// performFinalCleanups - This method clones the current Program and performs
141 /// a series of cleanups intended to get rid of extra cruft on the module
142 /// before handing it to the user...
143 ///
144 Module *performFinalCleanups() const;
145
Chris Lattner218e26e2002-12-23 23:49:59 +0000146 /// initializeExecutionEnvironment - This method is used to set up the
147 /// environment for executing LLVM programs.
148 ///
149 bool initializeExecutionEnvironment();
150
151 /// executeProgram - This method runs "Program", capturing the output of the
152 /// program to a file, returning the filename of the file. A recommended
153 /// filename may be optionally specified.
154 ///
155 std::string executeProgram(std::string RequestedOutputFilename = "",
Misha Brukman50733362003-07-24 18:17:43 +0000156 std::string Bytecode = "",
157 std::string SharedObject = "",
158 AbstractInterpreter *AI = 0);
159
160 /// executeProgramWithCBE - Used to create reference output with the C
161 /// backend, if reference output is not provided.
162 std::string executeProgramWithCBE(std::string RequestedOutputFilename = "",
163 std::string Bytecode = "",
164 std::string SharedObject = "");
Chris Lattner218e26e2002-12-23 23:49:59 +0000165
166 /// diffProgram - This method executes the specified module and diffs the
167 /// output against the file specified by ReferenceOutputFile. If the output
168 /// is different, true is returned.
169 ///
Misha Brukman50733362003-07-24 18:17:43 +0000170 bool diffProgram(const std::string &BytecodeFile = "",
171 const std::string &SharedObject = "",
Chris Lattner640f22e2003-04-24 17:02:17 +0000172 bool RemoveBytecode = false);
Chris Lattnerafade922002-11-20 22:28:10 +0000173};
174
Chris Lattner640f22e2003-04-24 17:02:17 +0000175/// getPassesString - Turn a list of passes into a string which indicates the
176/// command line options that must be passed to add the passes.
177///
178std::string getPassesString(const std::vector<const PassInfo*> &Passes);
179
Chris Lattneraae33f92003-04-24 22:24:58 +0000180// DeleteFunctionBody - "Remove" the function by deleting all of it's basic
181// blocks, making it external.
182//
183void DeleteFunctionBody(Function *F);
184
Chris Lattnerafade922002-11-20 22:28:10 +0000185#endif