blob: 0343f6381d604b6551f365ba89de7fee2e70785e [file] [log] [blame]
Chris Lattnerafade922002-11-20 22:28:10 +00001//===- BugDriver.cpp - Top-Level BugPoint class implementation ------------===//
2//
3// This class contains all of the shared state and information that is used by
4// the BugPoint tool to track down errors in optimizations. This class is the
5// main driver class that invokes all sub-functionality.
6//
7//===----------------------------------------------------------------------===//
8
9#include "BugDriver.h"
10#include "llvm/Module.h"
11#include "llvm/Bytecode/Reader.h"
12#include "llvm/Assembly/Parser.h"
13#include "llvm/Transforms/Utils/Linker.h"
14#include "llvm/Pass.h"
15#include <memory>
16
17/// ParseInputFile - Given a bytecode or assembly input filename, parse and
18/// return it, or return null if not possible.
19///
20Module *BugDriver::ParseInputFile(const std::string &InputFilename) const {
21 Module *Result = 0;
22 try {
23 Result = ParseBytecodeFile(InputFilename);
24 if (!Result && !(Result = ParseAssemblyFile(InputFilename))){
25 std::cerr << ToolName << ": could not read input file '"
26 << InputFilename << "'!\n";
27 }
28 } catch (const ParseException &E) {
29 std::cerr << ToolName << ": " << E.getMessage() << "\n";
30 Result = 0;
31 }
32 return Result;
33}
34
35// This method takes the specified list of LLVM input files, attempts to load
36// them, either as assembly or bytecode, then link them together.
37//
38bool BugDriver::addSources(const std::vector<std::string> &Filenames) {
39 assert(Program == 0 && "Cannot call addSources multiple times!");
40 assert(!Filenames.empty() && "Must specify at least on input filename!");
41
42 // Load the first input file...
43 Program = ParseInputFile(Filenames[0]);
44 if (Program == 0) return true;
45 std::cout << "Read input file : '" << Filenames[0] << "'\n";
46
47 for (unsigned i = 1, e = Filenames.size(); i != e; ++i) {
48 std::auto_ptr<Module> M(ParseInputFile(Filenames[i]));
49 if (M.get() == 0) return true;
50
51 std::cout << "Linking in input file: '" << Filenames[i] << "'\n";
52 std::string ErrorMessage;
53 if (LinkModules(Program, M.get(), &ErrorMessage)) {
54 std::cerr << ToolName << ": error linking in '" << Filenames[i] << "': "
55 << ErrorMessage << "\n";
56 return true;
57 }
58 }
59
60 std::cout << "*** All input ok\n";
61
62 // All input files read successfully!
63 return false;
64}
65
66
67
68/// run - The top level method that is invoked after all of the instance
69/// variables are set up from command line arguments.
70///
71bool BugDriver::run() {
72 // The first thing that we must do is determine what the problem is. Does the
73 // optimization series crash the compiler, or does it produce illegal code? We
74 // make the top-level decision by trying to run all of the passes on the the
75 // input program, which should generate a bytecode file. If it does generate
76 // a bytecode file, then we know the compiler didn't crash, so try to diagnose
77 // a miscompilation.
78 //
79 std::cout << "Running selected passes on program to test for crash: ";
80 if (runPasses(PassesToRun))
81 return debugCrash();
82 else
83 return debugMiscompilation();
84}
85
86
87/// debugMiscompilation - This method is used when the passes selected are not
88/// crashing, but the generated output is semantically different from the
89/// input.
90///
91bool BugDriver::debugMiscompilation() {
92 std::cout << "*** Debugging miscompilation!\n";
93 std::cerr << "Sorry, bugpoint cannot debug a miscompilation yet!\n";
94
95 // If no reference output was specified, run the program without optimizations
96 // to get a reference output.
97 //
98
99 return true;
100}