bugpoint.cpp revision 8ba15cb7099d9eadcb345328228d77ffa5afa42d
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/System/Valgrind.h"
29#include "llvm/LinkAllVMCore.h"
30using namespace llvm;
31
32// AsChild - Specifies that this invocation of bugpoint is being generated
33// from a parent process. It is not intended to be used by users so the
34// option is hidden.
35static cl::opt<bool>
36AsChild("as-child", cl::desc("Run bugpoint as child process"),
37        cl::ReallyHidden);
38
39static cl::opt<bool>
40FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
41                               "on program to find bugs"), cl::init(false));
42
43static cl::list<std::string>
44InputFilenames(cl::Positional, cl::OneOrMore,
45               cl::desc("<input llvm ll/bc files>"));
46
47static cl::opt<unsigned>
48TimeoutValue("timeout", cl::init(300), cl::value_desc("seconds"),
49             cl::desc("Number of seconds program is allowed to run before it "
50                      "is killed (default is 300s), 0 disables timeout"));
51
52static cl::opt<int>
53MemoryLimit("mlimit", cl::init(-1), cl::value_desc("MBytes"),
54             cl::desc("Maximum amount of memory to use. 0 disables check."
55                      " Defaults to 100MB (800MB under valgrind)."));
56
57static cl::opt<bool>
58UseValgrind("enable-valgrind",
59            cl::desc("Run optimizations through valgrind"));
60
61// The AnalysesList is automatically populated with registered Passes by the
62// PassNameParser.
63//
64static cl::list<const StaticPassInfo*, bool, PassNameParser>
65PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
66
67static cl::opt<bool>
68StandardCompileOpts("std-compile-opts",
69                   cl::desc("Include the standard compile time optimizations"));
70
71static cl::opt<bool>
72StandardLinkOpts("std-link-opts",
73                 cl::desc("Include the standard link time optimizations"));
74
75static cl::opt<std::string>
76OverrideTriple("mtriple", cl::desc("Override target triple for module"));
77
78/// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
79bool llvm::BugpointIsInterrupted = false;
80
81static void BugpointInterruptFunction() {
82  BugpointIsInterrupted = true;
83}
84
85// Hack to capture a pass list.
86namespace {
87  class AddToDriver : public PassManager {
88    BugDriver &D;
89  public:
90    AddToDriver(BugDriver &_D) : D(_D) {}
91
92    virtual void add(Pass *P) {
93      const StaticPassInfo *PI = P->getPassInfo();
94      D.addPasses(&PI, &PI + 1);
95    }
96  };
97}
98
99int main(int argc, char **argv) {
100  llvm::sys::PrintStackTraceOnErrorSignal();
101  llvm::PrettyStackTraceProgram X(argc, argv);
102  llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
103  cl::ParseCommandLineOptions(argc, argv,
104                              "LLVM automatic testcase reducer. See\nhttp://"
105                              "llvm.org/cmds/bugpoint.html"
106                              " for more information.\n");
107  sys::SetInterruptFunction(BugpointInterruptFunction);
108
109  LLVMContext& Context = getGlobalContext();
110  // If we have an override, set it and then track the triple we want Modules
111  // to use.
112  if (!OverrideTriple.empty()) {
113    TargetTriple.setTriple(OverrideTriple);
114    outs() << "Override triple set to '" << OverrideTriple << "'\n";
115  }
116
117  if (MemoryLimit < 0) {
118    // Set the default MemoryLimit.  Be sure to update the flag's description if
119    // you change this.
120    if (sys::RunningOnValgrind() || UseValgrind)
121      MemoryLimit = 800;
122    else
123      MemoryLimit = 100;
124  }
125
126  BugDriver D(argv[0], AsChild, FindBugs, TimeoutValue, MemoryLimit,
127              UseValgrind, Context);
128  if (D.addSources(InputFilenames)) return 1;
129
130  AddToDriver PM(D);
131  if (StandardCompileOpts) {
132    createStandardModulePasses(&PM, 3,
133                               /*OptimizeSize=*/ false,
134                               /*UnitAtATime=*/ true,
135                               /*UnrollLoops=*/ true,
136                               /*SimplifyLibCalls=*/ true,
137                               /*HaveExceptions=*/ true,
138                               createFunctionInliningPass());
139  }
140
141  if (StandardLinkOpts)
142    createStandardLTOPasses(&PM, /*Internalize=*/true,
143                            /*RunInliner=*/true,
144                            /*VerifyEach=*/false);
145
146  D.addPasses(PassList.begin(), PassList.end());
147
148  // Bugpoint has the ability of generating a plethora of core files, so to
149  // avoid filling up the disk, we prevent it
150  sys::Process::PreventCoreFiles();
151
152  std::string Error;
153  bool Failure = D.run(Error);
154  if (!Error.empty()) {
155    errs() << Error;
156    return 1;
157  }
158  return Failure;
159}
160