blob: a5f1e3f356382420e9059b1bd0de8195f93bfe90 [file] [log] [blame]
Chris Lattnerafade922002-11-20 22:28:10 +00001//===- BugDriver.h - Top-Level BugPoint class -------------------*- C++ -*-===//
John Criswell7c0e0222003-10-20 17:47:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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 Lattner218e26e2002-12-23 23:49:59 +000027class AbstractInterpreter;
Chris Lattner65207852003-01-23 02:48:33 +000028class Instruction;
Chris Lattnerafade922002-11-20 22:28:10 +000029
Chris Lattneraae33f92003-04-24 22:24:58 +000030class DebugCrashes;
Chris Lattner640f22e2003-04-24 17:02:17 +000031class ReduceMiscompilingPasses;
32class ReduceMiscompilingFunctions;
Chris Lattneraae33f92003-04-24 22:24:58 +000033class ReduceCrashingFunctions;
Chris Lattner286921e2003-04-24 23:51:38 +000034class ReduceCrashingBlocks;
Chris Lattner640f22e2003-04-24 17:02:17 +000035
Misha Brukmana259c9b2003-07-24 21:59:10 +000036class CBE;
37class GCC;
38
Chris Lattner47ae4a12003-08-05 15:51:05 +000039extern bool DisableSimplifyCFG;
40
Chris Lattnerafade922002-11-20 22:28:10 +000041class BugDriver {
42 const std::string ToolName; // Name of bugpoint
Misha Brukmana259c9b2003-07-24 21:59:10 +000043 std::string ReferenceOutputFile; // Name of `good' output file
Chris Lattnerafade922002-11-20 22:28:10 +000044 Module *Program; // The raw program, linked together
45 std::vector<const PassInfo*> PassesToRun;
Chris Lattner218e26e2002-12-23 23:49:59 +000046 AbstractInterpreter *Interpreter; // How to run the program
Misha Brukmana259c9b2003-07-24 21:59:10 +000047 CBE *cbe;
48 GCC *gcc;
Chris Lattner640f22e2003-04-24 17:02:17 +000049
Chris Lattneraae33f92003-04-24 22:24:58 +000050 // FIXME: sort out public/private distinctions...
Chris Lattner06905db2004-02-18 21:24:48 +000051 friend class ReducePassList;
Chris Lattner640f22e2003-04-24 17:02:17 +000052 friend class ReduceMiscompilingPasses;
53 friend class ReduceMiscompilingFunctions;
Misha Brukman50733362003-07-24 18:17:43 +000054 friend class ReduceMisCodegenFunctions;
Misha Brukman50733362003-07-24 18:17:43 +000055
Chris Lattnerafade922002-11-20 22:28:10 +000056public:
Misha Brukman50733362003-07-24 18:17:43 +000057 BugDriver(const char *toolname);
Chris Lattner218e26e2002-12-23 23:49:59 +000058
59 const std::string &getToolName() const { return ToolName; }
Chris Lattnerafade922002-11-20 22:28:10 +000060
61 // Set up methods... these methods are used to copy information about the
62 // command line arguments into instance variables of BugDriver.
63 //
64 bool addSources(const std::vector<std::string> &FileNames);
65 template<class It>
66 void addPasses(It I, It E) { PassesToRun.insert(PassesToRun.end(), I, E); }
Chris Lattner9c6cfe12003-10-17 23:03:16 +000067 void setPassesToRun(const std::vector<const PassInfo*> &PTR) {
68 PassesToRun = PTR;
69 }
Chris Lattnerafade922002-11-20 22:28:10 +000070
71 /// run - The top level method that is invoked after all of the instance
72 /// variables are set up from command line arguments.
73 ///
74 bool run();
75
Chris Lattner02526262004-02-18 21:02:04 +000076 /// debugOptimizerCrash - This method is called when some optimizer pass
77 /// crashes on input. It attempts to prune down the testcase to something
78 /// reasonable, and figure out exactly which pass is crashing.
Chris Lattnerafade922002-11-20 22:28:10 +000079 ///
Chris Lattner02526262004-02-18 21:02:04 +000080 bool debugOptimizerCrash();
81
82 /// debugCodeGeneratorCrash - This method is called when the code generator
83 /// crashes on an input. It attempts to reduce the input as much as possible
84 /// while still causing the code generator to crash.
85 bool debugCodeGeneratorCrash();
Chris Lattnerafade922002-11-20 22:28:10 +000086
Chris Lattnerafade922002-11-20 22:28:10 +000087 /// debugMiscompilation - This method is used when the passes selected are not
88 /// crashing, but the generated output is semantically different from the
89 /// input.
90 bool debugMiscompilation();
91
Chris Lattner218e26e2002-12-23 23:49:59 +000092 /// debugPassMiscompilation - This method is called when the specified pass
93 /// miscompiles Program as input. It tries to reduce the testcase to
94 /// something that smaller that still miscompiles the program.
95 /// ReferenceOutput contains the filename of the file containing the output we
96 /// are to match.
97 ///
98 bool debugPassMiscompilation(const PassInfo *ThePass,
99 const std::string &ReferenceOutput);
100
Misha Brukman50733362003-07-24 18:17:43 +0000101 /// compileSharedObject - This method creates a SharedObject from a given
102 /// BytecodeFile for debugging a code generator.
Chris Lattnera0f5b152003-10-14 21:09:11 +0000103 ///
104 std::string compileSharedObject(const std::string &BytecodeFile);
Misha Brukman50733362003-07-24 18:17:43 +0000105
106 /// debugCodeGenerator - This method narrows down a module to a function or
107 /// set of functions, using the CBE as a ``safe'' code generator for other
108 /// functions that are not under consideration.
109 bool debugCodeGenerator();
110
Misha Brukmanfe04db82003-07-28 20:59:16 +0000111 /// isExecutingJIT - Returns true if bugpoint is currently testing the JIT
112 ///
Misha Brukman91eabc12003-07-28 19:16:14 +0000113 bool isExecutingJIT();
114
Chris Lattner06905db2004-02-18 21:24:48 +0000115 /// runPasses - Run all of the passes in the "PassesToRun" list, discard the
116 /// output, and return true if any of the passes crashed.
117 bool runPasses(Module *M = 0) {
118 if (M == 0) M = Program;
119 std::swap(M, Program);
120 bool Result = runPasses(PassesToRun);
121 std::swap(M, Program);
122 return Result;
123 }
124
125 const Module *getProgram() const { return Program; }
126
127 /// setNewProgram - If we reduce or update the program somehow, call this
128 /// method to update bugdriver with it. This deletes the old module and sets
129 /// the specified one as the current program.
130 void setNewProgram(Module *M);
131
Chris Lattnerafade922002-11-20 22:28:10 +0000132private:
133 /// ParseInputFile - Given a bytecode or assembly input filename, parse and
134 /// return it, or return null if not possible.
135 ///
136 Module *ParseInputFile(const std::string &InputFilename) const;
137
Chris Lattnerafade922002-11-20 22:28:10 +0000138 /// writeProgramToFile - This writes the current "Program" to the named
139 /// bytecode file. If an error occurs, true is returned.
140 ///
Chris Lattner218e26e2002-12-23 23:49:59 +0000141 bool writeProgramToFile(const std::string &Filename, Module *M = 0) const;
Chris Lattnerafade922002-11-20 22:28:10 +0000142
143
144 /// EmitProgressBytecode - This function is used to output the current Program
Chris Lattnerfcb6ec02003-11-05 21:45:35 +0000145 /// to a file named "bugpoint-ID.bc".
Chris Lattnerafade922002-11-20 22:28:10 +0000146 ///
Chris Lattner640f22e2003-04-24 17:02:17 +0000147 void EmitProgressBytecode(const std::string &ID, bool NoFlyer = false);
Chris Lattnerafade922002-11-20 22:28:10 +0000148
149 /// runPasses - Run the specified passes on Program, outputting a bytecode
150 /// file and writting the filename into OutputFile if successful. If the
151 /// optimizations fail for some reason (optimizer crashes), return true,
152 /// otherwise return false. If DeleteOutput is set to true, the bytecode is
153 /// deleted on success, and the filename string is undefined. This prints to
154 /// cout a single line message indicating whether compilation was successful
Chris Lattner218e26e2002-12-23 23:49:59 +0000155 /// or failed, unless Quiet is set.
Chris Lattnerafade922002-11-20 22:28:10 +0000156 ///
157 bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
Chris Lattner218e26e2002-12-23 23:49:59 +0000158 std::string &OutputFilename, bool DeleteOutput = false,
159 bool Quiet = false) const;
Chris Lattnerafade922002-11-20 22:28:10 +0000160
161 /// runPasses - Just like the method above, but this just returns true or
162 /// false indicating whether or not the optimizer crashed on the specified
163 /// input (true = crashed).
164 ///
165 bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
166 bool DeleteOutput = true) const {
167 std::string Filename;
168 return runPasses(PassesToRun, Filename, DeleteOutput);
169 }
170
Misha Brukman50733362003-07-24 18:17:43 +0000171 /// PrintFunctionList - prints out list of problematic functions
Misha Brukmana259c9b2003-07-24 21:59:10 +0000172 ///
Misha Brukman50733362003-07-24 18:17:43 +0000173 static void PrintFunctionList(const std::vector<Function*> &Funcs);
174
Chris Lattner65207852003-01-23 02:48:33 +0000175 /// deleteInstructionFromProgram - This method clones the current Program and
176 /// deletes the specified instruction from the cloned module. It then runs a
177 /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code
178 /// which depends on the value. The modified module is then returned.
179 ///
180 Module *deleteInstructionFromProgram(Instruction *I, unsigned Simp) const;
181
Chris Lattnerba386d92003-02-28 16:13:20 +0000182 /// performFinalCleanups - This method clones the current Program and performs
Chris Lattner417477d2003-11-05 21:15:19 +0000183 /// a series of cleanups intended to get rid of extra cruft on the module. If
184 /// the MayModifySemantics argument is true, then the cleanups is allowed to
185 /// modify how the code behaves.
Chris Lattnerba386d92003-02-28 16:13:20 +0000186 ///
Chris Lattnerfcb6ec02003-11-05 21:45:35 +0000187 Module *performFinalCleanups(Module *M, bool MayModifySemantics = false);
Chris Lattnerba386d92003-02-28 16:13:20 +0000188
Chris Lattner218e26e2002-12-23 23:49:59 +0000189 /// initializeExecutionEnvironment - This method is used to set up the
190 /// environment for executing LLVM programs.
191 ///
192 bool initializeExecutionEnvironment();
193
194 /// executeProgram - This method runs "Program", capturing the output of the
195 /// program to a file, returning the filename of the file. A recommended
Chris Lattner7bb11542004-02-18 20:52:02 +0000196 /// filename may be optionally specified. If there is a problem with the code
197 /// generator (e.g., llc crashes), this will throw an exception.
Chris Lattner218e26e2002-12-23 23:49:59 +0000198 ///
199 std::string executeProgram(std::string RequestedOutputFilename = "",
Misha Brukman50733362003-07-24 18:17:43 +0000200 std::string Bytecode = "",
Chris Lattner769f1fe2003-10-14 21:59:36 +0000201 const std::string &SharedObjects = "",
Brian Gaekec5cad212004-02-11 18:37:32 +0000202 AbstractInterpreter *AI = 0,
203 bool *ProgramExitedNonzero = 0);
Misha Brukman50733362003-07-24 18:17:43 +0000204
205 /// executeProgramWithCBE - Used to create reference output with the C
Chris Lattner7bb11542004-02-18 20:52:02 +0000206 /// backend, if reference output is not provided. If there is a problem with
207 /// the code generator (e.g., llc crashes), this will throw an exception.
Chris Lattner769f1fe2003-10-14 21:59:36 +0000208 ///
Brian Gaekec5cad212004-02-11 18:37:32 +0000209 std::string executeProgramWithCBE(std::string OutputFile = "");
Chris Lattner218e26e2002-12-23 23:49:59 +0000210
211 /// diffProgram - This method executes the specified module and diffs the
212 /// output against the file specified by ReferenceOutputFile. If the output
Chris Lattner7bb11542004-02-18 20:52:02 +0000213 /// is different, true is returned. If there is a problem with the code
214 /// generator (e.g., llc crashes), this will throw an exception.
Chris Lattner218e26e2002-12-23 23:49:59 +0000215 ///
Misha Brukman50733362003-07-24 18:17:43 +0000216 bool diffProgram(const std::string &BytecodeFile = "",
Chris Lattner769f1fe2003-10-14 21:59:36 +0000217 const std::string &SharedObj = "",
Chris Lattner640f22e2003-04-24 17:02:17 +0000218 bool RemoveBytecode = false);
Chris Lattnerafade922002-11-20 22:28:10 +0000219};
220
Chris Lattner640f22e2003-04-24 17:02:17 +0000221/// getPassesString - Turn a list of passes into a string which indicates the
222/// command line options that must be passed to add the passes.
223///
224std::string getPassesString(const std::vector<const PassInfo*> &Passes);
225
Chris Lattneraae33f92003-04-24 22:24:58 +0000226// DeleteFunctionBody - "Remove" the function by deleting all of it's basic
227// blocks, making it external.
228//
229void DeleteFunctionBody(Function *F);
230
Brian Gaeked0fde302003-11-11 22:41:34 +0000231} // End llvm namespace
232
Chris Lattnerafade922002-11-20 22:28:10 +0000233#endif