blob: 3832e075a693c05702bd7f098667593044b25da1 [file] [log] [blame]
Chris Lattnerafade922002-11-20 22:28:10 +00001//===- BugDriver.cpp - Top-Level BugPoint class implementation ------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-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 Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerafade922002-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 Lattnerf1b20d82006-06-06 22:30:59 +000017#include "ToolRunner.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000018#include "llvm/IR/Module.h"
Duncan P. N. Exon Smith7a1a3e92015-03-26 05:03:10 +000019#include "llvm/IR/Verifier.h"
Chandler Carruth7fc162f2013-03-26 02:25:37 +000020#include "llvm/IRReader/IRReader.h"
Chandler Carruth8a67f122014-03-06 03:42:23 +000021#include "llvm/Linker/Linker.h"
Chris Lattnerafade922002-11-20 22:28:10 +000022#include "llvm/Pass.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/Support/CommandLine.h"
24#include "llvm/Support/FileUtilities.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000025#include "llvm/Support/Host.h"
Chris Lattner92bcb422009-07-02 22:46:18 +000026#include "llvm/Support/SourceMgr.h"
Chris Lattnerdf986172009-01-02 07:01:27 +000027#include "llvm/Support/raw_ostream.h"
Chris Lattnerafade922002-11-20 22:28:10 +000028#include <memory>
Brian Gaeked0fde302003-11-11 22:41:34 +000029using namespace llvm;
30
Daniel Dunbarca740962009-08-18 03:35:57 +000031namespace llvm {
Justin Bogner388e8b92016-09-02 01:21:37 +000032Triple TargetTriple;
Daniel Dunbarca740962009-08-18 03:35:57 +000033}
34
Rafael Espindola324f1412017-11-16 17:35:50 +000035DiscardTemp::~DiscardTemp() {
Rafael Espindolac1382282017-11-16 21:40:10 +000036 if (SaveTemps) {
37 if (Error E = File.keep())
38 errs() << "Failed to keep temp file " << toString(std::move(E)) << '\n';
39 return;
40 }
Rafael Espindola324f1412017-11-16 17:35:50 +000041 if (Error E = File.discard())
42 errs() << "Failed to delete temp file " << toString(std::move(E)) << '\n';
43}
44
Misha Brukman50733362003-07-24 18:17:43 +000045// Anonymous namespace to define command line options for debugging.
46//
47namespace {
Justin Bogner388e8b92016-09-02 01:21:37 +000048// 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
51// source.
52//
53cl::opt<std::string> OutputFile("output",
54 cl::desc("Specify a reference program output "
55 "(for miscompilation detection)"));
Misha Brukman50733362003-07-24 18:17:43 +000056}
57
Rafael Espindolacdac12c2018-02-14 21:44:34 +000058/// If we reduce or update the program somehow, call this method to update
59/// bugdriver with it. This deletes the old module and sets the specified one
60/// as the current program.
61void BugDriver::setNewProgram(std::unique_ptr<Module> M) {
62 Program = std::move(M);
Chris Lattner06905db2004-02-18 21:24:48 +000063}
64
Chris Lattner640f22e2003-04-24 17:02:17 +000065/// getPassesString - Turn a list of passes into a string which indicates the
66/// command line options that must be passed to add the passes.
67///
Rafael Espindola8261dfe2010-08-08 03:55:08 +000068std::string llvm::getPassesString(const std::vector<std::string> &Passes) {
Chris Lattner640f22e2003-04-24 17:02:17 +000069 std::string Result;
70 for (unsigned i = 0, e = Passes.size(); i != e; ++i) {
Justin Bogner388e8b92016-09-02 01:21:37 +000071 if (i)
72 Result += " ";
Chris Lattner640f22e2003-04-24 17:02:17 +000073 Result += "-";
Rafael Espindola8261dfe2010-08-08 03:55:08 +000074 Result += Passes[i];
Chris Lattner640f22e2003-04-24 17:02:17 +000075 }
76 return Result;
77}
78
Justin Bogner388e8b92016-09-02 01:21:37 +000079BugDriver::BugDriver(const char *toolname, bool find_bugs, unsigned timeout,
80 unsigned memlimit, bool use_valgrind, LLVMContext &ctxt)
81 : Context(ctxt), ToolName(toolname), ReferenceOutputFile(OutputFile),
82 Program(nullptr), Interpreter(nullptr), SafeInterpreter(nullptr),
83 cc(nullptr), run_find_bugs(find_bugs), Timeout(timeout),
84 MemoryLimit(memlimit), UseValgrind(use_valgrind) {}
Misha Brukman50733362003-07-24 18:17:43 +000085
Jeffrey Yasskinc1dc0672010-03-22 05:23:37 +000086BugDriver::~BugDriver() {
David Blaikie5ed33e02014-04-25 20:15:16 +000087 if (Interpreter != SafeInterpreter)
88 delete Interpreter;
89 delete SafeInterpreter;
Davide Italianof7b2acb2015-10-14 20:29:54 +000090 delete cc;
Jeffrey Yasskinc1dc0672010-03-22 05:23:37 +000091}
92
Rafael Espindola1bfd87a2014-08-26 17:19:03 +000093std::unique_ptr<Module> llvm::parseInputFile(StringRef Filename,
94 LLVMContext &Ctxt) {
Chris Lattner92bcb422009-07-02 22:46:18 +000095 SMDiagnostic Err;
Rafael Espindola81e49922014-08-26 17:29:46 +000096 std::unique_ptr<Module> Result = parseIRFile(Filename, Err, Ctxt);
Duncan P. N. Exon Smith6a62f782015-03-26 05:03:06 +000097 if (!Result) {
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000098 Err.print("bugpoint", errs());
Duncan P. N. Exon Smith6a62f782015-03-26 05:03:06 +000099 return Result;
100 }
Dan Gohmandad45ea2009-09-03 16:32:58 +0000101
Duncan P. N. Exon Smith7a1a3e92015-03-26 05:03:10 +0000102 if (verifyModule(*Result, &errs())) {
Duncan P. N. Exon Smith4e5fdbf2015-03-31 03:07:23 +0000103 errs() << "bugpoint: " << Filename << ": error: input module is broken!\n";
Duncan P. N. Exon Smith7a1a3e92015-03-26 05:03:10 +0000104 return std::unique_ptr<Module>();
105 }
106
Daniel Dunbarca740962009-08-18 03:35:57 +0000107 // If we don't have an override triple, use the first one to configure
108 // bugpoint, or use the host triple if none provided.
Duncan P. N. Exon Smith6a62f782015-03-26 05:03:06 +0000109 if (TargetTriple.getTriple().empty()) {
110 Triple TheTriple(Result->getTargetTriple());
Daniel Dunbarca740962009-08-18 03:35:57 +0000111
Duncan P. N. Exon Smith6a62f782015-03-26 05:03:06 +0000112 if (TheTriple.getTriple().empty())
113 TheTriple.setTriple(sys::getDefaultTargetTriple());
Michael J. Spencer56584fc2011-03-31 13:06:39 +0000114
Duncan P. N. Exon Smith6a62f782015-03-26 05:03:06 +0000115 TargetTriple.setTriple(TheTriple.getTriple());
Daniel Dunbarca740962009-08-18 03:35:57 +0000116 }
Duncan P. N. Exon Smith6a62f782015-03-26 05:03:06 +0000117
118 Result->setTargetTriple(TargetTriple.getTriple()); // override the triple
Chris Lattnerafade922002-11-20 22:28:10 +0000119 return Result;
120}
121
Rafael Espindolacdac12c2018-02-14 21:44:34 +0000122std::unique_ptr<Module> BugDriver::swapProgramIn(std::unique_ptr<Module> M) {
123 std::unique_ptr<Module> OldProgram = std::move(Program);
124 Program = std::move(M);
125 return OldProgram;
126}
127
Chris Lattnerafade922002-11-20 22:28:10 +0000128// This method takes the specified list of LLVM input files, attempts to load
Gabor Greif8ff70c22007-07-04 21:55:50 +0000129// them, either as assembly or bitcode, then link them together. It returns
130// true on failure (if, for example, an input bitcode file could not be
Brian Gaekedae7f922003-05-23 05:34:32 +0000131// parsed), and false on success.
Chris Lattnerafade922002-11-20 22:28:10 +0000132//
133bool BugDriver::addSources(const std::vector<std::string> &Filenames) {
Craig Topperc34a25d2014-04-28 04:05:08 +0000134 assert(!Program && "Cannot call addSources multiple times!");
Chris Lattnerafade922002-11-20 22:28:10 +0000135 assert(!Filenames.empty() && "Must specify at least on input filename!");
136
Nick Lewycky22ff7482010-04-12 05:08:25 +0000137 // Load the first input file.
Rafael Espindolacdac12c2018-02-14 21:44:34 +0000138 Program = parseInputFile(Filenames[0], Context);
Justin Bogner388e8b92016-09-02 01:21:37 +0000139 if (!Program)
140 return true;
Michael J. Spencer56584fc2011-03-31 13:06:39 +0000141
Rafael Espindola7f99f74b2010-08-07 23:03:21 +0000142 outs() << "Read input file : '" << Filenames[0] << "'\n";
Nick Lewycky22ff7482010-04-12 05:08:25 +0000143
144 for (unsigned i = 1, e = Filenames.size(); i != e; ++i) {
Rafael Espindola1bfd87a2014-08-26 17:19:03 +0000145 std::unique_ptr<Module> M = parseInputFile(Filenames[i], Context);
Justin Bogner388e8b92016-09-02 01:21:37 +0000146 if (!M.get())
147 return true;
Nick Lewycky22ff7482010-04-12 05:08:25 +0000148
Rafael Espindola7f99f74b2010-08-07 23:03:21 +0000149 outs() << "Linking in input file: '" << Filenames[i] << "'\n";
Rafael Espindolad912be92015-12-16 23:16:33 +0000150 if (Linker::linkModules(*Program, std::move(M)))
Nick Lewycky22ff7482010-04-12 05:08:25 +0000151 return true;
Chris Lattnerafade922002-11-20 22:28:10 +0000152 }
153
Rafael Espindola7f99f74b2010-08-07 23:03:21 +0000154 outs() << "*** All input ok\n";
Chris Lattnerafade922002-11-20 22:28:10 +0000155
156 // All input files read successfully!
157 return false;
158}
159
Chris Lattnerafade922002-11-20 22:28:10 +0000160/// run - The top level method that is invoked after all of the instance
161/// variables are set up from command line arguments.
162///
Justin Bognerd8090ae2016-09-06 17:18:22 +0000163Error BugDriver::run() {
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000164 if (run_find_bugs) {
165 // Rearrange the passes and apply them to the program. Repeat this process
166 // until the user kills the program or we find a bug.
Justin Bognerd8090ae2016-09-06 17:18:22 +0000167 return runManyPasses(PassesToRun);
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000168 }
Reid Spencerc4bb0522005-12-22 20:02:55 +0000169
Michael J. Spencer56584fc2011-03-31 13:06:39 +0000170 // If we're not running as a child, the first thing that we must do is
171 // determine what the problem is. Does the optimization series crash the
172 // compiler, or does it produce illegal code? We make the top-level
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +0000173 // decision by trying to run all of the passes on the input program,
Michael J. Spencer56584fc2011-03-31 13:06:39 +0000174 // which should generate a bitcode file. If it does generate a bitcode
175 // file, then we know the compiler didn't crash, so try to diagnose a
Reid Spencerc4bb0522005-12-22 20:02:55 +0000176 // miscompilation.
Chris Lattner99b85332003-10-13 21:04:26 +0000177 if (!PassesToRun.empty()) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000178 outs() << "Running selected passes on program to test for crash: ";
Rafael Espindolacdac12c2018-02-14 21:44:34 +0000179 if (runPasses(*Program, PassesToRun))
Chris Lattner02526262004-02-18 21:02:04 +0000180 return debugOptimizerCrash();
Chris Lattner99b85332003-10-13 21:04:26 +0000181 }
Misha Brukman50733362003-07-24 18:17:43 +0000182
Gabor Greif8ff70c22007-07-04 21:55:50 +0000183 // Set up the execution environment, selecting a method to run LLVM bitcode.
Justin Bognerd8090ae2016-09-06 17:18:22 +0000184 if (Error E = initializeExecutionEnvironment())
185 return E;
Misha Brukman50733362003-07-24 18:17:43 +0000186
Chris Lattner7c955fd2004-02-19 17:03:49 +0000187 // Test to see if we have a code generator crash.
Dan Gohmanac95cc72009-07-16 15:30:09 +0000188 outs() << "Running the code generator to test for a crash: ";
Rafael Espindolacdac12c2018-02-14 21:44:34 +0000189 if (Error E = compileProgram(*Program)) {
Justin Bognerd8090ae2016-09-06 17:18:22 +0000190 outs() << toString(std::move(E));
191 return debugCodeGeneratorCrash();
Chris Lattner7c955fd2004-02-19 17:03:49 +0000192 }
Nick Lewycky22ff7482010-04-12 05:08:25 +0000193 outs() << '\n';
Chris Lattner7c955fd2004-02-19 17:03:49 +0000194
Misha Brukman50733362003-07-24 18:17:43 +0000195 // Run the raw input to see where we are coming from. If a reference output
196 // was specified, make sure that the raw output matches it. If not, it's a
197 // problem in the front-end or the code generator.
198 //
Chris Lattnerc28c1d32003-08-22 18:57:43 +0000199 bool CreatedOutput = false;
Misha Brukman50733362003-07-24 18:17:43 +0000200 if (ReferenceOutputFile.empty()) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000201 outs() << "Generating reference output from raw program: ";
Rafael Espindolacdac12c2018-02-14 21:44:34 +0000202 if (Error E = createReferenceFile(*Program)) {
Justin Bognerd8090ae2016-09-06 17:18:22 +0000203 errs() << toString(std::move(E));
204 return debugCodeGeneratorCrash();
Chris Lattner02526262004-02-18 21:02:04 +0000205 }
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000206 CreatedOutput = true;
Misha Brukman50733362003-07-24 18:17:43 +0000207 }
208
Chris Lattnera5a96a92003-10-14 20:52:55 +0000209 // Make sure the reference output file gets deleted on exit from this
210 // function, if appropriate.
Rafael Espindola3b9eb802013-06-18 16:21:54 +0000211 std::string ROF(ReferenceOutputFile);
212 FileRemover RemoverInstance(ROF, CreatedOutput && !SaveTemps);
Chris Lattnera5a96a92003-10-14 20:52:55 +0000213
214 // Diff the output of the raw program against the reference output. If it
Michael J. Spencer56584fc2011-03-31 13:06:39 +0000215 // matches, then we assume there is a miscompilation bug and try to
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000216 // diagnose it.
Dan Gohmanac95cc72009-07-16 15:30:09 +0000217 outs() << "*** Checking the code generator...\n";
Rafael Espindolacdac12c2018-02-14 21:44:34 +0000218 Expected<bool> Diff = diffProgram(*Program, "", "", false);
Justin Bognerd8090ae2016-09-06 17:18:22 +0000219 if (Error E = Diff.takeError()) {
220 errs() << toString(std::move(E));
221 return debugCodeGeneratorCrash();
Nick Lewycky22ff7482010-04-12 05:08:25 +0000222 }
Justin Bognerd8090ae2016-09-06 17:18:22 +0000223 if (!*Diff) {
Nick Lewycky22ff7482010-04-12 05:08:25 +0000224 outs() << "\n*** Output matches: Debugging miscompilation!\n";
Justin Bognerd8090ae2016-09-06 17:18:22 +0000225 if (Error E = debugMiscompilation()) {
226 errs() << toString(std::move(E));
227 return debugCodeGeneratorCrash();
Chris Lattner02526262004-02-18 21:02:04 +0000228 }
Justin Bognerd8090ae2016-09-06 17:18:22 +0000229 return Error::success();
Chris Lattnera5a96a92003-10-14 20:52:55 +0000230 }
231
Dan Gohmanac95cc72009-07-16 15:30:09 +0000232 outs() << "\n*** Input program does not match reference diff!\n";
233 outs() << "Debugging code generator problem!\n";
Justin Bognerd8090ae2016-09-06 17:18:22 +0000234 if (Error E = debugCodeGenerator()) {
235 errs() << toString(std::move(E));
236 return debugCodeGeneratorCrash();
Chris Lattner7c955fd2004-02-19 17:03:49 +0000237 }
Justin Bognerd8090ae2016-09-06 17:18:22 +0000238 return Error::success();
Misha Brukman50733362003-07-24 18:17:43 +0000239}
240
Justin Bogner388e8b92016-09-02 01:21:37 +0000241void llvm::PrintFunctionList(const std::vector<Function *> &Funcs) {
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000242 unsigned NumPrint = Funcs.size();
Justin Bogner388e8b92016-09-02 01:21:37 +0000243 if (NumPrint > 10)
244 NumPrint = 10;
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000245 for (unsigned i = 0; i != NumPrint; ++i)
Dan Gohmanac95cc72009-07-16 15:30:09 +0000246 outs() << " " << Funcs[i]->getName();
Chris Lattnerefdc0b52004-03-14 20:50:42 +0000247 if (NumPrint < Funcs.size())
Dan Gohmanac95cc72009-07-16 15:30:09 +0000248 outs() << "... <" << Funcs.size() << " total>";
249 outs().flush();
Chris Lattnerafade922002-11-20 22:28:10 +0000250}
Bill Wendling4e3be892006-10-25 18:36:14 +0000251
Justin Bogner388e8b92016-09-02 01:21:37 +0000252void llvm::PrintGlobalVariableList(const std::vector<GlobalVariable *> &GVs) {
Bill Wendling4e3be892006-10-25 18:36:14 +0000253 unsigned NumPrint = GVs.size();
Justin Bogner388e8b92016-09-02 01:21:37 +0000254 if (NumPrint > 10)
255 NumPrint = 10;
Bill Wendling4e3be892006-10-25 18:36:14 +0000256 for (unsigned i = 0; i != NumPrint; ++i)
Dan Gohmanac95cc72009-07-16 15:30:09 +0000257 outs() << " " << GVs[i]->getName();
Bill Wendling4e3be892006-10-25 18:36:14 +0000258 if (NumPrint < GVs.size())
Dan Gohmanac95cc72009-07-16 15:30:09 +0000259 outs() << "... <" << GVs.size() << " total>";
260 outs().flush();
Bill Wendling4e3be892006-10-25 18:36:14 +0000261}