97ff5f701f0e01e1e30f85082061feb0ea4aeb82
[oota-llvm.git] / tools / bugpoint / bugpoint.cpp
1 //===- bugpoint.cpp - The LLVM Bugpoint utility ---------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This program is an automated compiler debugger tool.  It is used to narrow
11 // down miscompilations and crash problems to a specific pass in the compiler,
12 // and the specific Module or Function input that is causing the problem.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "BugDriver.h"
17 #include "ToolRunner.h"
18 #include "llvm/LinkAllPasses.h"
19 #include "llvm/Support/PassNameParser.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/PluginLoader.h"
22 #include "llvm/System/Process.h"
23 #include "llvm/System/Signals.h"
24 #include "llvm/LinkAllVMCore.h"
25 using namespace llvm;
26
27 // AsChild - Specifies that this invocation of bugpoint is being generated
28 // from a parent process. It is not intended to be used by users so the 
29 // option is hidden.
30 static cl::opt<bool> 
31 AsChild("as-child", cl::desc("Run bugpoint as child process"), 
32         cl::ReallyHidden);
33           
34 static cl::opt<bool> 
35 FindBugs("find-bugs", cl::desc("Run many different optimization sequences"
36                                "on program to find bugs"), cl::init(false));
37
38 static cl::list<std::string>
39 InputFilenames(cl::Positional, cl::OneOrMore,
40                cl::desc("<input llvm ll/bc files>"));
41
42 static cl::opt<unsigned>
43 TimeoutValue("timeout", cl::init(300), cl::value_desc("seconds"),
44              cl::desc("Number of seconds program is allowed to run before it "
45                       "is killed (default is 300s), 0 disables timeout"));
46
47 // The AnalysesList is automatically populated with registered Passes by the
48 // PassNameParser.
49 //
50 static cl::list<const PassInfo*, bool, PassNameParser>
51 PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
52
53 /// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
54 bool llvm::BugpointIsInterrupted = false;
55
56 static void BugpointInterruptFunction() {
57   BugpointIsInterrupted = true;
58 }
59
60 int main(int argc, char **argv) {
61   cl::ParseCommandLineOptions(argc, argv,
62                               " LLVM automatic testcase reducer. See\nhttp://"
63                               "llvm.org/docs/CommandGuide/bugpoint.html"
64                               " for more information.\n");
65   sys::PrintStackTraceOnErrorSignal();
66   sys::SetInterruptFunction(BugpointInterruptFunction);
67   
68   BugDriver D(argv[0],AsChild,FindBugs,TimeoutValue);
69   if (D.addSources(InputFilenames)) return 1;
70   D.addPasses(PassList.begin(), PassList.end());
71
72   // Bugpoint has the ability of generating a plethora of core files, so to
73   // avoid filling up the disk, we prevent it
74   sys::Process::PreventCoreFiles();
75
76   try {
77     return D.run();
78   } catch (ToolExecutionError &TEE) {
79     std::cerr << "Tool execution error: " << TEE.what() << '\n';
80   } catch (const std::string& msg) {
81     std::cerr << argv[0] << ": " << msg << "\n";
82   } catch (...) {
83     std::cerr << "Whoops, an exception leaked out of bugpoint.  "
84               << "This is a bug in bugpoint!\n";
85   }
86   return 1;
87 }