bugpoint.cpp revision 249423
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"
19249423Sdim#include "llvm/LinkAllIR.h"
20193323Sed#include "llvm/LinkAllPasses.h"
21226584Sdim#include "llvm/PassManager.h"
22193323Sed#include "llvm/Support/CommandLine.h"
23193323Sed#include "llvm/Support/ManagedStatic.h"
24249423Sdim#include "llvm/Support/PassNameParser.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
37193323Sedstatic 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"),
52205407Srdivacky             cl::desc("Maximum amount of memory to use. 0 disables check."
53205407Srdivacky                      " Defaults to 100MB (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>
66198090SrdivackyStandardCompileOpts("std-compile-opts",
67198090Srdivacky                   cl::desc("Include the standard compile time optimizations"));
68198090Srdivacky
69198090Srdivackystatic cl::opt<bool>
70198090SrdivackyStandardLinkOpts("std-link-opts",
71198090Srdivacky                 cl::desc("Include the standard link time optimizations"));
72198090Srdivacky
73223013Sdimstatic cl::opt<bool>
74223013SdimOptLevelO1("O1",
75223013Sdim           cl::desc("Optimization level 1. Similar to llvm-gcc -O1"));
76223013Sdim
77223013Sdimstatic cl::opt<bool>
78223013SdimOptLevelO2("O2",
79223013Sdim           cl::desc("Optimization level 2. Similar to llvm-gcc -O2"));
80223013Sdim
81223013Sdimstatic cl::opt<bool>
82223013SdimOptLevelO3("O3",
83223013Sdim           cl::desc("Optimization level 3. Similar to llvm-gcc -O3"));
84223013Sdim
85198090Srdivackystatic cl::opt<std::string>
86198090SrdivackyOverrideTriple("mtriple", cl::desc("Override target triple for module"));
87198090Srdivacky
88193323Sed/// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
89193323Sedbool llvm::BugpointIsInterrupted = false;
90193323Sed
91218885Sdim#ifndef DEBUG_BUGPOINT
92193323Sedstatic void BugpointInterruptFunction() {
93193323Sed  BugpointIsInterrupted = true;
94193323Sed}
95218885Sdim#endif
96193323Sed
97198090Srdivacky// Hack to capture a pass list.
98198090Srdivackynamespace {
99223013Sdim  class AddToDriver : public FunctionPassManager {
100198090Srdivacky    BugDriver &D;
101198090Srdivacky  public:
102223013Sdim    AddToDriver(BugDriver &_D) : FunctionPassManager(0), D(_D) {}
103198090Srdivacky
104198090Srdivacky    virtual void add(Pass *P) {
105212793Sdim      const void *ID = P->getPassID();
106212793Sdim      const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
107212793Sdim      D.addPass(PI->getPassArgument());
108198090Srdivacky    }
109198090Srdivacky  };
110198090Srdivacky}
111198090Srdivacky
112193323Sedint main(int argc, char **argv) {
113218885Sdim#ifndef DEBUG_BUGPOINT
114193323Sed  llvm::sys::PrintStackTraceOnErrorSignal();
115193323Sed  llvm::PrettyStackTraceProgram X(argc, argv);
116193323Sed  llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
117218885Sdim#endif
118218885Sdim
119218885Sdim  // Initialize passes
120218885Sdim  PassRegistry &Registry = *PassRegistry::getPassRegistry();
121218885Sdim  initializeCore(Registry);
122218885Sdim  initializeScalarOpts(Registry);
123249423Sdim  initializeObjCARCOpts(Registry);
124234353Sdim  initializeVectorization(Registry);
125218885Sdim  initializeIPO(Registry);
126218885Sdim  initializeAnalysis(Registry);
127218885Sdim  initializeIPA(Registry);
128218885Sdim  initializeTransformUtils(Registry);
129218885Sdim  initializeInstCombine(Registry);
130218885Sdim  initializeInstrumentation(Registry);
131218885Sdim  initializeTarget(Registry);
132218885Sdim
133193323Sed  cl::ParseCommandLineOptions(argc, argv,
134193323Sed                              "LLVM automatic testcase reducer. See\nhttp://"
135193323Sed                              "llvm.org/cmds/bugpoint.html"
136193323Sed                              " for more information.\n");
137218885Sdim#ifndef DEBUG_BUGPOINT
138193323Sed  sys::SetInterruptFunction(BugpointInterruptFunction);
139218885Sdim#endif
140195340Sed
141198090Srdivacky  LLVMContext& Context = getGlobalContext();
142198090Srdivacky  // If we have an override, set it and then track the triple we want Modules
143198090Srdivacky  // to use.
144198090Srdivacky  if (!OverrideTriple.empty()) {
145212793Sdim    TargetTriple.setTriple(Triple::normalize(OverrideTriple));
146212793Sdim    outs() << "Override triple set to '" << TargetTriple.getTriple() << "'\n";
147198090Srdivacky  }
148198090Srdivacky
149205407Srdivacky  if (MemoryLimit < 0) {
150205407Srdivacky    // Set the default MemoryLimit.  Be sure to update the flag's description if
151205407Srdivacky    // you change this.
152205407Srdivacky    if (sys::RunningOnValgrind() || UseValgrind)
153205407Srdivacky      MemoryLimit = 800;
154205407Srdivacky    else
155205407Srdivacky      MemoryLimit = 100;
156205407Srdivacky  }
157205407Srdivacky
158212793Sdim  BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit,
159205407Srdivacky              UseValgrind, Context);
160193323Sed  if (D.addSources(InputFilenames)) return 1;
161198090Srdivacky
162198090Srdivacky  AddToDriver PM(D);
163198090Srdivacky  if (StandardCompileOpts) {
164223013Sdim    PassManagerBuilder Builder;
165223013Sdim    Builder.OptLevel = 3;
166223013Sdim    Builder.Inliner = createFunctionInliningPass();
167223013Sdim    Builder.populateModulePassManager(PM);
168198090Srdivacky  }
169198090Srdivacky
170223013Sdim  if (StandardLinkOpts) {
171223013Sdim    PassManagerBuilder Builder;
172223013Sdim    Builder.populateLTOPassManager(PM, /*Internalize=*/true,
173223013Sdim                                   /*RunInliner=*/true);
174223013Sdim  }
175198090Srdivacky
176223013Sdim  if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
177223013Sdim    PassManagerBuilder Builder;
178223013Sdim    if (OptLevelO1)
179223013Sdim      Builder.Inliner = createAlwaysInlinerPass();
180223013Sdim    else if (OptLevelO2)
181223013Sdim      Builder.Inliner = createFunctionInliningPass(225);
182223013Sdim    else
183223013Sdim      Builder.Inliner = createFunctionInliningPass(275);
184193323Sed
185223013Sdim    // Note that although clang/llvm-gcc use two separate passmanagers
186223013Sdim    // here, it shouldn't normally make a difference.
187223013Sdim    Builder.populateFunctionPassManager(PM);
188223013Sdim    Builder.populateModulePassManager(PM);
189223013Sdim  }
190223013Sdim
191212793Sdim  for (std::vector<const PassInfo*>::iterator I = PassList.begin(),
192212793Sdim         E = PassList.end();
193212793Sdim       I != E; ++I) {
194212793Sdim    const PassInfo* PI = *I;
195212793Sdim    D.addPass(PI->getPassArgument());
196212793Sdim  }
197212793Sdim
198193323Sed  // Bugpoint has the ability of generating a plethora of core files, so to
199193323Sed  // avoid filling up the disk, we prevent it
200218885Sdim#ifndef DEBUG_BUGPOINT
201193323Sed  sys::Process::PreventCoreFiles();
202218885Sdim#endif
203193323Sed
204207618Srdivacky  std::string Error;
205207618Srdivacky  bool Failure = D.run(Error);
206207618Srdivacky  if (!Error.empty()) {
207207618Srdivacky    errs() << Error;
208207618Srdivacky    return 1;
209193323Sed  }
210207618Srdivacky  return Failure;
211193323Sed}
212