1 //===- bugpoint.cpp - The LLVM Bugpoint utility ---------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This program is an automated compiler debugger tool. It is used to narrow
10 // down miscompilations and crash problems to a specific pass in the compiler,
11 // and the specific Module or Function input that is causing the problem.
13 //===----------------------------------------------------------------------===//
15 #include "BugDriver.h"
16 #include "ToolRunner.h"
17 #include "llvm/Config/llvm-config.h"
18 #include "llvm/IR/LLVMContext.h"
19 #include "llvm/IR/LegacyPassManager.h"
20 #include "llvm/IR/LegacyPassNameParser.h"
21 #include "llvm/InitializePasses.h"
22 #include "llvm/LinkAllIR.h"
23 #include "llvm/LinkAllPasses.h"
24 #include "llvm/Passes/PassPlugin.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/InitLLVM.h"
27 #include "llvm/Support/ManagedStatic.h"
28 #include "llvm/Support/PluginLoader.h"
29 #include "llvm/Support/PrettyStackTrace.h"
30 #include "llvm/Support/Process.h"
31 #include "llvm/Support/TargetSelect.h"
32 #include "llvm/Support/Valgrind.h"
33 #include "llvm/Transforms/IPO/AlwaysInliner.h"
34 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
36 // Enable this macro to debug bugpoint itself.
37 //#define DEBUG_BUGPOINT 1
42 FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
43 "on program to find bugs"),
46 static cl::list
<std::string
>
47 InputFilenames(cl::Positional
, cl::OneOrMore
,
48 cl::desc("<input llvm ll/bc files>"));
50 static cl::opt
<unsigned> TimeoutValue(
51 "timeout", cl::init(300), cl::value_desc("seconds"),
52 cl::desc("Number of seconds program is allowed to run before it "
53 "is killed (default is 300s), 0 disables timeout"));
55 static cl::opt
<int> MemoryLimit(
56 "mlimit", cl::init(-1), cl::value_desc("MBytes"),
57 cl::desc("Maximum amount of memory to use. 0 disables check. Defaults to "
58 "400MB (800MB under valgrind, 0 with sanitizers)."));
61 UseValgrind("enable-valgrind",
62 cl::desc("Run optimizations through valgrind"));
64 // The AnalysesList is automatically populated with registered Passes by the
67 static cl::list
<const PassInfo
*, bool, PassNameParser
>
68 PassList(cl::desc("Passes available:"), cl::ZeroOrMore
);
71 StandardLinkOpts("std-link-opts",
72 cl::desc("Include the standard link time optimizations"));
75 OptLevelO1("O1", cl::desc("Optimization level 1. Identical to 'opt -O1'"));
78 OptLevelO2("O2", cl::desc("Optimization level 2. Identical to 'opt -O2'"));
80 static cl::opt
<bool> OptLevelOs(
83 "Like -O2 with extra optimizations for size. Similar to clang -Os"));
87 cl::desc("Like -Os but reduces code size further. Similar to clang -Oz"));
90 OptLevelO3("O3", cl::desc("Optimization level 3. Identical to 'opt -O3'"));
92 static cl::opt
<std::string
>
93 OverrideTriple("mtriple", cl::desc("Override target triple for module"));
95 /// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
96 bool llvm::BugpointIsInterrupted
= false;
98 #ifndef DEBUG_BUGPOINT
99 static void BugpointInterruptFunction() { BugpointIsInterrupted
= true; }
102 // Hack to capture a pass list.
104 class AddToDriver
: public legacy::FunctionPassManager
{
108 AddToDriver(BugDriver
&_D
) : FunctionPassManager(nullptr), D(_D
) {}
110 void add(Pass
*P
) override
{
111 const void *ID
= P
->getPassID();
112 const PassInfo
*PI
= PassRegistry::getPassRegistry()->getPassInfo(ID
);
113 D
.addPass(std::string(PI
->getPassArgument()));
118 // This routine adds optimization passes based on selected optimization level,
121 // OptLevel - Optimization Level
122 static void AddOptimizationPasses(legacy::FunctionPassManager
&FPM
,
124 unsigned SizeLevel
) {
125 PassManagerBuilder Builder
;
126 Builder
.OptLevel
= OptLevel
;
127 Builder
.SizeLevel
= SizeLevel
;
130 Builder
.Inliner
= createFunctionInliningPass(OptLevel
, SizeLevel
, false);
132 Builder
.Inliner
= createAlwaysInlinerLegacyPass();
134 Builder
.populateFunctionPassManager(FPM
);
135 Builder
.populateModulePassManager(FPM
);
138 #define HANDLE_EXTENSION(Ext) \
139 llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
140 #include "llvm/Support/Extension.def"
142 int main(int argc
, char **argv
) {
143 #ifndef DEBUG_BUGPOINT
144 InitLLVM
X(argc
, argv
);
148 PassRegistry
&Registry
= *PassRegistry::getPassRegistry();
149 initializeCore(Registry
);
150 initializeScalarOpts(Registry
);
151 initializeObjCARCOpts(Registry
);
152 initializeVectorization(Registry
);
153 initializeIPO(Registry
);
154 initializeAnalysis(Registry
);
155 initializeTransformUtils(Registry
);
156 initializeInstCombine(Registry
);
157 initializeAggressiveInstCombine(Registry
);
158 initializeInstrumentation(Registry
);
159 initializeTarget(Registry
);
161 if (std::getenv("bar") == (char*) -1) {
162 InitializeAllTargets();
163 InitializeAllTargetMCs();
164 InitializeAllAsmPrinters();
165 InitializeAllAsmParsers();
168 cl::ParseCommandLineOptions(argc
, argv
,
169 "LLVM automatic testcase reducer. See\nhttp://"
170 "llvm.org/cmds/bugpoint.html"
171 " for more information.\n");
172 #ifndef DEBUG_BUGPOINT
173 sys::SetInterruptFunction(BugpointInterruptFunction
);
177 // If we have an override, set it and then track the triple we want Modules
179 if (!OverrideTriple
.empty()) {
180 TargetTriple
.setTriple(Triple::normalize(OverrideTriple
));
181 outs() << "Override triple set to '" << TargetTriple
.getTriple() << "'\n";
184 if (MemoryLimit
< 0) {
185 // Set the default MemoryLimit. Be sure to update the flag's description if
187 if (sys::RunningOnValgrind() || UseValgrind
)
191 #if (LLVM_ADDRESS_SANITIZER_BUILD || LLVM_MEMORY_SANITIZER_BUILD || \
192 LLVM_THREAD_SANITIZER_BUILD)
193 // Starting from kernel 4.9 memory allocated with mmap is counted against
194 // RLIMIT_DATA. Sanitizers need to allocate tens of terabytes for shadow.
199 BugDriver
D(argv
[0], FindBugs
, TimeoutValue
, MemoryLimit
, UseValgrind
,
201 if (D
.addSources(InputFilenames
))
206 if (StandardLinkOpts
) {
207 PassManagerBuilder Builder
;
208 Builder
.Inliner
= createFunctionInliningPass();
209 Builder
.populateLTOPassManager(PM
);
213 AddOptimizationPasses(PM
, 1, 0);
215 AddOptimizationPasses(PM
, 2, 0);
217 AddOptimizationPasses(PM
, 3, 0);
219 AddOptimizationPasses(PM
, 2, 1);
221 AddOptimizationPasses(PM
, 2, 2);
223 for (const PassInfo
*PI
: PassList
)
224 D
.addPass(std::string(PI
->getPassArgument()));
226 // Bugpoint has the ability of generating a plethora of core files, so to
227 // avoid filling up the disk, we prevent it
228 #ifndef DEBUG_BUGPOINT
229 sys::Process::PreventCoreFiles();
232 // Needed to pull in symbols from statically linked extensions, including static
233 // registration. It is unused otherwise because bugpoint has no support for
235 #define HANDLE_EXTENSION(Ext) \
236 (void)get##Ext##PluginInfo();
237 #include "llvm/Support/Extension.def"
239 if (Error E
= D
.run()) {
240 errs() << toString(std::move(E
));