1 //===- BugDriver.cpp - Top-Level BugPoint class implementation ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #include "BugDriver.h"
17 #include "ToolRunner.h"
18 #include "llvm/IR/DiagnosticPrinter.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/IR/Verifier.h"
21 #include "llvm/IRReader/IRReader.h"
22 #include "llvm/Linker/Linker.h"
23 #include "llvm/Pass.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/FileUtilities.h"
26 #include "llvm/Support/Host.h"
27 #include "llvm/Support/SourceMgr.h"
28 #include "llvm/Support/raw_ostream.h"
36 // Anonymous namespace to define command line options for debugging.
39 // Output - The user can specify a file containing the expected output of the
40 // program. If this filename is set, it is used as the reference diff source,
41 // otherwise the raw input run through an interpreter is used as the reference
45 OutputFile("output", cl::desc("Specify a reference program output "
46 "(for miscompilation detection)"));
49 /// setNewProgram - If we reduce or update the program somehow, call this method
50 /// to update bugdriver with it. This deletes the old module and sets the
51 /// specified one as the current program.
52 void BugDriver::setNewProgram(Module *M) {
58 /// getPassesString - Turn a list of passes into a string which indicates the
59 /// command line options that must be passed to add the passes.
61 std::string llvm::getPassesString(const std::vector<std::string> &Passes) {
63 for (unsigned i = 0, e = Passes.size(); i != e; ++i) {
71 BugDriver::BugDriver(const char *toolname, bool find_bugs,
72 unsigned timeout, unsigned memlimit, bool use_valgrind,
74 : Context(ctxt), ToolName(toolname), ReferenceOutputFile(OutputFile),
75 Program(nullptr), Interpreter(nullptr), SafeInterpreter(nullptr),
76 cc(nullptr), run_find_bugs(find_bugs), Timeout(timeout),
77 MemoryLimit(memlimit), UseValgrind(use_valgrind) {}
79 BugDriver::~BugDriver() {
81 if (Interpreter != SafeInterpreter)
83 delete SafeInterpreter;
87 std::unique_ptr<Module> llvm::parseInputFile(StringRef Filename,
90 std::unique_ptr<Module> Result = parseIRFile(Filename, Err, Ctxt);
92 Err.print("bugpoint", errs());
96 if (verifyModule(*Result, &errs())) {
97 errs() << "bugpoint: " << Filename << ": error: input module is broken!\n";
98 return std::unique_ptr<Module>();
101 // If we don't have an override triple, use the first one to configure
102 // bugpoint, or use the host triple if none provided.
103 if (TargetTriple.getTriple().empty()) {
104 Triple TheTriple(Result->getTargetTriple());
106 if (TheTriple.getTriple().empty())
107 TheTriple.setTriple(sys::getDefaultTargetTriple());
109 TargetTriple.setTriple(TheTriple.getTriple());
112 Result->setTargetTriple(TargetTriple.getTriple()); // override the triple
116 static void diagnosticHandler(const DiagnosticInfo &DI) {
117 DiagnosticPrinterRawOStream DP(errs());
122 // This method takes the specified list of LLVM input files, attempts to load
123 // them, either as assembly or bitcode, then link them together. It returns
124 // true on failure (if, for example, an input bitcode file could not be
125 // parsed), and false on success.
127 bool BugDriver::addSources(const std::vector<std::string> &Filenames) {
128 assert(!Program && "Cannot call addSources multiple times!");
129 assert(!Filenames.empty() && "Must specify at least on input filename!");
131 // Load the first input file.
132 Program = parseInputFile(Filenames[0], Context).release();
133 if (!Program) return true;
135 outs() << "Read input file : '" << Filenames[0] << "'\n";
137 for (unsigned i = 1, e = Filenames.size(); i != e; ++i) {
138 std::unique_ptr<Module> M = parseInputFile(Filenames[i], Context);
139 if (!M.get()) return true;
141 outs() << "Linking in input file: '" << Filenames[i] << "'\n";
142 if (Linker::linkModules(*Program, *M, diagnosticHandler))
146 outs() << "*** All input ok\n";
148 // All input files read successfully!
154 /// run - The top level method that is invoked after all of the instance
155 /// variables are set up from command line arguments.
157 bool BugDriver::run(std::string &ErrMsg) {
159 // Rearrange the passes and apply them to the program. Repeat this process
160 // until the user kills the program or we find a bug.
161 return runManyPasses(PassesToRun, ErrMsg);
164 // If we're not running as a child, the first thing that we must do is
165 // determine what the problem is. Does the optimization series crash the
166 // compiler, or does it produce illegal code? We make the top-level
167 // decision by trying to run all of the passes on the input program,
168 // which should generate a bitcode file. If it does generate a bitcode
169 // file, then we know the compiler didn't crash, so try to diagnose a
171 if (!PassesToRun.empty()) {
172 outs() << "Running selected passes on program to test for crash: ";
173 if (runPasses(Program, PassesToRun))
174 return debugOptimizerCrash();
177 // Set up the execution environment, selecting a method to run LLVM bitcode.
178 if (initializeExecutionEnvironment()) return true;
180 // Test to see if we have a code generator crash.
181 outs() << "Running the code generator to test for a crash: ";
183 compileProgram(Program, &Error);
184 if (!Error.empty()) {
186 return debugCodeGeneratorCrash(ErrMsg);
190 // Run the raw input to see where we are coming from. If a reference output
191 // was specified, make sure that the raw output matches it. If not, it's a
192 // problem in the front-end or the code generator.
194 bool CreatedOutput = false;
195 if (ReferenceOutputFile.empty()) {
196 outs() << "Generating reference output from raw program: ";
197 if (!createReferenceFile(Program)) {
198 return debugCodeGeneratorCrash(ErrMsg);
200 CreatedOutput = true;
203 // Make sure the reference output file gets deleted on exit from this
204 // function, if appropriate.
205 std::string ROF(ReferenceOutputFile);
206 FileRemover RemoverInstance(ROF, CreatedOutput && !SaveTemps);
208 // Diff the output of the raw program against the reference output. If it
209 // matches, then we assume there is a miscompilation bug and try to
211 outs() << "*** Checking the code generator...\n";
212 bool Diff = diffProgram(Program, "", "", false, &Error);
213 if (!Error.empty()) {
215 return debugCodeGeneratorCrash(ErrMsg);
218 outs() << "\n*** Output matches: Debugging miscompilation!\n";
219 debugMiscompilation(&Error);
220 if (!Error.empty()) {
222 return debugCodeGeneratorCrash(ErrMsg);
227 outs() << "\n*** Input program does not match reference diff!\n";
228 outs() << "Debugging code generator problem!\n";
229 bool Failure = debugCodeGenerator(&Error);
230 if (!Error.empty()) {
232 return debugCodeGeneratorCrash(ErrMsg);
237 void llvm::PrintFunctionList(const std::vector<Function*> &Funcs) {
238 unsigned NumPrint = Funcs.size();
239 if (NumPrint > 10) NumPrint = 10;
240 for (unsigned i = 0; i != NumPrint; ++i)
241 outs() << " " << Funcs[i]->getName();
242 if (NumPrint < Funcs.size())
243 outs() << "... <" << Funcs.size() << " total>";
247 void llvm::PrintGlobalVariableList(const std::vector<GlobalVariable*> &GVs) {
248 unsigned NumPrint = GVs.size();
249 if (NumPrint > 10) NumPrint = 10;
250 for (unsigned i = 0; i != NumPrint; ++i)
251 outs() << " " << GVs[i]->getName();
252 if (NumPrint < GVs.size())
253 outs() << "... <" << GVs.size() << " total>";