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/LinkAllIR.h"
22 #include "llvm/LinkAllPasses.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/InitLLVM.h"
25 #include "llvm/Support/ManagedStatic.h"
26 #include "llvm/Support/PluginLoader.h"
27 #include "llvm/Support/PrettyStackTrace.h"
28 #include "llvm/Support/Process.h"
29 #include "llvm/Support/TargetSelect.h"
30 #include "llvm/Support/Valgrind.h"
31 #include "llvm/Transforms/IPO/AlwaysInliner.h"
32 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
34 // Enable this macro to debug bugpoint itself.
35 //#define DEBUG_BUGPOINT 1
40 FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
41 "on program to find bugs"),
44 static cl::list
<std::string
>
45 InputFilenames(cl::Positional
, cl::OneOrMore
,
46 cl::desc("<input llvm ll/bc files>"));
48 static cl::opt
<unsigned> TimeoutValue(
49 "timeout", cl::init(300), cl::value_desc("seconds"),
50 cl::desc("Number of seconds program is allowed to run before it "
51 "is killed (default is 300s), 0 disables timeout"));
53 static cl::opt
<int> MemoryLimit(
54 "mlimit", cl::init(-1), cl::value_desc("MBytes"),
55 cl::desc("Maximum amount of memory to use. 0 disables check. Defaults to "
56 "400MB (800MB under valgrind, 0 with sanitizers)."));
59 UseValgrind("enable-valgrind",
60 cl::desc("Run optimizations through valgrind"));
62 // The AnalysesList is automatically populated with registered Passes by the
65 static cl::list
<const PassInfo
*, bool, PassNameParser
>
66 PassList(cl::desc("Passes available:"), cl::ZeroOrMore
);
69 StandardLinkOpts("std-link-opts",
70 cl::desc("Include the standard link time optimizations"));
73 OptLevelO1("O1", cl::desc("Optimization level 1. Identical to 'opt -O1'"));
76 OptLevelO2("O2", cl::desc("Optimization level 2. Identical to 'opt -O2'"));
78 static cl::opt
<bool> OptLevelOs(
81 "Like -O2 with extra optimizations for size. Similar to clang -Os"));
84 OptLevelO3("O3", cl::desc("Optimization level 3. Identical to 'opt -O3'"));
86 static cl::opt
<std::string
>
87 OverrideTriple("mtriple", cl::desc("Override target triple for module"));
89 /// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
90 bool llvm::BugpointIsInterrupted
= false;
92 #ifndef DEBUG_BUGPOINT
93 static void BugpointInterruptFunction() { BugpointIsInterrupted
= true; }
96 // Hack to capture a pass list.
98 class AddToDriver
: public legacy::FunctionPassManager
{
102 AddToDriver(BugDriver
&_D
) : FunctionPassManager(nullptr), D(_D
) {}
104 void add(Pass
*P
) override
{
105 const void *ID
= P
->getPassID();
106 const PassInfo
*PI
= PassRegistry::getPassRegistry()->getPassInfo(ID
);
107 D
.addPass(PI
->getPassArgument());
112 #ifdef LINK_POLLY_INTO_TOOLS
114 void initializePollyPasses(llvm::PassRegistry
&Registry
);
118 int main(int argc
, char **argv
) {
119 #ifndef DEBUG_BUGPOINT
120 InitLLVM
X(argc
, argv
);
124 PassRegistry
&Registry
= *PassRegistry::getPassRegistry();
125 initializeCore(Registry
);
126 initializeScalarOpts(Registry
);
127 initializeObjCARCOpts(Registry
);
128 initializeVectorization(Registry
);
129 initializeIPO(Registry
);
130 initializeAnalysis(Registry
);
131 initializeTransformUtils(Registry
);
132 initializeInstCombine(Registry
);
133 initializeAggressiveInstCombine(Registry
);
134 initializeInstrumentation(Registry
);
135 initializeTarget(Registry
);
137 #ifdef LINK_POLLY_INTO_TOOLS
138 polly::initializePollyPasses(Registry
);
141 if (std::getenv("bar") == (char*) -1) {
142 InitializeAllTargets();
143 InitializeAllTargetMCs();
144 InitializeAllAsmPrinters();
145 InitializeAllAsmParsers();
148 cl::ParseCommandLineOptions(argc
, argv
,
149 "LLVM automatic testcase reducer. See\nhttp://"
150 "llvm.org/cmds/bugpoint.html"
151 " for more information.\n");
152 #ifndef DEBUG_BUGPOINT
153 sys::SetInterruptFunction(BugpointInterruptFunction
);
157 // If we have an override, set it and then track the triple we want Modules
159 if (!OverrideTriple
.empty()) {
160 TargetTriple
.setTriple(Triple::normalize(OverrideTriple
));
161 outs() << "Override triple set to '" << TargetTriple
.getTriple() << "'\n";
164 if (MemoryLimit
< 0) {
165 // Set the default MemoryLimit. Be sure to update the flag's description if
167 if (sys::RunningOnValgrind() || UseValgrind
)
171 #if (LLVM_ADDRESS_SANITIZER_BUILD || LLVM_MEMORY_SANITIZER_BUILD || \
172 LLVM_THREAD_SANITIZER_BUILD)
173 // Starting from kernel 4.9 memory allocated with mmap is counted against
174 // RLIMIT_DATA. Sanitizers need to allocate tens of terabytes for shadow.
179 BugDriver
D(argv
[0], FindBugs
, TimeoutValue
, MemoryLimit
, UseValgrind
,
181 if (D
.addSources(InputFilenames
))
186 if (StandardLinkOpts
) {
187 PassManagerBuilder Builder
;
188 Builder
.Inliner
= createFunctionInliningPass();
189 Builder
.populateLTOPassManager(PM
);
192 if (OptLevelO1
|| OptLevelO2
|| OptLevelO3
) {
193 PassManagerBuilder Builder
;
195 Builder
.Inliner
= createAlwaysInlinerLegacyPass();
196 else if (OptLevelOs
|| OptLevelO2
)
197 Builder
.Inliner
= createFunctionInliningPass(
198 2, OptLevelOs
? 1 : 0, false);
200 Builder
.Inliner
= createFunctionInliningPass(275);
201 Builder
.populateFunctionPassManager(PM
);
202 Builder
.populateModulePassManager(PM
);
205 for (const PassInfo
*PI
: PassList
)
206 D
.addPass(PI
->getPassArgument());
208 // Bugpoint has the ability of generating a plethora of core files, so to
209 // avoid filling up the disk, we prevent it
210 #ifndef DEBUG_BUGPOINT
211 sys::Process::PreventCoreFiles();
214 if (Error E
= D
.run()) {
215 errs() << toString(std::move(E
));