blob: 78f35293d2a5fb316b8e5e4866916ddbe3c76653 [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"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Module.h"
Duncan P. N. Exon Smith5fbcc462015-03-26 05:03:10 +000019#include "llvm/IR/Verifier.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000020#include "llvm/IRReader/IRReader.h"
Chandler Carruth6cc07df2014-03-06 03:42:23 +000021#include "llvm/Linker/Linker.h"
Chris Lattner73a6bdd2002-11-20 22:28:10 +000022#include "llvm/Pass.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000023#include "llvm/Support/CommandLine.h"
24#include "llvm/Support/FileUtilities.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000025#include "llvm/Support/Host.h"
Chris Lattnera76611a2009-07-02 22:46:18 +000026#include "llvm/Support/SourceMgr.h"
Chris Lattnerac161bf2009-01-02 07:01:27 +000027#include "llvm/Support/raw_ostream.h"
Chris Lattner73a6bdd2002-11-20 22:28:10 +000028#include <memory>
Brian Gaeke960707c2003-11-11 22:41:34 +000029using namespace llvm;
30
Daniel Dunbar8575a602009-08-18 03:35:57 +000031namespace llvm {
Justin Bogner8d0a0812016-09-02 01:21:37 +000032Triple TargetTriple;
Daniel Dunbar8575a602009-08-18 03:35:57 +000033}
34
Misha Brukmand792c9b2003-07-24 18:17:43 +000035// Anonymous namespace to define command line options for debugging.
36//
37namespace {
Justin Bogner8d0a0812016-09-02 01:21:37 +000038// Output - The user can specify a file containing the expected output of the
39// program. If this filename is set, it is used as the reference diff source,
40// otherwise the raw input run through an interpreter is used as the reference
41// source.
42//
43cl::opt<std::string> OutputFile("output",
44 cl::desc("Specify a reference program output "
45 "(for miscompilation detection)"));
Misha Brukmand792c9b2003-07-24 18:17:43 +000046}
47
Chris Lattner327019b2004-02-18 21:24:48 +000048/// setNewProgram - If we reduce or update the program somehow, call this method
49/// to update bugdriver with it. This deletes the old module and sets the
50/// specified one as the current program.
51void BugDriver::setNewProgram(Module *M) {
52 delete Program;
53 Program = M;
54}
55
Chris Lattner16a41312003-04-24 17:02:17 +000056/// getPassesString - Turn a list of passes into a string which indicates the
57/// command line options that must be passed to add the passes.
58///
Rafael Espindola33e81a82010-08-08 03:55:08 +000059std::string llvm::getPassesString(const std::vector<std::string> &Passes) {
Chris Lattner16a41312003-04-24 17:02:17 +000060 std::string Result;
61 for (unsigned i = 0, e = Passes.size(); i != e; ++i) {
Justin Bogner8d0a0812016-09-02 01:21:37 +000062 if (i)
63 Result += " ";
Chris Lattner16a41312003-04-24 17:02:17 +000064 Result += "-";
Rafael Espindola33e81a82010-08-08 03:55:08 +000065 Result += Passes[i];
Chris Lattner16a41312003-04-24 17:02:17 +000066 }
67 return Result;
68}
69
Justin Bogner8d0a0812016-09-02 01:21:37 +000070BugDriver::BugDriver(const char *toolname, bool find_bugs, unsigned timeout,
71 unsigned memlimit, bool use_valgrind, LLVMContext &ctxt)
72 : Context(ctxt), ToolName(toolname), ReferenceOutputFile(OutputFile),
73 Program(nullptr), Interpreter(nullptr), SafeInterpreter(nullptr),
74 cc(nullptr), run_find_bugs(find_bugs), Timeout(timeout),
75 MemoryLimit(memlimit), UseValgrind(use_valgrind) {}
Misha Brukmand792c9b2003-07-24 18:17:43 +000076
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000077BugDriver::~BugDriver() {
78 delete Program;
David Blaikie37436ed2014-04-25 20:15:16 +000079 if (Interpreter != SafeInterpreter)
80 delete Interpreter;
81 delete SafeInterpreter;
Davide Italianoab256212015-10-14 20:29:54 +000082 delete cc;
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000083}
84
Rafael Espindola28b351a2014-08-26 17:19:03 +000085std::unique_ptr<Module> llvm::parseInputFile(StringRef Filename,
86 LLVMContext &Ctxt) {
Chris Lattnera76611a2009-07-02 22:46:18 +000087 SMDiagnostic Err;
Rafael Espindolad233b062014-08-26 17:29:46 +000088 std::unique_ptr<Module> Result = parseIRFile(Filename, Err, Ctxt);
Duncan P. N. Exon Smith866aed72015-03-26 05:03:06 +000089 if (!Result) {
Chris Lattnera3a06812011-10-16 04:47:35 +000090 Err.print("bugpoint", errs());
Duncan P. N. Exon Smith866aed72015-03-26 05:03:06 +000091 return Result;
92 }
Dan Gohman728a81a2009-09-03 16:32:58 +000093
Duncan P. N. Exon Smith5fbcc462015-03-26 05:03:10 +000094 if (verifyModule(*Result, &errs())) {
Duncan P. N. Exon Smith46282822015-03-31 03:07:23 +000095 errs() << "bugpoint: " << Filename << ": error: input module is broken!\n";
Duncan P. N. Exon Smith5fbcc462015-03-26 05:03:10 +000096 return std::unique_ptr<Module>();
97 }
98
Daniel Dunbar8575a602009-08-18 03:35:57 +000099 // If we don't have an override triple, use the first one to configure
100 // bugpoint, or use the host triple if none provided.
Duncan P. N. Exon Smith866aed72015-03-26 05:03:06 +0000101 if (TargetTriple.getTriple().empty()) {
102 Triple TheTriple(Result->getTargetTriple());
Daniel Dunbar8575a602009-08-18 03:35:57 +0000103
Duncan P. N. Exon Smith866aed72015-03-26 05:03:06 +0000104 if (TheTriple.getTriple().empty())
105 TheTriple.setTriple(sys::getDefaultTargetTriple());
Michael J. Spencer3df5c042011-03-31 13:06:39 +0000106
Duncan P. N. Exon Smith866aed72015-03-26 05:03:06 +0000107 TargetTriple.setTriple(TheTriple.getTriple());
Daniel Dunbar8575a602009-08-18 03:35:57 +0000108 }
Duncan P. N. Exon Smith866aed72015-03-26 05:03:06 +0000109
110 Result->setTargetTriple(TargetTriple.getTriple()); // override the triple
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000111 return Result;
112}
113
114// This method takes the specified list of LLVM input files, attempts to load
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000115// them, either as assembly or bitcode, then link them together. It returns
116// true on failure (if, for example, an input bitcode file could not be
Brian Gaeke61a05da2003-05-23 05:34:32 +0000117// parsed), and false on success.
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000118//
119bool BugDriver::addSources(const std::vector<std::string> &Filenames) {
Craig Toppere73658d2014-04-28 04:05:08 +0000120 assert(!Program && "Cannot call addSources multiple times!");
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000121 assert(!Filenames.empty() && "Must specify at least on input filename!");
122
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000123 // Load the first input file.
Rafael Espindola28b351a2014-08-26 17:19:03 +0000124 Program = parseInputFile(Filenames[0], Context).release();
Justin Bogner8d0a0812016-09-02 01:21:37 +0000125 if (!Program)
126 return true;
Michael J. Spencer3df5c042011-03-31 13:06:39 +0000127
Rafael Espindolabbdce492010-08-07 23:03:21 +0000128 outs() << "Read input file : '" << Filenames[0] << "'\n";
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000129
130 for (unsigned i = 1, e = Filenames.size(); i != e; ++i) {
Rafael Espindola28b351a2014-08-26 17:19:03 +0000131 std::unique_ptr<Module> M = parseInputFile(Filenames[i], Context);
Justin Bogner8d0a0812016-09-02 01:21:37 +0000132 if (!M.get())
133 return true;
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000134
Rafael Espindolabbdce492010-08-07 23:03:21 +0000135 outs() << "Linking in input file: '" << Filenames[i] << "'\n";
Rafael Espindola434e9562015-12-16 23:16:33 +0000136 if (Linker::linkModules(*Program, std::move(M)))
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000137 return true;
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000138 }
139
Rafael Espindolabbdce492010-08-07 23:03:21 +0000140 outs() << "*** All input ok\n";
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000141
142 // All input files read successfully!
143 return false;
144}
145
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000146/// run - The top level method that is invoked after all of the instance
147/// variables are set up from command line arguments.
148///
Justin Bogner1c039152016-09-06 17:18:22 +0000149Error BugDriver::run() {
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000150 if (run_find_bugs) {
151 // Rearrange the passes and apply them to the program. Repeat this process
152 // until the user kills the program or we find a bug.
Justin Bogner1c039152016-09-06 17:18:22 +0000153 return runManyPasses(PassesToRun);
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000154 }
Reid Spencer842118c2005-12-22 20:02:55 +0000155
Michael J. Spencer3df5c042011-03-31 13:06:39 +0000156 // If we're not running as a child, the first thing that we must do is
157 // determine what the problem is. Does the optimization series crash the
158 // compiler, or does it produce illegal code? We make the top-level
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000159 // decision by trying to run all of the passes on the input program,
Michael J. Spencer3df5c042011-03-31 13:06:39 +0000160 // which should generate a bitcode file. If it does generate a bitcode
161 // file, then we know the compiler didn't crash, so try to diagnose a
Reid Spencer842118c2005-12-22 20:02:55 +0000162 // miscompilation.
Chris Lattner6ca0b872003-10-13 21:04:26 +0000163 if (!PassesToRun.empty()) {
Dan Gohmanee051522009-07-16 15:30:09 +0000164 outs() << "Running selected passes on program to test for crash: ";
Rafael Espindola37302ea2010-08-05 02:16:32 +0000165 if (runPasses(Program, PassesToRun))
Chris Lattneread1dff2004-02-18 21:02:04 +0000166 return debugOptimizerCrash();
Chris Lattner6ca0b872003-10-13 21:04:26 +0000167 }
Misha Brukmand792c9b2003-07-24 18:17:43 +0000168
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000169 // Set up the execution environment, selecting a method to run LLVM bitcode.
Justin Bogner1c039152016-09-06 17:18:22 +0000170 if (Error E = initializeExecutionEnvironment())
171 return E;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000172
Chris Lattner5e9868a2004-02-19 17:03:49 +0000173 // Test to see if we have a code generator crash.
Dan Gohmanee051522009-07-16 15:30:09 +0000174 outs() << "Running the code generator to test for a crash: ";
Justin Bogner1c039152016-09-06 17:18:22 +0000175 if (Error E = compileProgram(Program)) {
176 outs() << toString(std::move(E));
177 return debugCodeGeneratorCrash();
Chris Lattner5e9868a2004-02-19 17:03:49 +0000178 }
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000179 outs() << '\n';
Chris Lattner5e9868a2004-02-19 17:03:49 +0000180
Misha Brukmand792c9b2003-07-24 18:17:43 +0000181 // Run the raw input to see where we are coming from. If a reference output
182 // was specified, make sure that the raw output matches it. If not, it's a
183 // problem in the front-end or the code generator.
184 //
Chris Lattnerd1123fd2003-08-22 18:57:43 +0000185 bool CreatedOutput = false;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000186 if (ReferenceOutputFile.empty()) {
Dan Gohmanee051522009-07-16 15:30:09 +0000187 outs() << "Generating reference output from raw program: ";
Justin Bogner1c039152016-09-06 17:18:22 +0000188 if (Error E = createReferenceFile(Program)) {
189 errs() << toString(std::move(E));
190 return debugCodeGeneratorCrash();
Chris Lattneread1dff2004-02-18 21:02:04 +0000191 }
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000192 CreatedOutput = true;
Misha Brukmand792c9b2003-07-24 18:17:43 +0000193 }
194
Chris Lattner2ed06422003-10-14 20:52:55 +0000195 // Make sure the reference output file gets deleted on exit from this
196 // function, if appropriate.
Rafael Espindola319d9752013-06-18 16:21:54 +0000197 std::string ROF(ReferenceOutputFile);
198 FileRemover RemoverInstance(ROF, CreatedOutput && !SaveTemps);
Chris Lattner2ed06422003-10-14 20:52:55 +0000199
200 // Diff the output of the raw program against the reference output. If it
Michael J. Spencer3df5c042011-03-31 13:06:39 +0000201 // matches, then we assume there is a miscompilation bug and try to
Patrick Jenkinsc46c0382006-08-15 16:40:49 +0000202 // diagnose it.
Dan Gohmanee051522009-07-16 15:30:09 +0000203 outs() << "*** Checking the code generator...\n";
Justin Bogner1c039152016-09-06 17:18:22 +0000204 Expected<bool> Diff = diffProgram(Program, "", "", false);
205 if (Error E = Diff.takeError()) {
206 errs() << toString(std::move(E));
207 return debugCodeGeneratorCrash();
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000208 }
Justin Bogner1c039152016-09-06 17:18:22 +0000209 if (!*Diff) {
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000210 outs() << "\n*** Output matches: Debugging miscompilation!\n";
Justin Bogner1c039152016-09-06 17:18:22 +0000211 if (Error E = debugMiscompilation()) {
212 errs() << toString(std::move(E));
213 return debugCodeGeneratorCrash();
Chris Lattneread1dff2004-02-18 21:02:04 +0000214 }
Justin Bogner1c039152016-09-06 17:18:22 +0000215 return Error::success();
Chris Lattner2ed06422003-10-14 20:52:55 +0000216 }
217
Dan Gohmanee051522009-07-16 15:30:09 +0000218 outs() << "\n*** Input program does not match reference diff!\n";
219 outs() << "Debugging code generator problem!\n";
Justin Bogner1c039152016-09-06 17:18:22 +0000220 if (Error E = debugCodeGenerator()) {
221 errs() << toString(std::move(E));
222 return debugCodeGeneratorCrash();
Chris Lattner5e9868a2004-02-19 17:03:49 +0000223 }
Justin Bogner1c039152016-09-06 17:18:22 +0000224 return Error::success();
Misha Brukmand792c9b2003-07-24 18:17:43 +0000225}
226
Justin Bogner8d0a0812016-09-02 01:21:37 +0000227void llvm::PrintFunctionList(const std::vector<Function *> &Funcs) {
Chris Lattnerfd72bed2004-03-14 20:50:42 +0000228 unsigned NumPrint = Funcs.size();
Justin Bogner8d0a0812016-09-02 01:21:37 +0000229 if (NumPrint > 10)
230 NumPrint = 10;
Chris Lattnerfd72bed2004-03-14 20:50:42 +0000231 for (unsigned i = 0; i != NumPrint; ++i)
Dan Gohmanee051522009-07-16 15:30:09 +0000232 outs() << " " << Funcs[i]->getName();
Chris Lattnerfd72bed2004-03-14 20:50:42 +0000233 if (NumPrint < Funcs.size())
Dan Gohmanee051522009-07-16 15:30:09 +0000234 outs() << "... <" << Funcs.size() << " total>";
235 outs().flush();
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000236}
Bill Wendling3f833432006-10-25 18:36:14 +0000237
Justin Bogner8d0a0812016-09-02 01:21:37 +0000238void llvm::PrintGlobalVariableList(const std::vector<GlobalVariable *> &GVs) {
Bill Wendling3f833432006-10-25 18:36:14 +0000239 unsigned NumPrint = GVs.size();
Justin Bogner8d0a0812016-09-02 01:21:37 +0000240 if (NumPrint > 10)
241 NumPrint = 10;
Bill Wendling3f833432006-10-25 18:36:14 +0000242 for (unsigned i = 0; i != NumPrint; ++i)
Dan Gohmanee051522009-07-16 15:30:09 +0000243 outs() << " " << GVs[i]->getName();
Bill Wendling3f833432006-10-25 18:36:14 +0000244 if (NumPrint < GVs.size())
Dan Gohmanee051522009-07-16 15:30:09 +0000245 outs() << "... <" << GVs.size() << " total>";
246 outs().flush();
Bill Wendling3f833432006-10-25 18:36:14 +0000247}