1 //===- bugpoint.cpp - The LLVM Bugpoint utility ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #include "BugDriver.h"
17 #include "ToolRunner.h"
18 #include "llvm/IR/LLVMContext.h"
19 #include "llvm/IR/LegacyPassManager.h"
20 #include "llvm/IR/LegacyPassNameParser.h"
21 #include "llvm/LinkAllIR.h"
22 #include "llvm/LinkAllPasses.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/ManagedStatic.h"
25 #include "llvm/Support/PluginLoader.h"
26 #include "llvm/Support/PrettyStackTrace.h"
27 #include "llvm/Support/Process.h"
28 #include "llvm/Support/Signals.h"
29 #include "llvm/Support/Valgrind.h"
30 #include "llvm/Transforms/IPO/AlwaysInliner.h"
31 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
33 // Enable this macro to debug bugpoint itself.
34 //#define DEBUG_BUGPOINT 1
39 FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
40 "on program to find bugs"),
43 static cl::list<std::string>
44 InputFilenames(cl::Positional, cl::OneOrMore,
45 cl::desc("<input llvm ll/bc files>"));
47 static cl::opt<unsigned> TimeoutValue(
48 "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"));
53 MemoryLimit("mlimit", cl::init(-1), cl::value_desc("MBytes"),
54 cl::desc("Maximum amount of memory to use. 0 disables check."
55 " Defaults to 400MB (800MB under valgrind)."));
58 UseValgrind("enable-valgrind",
59 cl::desc("Run optimizations through valgrind"));
61 // The AnalysesList is automatically populated with registered Passes by the
64 static cl::list<const PassInfo *, bool, PassNameParser>
65 PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
68 StandardLinkOpts("std-link-opts",
69 cl::desc("Include the standard link time optimizations"));
72 OptLevelO1("O1", cl::desc("Optimization level 1. Identical to 'opt -O1'"));
75 OptLevelO2("O2", cl::desc("Optimization level 2. Identical to 'opt -O2'"));
77 static cl::opt<bool> OptLevelOs(
80 "Like -O2 with extra optimizations for size. Similar to clang -Os"));
83 OptLevelO3("O3", cl::desc("Optimization level 3. Identical to 'opt -O3'"));
85 static cl::opt<std::string>
86 OverrideTriple("mtriple", cl::desc("Override target triple for module"));
88 /// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
89 bool llvm::BugpointIsInterrupted = false;
91 #ifndef DEBUG_BUGPOINT
92 static void BugpointInterruptFunction() { BugpointIsInterrupted = true; }
95 // Hack to capture a pass list.
97 class AddToDriver : public legacy::FunctionPassManager {
101 AddToDriver(BugDriver &_D) : FunctionPassManager(nullptr), D(_D) {}
103 void add(Pass *P) override {
104 const void *ID = P->getPassID();
105 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
106 D.addPass(PI->getPassArgument());
111 #ifdef LINK_POLLY_INTO_TOOLS
113 void initializePollyPasses(llvm::PassRegistry &Registry);
117 int main(int argc, char **argv) {
118 #ifndef DEBUG_BUGPOINT
119 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
120 llvm::PrettyStackTraceProgram X(argc, argv);
121 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
125 PassRegistry &Registry = *PassRegistry::getPassRegistry();
126 initializeCore(Registry);
127 initializeScalarOpts(Registry);
128 initializeObjCARCOpts(Registry);
129 initializeVectorization(Registry);
130 initializeIPO(Registry);
131 initializeAnalysis(Registry);
132 initializeTransformUtils(Registry);
133 initializeInstCombine(Registry);
134 initializeInstrumentation(Registry);
135 initializeTarget(Registry);
137 #ifdef LINK_POLLY_INTO_TOOLS
138 polly::initializePollyPasses(Registry);
141 cl::ParseCommandLineOptions(argc, argv,
142 "LLVM automatic testcase reducer. See\nhttp://"
143 "llvm.org/cmds/bugpoint.html"
144 " for more information.\n");
145 #ifndef DEBUG_BUGPOINT
146 sys::SetInterruptFunction(BugpointInterruptFunction);
150 // If we have an override, set it and then track the triple we want Modules
152 if (!OverrideTriple.empty()) {
153 TargetTriple.setTriple(Triple::normalize(OverrideTriple));
154 outs() << "Override triple set to '" << TargetTriple.getTriple() << "'\n";
157 if (MemoryLimit < 0) {
158 // Set the default MemoryLimit. Be sure to update the flag's description if
160 if (sys::RunningOnValgrind() || UseValgrind)
166 BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit, UseValgrind,
168 if (D.addSources(InputFilenames))
173 if (StandardLinkOpts) {
174 PassManagerBuilder Builder;
175 Builder.Inliner = createFunctionInliningPass();
176 Builder.populateLTOPassManager(PM);
179 if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
180 PassManagerBuilder Builder;
182 Builder.Inliner = createAlwaysInlinerLegacyPass();
183 else if (OptLevelOs || OptLevelO2)
184 Builder.Inliner = createFunctionInliningPass(2, OptLevelOs ? 1 : 0);
186 Builder.Inliner = createFunctionInliningPass(275);
187 Builder.populateFunctionPassManager(PM);
188 Builder.populateModulePassManager(PM);
191 for (const PassInfo *PI : PassList)
192 D.addPass(PI->getPassArgument());
194 // Bugpoint has the ability of generating a plethora of core files, so to
195 // avoid filling up the disk, we prevent it
196 #ifndef DEBUG_BUGPOINT
197 sys::Process::PreventCoreFiles();
200 if (Error E = D.run()) {
201 errs() << toString(std::move(E));