bugpoint.cpp revision 1488670076908261af145778c23cb9aca30904e5
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/Support/StandardPasses.h"
26#include "llvm/System/Process.h"
27#include "llvm/System/Signals.h"
28#include "llvm/LinkAllVMCore.h"
29using namespace llvm;
30
31// AsChild - Specifies that this invocation of bugpoint is being generated
32// from a parent process. It is not intended to be used by users so the
33// option is hidden.
34static cl::opt<bool>
35AsChild("as-child", cl::desc("Run bugpoint as child process"),
36        cl::ReallyHidden);
37
38static cl::opt<bool>
39FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
40                               "on program to find bugs"), cl::init(false));
41
42static cl::list<std::string>
43InputFilenames(cl::Positional, cl::OneOrMore,
44               cl::desc("<input llvm ll/bc files>"));
45
46static cl::opt<unsigned>
47TimeoutValue("timeout", cl::init(300), cl::value_desc("seconds"),
48             cl::desc("Number of seconds program is allowed to run before it "
49                      "is killed (default is 300s), 0 disables timeout"));
50
51static cl::opt<unsigned>
52MemoryLimit("mlimit", cl::init(100), cl::value_desc("MBytes"),
53             cl::desc("Maximum amount of memory to use. 0 disables check."));
54
55// The AnalysesList is automatically populated with registered Passes by the
56// PassNameParser.
57//
58static cl::list<const PassInfo*, bool, PassNameParser>
59PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
60
61static cl::opt<bool>
62StandardCompileOpts("std-compile-opts",
63                   cl::desc("Include the standard compile time optimizations"));
64
65static cl::opt<bool>
66StandardLinkOpts("std-link-opts",
67                 cl::desc("Include the standard link time optimizations"));
68
69/// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
70bool llvm::BugpointIsInterrupted = false;
71
72static void BugpointInterruptFunction() {
73  BugpointIsInterrupted = true;
74}
75
76// Hack to capture a pass list.
77namespace {
78  class AddToDriver : public PassManager {
79    BugDriver &D;
80  public:
81    AddToDriver(BugDriver &_D) : D(_D) {}
82
83    virtual void add(Pass *P) {
84      const PassInfo *PI = P->getPassInfo();
85      D.addPasses(&PI, &PI + 1);
86    }
87  };
88}
89
90int main(int argc, char **argv) {
91  llvm::sys::PrintStackTraceOnErrorSignal();
92  llvm::PrettyStackTraceProgram X(argc, argv);
93  llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
94  cl::ParseCommandLineOptions(argc, argv,
95                              "LLVM automatic testcase reducer. See\nhttp://"
96                              "llvm.org/cmds/bugpoint.html"
97                              " for more information.\n");
98  sys::SetInterruptFunction(BugpointInterruptFunction);
99
100  LLVMContext& Context = getGlobalContext();
101  BugDriver D(argv[0], AsChild, FindBugs, TimeoutValue, MemoryLimit, Context);
102  if (D.addSources(InputFilenames)) return 1;
103
104  AddToDriver PM(D);
105  if (StandardCompileOpts) {
106    createStandardModulePasses(&PM, 3,
107                               /*OptimizeSize=*/ false,
108                               /*UnitAtATime=*/ true,
109                               /*UnrollLoops=*/ true,
110                               /*SimplifyLibCalls=*/ true,
111                               /*HaveExceptions=*/ true,
112                               createFunctionInliningPass());
113  }
114
115  if (StandardLinkOpts)
116    createStandardLTOPasses(&PM, /*Internalize=*/false,
117                            /*RunInliner=*/true,
118                            /*VerifyEach=*/false);
119
120  D.addPasses(PassList.begin(), PassList.end());
121
122  // Bugpoint has the ability of generating a plethora of core files, so to
123  // avoid filling up the disk, we prevent it
124  sys::Process::PreventCoreFiles();
125
126  try {
127    return D.run();
128  } catch (ToolExecutionError &TEE) {
129    errs() << "Tool execution error: " << TEE.what() << '\n';
130  } catch (const std::string& msg) {
131    errs() << argv[0] << ": " << msg << "\n";
132  } catch (const std::bad_alloc &e) {
133    errs() << "Oh no, a bugpoint process ran out of memory!\n"
134              "To increase the allocation limits for bugpoint child\n"
135              "processes, use the -mlimit option.\n";
136  } catch (const std::exception &e) {
137    errs() << "Whoops, a std::exception leaked out of bugpoint: "
138           << e.what() << "\n"
139           << "This is a bug in bugpoint!\n";
140  } catch (...) {
141    errs() << "Whoops, an exception leaked out of bugpoint.  "
142           << "This is a bug in bugpoint!\n";
143  }
144  return 1;
145}
146