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/Module.h"
19 #include "llvm/IR/Verifier.h"
20 #include "llvm/IRReader/IRReader.h"
21 #include "llvm/Linker/Linker.h"
22 #include "llvm/Pass.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/FileUtilities.h"
25 #include "llvm/Support/Host.h"
26 #include "llvm/Support/SourceMgr.h"
27 #include "llvm/Support/raw_ostream.h"
35 DiscardTemp::~DiscardTemp() {
37 if (Error E = File.keep())
38 errs() << "Failed to keep temp file " << toString(std::move(E)) << '\n';
41 if (Error E = File.discard())
42 errs() << "Failed to delete temp file " << toString(std::move(E)) << '\n';
45 // Anonymous namespace to define command line options for debugging.
48 // Output - The user can specify a file containing the expected output of the
49 // program. If this filename is set, it is used as the reference diff source,
50 // otherwise the raw input run through an interpreter is used as the reference
53 cl::opt<std::string> OutputFile("output",
54 cl::desc("Specify a reference program output "
55 "(for miscompilation detection)"));
58 /// setNewProgram - If we reduce or update the program somehow, call this method
59 /// to update bugdriver with it. This deletes the old module and sets the
60 /// specified one as the current program.
61 void BugDriver::setNewProgram(Module *M) {
66 /// getPassesString - Turn a list of passes into a string which indicates the
67 /// command line options that must be passed to add the passes.
69 std::string llvm::getPassesString(const std::vector<std::string> &Passes) {
71 for (unsigned i = 0, e = Passes.size(); i != e; ++i) {
80 BugDriver::BugDriver(const char *toolname, bool find_bugs, unsigned timeout,
81 unsigned memlimit, bool use_valgrind, LLVMContext &ctxt)
82 : Context(ctxt), ToolName(toolname), ReferenceOutputFile(OutputFile),
83 Program(nullptr), Interpreter(nullptr), SafeInterpreter(nullptr),
84 cc(nullptr), run_find_bugs(find_bugs), Timeout(timeout),
85 MemoryLimit(memlimit), UseValgrind(use_valgrind) {}
87 BugDriver::~BugDriver() {
89 if (Interpreter != SafeInterpreter)
91 delete SafeInterpreter;
95 std::unique_ptr<Module> llvm::parseInputFile(StringRef Filename,
98 std::unique_ptr<Module> Result = parseIRFile(Filename, Err, Ctxt);
100 Err.print("bugpoint", errs());
104 if (verifyModule(*Result, &errs())) {
105 errs() << "bugpoint: " << Filename << ": error: input module is broken!\n";
106 return std::unique_ptr<Module>();
109 // If we don't have an override triple, use the first one to configure
110 // bugpoint, or use the host triple if none provided.
111 if (TargetTriple.getTriple().empty()) {
112 Triple TheTriple(Result->getTargetTriple());
114 if (TheTriple.getTriple().empty())
115 TheTriple.setTriple(sys::getDefaultTargetTriple());
117 TargetTriple.setTriple(TheTriple.getTriple());
120 Result->setTargetTriple(TargetTriple.getTriple()); // override the triple
124 // This method takes the specified list of LLVM input files, attempts to load
125 // them, either as assembly or bitcode, then link them together. It returns
126 // true on failure (if, for example, an input bitcode file could not be
127 // parsed), and false on success.
129 bool BugDriver::addSources(const std::vector<std::string> &Filenames) {
130 assert(!Program && "Cannot call addSources multiple times!");
131 assert(!Filenames.empty() && "Must specify at least on input filename!");
133 // Load the first input file.
134 Program = parseInputFile(Filenames[0], Context).release();
138 outs() << "Read input file : '" << Filenames[0] << "'\n";
140 for (unsigned i = 1, e = Filenames.size(); i != e; ++i) {
141 std::unique_ptr<Module> M = parseInputFile(Filenames[i], Context);
145 outs() << "Linking in input file: '" << Filenames[i] << "'\n";
146 if (Linker::linkModules(*Program, std::move(M)))
150 outs() << "*** All input ok\n";
152 // All input files read successfully!
156 /// run - The top level method that is invoked after all of the instance
157 /// variables are set up from command line arguments.
159 Error BugDriver::run() {
161 // Rearrange the passes and apply them to the program. Repeat this process
162 // until the user kills the program or we find a bug.
163 return runManyPasses(PassesToRun);
166 // If we're not running as a child, the first thing that we must do is
167 // determine what the problem is. Does the optimization series crash the
168 // compiler, or does it produce illegal code? We make the top-level
169 // decision by trying to run all of the passes on the input program,
170 // which should generate a bitcode file. If it does generate a bitcode
171 // file, then we know the compiler didn't crash, so try to diagnose a
173 if (!PassesToRun.empty()) {
174 outs() << "Running selected passes on program to test for crash: ";
175 if (runPasses(Program, PassesToRun))
176 return debugOptimizerCrash();
179 // Set up the execution environment, selecting a method to run LLVM bitcode.
180 if (Error E = initializeExecutionEnvironment())
183 // Test to see if we have a code generator crash.
184 outs() << "Running the code generator to test for a crash: ";
185 if (Error E = compileProgram(Program)) {
186 outs() << toString(std::move(E));
187 return debugCodeGeneratorCrash();
191 // Run the raw input to see where we are coming from. If a reference output
192 // was specified, make sure that the raw output matches it. If not, it's a
193 // problem in the front-end or the code generator.
195 bool CreatedOutput = false;
196 if (ReferenceOutputFile.empty()) {
197 outs() << "Generating reference output from raw program: ";
198 if (Error E = createReferenceFile(Program)) {
199 errs() << toString(std::move(E));
200 return debugCodeGeneratorCrash();
202 CreatedOutput = true;
205 // Make sure the reference output file gets deleted on exit from this
206 // function, if appropriate.
207 std::string ROF(ReferenceOutputFile);
208 FileRemover RemoverInstance(ROF, CreatedOutput && !SaveTemps);
210 // Diff the output of the raw program against the reference output. If it
211 // matches, then we assume there is a miscompilation bug and try to
213 outs() << "*** Checking the code generator...\n";
214 Expected<bool> Diff = diffProgram(Program, "", "", false);
215 if (Error E = Diff.takeError()) {
216 errs() << toString(std::move(E));
217 return debugCodeGeneratorCrash();
220 outs() << "\n*** Output matches: Debugging miscompilation!\n";
221 if (Error E = debugMiscompilation()) {
222 errs() << toString(std::move(E));
223 return debugCodeGeneratorCrash();
225 return Error::success();
228 outs() << "\n*** Input program does not match reference diff!\n";
229 outs() << "Debugging code generator problem!\n";
230 if (Error E = debugCodeGenerator()) {
231 errs() << toString(std::move(E));
232 return debugCodeGeneratorCrash();
234 return Error::success();
237 void llvm::PrintFunctionList(const std::vector<Function *> &Funcs) {
238 unsigned NumPrint = Funcs.size();
241 for (unsigned i = 0; i != NumPrint; ++i)
242 outs() << " " << Funcs[i]->getName();
243 if (NumPrint < Funcs.size())
244 outs() << "... <" << Funcs.size() << " total>";
248 void llvm::PrintGlobalVariableList(const std::vector<GlobalVariable *> &GVs) {
249 unsigned NumPrint = GVs.size();
252 for (unsigned i = 0; i != NumPrint; ++i)
253 outs() << " " << GVs[i]->getName();
254 if (NumPrint < GVs.size())
255 outs() << "... <" << GVs.size() << " total>";