blob: 491bee9b8fcef404404bf3f9edbe0b86167997ac [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//===----------------------------------------------------------------------===//
9//
Chris Lattnerafade922002-11-20 22:28:10 +000010//
11// This class contains all of the shared state and information that is used by
12// the BugPoint tool to track down errors in optimizations. This class is the
13// main driver class that invokes all sub-functionality.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef BUGDRIVER_H
18#define BUGDRIVER_H
19
20#include <vector>
21#include <string>
Misha Brukman50733362003-07-24 18:17:43 +000022
Chris Lattnerafade922002-11-20 22:28:10 +000023class PassInfo;
24class Module;
25class Function;
Chris Lattner218e26e2002-12-23 23:49:59 +000026class AbstractInterpreter;
Chris Lattner65207852003-01-23 02:48:33 +000027class Instruction;
Chris Lattnerafade922002-11-20 22:28:10 +000028
Chris Lattneraae33f92003-04-24 22:24:58 +000029class DebugCrashes;
Chris Lattner640f22e2003-04-24 17:02:17 +000030class ReduceMiscompilingPasses;
31class ReduceMiscompilingFunctions;
Chris Lattneraae33f92003-04-24 22:24:58 +000032class ReduceCrashingFunctions;
Chris Lattner286921e2003-04-24 23:51:38 +000033class ReduceCrashingBlocks;
Chris Lattner640f22e2003-04-24 17:02:17 +000034
Misha Brukmana259c9b2003-07-24 21:59:10 +000035class CBE;
36class GCC;
37
Chris Lattner47ae4a12003-08-05 15:51:05 +000038extern bool DisableSimplifyCFG;
39
Chris Lattnerafade922002-11-20 22:28:10 +000040class BugDriver {
41 const std::string ToolName; // Name of bugpoint
Misha Brukmana259c9b2003-07-24 21:59:10 +000042 std::string ReferenceOutputFile; // Name of `good' output file
Chris Lattnerafade922002-11-20 22:28:10 +000043 Module *Program; // The raw program, linked together
44 std::vector<const PassInfo*> PassesToRun;
Chris Lattner218e26e2002-12-23 23:49:59 +000045 AbstractInterpreter *Interpreter; // How to run the program
Misha Brukmana259c9b2003-07-24 21:59:10 +000046 CBE *cbe;
47 GCC *gcc;
Chris Lattner640f22e2003-04-24 17:02:17 +000048
Chris Lattneraae33f92003-04-24 22:24:58 +000049 // FIXME: sort out public/private distinctions...
50 friend class DebugCrashes;
Chris Lattner640f22e2003-04-24 17:02:17 +000051 friend class ReduceMiscompilingPasses;
52 friend class ReduceMiscompilingFunctions;
Misha Brukman50733362003-07-24 18:17:43 +000053 friend class ReduceMisCodegenFunctions;
Chris Lattneraae33f92003-04-24 22:24:58 +000054 friend class ReduceCrashingFunctions;
Chris Lattner286921e2003-04-24 23:51:38 +000055 friend class ReduceCrashingBlocks;
Misha Brukman50733362003-07-24 18:17:43 +000056
Chris Lattnerafade922002-11-20 22:28:10 +000057public:
Misha Brukman50733362003-07-24 18:17:43 +000058 BugDriver(const char *toolname);
Chris Lattner218e26e2002-12-23 23:49:59 +000059
60 const std::string &getToolName() const { return ToolName; }
Chris Lattnerafade922002-11-20 22:28:10 +000061
62 // Set up methods... these methods are used to copy information about the
63 // command line arguments into instance variables of BugDriver.
64 //
65 bool addSources(const std::vector<std::string> &FileNames);
66 template<class It>
67 void addPasses(It I, It E) { PassesToRun.insert(PassesToRun.end(), I, E); }
Chris Lattner9c6cfe12003-10-17 23:03:16 +000068 void setPassesToRun(const std::vector<const PassInfo*> &PTR) {
69 PassesToRun = PTR;
70 }
Chris Lattnerafade922002-11-20 22:28:10 +000071
72 /// run - The top level method that is invoked after all of the instance
73 /// variables are set up from command line arguments.
74 ///
75 bool run();
76
77 /// debugCrash - This method is called when some pass crashes on input. It
78 /// attempts to prune down the testcase to something reasonable, and figure
79 /// out exactly which pass is crashing.
80 ///
81 bool debugCrash();
82
Chris Lattnerafade922002-11-20 22:28:10 +000083 /// debugMiscompilation - This method is used when the passes selected are not
84 /// crashing, but the generated output is semantically different from the
85 /// input.
86 bool debugMiscompilation();
87
Chris Lattner218e26e2002-12-23 23:49:59 +000088 /// debugPassMiscompilation - This method is called when the specified pass
89 /// miscompiles Program as input. It tries to reduce the testcase to
90 /// something that smaller that still miscompiles the program.
91 /// ReferenceOutput contains the filename of the file containing the output we
92 /// are to match.
93 ///
94 bool debugPassMiscompilation(const PassInfo *ThePass,
95 const std::string &ReferenceOutput);
96
Misha Brukman50733362003-07-24 18:17:43 +000097 /// compileSharedObject - This method creates a SharedObject from a given
98 /// BytecodeFile for debugging a code generator.
Chris Lattnera0f5b152003-10-14 21:09:11 +000099 ///
100 std::string compileSharedObject(const std::string &BytecodeFile);
Misha Brukman50733362003-07-24 18:17:43 +0000101
102 /// debugCodeGenerator - This method narrows down a module to a function or
103 /// set of functions, using the CBE as a ``safe'' code generator for other
104 /// functions that are not under consideration.
105 bool debugCodeGenerator();
106
Misha Brukmanfe04db82003-07-28 20:59:16 +0000107 /// isExecutingJIT - Returns true if bugpoint is currently testing the JIT
108 ///
Misha Brukman91eabc12003-07-28 19:16:14 +0000109 bool isExecutingJIT();
110
Chris Lattnerafade922002-11-20 22:28:10 +0000111private:
112 /// ParseInputFile - Given a bytecode or assembly input filename, parse and
113 /// return it, or return null if not possible.
114 ///
115 Module *ParseInputFile(const std::string &InputFilename) const;
116
Chris Lattnerafade922002-11-20 22:28:10 +0000117 /// writeProgramToFile - This writes the current "Program" to the named
118 /// bytecode file. If an error occurs, true is returned.
119 ///
Chris Lattner218e26e2002-12-23 23:49:59 +0000120 bool writeProgramToFile(const std::string &Filename, Module *M = 0) const;
Chris Lattnerafade922002-11-20 22:28:10 +0000121
122
123 /// EmitProgressBytecode - This function is used to output the current Program
124 /// to a file named "bugpoing-ID.bc".
125 ///
Chris Lattner640f22e2003-04-24 17:02:17 +0000126 void EmitProgressBytecode(const std::string &ID, bool NoFlyer = false);
Chris Lattnerafade922002-11-20 22:28:10 +0000127
128 /// runPasses - Run the specified passes on Program, outputting a bytecode
129 /// file and writting the filename into OutputFile if successful. If the
130 /// optimizations fail for some reason (optimizer crashes), return true,
131 /// otherwise return false. If DeleteOutput is set to true, the bytecode is
132 /// deleted on success, and the filename string is undefined. This prints to
133 /// cout a single line message indicating whether compilation was successful
Chris Lattner218e26e2002-12-23 23:49:59 +0000134 /// or failed, unless Quiet is set.
Chris Lattnerafade922002-11-20 22:28:10 +0000135 ///
136 bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
Chris Lattner218e26e2002-12-23 23:49:59 +0000137 std::string &OutputFilename, bool DeleteOutput = false,
138 bool Quiet = false) const;
Chris Lattnerafade922002-11-20 22:28:10 +0000139
140 /// runPasses - Just like the method above, but this just returns true or
141 /// false indicating whether or not the optimizer crashed on the specified
142 /// input (true = crashed).
143 ///
144 bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
145 bool DeleteOutput = true) const {
146 std::string Filename;
147 return runPasses(PassesToRun, Filename, DeleteOutput);
148 }
149
Misha Brukman50733362003-07-24 18:17:43 +0000150 /// PrintFunctionList - prints out list of problematic functions
Misha Brukmana259c9b2003-07-24 21:59:10 +0000151 ///
Misha Brukman50733362003-07-24 18:17:43 +0000152 static void PrintFunctionList(const std::vector<Function*> &Funcs);
153
Chris Lattner65207852003-01-23 02:48:33 +0000154 /// deleteInstructionFromProgram - This method clones the current Program and
155 /// deletes the specified instruction from the cloned module. It then runs a
156 /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code
157 /// which depends on the value. The modified module is then returned.
158 ///
159 Module *deleteInstructionFromProgram(Instruction *I, unsigned Simp) const;
160
Chris Lattnerba386d92003-02-28 16:13:20 +0000161 /// performFinalCleanups - This method clones the current Program and performs
162 /// a series of cleanups intended to get rid of extra cruft on the module
Chris Lattner587a6ce2003-08-01 16:13:49 +0000163 /// before handing it to the user... if the module parameter is specified, it
164 /// operates directly on the specified Module, modifying it in place.
Chris Lattnerba386d92003-02-28 16:13:20 +0000165 ///
Chris Lattner587a6ce2003-08-01 16:13:49 +0000166 Module *performFinalCleanups(Module *M = 0) const;
Chris Lattnerba386d92003-02-28 16:13:20 +0000167
Chris Lattner218e26e2002-12-23 23:49:59 +0000168 /// initializeExecutionEnvironment - This method is used to set up the
169 /// environment for executing LLVM programs.
170 ///
171 bool initializeExecutionEnvironment();
172
173 /// executeProgram - This method runs "Program", capturing the output of the
174 /// program to a file, returning the filename of the file. A recommended
175 /// filename may be optionally specified.
176 ///
177 std::string executeProgram(std::string RequestedOutputFilename = "",
Misha Brukman50733362003-07-24 18:17:43 +0000178 std::string Bytecode = "",
Chris Lattner769f1fe2003-10-14 21:59:36 +0000179 const std::string &SharedObjects = "",
Misha Brukman50733362003-07-24 18:17:43 +0000180 AbstractInterpreter *AI = 0);
181
182 /// executeProgramWithCBE - Used to create reference output with the C
183 /// backend, if reference output is not provided.
Chris Lattner769f1fe2003-10-14 21:59:36 +0000184 ///
185 std::string executeProgramWithCBE(std::string OutputFile = "",
186 std::string BytecodeFile = "",
187 const std::string &SharedObj = "") {
188 return executeProgram(OutputFile, BytecodeFile, SharedObj,
189 (AbstractInterpreter*)cbe);
190 }
Chris Lattner218e26e2002-12-23 23:49:59 +0000191
192 /// diffProgram - This method executes the specified module and diffs the
193 /// output against the file specified by ReferenceOutputFile. If the output
194 /// is different, true is returned.
195 ///
Misha Brukman50733362003-07-24 18:17:43 +0000196 bool diffProgram(const std::string &BytecodeFile = "",
Chris Lattner769f1fe2003-10-14 21:59:36 +0000197 const std::string &SharedObj = "",
Chris Lattner640f22e2003-04-24 17:02:17 +0000198 bool RemoveBytecode = false);
Chris Lattnerafade922002-11-20 22:28:10 +0000199};
200
Chris Lattner640f22e2003-04-24 17:02:17 +0000201/// getPassesString - Turn a list of passes into a string which indicates the
202/// command line options that must be passed to add the passes.
203///
204std::string getPassesString(const std::vector<const PassInfo*> &Passes);
205
Chris Lattneraae33f92003-04-24 22:24:58 +0000206// DeleteFunctionBody - "Remove" the function by deleting all of it's basic
207// blocks, making it external.
208//
209void DeleteFunctionBody(Function *F);
210
Chris Lattnerafade922002-11-20 22:28:10 +0000211#endif