9aedd7e9e6bbc9e27e8337ea522ebe406c5eed8e
[oota-llvm.git] / tools / bugpoint / bugpoint.cpp
1 //===- bugpoint.cpp - The LLVM Bugpoint utility ---------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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/LLVMContext.h"
20 #include "llvm/Support/PassNameParser.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/ManagedStatic.h"
23 #include "llvm/Support/PluginLoader.h"
24 #include "llvm/Support/PrettyStackTrace.h"
25 #include "llvm/System/Process.h"
26 #include "llvm/System/Signals.h"
27 #include "llvm/LinkAllVMCore.h"
28 using namespace llvm;
29
30 // AsChild - Specifies that this invocation of bugpoint is being generated
31 // from a parent process. It is not intended to be used by users so the 
32 // option is hidden.
33 static cl::opt<bool> 
34 AsChild("as-child", cl::desc("Run bugpoint as child process"), 
35         cl::ReallyHidden);
36           
37 static cl::opt<bool> 
38 FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
39                                "on program to find bugs"), cl::init(false));
40
41 static cl::list<std::string>
42 InputFilenames(cl::Positional, cl::OneOrMore,
43                cl::desc("<input llvm ll/bc files>"));
44
45 static cl::opt<unsigned>
46 TimeoutValue("timeout", cl::init(300), cl::value_desc("seconds"),
47              cl::desc("Number of seconds program is allowed to run before it "
48                       "is killed (default is 300s), 0 disables timeout"));
49
50 static cl::opt<unsigned>
51 MemoryLimit("mlimit", cl::init(100), cl::value_desc("MBytes"),
52              cl::desc("Maximum amount of memory to use. 0 disables check."));
53
54 // The AnalysesList is automatically populated with registered Passes by the
55 // PassNameParser.
56 //
57 static cl::list<const PassInfo*, bool, PassNameParser>
58 PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
59
60 /// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
61 bool llvm::BugpointIsInterrupted = false;
62
63 static void BugpointInterruptFunction() {
64   BugpointIsInterrupted = true;
65 }
66
67 int main(int argc, char **argv) {
68   llvm::sys::PrintStackTraceOnErrorSignal();
69   llvm::PrettyStackTraceProgram X(argc, argv);
70   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
71   cl::ParseCommandLineOptions(argc, argv,
72                               "LLVM automatic testcase reducer. See\nhttp://"
73                               "llvm.org/cmds/bugpoint.html"
74                               " for more information.\n");
75   sys::SetInterruptFunction(BugpointInterruptFunction);
76
77   LLVMContext& Context = getGlobalContext();
78   BugDriver D(argv[0], AsChild, FindBugs, TimeoutValue, MemoryLimit, Context);
79   if (D.addSources(InputFilenames)) return 1;
80   D.addPasses(PassList.begin(), PassList.end());
81
82   // Bugpoint has the ability of generating a plethora of core files, so to
83   // avoid filling up the disk, we prevent it
84   sys::Process::PreventCoreFiles();
85
86   try {
87     return D.run();
88   } catch (ToolExecutionError &TEE) {
89     errs() << "Tool execution error: " << TEE.what() << '\n';
90   } catch (const std::string& msg) {
91     errs() << argv[0] << ": " << msg << "\n";
92   } catch (const std::bad_alloc &e) {
93     errs() << "Oh no, a bugpoint process ran out of memory!\n"
94               "To increase the allocation limits for bugpoint child\n"
95               "processes, use the -mlimit option.\n";
96   } catch (const std::exception &e) {
97     errs() << "Whoops, a std::exception leaked out of bugpoint: "
98            << e.what() << "\n"
99            << "This is a bug in bugpoint!\n";
100   } catch (...) {
101     errs() << "Whoops, an exception leaked out of bugpoint.  "
102            << "This is a bug in bugpoint!\n";
103   }
104   return 1;
105 }