bugpoint.cpp revision 205407
1193323Sed//===- bugpoint.cpp - The LLVM Bugpoint utility ---------------------------===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This program is an automated compiler debugger tool.  It is used to narrow
11193323Sed// down miscompilations and crash problems to a specific pass in the compiler,
12193323Sed// and the specific Module or Function input that is causing the problem.
13193323Sed//
14193323Sed//===----------------------------------------------------------------------===//
15193323Sed
16193323Sed#include "BugDriver.h"
17193323Sed#include "ToolRunner.h"
18193323Sed#include "llvm/LinkAllPasses.h"
19195340Sed#include "llvm/LLVMContext.h"
20193323Sed#include "llvm/Support/PassNameParser.h"
21193323Sed#include "llvm/Support/CommandLine.h"
22193323Sed#include "llvm/Support/ManagedStatic.h"
23193323Sed#include "llvm/Support/PluginLoader.h"
24193323Sed#include "llvm/Support/PrettyStackTrace.h"
25198090Srdivacky#include "llvm/Support/StandardPasses.h"
26193323Sed#include "llvm/System/Process.h"
27193323Sed#include "llvm/System/Signals.h"
28205407Srdivacky#include "llvm/System/Valgrind.h"
29193323Sed#include "llvm/LinkAllVMCore.h"
30193323Sedusing namespace llvm;
31193323Sed
32193323Sed// AsChild - Specifies that this invocation of bugpoint is being generated
33193323Sed// from a parent process. It is not intended to be used by users so the
34193323Sed// option is hidden.
35193323Sedstatic cl::opt<bool>
36193323SedAsChild("as-child", cl::desc("Run bugpoint as child process"),
37193323Sed        cl::ReallyHidden);
38193323Sed
39193323Sedstatic cl::opt<bool>
40193323SedFindBugs("find-bugs", cl::desc("Run many different optimization sequences "
41193323Sed                               "on program to find bugs"), cl::init(false));
42193323Sed
43193323Sedstatic cl::list<std::string>
44193323SedInputFilenames(cl::Positional, cl::OneOrMore,
45193323Sed               cl::desc("<input llvm ll/bc files>"));
46193323Sed
47193323Sedstatic cl::opt<unsigned>
48193323SedTimeoutValue("timeout", cl::init(300), cl::value_desc("seconds"),
49193323Sed             cl::desc("Number of seconds program is allowed to run before it "
50193323Sed                      "is killed (default is 300s), 0 disables timeout"));
51193323Sed
52205407Srdivackystatic cl::opt<int>
53205407SrdivackyMemoryLimit("mlimit", cl::init(-1), cl::value_desc("MBytes"),
54205407Srdivacky             cl::desc("Maximum amount of memory to use. 0 disables check."
55205407Srdivacky                      " Defaults to 100MB (800MB under valgrind)."));
56193323Sed
57205407Srdivackystatic cl::opt<bool>
58205407SrdivackyUseValgrind("enable-valgrind",
59205407Srdivacky            cl::desc("Run optimizations through valgrind"));
60205407Srdivacky
61193323Sed// The AnalysesList is automatically populated with registered Passes by the
62193323Sed// PassNameParser.
63193323Sed//
64193323Sedstatic cl::list<const PassInfo*, bool, PassNameParser>
65193323SedPassList(cl::desc("Passes available:"), cl::ZeroOrMore);
66193323Sed
67198090Srdivackystatic cl::opt<bool>
68198090SrdivackyStandardCompileOpts("std-compile-opts",
69198090Srdivacky                   cl::desc("Include the standard compile time optimizations"));
70198090Srdivacky
71198090Srdivackystatic cl::opt<bool>
72198090SrdivackyStandardLinkOpts("std-link-opts",
73198090Srdivacky                 cl::desc("Include the standard link time optimizations"));
74198090Srdivacky
75198090Srdivackystatic cl::opt<std::string>
76198090SrdivackyOverrideTriple("mtriple", cl::desc("Override target triple for module"));
77198090Srdivacky
78193323Sed/// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
79193323Sedbool llvm::BugpointIsInterrupted = false;
80193323Sed
81193323Sedstatic void BugpointInterruptFunction() {
82193323Sed  BugpointIsInterrupted = true;
83193323Sed}
84193323Sed
85198090Srdivacky// Hack to capture a pass list.
86198090Srdivackynamespace {
87198090Srdivacky  class AddToDriver : public PassManager {
88198090Srdivacky    BugDriver &D;
89198090Srdivacky  public:
90198090Srdivacky    AddToDriver(BugDriver &_D) : D(_D) {}
91198090Srdivacky
92198090Srdivacky    virtual void add(Pass *P) {
93198090Srdivacky      const PassInfo *PI = P->getPassInfo();
94198090Srdivacky      D.addPasses(&PI, &PI + 1);
95198090Srdivacky    }
96198090Srdivacky  };
97198090Srdivacky}
98198090Srdivacky
99193323Sedint main(int argc, char **argv) {
100193323Sed  llvm::sys::PrintStackTraceOnErrorSignal();
101193323Sed  llvm::PrettyStackTraceProgram X(argc, argv);
102193323Sed  llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
103193323Sed  cl::ParseCommandLineOptions(argc, argv,
104193323Sed                              "LLVM automatic testcase reducer. See\nhttp://"
105193323Sed                              "llvm.org/cmds/bugpoint.html"
106193323Sed                              " for more information.\n");
107193323Sed  sys::SetInterruptFunction(BugpointInterruptFunction);
108195340Sed
109198090Srdivacky  LLVMContext& Context = getGlobalContext();
110198090Srdivacky  // If we have an override, set it and then track the triple we want Modules
111198090Srdivacky  // to use.
112198090Srdivacky  if (!OverrideTriple.empty()) {
113198090Srdivacky    TargetTriple.setTriple(OverrideTriple);
114198090Srdivacky    outs() << "Override triple set to '" << OverrideTriple << "'\n";
115198090Srdivacky  }
116198090Srdivacky
117205407Srdivacky  if (MemoryLimit < 0) {
118205407Srdivacky    // Set the default MemoryLimit.  Be sure to update the flag's description if
119205407Srdivacky    // you change this.
120205407Srdivacky    if (sys::RunningOnValgrind() || UseValgrind)
121205407Srdivacky      MemoryLimit = 800;
122205407Srdivacky    else
123205407Srdivacky      MemoryLimit = 100;
124205407Srdivacky  }
125205407Srdivacky
126205407Srdivacky  BugDriver D(argv[0], AsChild, FindBugs, TimeoutValue, MemoryLimit,
127205407Srdivacky              UseValgrind, Context);
128193323Sed  if (D.addSources(InputFilenames)) return 1;
129198090Srdivacky
130198090Srdivacky  AddToDriver PM(D);
131198090Srdivacky  if (StandardCompileOpts) {
132198090Srdivacky    createStandardModulePasses(&PM, 3,
133198090Srdivacky                               /*OptimizeSize=*/ false,
134198090Srdivacky                               /*UnitAtATime=*/ true,
135198090Srdivacky                               /*UnrollLoops=*/ true,
136198090Srdivacky                               /*SimplifyLibCalls=*/ true,
137198090Srdivacky                               /*HaveExceptions=*/ true,
138198090Srdivacky                               createFunctionInliningPass());
139198090Srdivacky  }
140198090Srdivacky
141198090Srdivacky  if (StandardLinkOpts)
142198090Srdivacky    createStandardLTOPasses(&PM, /*Internalize=*/true,
143198090Srdivacky                            /*RunInliner=*/true,
144198090Srdivacky                            /*VerifyEach=*/false);
145198090Srdivacky
146193323Sed  D.addPasses(PassList.begin(), PassList.end());
147193323Sed
148193323Sed  // Bugpoint has the ability of generating a plethora of core files, so to
149193323Sed  // avoid filling up the disk, we prevent it
150193323Sed  sys::Process::PreventCoreFiles();
151193323Sed
152193323Sed  try {
153193323Sed    return D.run();
154193323Sed  } catch (ToolExecutionError &TEE) {
155198090Srdivacky    errs() << "Tool execution error: " << TEE.what() << '\n';
156193323Sed  } catch (const std::string& msg) {
157198090Srdivacky    errs() << argv[0] << ": " << msg << "\n";
158198090Srdivacky  } catch (const std::bad_alloc&) {
159198090Srdivacky    errs() << "Oh no, a bugpoint process ran out of memory!\n"
160198090Srdivacky              "To increase the allocation limits for bugpoint child\n"
161198090Srdivacky              "processes, use the -mlimit option.\n";
162193323Sed  } catch (const std::exception &e) {
163198090Srdivacky    errs() << "Whoops, a std::exception leaked out of bugpoint: "
164198090Srdivacky           << e.what() << "\n"
165198090Srdivacky           << "This is a bug in bugpoint!\n";
166193323Sed  } catch (...) {
167198090Srdivacky    errs() << "Whoops, an exception leaked out of bugpoint.  "
168198090Srdivacky           << "This is a bug in bugpoint!\n";
169193323Sed  }
170193323Sed  return 1;
171193323Sed}
172