Return std::unique_ptr from SplitFunctionsOutOfModule. NFC.
[oota-llvm.git] / tools / bugpoint / BugDriver.cpp
1 //===- BugDriver.cpp - Top-Level BugPoint class implementation ------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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 #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"
29 #include <memory>
30 using namespace llvm;
31
32 namespace llvm {
33   Triple TargetTriple;
34 }
35
36 // Anonymous namespace to define command line options for debugging.
37 //
38 namespace {
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
42   // source.
43   //
44   cl::opt<std::string>
45   OutputFile("output", cl::desc("Specify a reference program output "
46                                 "(for miscompilation detection)"));
47 }
48
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) {
53   delete Program;
54   Program = M;
55 }
56
57
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.
60 ///
61 std::string llvm::getPassesString(const std::vector<std::string> &Passes) {
62   std::string Result;
63   for (unsigned i = 0, e = Passes.size(); i != e; ++i) {
64     if (i) Result += " ";
65     Result += "-";
66     Result += Passes[i];
67   }
68   return Result;
69 }
70
71 BugDriver::BugDriver(const char *toolname, bool find_bugs,
72                      unsigned timeout, unsigned memlimit, bool use_valgrind,
73                      LLVMContext& ctxt)
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) {}
78
79 BugDriver::~BugDriver() {
80   delete Program;
81   if (Interpreter != SafeInterpreter)
82     delete Interpreter;
83   delete SafeInterpreter;
84   delete cc;
85 }
86
87 std::unique_ptr<Module> llvm::parseInputFile(StringRef Filename,
88                                              LLVMContext &Ctxt) {
89   SMDiagnostic Err;
90   std::unique_ptr<Module> Result = parseIRFile(Filename, Err, Ctxt);
91   if (!Result) {
92     Err.print("bugpoint", errs());
93     return Result;
94   }
95
96   if (verifyModule(*Result, &errs())) {
97     errs() << "bugpoint: " << Filename << ": error: input module is broken!\n";
98     return std::unique_ptr<Module>();
99   }
100
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());
105
106     if (TheTriple.getTriple().empty())
107       TheTriple.setTriple(sys::getDefaultTargetTriple());
108
109     TargetTriple.setTriple(TheTriple.getTriple());
110   }
111
112   Result->setTargetTriple(TargetTriple.getTriple()); // override the triple
113   return Result;
114 }
115
116 static void diagnosticHandler(const DiagnosticInfo &DI) {
117   DiagnosticPrinterRawOStream DP(errs());
118   DI.print(DP);
119   errs() << '\n';
120 }
121
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.
126 //
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!");
130
131   // Load the first input file.
132   Program = parseInputFile(Filenames[0], Context).release();
133   if (!Program) return true;
134
135   outs() << "Read input file      : '" << Filenames[0] << "'\n";
136
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;
140
141     outs() << "Linking in input file: '" << Filenames[i] << "'\n";
142     if (Linker::linkModules(*Program, *M, diagnosticHandler))
143       return true;
144   }
145
146   outs() << "*** All input ok\n";
147
148   // All input files read successfully!
149   return false;
150 }
151
152
153
154 /// run - The top level method that is invoked after all of the instance
155 /// variables are set up from command line arguments.
156 ///
157 bool BugDriver::run(std::string &ErrMsg) {
158   if (run_find_bugs) {
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);
162   }
163
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
170   // miscompilation.
171   if (!PassesToRun.empty()) {
172     outs() << "Running selected passes on program to test for crash: ";
173     if (runPasses(Program, PassesToRun))
174       return debugOptimizerCrash();
175   }
176
177   // Set up the execution environment, selecting a method to run LLVM bitcode.
178   if (initializeExecutionEnvironment()) return true;
179
180   // Test to see if we have a code generator crash.
181   outs() << "Running the code generator to test for a crash: ";
182   std::string Error;
183   compileProgram(Program, &Error);
184   if (!Error.empty()) {
185     outs() << Error;
186     return debugCodeGeneratorCrash(ErrMsg);
187   }
188   outs() << '\n';
189
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.
193   //
194   bool CreatedOutput = false;
195   if (ReferenceOutputFile.empty()) {
196     outs() << "Generating reference output from raw program: ";
197     if (!createReferenceFile(Program)) {
198       return debugCodeGeneratorCrash(ErrMsg);
199     }
200     CreatedOutput = true;
201   }
202
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);
207
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
210   // diagnose it.
211   outs() << "*** Checking the code generator...\n";
212   bool Diff = diffProgram(Program, "", "", false, &Error);
213   if (!Error.empty()) {
214     errs() << Error;
215     return debugCodeGeneratorCrash(ErrMsg);
216   }
217   if (!Diff) {
218     outs() << "\n*** Output matches: Debugging miscompilation!\n";
219     debugMiscompilation(&Error);
220     if (!Error.empty()) {
221       errs() << Error;
222       return debugCodeGeneratorCrash(ErrMsg);
223     }
224     return false;
225   }
226
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()) {
231     errs() << Error;
232     return debugCodeGeneratorCrash(ErrMsg);
233   }
234   return Failure;
235 }
236
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>";
244   outs().flush();
245 }
246
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>";
254   outs().flush();
255 }