1 //===- BugDriver.cpp - Top-Level BugPoint class implementation ------------===//
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.
7 //===----------------------------------------------------------------------===//
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"
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.
20 std::string getPassesString(const std::vector<const PassInfo*> &Passes) {
22 for (unsigned i = 0, e = Passes.size(); i != e; ++i) {
25 Result += Passes[i]->getPassArgument();
30 // DeleteFunctionBody - "Remove" the function by deleting all of its basic
31 // blocks, making it external.
33 void DeleteFunctionBody(Function *F) {
34 // First, break circular use/def chain references...
35 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
36 I->dropAllReferences();
38 // Next, delete all of the basic blocks.
39 F->getBasicBlockList().clear();
40 F->setLinkage(GlobalValue::ExternalLinkage);
41 assert(F->isExternal() && "This didn't make the function external!");
44 /// ParseInputFile - Given a bytecode or assembly input filename, parse and
45 /// return it, or return null if not possible.
47 Module *BugDriver::ParseInputFile(const std::string &InputFilename) const {
50 Result = ParseBytecodeFile(InputFilename);
51 if (!Result && !(Result = ParseAssemblyFile(InputFilename))){
52 std::cerr << ToolName << ": could not read input file '"
53 << InputFilename << "'!\n";
55 } catch (const ParseException &E) {
56 std::cerr << ToolName << ": " << E.getMessage() << "\n";
62 // This method takes the specified list of LLVM input files, attempts to load
63 // them, either as assembly or bytecode, then link them together. It returns
64 // true on failure (if, for example, an input bytecode file could not be
65 // parsed), and false on success.
67 bool BugDriver::addSources(const std::vector<std::string> &Filenames) {
68 assert(Program == 0 && "Cannot call addSources multiple times!");
69 assert(!Filenames.empty() && "Must specify at least on input filename!");
71 // Load the first input file...
72 Program = ParseInputFile(Filenames[0]);
73 if (Program == 0) return true;
74 std::cout << "Read input file : '" << Filenames[0] << "'\n";
76 for (unsigned i = 1, e = Filenames.size(); i != e; ++i) {
77 std::auto_ptr<Module> M(ParseInputFile(Filenames[i]));
78 if (M.get() == 0) return true;
80 std::cout << "Linking in input file: '" << Filenames[i] << "'\n";
81 std::string ErrorMessage;
82 if (LinkModules(Program, M.get(), &ErrorMessage)) {
83 std::cerr << ToolName << ": error linking in '" << Filenames[i] << "': "
84 << ErrorMessage << "\n";
89 std::cout << "*** All input ok\n";
91 // All input files read successfully!
97 /// run - The top level method that is invoked after all of the instance
98 /// variables are set up from command line arguments.
100 bool BugDriver::run() {
101 // The first thing that we must do is determine what the problem is. Does the
102 // optimization series crash the compiler, or does it produce illegal code? We
103 // make the top-level decision by trying to run all of the passes on the the
104 // input program, which should generate a bytecode file. If it does generate
105 // a bytecode file, then we know the compiler didn't crash, so try to diagnose
108 std::cout << "Running selected passes on program to test for crash: ";
109 if (runPasses(PassesToRun))
112 return debugMiscompilation();