[yaml2obj/obj2yaml] - Add support for .stack_sizes sections.
[llvm-complete.git] / tools / bugpoint / BugDriver.cpp
blob942028cad80b1ceb7f7394dfee640fde69a4fd53
1 //===- BugDriver.cpp - Top-Level BugPoint class implementation ------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This class contains all of the shared state and information that is used by
10 // the BugPoint tool to track down errors in optimizations. This class is the
11 // main driver class that invokes all sub-functionality.
13 //===----------------------------------------------------------------------===//
15 #include "BugDriver.h"
16 #include "ToolRunner.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/IR/Verifier.h"
19 #include "llvm/IRReader/IRReader.h"
20 #include "llvm/Linker/Linker.h"
21 #include "llvm/Pass.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/FileUtilities.h"
24 #include "llvm/Support/Host.h"
25 #include "llvm/Support/SourceMgr.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include <memory>
28 using namespace llvm;
30 namespace llvm {
31 Triple TargetTriple;
34 DiscardTemp::~DiscardTemp() {
35 if (SaveTemps) {
36 if (Error E = File.keep())
37 errs() << "Failed to keep temp file " << toString(std::move(E)) << '\n';
38 return;
40 if (Error E = File.discard())
41 errs() << "Failed to delete temp file " << toString(std::move(E)) << '\n';
44 // Anonymous namespace to define command line options for debugging.
46 namespace {
47 // Output - The user can specify a file containing the expected output of the
48 // program. If this filename is set, it is used as the reference diff source,
49 // otherwise the raw input run through an interpreter is used as the reference
50 // source.
52 cl::opt<std::string> OutputFile("output",
53 cl::desc("Specify a reference program output "
54 "(for miscompilation detection)"));
57 /// If we reduce or update the program somehow, call this method to update
58 /// bugdriver with it. This deletes the old module and sets the specified one
59 /// as the current program.
60 void BugDriver::setNewProgram(std::unique_ptr<Module> M) {
61 Program = std::move(M);
64 /// getPassesString - Turn a list of passes into a string which indicates the
65 /// command line options that must be passed to add the passes.
66 ///
67 std::string llvm::getPassesString(const std::vector<std::string> &Passes) {
68 std::string Result;
69 for (unsigned i = 0, e = Passes.size(); i != e; ++i) {
70 if (i)
71 Result += " ";
72 Result += "-";
73 Result += Passes[i];
75 return Result;
78 BugDriver::BugDriver(const char *toolname, bool find_bugs, unsigned timeout,
79 unsigned memlimit, bool use_valgrind, LLVMContext &ctxt)
80 : Context(ctxt), ToolName(toolname), ReferenceOutputFile(OutputFile),
81 Program(nullptr), Interpreter(nullptr), SafeInterpreter(nullptr),
82 cc(nullptr), run_find_bugs(find_bugs), Timeout(timeout),
83 MemoryLimit(memlimit), UseValgrind(use_valgrind) {}
85 BugDriver::~BugDriver() {
86 if (Interpreter != SafeInterpreter)
87 delete Interpreter;
88 delete SafeInterpreter;
89 delete cc;
92 std::unique_ptr<Module> llvm::parseInputFile(StringRef Filename,
93 LLVMContext &Ctxt) {
94 SMDiagnostic Err;
95 std::unique_ptr<Module> Result = parseIRFile(Filename, Err, Ctxt);
96 if (!Result) {
97 Err.print("bugpoint", errs());
98 return Result;
101 if (verifyModule(*Result, &errs())) {
102 errs() << "bugpoint: " << Filename << ": error: input module is broken!\n";
103 return std::unique_ptr<Module>();
106 // If we don't have an override triple, use the first one to configure
107 // bugpoint, or use the host triple if none provided.
108 if (TargetTriple.getTriple().empty()) {
109 Triple TheTriple(Result->getTargetTriple());
111 if (TheTriple.getTriple().empty())
112 TheTriple.setTriple(sys::getDefaultTargetTriple());
114 TargetTriple.setTriple(TheTriple.getTriple());
117 Result->setTargetTriple(TargetTriple.getTriple()); // override the triple
118 return Result;
121 std::unique_ptr<Module> BugDriver::swapProgramIn(std::unique_ptr<Module> M) {
122 std::unique_ptr<Module> OldProgram = std::move(Program);
123 Program = std::move(M);
124 return OldProgram;
127 // This method takes the specified list of LLVM input files, attempts to load
128 // them, either as assembly or bitcode, then link them together. It returns
129 // true on failure (if, for example, an input bitcode file could not be
130 // parsed), and false on success.
132 bool BugDriver::addSources(const std::vector<std::string> &Filenames) {
133 assert(!Program && "Cannot call addSources multiple times!");
134 assert(!Filenames.empty() && "Must specify at least on input filename!");
136 // Load the first input file.
137 Program = parseInputFile(Filenames[0], Context);
138 if (!Program)
139 return true;
141 outs() << "Read input file : '" << Filenames[0] << "'\n";
143 for (unsigned i = 1, e = Filenames.size(); i != e; ++i) {
144 std::unique_ptr<Module> M = parseInputFile(Filenames[i], Context);
145 if (!M.get())
146 return true;
148 outs() << "Linking in input file: '" << Filenames[i] << "'\n";
149 if (Linker::linkModules(*Program, std::move(M)))
150 return true;
153 outs() << "*** All input ok\n";
155 // All input files read successfully!
156 return false;
159 /// run - The top level method that is invoked after all of the instance
160 /// variables are set up from command line arguments.
162 Error BugDriver::run() {
163 if (run_find_bugs) {
164 // Rearrange the passes and apply them to the program. Repeat this process
165 // until the user kills the program or we find a bug.
166 return runManyPasses(PassesToRun);
169 // If we're not running as a child, the first thing that we must do is
170 // determine what the problem is. Does the optimization series crash the
171 // compiler, or does it produce illegal code? We make the top-level
172 // decision by trying to run all of the passes on the input program,
173 // which should generate a bitcode file. If it does generate a bitcode
174 // file, then we know the compiler didn't crash, so try to diagnose a
175 // miscompilation.
176 if (!PassesToRun.empty()) {
177 outs() << "Running selected passes on program to test for crash: ";
178 if (runPasses(*Program, PassesToRun))
179 return debugOptimizerCrash();
182 // Set up the execution environment, selecting a method to run LLVM bitcode.
183 if (Error E = initializeExecutionEnvironment())
184 return E;
186 // Test to see if we have a code generator crash.
187 outs() << "Running the code generator to test for a crash: ";
188 if (Error E = compileProgram(*Program)) {
189 outs() << toString(std::move(E));
190 return debugCodeGeneratorCrash();
192 outs() << '\n';
194 // Run the raw input to see where we are coming from. If a reference output
195 // was specified, make sure that the raw output matches it. If not, it's a
196 // problem in the front-end or the code generator.
198 bool CreatedOutput = false;
199 if (ReferenceOutputFile.empty()) {
200 outs() << "Generating reference output from raw program: ";
201 if (Error E = createReferenceFile(*Program)) {
202 errs() << toString(std::move(E));
203 return debugCodeGeneratorCrash();
205 CreatedOutput = true;
208 // Make sure the reference output file gets deleted on exit from this
209 // function, if appropriate.
210 std::string ROF(ReferenceOutputFile);
211 FileRemover RemoverInstance(ROF, CreatedOutput && !SaveTemps);
213 // Diff the output of the raw program against the reference output. If it
214 // matches, then we assume there is a miscompilation bug and try to
215 // diagnose it.
216 outs() << "*** Checking the code generator...\n";
217 Expected<bool> Diff = diffProgram(*Program, "", "", false);
218 if (Error E = Diff.takeError()) {
219 errs() << toString(std::move(E));
220 return debugCodeGeneratorCrash();
222 if (!*Diff) {
223 outs() << "\n*** Output matches: Debugging miscompilation!\n";
224 if (Error E = debugMiscompilation()) {
225 errs() << toString(std::move(E));
226 return debugCodeGeneratorCrash();
228 return Error::success();
231 outs() << "\n*** Input program does not match reference diff!\n";
232 outs() << "Debugging code generator problem!\n";
233 if (Error E = debugCodeGenerator()) {
234 errs() << toString(std::move(E));
235 return debugCodeGeneratorCrash();
237 return Error::success();
240 void llvm::PrintFunctionList(const std::vector<Function *> &Funcs) {
241 unsigned NumPrint = Funcs.size();
242 if (NumPrint > 10)
243 NumPrint = 10;
244 for (unsigned i = 0; i != NumPrint; ++i)
245 outs() << " " << Funcs[i]->getName();
246 if (NumPrint < Funcs.size())
247 outs() << "... <" << Funcs.size() << " total>";
248 outs().flush();
251 void llvm::PrintGlobalVariableList(const std::vector<GlobalVariable *> &GVs) {
252 unsigned NumPrint = GVs.size();
253 if (NumPrint > 10)
254 NumPrint = 10;
255 for (unsigned i = 0; i != NumPrint; ++i)
256 outs() << " " << GVs[i]->getName();
257 if (NumPrint < GVs.size())
258 outs() << "... <" << GVs.size() << " total>";
259 outs().flush();