blob: 9edc242d470ed8473d3af580048e6b356041476d [file] [log] [blame]
Chris Lattner73a6bdd2002-11-20 22:28:10 +00001//===- BugDriver.cpp - Top-Level BugPoint class implementation ------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswell09344dc2003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner345353d2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswell09344dc2003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner73a6bdd2002-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#include "BugDriver.h"
Chris Lattnerffac2862006-06-06 22:30:59 +000017#include "ToolRunner.h"
Rafael Espindolaf49a38f2015-12-04 22:08:53 +000018#include "llvm/IR/DiagnosticPrinter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Module.h"
Duncan P. N. Exon Smith5fbcc462015-03-26 05:03:10 +000020#include "llvm/IR/Verifier.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000021#include "llvm/IRReader/IRReader.h"
Chandler Carruth6cc07df2014-03-06 03:42:23 +000022#include "llvm/Linker/Linker.h"
Chris Lattner73a6bdd2002-11-20 22:28:10 +000023#include "llvm/Pass.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000024#include "llvm/Support/CommandLine.h"
25#include "llvm/Support/FileUtilities.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000026#include "llvm/Support/Host.h"
Chris Lattnera76611a2009-07-02 22:46:18 +000027#include "llvm/Support/SourceMgr.h"
Chris Lattnerac161bf2009-01-02 07:01:27 +000028#include "llvm/Support/raw_ostream.h"
Chris Lattner73a6bdd2002-11-20 22:28:10 +000029#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000030using namespace llvm;
31
Daniel Dunbar8575a602009-08-18 03:35:57 +000032namespace llvm {
33 Triple TargetTriple;
34}
35
Misha Brukmand792c9b2003-07-24 18:17:43 +000036// Anonymous namespace to define command line options for debugging.
37//
38namespace {
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 //
Misha Brukman650ba8e2005-04-22 00:00:37 +000044 cl::opt<std::string>
Misha Brukmand792c9b2003-07-24 18:17:43 +000045 OutputFile("output", cl::desc("Specify a reference program output "
46 "(for miscompilation detection)"));
Misha Brukmand792c9b2003-07-24 18:17:43 +000047}
48
Chris Lattner327019b2004-02-18 21:24:48 +000049/// 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.
52void BugDriver::setNewProgram(Module *M) {
53 delete Program;
54 Program = M;
55}
56
57
Chris Lattner16a41312003-04-24 17:02:17 +000058/// 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///
Rafael Espindola33e81a82010-08-08 03:55:08 +000061std::string llvm::getPassesString(const std::vector<std::string> &Passes) {
Chris Lattner16a41312003-04-24 17:02:17 +000062 std::string Result;
63 for (unsigned i = 0, e = Passes.size(); i != e; ++i) {
64 if (i) Result += " ";
65 Result += "-";
Rafael Espindola33e81a82010-08-08 03:55:08 +000066 Result += Passes[i];
Chris Lattner16a41312003-04-24 17:02:17 +000067 }
68 return Result;
69}
70
Rafael Espindolabbdce492010-08-07 23:03:21 +000071BugDriver::BugDriver(const char *toolname, bool find_bugs,
Jeffrey Yasskin71bd0f42010-03-19 00:09:28 +000072 unsigned timeout, unsigned memlimit, bool use_valgrind,
Owen Anderson2a154432009-07-01 23:13:44 +000073 LLVMContext& ctxt)
Owen Anderson6773d382009-07-01 16:58:40 +000074 : Context(ctxt), ToolName(toolname), ReferenceOutputFile(OutputFile),
Craig Toppere6cb63e2014-04-25 04:24:47 +000075 Program(nullptr), Interpreter(nullptr), SafeInterpreter(nullptr),
Davide Italianoab256212015-10-14 20:29:54 +000076 cc(nullptr), run_find_bugs(find_bugs), Timeout(timeout),
Jeffrey Yasskin71bd0f42010-03-19 00:09:28 +000077 MemoryLimit(memlimit), UseValgrind(use_valgrind) {}
Misha Brukmand792c9b2003-07-24 18:17:43 +000078
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000079BugDriver::~BugDriver() {
80 delete Program;
David Blaikie37436ed2014-04-25 20:15:16 +000081 if (Interpreter != SafeInterpreter)
82 delete Interpreter;
83 delete SafeInterpreter;
Davide Italianoab256212015-10-14 20:29:54 +000084 delete cc;
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000085}
86
Rafael Espindola28b351a2014-08-26 17:19:03 +000087std::unique_ptr<Module> llvm::parseInputFile(StringRef Filename,
88 LLVMContext &Ctxt) {
Chris Lattnera76611a2009-07-02 22:46:18 +000089 SMDiagnostic Err;
Rafael Espindolad233b062014-08-26 17:29:46 +000090 std::unique_ptr<Module> Result = parseIRFile(Filename, Err, Ctxt);
Duncan P. N. Exon Smith866aed72015-03-26 05:03:06 +000091 if (!Result) {
Chris Lattnera3a06812011-10-16 04:47:35 +000092 Err.print("bugpoint", errs());
Duncan P. N. Exon Smith866aed72015-03-26 05:03:06 +000093 return Result;
94 }
Dan Gohman728a81a2009-09-03 16:32:58 +000095
Duncan P. N. Exon Smith5fbcc462015-03-26 05:03:10 +000096 if (verifyModule(*Result, &errs())) {
Duncan P. N. Exon Smith46282822015-03-31 03:07:23 +000097 errs() << "bugpoint: " << Filename << ": error: input module is broken!\n";
Duncan P. N. Exon Smith5fbcc462015-03-26 05:03:10 +000098 return std::unique_ptr<Module>();
99 }
100
Daniel Dunbar8575a602009-08-18 03:35:57 +0000101 // If we don't have an override triple, use the first one to configure
102 // bugpoint, or use the host triple if none provided.
Duncan P. N. Exon Smith866aed72015-03-26 05:03:06 +0000103 if (TargetTriple.getTriple().empty()) {
104 Triple TheTriple(Result->getTargetTriple());
Daniel Dunbar8575a602009-08-18 03:35:57 +0000105
Duncan P. N. Exon Smith866aed72015-03-26 05:03:06 +0000106 if (TheTriple.getTriple().empty())
107 TheTriple.setTriple(sys::getDefaultTargetTriple());
Michael J. Spencer3df5c042011-03-31 13:06:39 +0000108
Duncan P. N. Exon Smith866aed72015-03-26 05:03:06 +0000109 TargetTriple.setTriple(TheTriple.getTriple());
Daniel Dunbar8575a602009-08-18 03:35:57 +0000110 }
Duncan P. N. Exon Smith866aed72015-03-26 05:03:06 +0000111
112 Result->setTargetTriple(TargetTriple.getTriple()); // override the triple
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000113 return Result;
114}
115
Rafael Espindolaf49a38f2015-12-04 22:08:53 +0000116static void diagnosticHandler(const DiagnosticInfo &DI) {
117 DiagnosticPrinterRawOStream DP(errs());
118 DI.print(DP);
119 errs() << '\n';
120}
121
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000122// This method takes the specified list of LLVM input files, attempts to load
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000123// 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
Brian Gaeke61a05da2003-05-23 05:34:32 +0000125// parsed), and false on success.
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000126//
127bool BugDriver::addSources(const std::vector<std::string> &Filenames) {
Craig Toppere73658d2014-04-28 04:05:08 +0000128 assert(!Program && "Cannot call addSources multiple times!");
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000129 assert(!Filenames.empty() && "Must specify at least on input filename!");
130
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000131 // Load the first input file.
Rafael Espindola28b351a2014-08-26 17:19:03 +0000132 Program = parseInputFile(Filenames[0], Context).release();
Craig Toppere6cb63e2014-04-25 04:24:47 +0000133 if (!Program) return true;
Michael J. Spencer3df5c042011-03-31 13:06:39 +0000134
Rafael Espindolabbdce492010-08-07 23:03:21 +0000135 outs() << "Read input file : '" << Filenames[0] << "'\n";
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000136
137 for (unsigned i = 1, e = Filenames.size(); i != e; ++i) {
Rafael Espindola28b351a2014-08-26 17:19:03 +0000138 std::unique_ptr<Module> M = parseInputFile(Filenames[i], Context);
Craig Toppere6cb63e2014-04-25 04:24:47 +0000139 if (!M.get()) return true;
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000140
Rafael Espindolabbdce492010-08-07 23:03:21 +0000141 outs() << "Linking in input file: '" << Filenames[i] << "'\n";
Rafael Espindolaf49a38f2015-12-04 22:08:53 +0000142 if (Linker::linkModules(*Program, *M, diagnosticHandler))
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000143 return true;
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000144 }
145
Rafael Espindolabbdce492010-08-07 23:03:21 +0000146 outs() << "*** All input ok\n";
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000147
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///
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000157bool BugDriver::run(std::string &ErrMsg) {
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000158 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.
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000161 return runManyPasses(PassesToRun, ErrMsg);
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000162 }
Reid Spencer842118c2005-12-22 20:02:55 +0000163
Michael J. Spencer3df5c042011-03-31 13:06:39 +0000164 // 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
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000167 // decision by trying to run all of the passes on the input program,
Michael J. Spencer3df5c042011-03-31 13:06:39 +0000168 // 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
Reid Spencer842118c2005-12-22 20:02:55 +0000170 // miscompilation.
Chris Lattner6ca0b872003-10-13 21:04:26 +0000171 if (!PassesToRun.empty()) {
Dan Gohmanee051522009-07-16 15:30:09 +0000172 outs() << "Running selected passes on program to test for crash: ";
Rafael Espindola37302ea2010-08-05 02:16:32 +0000173 if (runPasses(Program, PassesToRun))
Chris Lattneread1dff2004-02-18 21:02:04 +0000174 return debugOptimizerCrash();
Chris Lattner6ca0b872003-10-13 21:04:26 +0000175 }
Misha Brukmand792c9b2003-07-24 18:17:43 +0000176
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000177 // Set up the execution environment, selecting a method to run LLVM bitcode.
Misha Brukmand792c9b2003-07-24 18:17:43 +0000178 if (initializeExecutionEnvironment()) return true;
179
Chris Lattner5e9868a2004-02-19 17:03:49 +0000180 // Test to see if we have a code generator crash.
Dan Gohmanee051522009-07-16 15:30:09 +0000181 outs() << "Running the code generator to test for a crash: ";
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000182 std::string Error;
183 compileProgram(Program, &Error);
184 if (!Error.empty()) {
185 outs() << Error;
186 return debugCodeGeneratorCrash(ErrMsg);
Chris Lattner5e9868a2004-02-19 17:03:49 +0000187 }
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000188 outs() << '\n';
Chris Lattner5e9868a2004-02-19 17:03:49 +0000189
Misha Brukmand792c9b2003-07-24 18:17:43 +0000190 // 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 //
Chris Lattnerd1123fd2003-08-22 18:57:43 +0000194 bool CreatedOutput = false;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000195 if (ReferenceOutputFile.empty()) {
Dan Gohmanee051522009-07-16 15:30:09 +0000196 outs() << "Generating reference output from raw program: ";
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000197 if (!createReferenceFile(Program)) {
198 return debugCodeGeneratorCrash(ErrMsg);
Chris Lattneread1dff2004-02-18 21:02:04 +0000199 }
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000200 CreatedOutput = true;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000201 }
202
Chris Lattner2ed06422003-10-14 20:52:55 +0000203 // Make sure the reference output file gets deleted on exit from this
204 // function, if appropriate.
Rafael Espindola319d9752013-06-18 16:21:54 +0000205 std::string ROF(ReferenceOutputFile);
206 FileRemover RemoverInstance(ROF, CreatedOutput && !SaveTemps);
Chris Lattner2ed06422003-10-14 20:52:55 +0000207
208 // Diff the output of the raw program against the reference output. If it
Michael J. Spencer3df5c042011-03-31 13:06:39 +0000209 // matches, then we assume there is a miscompilation bug and try to
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000210 // diagnose it.
Dan Gohmanee051522009-07-16 15:30:09 +0000211 outs() << "*** Checking the code generator...\n";
Rafael Espindolac89b1ef2010-07-30 14:19:00 +0000212 bool Diff = diffProgram(Program, "", "", false, &Error);
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000213 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);
Chris Lattneread1dff2004-02-18 21:02:04 +0000223 }
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000224 return false;
Chris Lattner2ed06422003-10-14 20:52:55 +0000225 }
226
Dan Gohmanee051522009-07-16 15:30:09 +0000227 outs() << "\n*** Input program does not match reference diff!\n";
228 outs() << "Debugging code generator problem!\n";
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000229 bool Failure = debugCodeGenerator(&Error);
230 if (!Error.empty()) {
231 errs() << Error;
232 return debugCodeGeneratorCrash(ErrMsg);
Chris Lattner5e9868a2004-02-19 17:03:49 +0000233 }
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000234 return Failure;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000235}
236
Chris Lattnerfd72bed2004-03-14 20:50:42 +0000237void 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)
Dan Gohmanee051522009-07-16 15:30:09 +0000241 outs() << " " << Funcs[i]->getName();
Chris Lattnerfd72bed2004-03-14 20:50:42 +0000242 if (NumPrint < Funcs.size())
Dan Gohmanee051522009-07-16 15:30:09 +0000243 outs() << "... <" << Funcs.size() << " total>";
244 outs().flush();
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000245}
Bill Wendling3f833432006-10-25 18:36:14 +0000246
247void 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)
Dan Gohmanee051522009-07-16 15:30:09 +0000251 outs() << " " << GVs[i]->getName();
Bill Wendling3f833432006-10-25 18:36:14 +0000252 if (NumPrint < GVs.size())
Dan Gohmanee051522009-07-16 15:30:09 +0000253 outs() << "... <" << GVs.size() << " total>";
254 outs().flush();
Bill Wendling3f833432006-10-25 18:36:14 +0000255}