05dff34b84ecb2d9664946fdecdae4acafe53a78
[oota-llvm.git] / tools / bugpoint / BugDriver.cpp
1 //===- 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 /// getPassesString - Turn a list of passes into a string which indicates the
18 /// command line options that must be passed to add the passes.
19 ///
20 std::string getPassesString(const std::vector<const PassInfo*> &Passes) {
21   std::string Result;
22   for (unsigned i = 0, e = Passes.size(); i != e; ++i) {
23     if (i) Result += " ";
24     Result += "-";
25     Result += Passes[i]->getPassArgument();
26   }
27   return Result;
28 }
29
30
31 /// ParseInputFile - Given a bytecode or assembly input filename, parse and
32 /// return it, or return null if not possible.
33 ///
34 Module *BugDriver::ParseInputFile(const std::string &InputFilename) const {
35   Module *Result = 0;
36   try {
37     Result = ParseBytecodeFile(InputFilename);
38     if (!Result && !(Result = ParseAssemblyFile(InputFilename))){
39       std::cerr << ToolName << ": could not read input file '"
40                 << InputFilename << "'!\n";
41     }
42   } catch (const ParseException &E) {
43     std::cerr << ToolName << ": " << E.getMessage() << "\n";
44     Result = 0;
45   }
46   return Result;
47 }
48
49 // This method takes the specified list of LLVM input files, attempts to load
50 // them, either as assembly or bytecode, then link them together.
51 //
52 bool BugDriver::addSources(const std::vector<std::string> &Filenames) {
53   assert(Program == 0 && "Cannot call addSources multiple times!");
54   assert(!Filenames.empty() && "Must specify at least on input filename!");
55
56   // Load the first input file...
57   Program = ParseInputFile(Filenames[0]);
58   if (Program == 0) return true;
59   std::cout << "Read input file      : '" << Filenames[0] << "'\n";
60
61   for (unsigned i = 1, e = Filenames.size(); i != e; ++i) {
62     std::auto_ptr<Module> M(ParseInputFile(Filenames[i]));
63     if (M.get() == 0) return true;
64
65     std::cout << "Linking in input file: '" << Filenames[i] << "'\n";
66     std::string ErrorMessage;
67     if (LinkModules(Program, M.get(), &ErrorMessage)) {
68       std::cerr << ToolName << ": error linking in '" << Filenames[i] << "': "
69                 << ErrorMessage << "\n";
70       return true;
71     }
72   }
73
74   std::cout << "*** All input ok\n";
75
76   // All input files read successfully!
77   return false;
78 }
79
80
81
82 /// run - The top level method that is invoked after all of the instance
83 /// variables are set up from command line arguments.
84 ///
85 bool BugDriver::run() {
86   // The first thing that we must do is determine what the problem is.  Does the
87   // optimization series crash the compiler, or does it produce illegal code? We
88   // make the top-level decision by trying to run all of the passes on the the
89   // input program, which should generate a bytecode file.  If it does generate
90   // a bytecode file, then we know the compiler didn't crash, so try to diagnose
91   // a miscompilation.
92   //
93   std::cout << "Running selected passes on program to test for crash: ";
94   if (runPasses(PassesToRun))
95     return debugCrash();
96   else
97     return debugMiscompilation();
98 }