bugpoint.cpp revision 309124
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"
18249423Sdim#include "llvm/IR/LLVMContext.h"
19288943Sdim#include "llvm/IR/LegacyPassManager.h"
20276479Sdim#include "llvm/IR/LegacyPassNameParser.h"
21249423Sdim#include "llvm/LinkAllIR.h"
22193323Sed#include "llvm/LinkAllPasses.h"
23193323Sed#include "llvm/Support/CommandLine.h"
24193323Sed#include "llvm/Support/ManagedStatic.h"
25193323Sed#include "llvm/Support/PluginLoader.h"
26193323Sed#include "llvm/Support/PrettyStackTrace.h"
27218885Sdim#include "llvm/Support/Process.h"
28218885Sdim#include "llvm/Support/Signals.h"
29218885Sdim#include "llvm/Support/Valgrind.h"
30226584Sdim#include "llvm/Transforms/IPO/PassManagerBuilder.h"
31218885Sdim
32218885Sdim//Enable this macro to debug bugpoint itself.
33218885Sdim//#define DEBUG_BUGPOINT 1
34218885Sdim
35193323Sedusing namespace llvm;
36193323Sed
37276479Sdimstatic cl::opt<bool>
38193323SedFindBugs("find-bugs", cl::desc("Run many different optimization sequences "
39193323Sed                               "on program to find bugs"), cl::init(false));
40193323Sed
41193323Sedstatic cl::list<std::string>
42193323SedInputFilenames(cl::Positional, cl::OneOrMore,
43193323Sed               cl::desc("<input llvm ll/bc files>"));
44193323Sed
45193323Sedstatic cl::opt<unsigned>
46193323SedTimeoutValue("timeout", cl::init(300), cl::value_desc("seconds"),
47193323Sed             cl::desc("Number of seconds program is allowed to run before it "
48193323Sed                      "is killed (default is 300s), 0 disables timeout"));
49193323Sed
50205407Srdivackystatic cl::opt<int>
51205407SrdivackyMemoryLimit("mlimit", cl::init(-1), cl::value_desc("MBytes"),
52261991Sdim            cl::desc("Maximum amount of memory to use. 0 disables check."
53288943Sdim                     " Defaults to 400MB (800MB under valgrind)."));
54193323Sed
55205407Srdivackystatic cl::opt<bool>
56205407SrdivackyUseValgrind("enable-valgrind",
57205407Srdivacky            cl::desc("Run optimizations through valgrind"));
58205407Srdivacky
59193323Sed// The AnalysesList is automatically populated with registered Passes by the
60193323Sed// PassNameParser.
61193323Sed//
62193323Sedstatic cl::list<const PassInfo*, bool, PassNameParser>
63193323SedPassList(cl::desc("Passes available:"), cl::ZeroOrMore);
64193323Sed
65198090Srdivackystatic cl::opt<bool>
66276479SdimStandardLinkOpts("std-link-opts",
67198090Srdivacky                 cl::desc("Include the standard link time optimizations"));
68198090Srdivacky
69223013Sdimstatic cl::opt<bool>
70223013SdimOptLevelO1("O1",
71276479Sdim           cl::desc("Optimization level 1. Identical to 'opt -O1'"));
72223013Sdim
73223013Sdimstatic cl::opt<bool>
74223013SdimOptLevelO2("O2",
75276479Sdim           cl::desc("Optimization level 2. Identical to 'opt -O2'"));
76223013Sdim
77223013Sdimstatic cl::opt<bool>
78223013SdimOptLevelO3("O3",
79276479Sdim           cl::desc("Optimization level 3. Identical to 'opt -O3'"));
80223013Sdim
81198090Srdivackystatic cl::opt<std::string>
82198090SrdivackyOverrideTriple("mtriple", cl::desc("Override target triple for module"));
83198090Srdivacky
84193323Sed/// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
85193323Sedbool llvm::BugpointIsInterrupted = false;
86193323Sed
87218885Sdim#ifndef DEBUG_BUGPOINT
88193323Sedstatic void BugpointInterruptFunction() {
89193323Sed  BugpointIsInterrupted = true;
90193323Sed}
91218885Sdim#endif
92193323Sed
93198090Srdivacky// Hack to capture a pass list.
94198090Srdivackynamespace {
95288943Sdim  class AddToDriver : public legacy::FunctionPassManager {
96198090Srdivacky    BugDriver &D;
97198090Srdivacky  public:
98276479Sdim    AddToDriver(BugDriver &_D) : FunctionPassManager(nullptr), D(_D) {}
99276479Sdim
100276479Sdim    void add(Pass *P) override {
101212793Sdim      const void *ID = P->getPassID();
102212793Sdim      const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
103212793Sdim      D.addPass(PI->getPassArgument());
104198090Srdivacky    }
105198090Srdivacky  };
106198090Srdivacky}
107198090Srdivacky
108276479Sdim#ifdef LINK_POLLY_INTO_TOOLS
109276479Sdimnamespace polly {
110276479Sdimvoid initializePollyPasses(llvm::PassRegistry &Registry);
111276479Sdim}
112276479Sdim#endif
113276479Sdim
114193323Sedint main(int argc, char **argv) {
115218885Sdim#ifndef DEBUG_BUGPOINT
116309124Sdim  llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
117193323Sed  llvm::PrettyStackTraceProgram X(argc, argv);
118193323Sed  llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
119218885Sdim#endif
120276479Sdim
121218885Sdim  // Initialize passes
122218885Sdim  PassRegistry &Registry = *PassRegistry::getPassRegistry();
123218885Sdim  initializeCore(Registry);
124218885Sdim  initializeScalarOpts(Registry);
125249423Sdim  initializeObjCARCOpts(Registry);
126234353Sdim  initializeVectorization(Registry);
127218885Sdim  initializeIPO(Registry);
128218885Sdim  initializeAnalysis(Registry);
129218885Sdim  initializeTransformUtils(Registry);
130218885Sdim  initializeInstCombine(Registry);
131218885Sdim  initializeInstrumentation(Registry);
132218885Sdim  initializeTarget(Registry);
133276479Sdim
134276479Sdim#ifdef LINK_POLLY_INTO_TOOLS
135276479Sdim  polly::initializePollyPasses(Registry);
136276479Sdim#endif
137276479Sdim
138193323Sed  cl::ParseCommandLineOptions(argc, argv,
139193323Sed                              "LLVM automatic testcase reducer. See\nhttp://"
140193323Sed                              "llvm.org/cmds/bugpoint.html"
141193323Sed                              " for more information.\n");
142218885Sdim#ifndef DEBUG_BUGPOINT
143193323Sed  sys::SetInterruptFunction(BugpointInterruptFunction);
144218885Sdim#endif
145195340Sed
146309124Sdim  LLVMContext Context;
147198090Srdivacky  // If we have an override, set it and then track the triple we want Modules
148198090Srdivacky  // to use.
149198090Srdivacky  if (!OverrideTriple.empty()) {
150212793Sdim    TargetTriple.setTriple(Triple::normalize(OverrideTriple));
151212793Sdim    outs() << "Override triple set to '" << TargetTriple.getTriple() << "'\n";
152198090Srdivacky  }
153198090Srdivacky
154205407Srdivacky  if (MemoryLimit < 0) {
155205407Srdivacky    // Set the default MemoryLimit.  Be sure to update the flag's description if
156205407Srdivacky    // you change this.
157205407Srdivacky    if (sys::RunningOnValgrind() || UseValgrind)
158205407Srdivacky      MemoryLimit = 800;
159205407Srdivacky    else
160288943Sdim      MemoryLimit = 400;
161205407Srdivacky  }
162205407Srdivacky
163212793Sdim  BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit,
164205407Srdivacky              UseValgrind, Context);
165193323Sed  if (D.addSources(InputFilenames)) return 1;
166276479Sdim
167198090Srdivacky  AddToDriver PM(D);
168276479Sdim
169223013Sdim  if (StandardLinkOpts) {
170223013Sdim    PassManagerBuilder Builder;
171280031Sdim    Builder.Inliner = createFunctionInliningPass();
172280031Sdim    Builder.populateLTOPassManager(PM);
173223013Sdim  }
174198090Srdivacky
175223013Sdim  if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
176223013Sdim    PassManagerBuilder Builder;
177223013Sdim    if (OptLevelO1)
178223013Sdim      Builder.Inliner = createAlwaysInlinerPass();
179223013Sdim    else if (OptLevelO2)
180223013Sdim      Builder.Inliner = createFunctionInliningPass(225);
181223013Sdim    else
182223013Sdim      Builder.Inliner = createFunctionInliningPass(275);
183223013Sdim    Builder.populateFunctionPassManager(PM);
184223013Sdim    Builder.populateModulePassManager(PM);
185223013Sdim  }
186223013Sdim
187296417Sdim  for (const PassInfo *PI : PassList)
188212793Sdim    D.addPass(PI->getPassArgument());
189212793Sdim
190193323Sed  // Bugpoint has the ability of generating a plethora of core files, so to
191193323Sed  // avoid filling up the disk, we prevent it
192218885Sdim#ifndef DEBUG_BUGPOINT
193193323Sed  sys::Process::PreventCoreFiles();
194218885Sdim#endif
195193323Sed
196207618Srdivacky  std::string Error;
197207618Srdivacky  bool Failure = D.run(Error);
198207618Srdivacky  if (!Error.empty()) {
199207618Srdivacky    errs() << Error;
200207618Srdivacky    return 1;
201193323Sed  }
202207618Srdivacky  return Failure;
203193323Sed}
204