blob: e1af721a75cb37d2172c0314e8ff9bc26343bc7b [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
Chris Lattnerafade922002-11-20 22:28:10 +000022class PassInfo;
23class Module;
24class Function;
Chris Lattner218e26e2002-12-23 23:49:59 +000025class AbstractInterpreter;
Chris Lattner65207852003-01-23 02:48:33 +000026class Instruction;
Chris Lattnerafade922002-11-20 22:28:10 +000027
Chris Lattneraae33f92003-04-24 22:24:58 +000028class DebugCrashes;
Chris Lattner640f22e2003-04-24 17:02:17 +000029class ReduceMiscompilingPasses;
30class ReduceMiscompilingFunctions;
Chris Lattneraae33f92003-04-24 22:24:58 +000031class ReduceCrashingFunctions;
Chris Lattner286921e2003-04-24 23:51:38 +000032class ReduceCrashingBlocks;
Chris Lattner640f22e2003-04-24 17:02:17 +000033
Misha Brukmana259c9b2003-07-24 21:59:10 +000034class CBE;
35class GCC;
36
Chris Lattner47ae4a12003-08-05 15:51:05 +000037extern bool DisableSimplifyCFG;
38
Chris Lattnerafade922002-11-20 22:28:10 +000039class BugDriver {
40 const std::string ToolName; // Name of bugpoint
Misha Brukmana259c9b2003-07-24 21:59:10 +000041 std::string ReferenceOutputFile; // Name of `good' output file
Chris Lattnerafade922002-11-20 22:28:10 +000042 Module *Program; // The raw program, linked together
43 std::vector<const PassInfo*> PassesToRun;
Chris Lattner218e26e2002-12-23 23:49:59 +000044 AbstractInterpreter *Interpreter; // How to run the program
Misha Brukmana259c9b2003-07-24 21:59:10 +000045 CBE *cbe;
46 GCC *gcc;
Chris Lattner640f22e2003-04-24 17:02:17 +000047
Chris Lattneraae33f92003-04-24 22:24:58 +000048 // FIXME: sort out public/private distinctions...
49 friend class DebugCrashes;
Chris Lattner640f22e2003-04-24 17:02:17 +000050 friend class ReduceMiscompilingPasses;
51 friend class ReduceMiscompilingFunctions;
Misha Brukman50733362003-07-24 18:17:43 +000052 friend class ReduceMisCodegenFunctions;
Chris Lattneraae33f92003-04-24 22:24:58 +000053 friend class ReduceCrashingFunctions;
Chris Lattner286921e2003-04-24 23:51:38 +000054 friend class ReduceCrashingBlocks;
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
76 /// debugCrash - This method is called when some pass crashes on input. It
77 /// attempts to prune down the testcase to something reasonable, and figure
78 /// out exactly which pass is crashing.
79 ///
80 bool debugCrash();
81
Chris Lattnerafade922002-11-20 22:28:10 +000082 /// debugMiscompilation - This method is used when the passes selected are not
83 /// crashing, but the generated output is semantically different from the
84 /// input.
85 bool debugMiscompilation();
86
Chris Lattner218e26e2002-12-23 23:49:59 +000087 /// debugPassMiscompilation - This method is called when the specified pass
88 /// miscompiles Program as input. It tries to reduce the testcase to
89 /// something that smaller that still miscompiles the program.
90 /// ReferenceOutput contains the filename of the file containing the output we
91 /// are to match.
92 ///
93 bool debugPassMiscompilation(const PassInfo *ThePass,
94 const std::string &ReferenceOutput);
95
Misha Brukman50733362003-07-24 18:17:43 +000096 /// compileSharedObject - This method creates a SharedObject from a given
97 /// BytecodeFile for debugging a code generator.
Chris Lattnera0f5b152003-10-14 21:09:11 +000098 ///
99 std::string compileSharedObject(const std::string &BytecodeFile);
Misha Brukman50733362003-07-24 18:17:43 +0000100
101 /// debugCodeGenerator - This method narrows down a module to a function or
102 /// set of functions, using the CBE as a ``safe'' code generator for other
103 /// functions that are not under consideration.
104 bool debugCodeGenerator();
105
Misha Brukmanfe04db82003-07-28 20:59:16 +0000106 /// isExecutingJIT - Returns true if bugpoint is currently testing the JIT
107 ///
Misha Brukman91eabc12003-07-28 19:16:14 +0000108 bool isExecutingJIT();
109
Chris Lattnerafade922002-11-20 22:28:10 +0000110private:
111 /// ParseInputFile - Given a bytecode or assembly input filename, parse and
112 /// return it, or return null if not possible.
113 ///
114 Module *ParseInputFile(const std::string &InputFilename) const;
115
Chris Lattnerafade922002-11-20 22:28:10 +0000116 /// writeProgramToFile - This writes the current "Program" to the named
117 /// bytecode file. If an error occurs, true is returned.
118 ///
Chris Lattner218e26e2002-12-23 23:49:59 +0000119 bool writeProgramToFile(const std::string &Filename, Module *M = 0) const;
Chris Lattnerafade922002-11-20 22:28:10 +0000120
121
122 /// EmitProgressBytecode - This function is used to output the current Program
Chris Lattnerfcb6ec02003-11-05 21:45:35 +0000123 /// to a file named "bugpoint-ID.bc".
Chris Lattnerafade922002-11-20 22:28:10 +0000124 ///
Chris Lattner640f22e2003-04-24 17:02:17 +0000125 void EmitProgressBytecode(const std::string &ID, bool NoFlyer = false);
Chris Lattnerafade922002-11-20 22:28:10 +0000126
127 /// runPasses - Run the specified passes on Program, outputting a bytecode
128 /// file and writting the filename into OutputFile if successful. If the
129 /// optimizations fail for some reason (optimizer crashes), return true,
130 /// otherwise return false. If DeleteOutput is set to true, the bytecode is
131 /// deleted on success, and the filename string is undefined. This prints to
132 /// cout a single line message indicating whether compilation was successful
Chris Lattner218e26e2002-12-23 23:49:59 +0000133 /// or failed, unless Quiet is set.
Chris Lattnerafade922002-11-20 22:28:10 +0000134 ///
135 bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
Chris Lattner218e26e2002-12-23 23:49:59 +0000136 std::string &OutputFilename, bool DeleteOutput = false,
137 bool Quiet = false) const;
Chris Lattnerafade922002-11-20 22:28:10 +0000138
139 /// runPasses - Just like the method above, but this just returns true or
140 /// false indicating whether or not the optimizer crashed on the specified
141 /// input (true = crashed).
142 ///
143 bool runPasses(const std::vector<const PassInfo*> &PassesToRun,
144 bool DeleteOutput = true) const {
145 std::string Filename;
146 return runPasses(PassesToRun, Filename, DeleteOutput);
147 }
148
Misha Brukman50733362003-07-24 18:17:43 +0000149 /// PrintFunctionList - prints out list of problematic functions
Misha Brukmana259c9b2003-07-24 21:59:10 +0000150 ///
Misha Brukman50733362003-07-24 18:17:43 +0000151 static void PrintFunctionList(const std::vector<Function*> &Funcs);
152
Chris Lattner65207852003-01-23 02:48:33 +0000153 /// deleteInstructionFromProgram - This method clones the current Program and
154 /// deletes the specified instruction from the cloned module. It then runs a
155 /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code
156 /// which depends on the value. The modified module is then returned.
157 ///
158 Module *deleteInstructionFromProgram(Instruction *I, unsigned Simp) const;
159
Chris Lattnerba386d92003-02-28 16:13:20 +0000160 /// performFinalCleanups - This method clones the current Program and performs
Chris Lattner417477d2003-11-05 21:15:19 +0000161 /// a series of cleanups intended to get rid of extra cruft on the module. If
162 /// the MayModifySemantics argument is true, then the cleanups is allowed to
163 /// modify how the code behaves.
Chris Lattnerba386d92003-02-28 16:13:20 +0000164 ///
Chris Lattnerfcb6ec02003-11-05 21:45:35 +0000165 Module *performFinalCleanups(Module *M, bool MayModifySemantics = false);
Chris Lattnerba386d92003-02-28 16:13:20 +0000166
Chris Lattner218e26e2002-12-23 23:49:59 +0000167 /// initializeExecutionEnvironment - This method is used to set up the
168 /// environment for executing LLVM programs.
169 ///
170 bool initializeExecutionEnvironment();
171
172 /// executeProgram - This method runs "Program", capturing the output of the
173 /// program to a file, returning the filename of the file. A recommended
174 /// filename may be optionally specified.
175 ///
176 std::string executeProgram(std::string RequestedOutputFilename = "",
Misha Brukman50733362003-07-24 18:17:43 +0000177 std::string Bytecode = "",
Chris Lattner769f1fe2003-10-14 21:59:36 +0000178 const std::string &SharedObjects = "",
Misha Brukman50733362003-07-24 18:17:43 +0000179 AbstractInterpreter *AI = 0);
180
181 /// executeProgramWithCBE - Used to create reference output with the C
182 /// backend, if reference output is not provided.
Chris Lattner769f1fe2003-10-14 21:59:36 +0000183 ///
184 std::string executeProgramWithCBE(std::string OutputFile = "",
185 std::string BytecodeFile = "",
186 const std::string &SharedObj = "") {
187 return executeProgram(OutputFile, BytecodeFile, SharedObj,
188 (AbstractInterpreter*)cbe);
189 }
Chris Lattner218e26e2002-12-23 23:49:59 +0000190
191 /// diffProgram - This method executes the specified module and diffs the
192 /// output against the file specified by ReferenceOutputFile. If the output
193 /// is different, true is returned.
194 ///
Misha Brukman50733362003-07-24 18:17:43 +0000195 bool diffProgram(const std::string &BytecodeFile = "",
Chris Lattner769f1fe2003-10-14 21:59:36 +0000196 const std::string &SharedObj = "",
Chris Lattner640f22e2003-04-24 17:02:17 +0000197 bool RemoveBytecode = false);
Chris Lattnerafade922002-11-20 22:28:10 +0000198};
199
Chris Lattner640f22e2003-04-24 17:02:17 +0000200/// getPassesString - Turn a list of passes into a string which indicates the
201/// command line options that must be passed to add the passes.
202///
203std::string getPassesString(const std::vector<const PassInfo*> &Passes);
204
Chris Lattneraae33f92003-04-24 22:24:58 +0000205// DeleteFunctionBody - "Remove" the function by deleting all of it's basic
206// blocks, making it external.
207//
208void DeleteFunctionBody(Function *F);
209
Chris Lattnerafade922002-11-20 22:28:10 +0000210#endif