1 //===- bugpoint.cpp - The LLVM Bugpoint utility ---------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "BugDriver.h"
17 #include "ToolRunner.h"
18 #include "llvm/Config/llvm-config.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/IR/LegacyPassManager.h"
21 #include "llvm/IR/LegacyPassNameParser.h"
22 #include "llvm/LinkAllIR.h"
23 #include "llvm/LinkAllPasses.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/InitLLVM.h"
26 #include "llvm/Support/ManagedStatic.h"
27 #include "llvm/Support/PluginLoader.h"
28 #include "llvm/Support/PrettyStackTrace.h"
29 #include "llvm/Support/Process.h"
30 #include "llvm/Support/TargetSelect.h"
31 #include "llvm/Support/Valgrind.h"
32 #include "llvm/Transforms/IPO/AlwaysInliner.h"
33 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
34
35 // Enable this macro to debug bugpoint itself.
36 //#define DEBUG_BUGPOINT 1
37
38 using namespace llvm;
39
40 static cl::opt<bool>
41 FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
42 "on program to find bugs"),
43 cl::init(false));
44
45 static cl::list<std::string>
46 InputFilenames(cl::Positional, cl::OneOrMore,
47 cl::desc("<input llvm ll/bc files>"));
48
49 static cl::opt<unsigned> TimeoutValue(
50 "timeout", cl::init(300), cl::value_desc("seconds"),
51 cl::desc("Number of seconds program is allowed to run before it "
52 "is killed (default is 300s), 0 disables timeout"));
53
54 static cl::opt<int> MemoryLimit(
55 "mlimit", cl::init(-1), cl::value_desc("MBytes"),
56 cl::desc("Maximum amount of memory to use. 0 disables check. Defaults to "
57 "400MB (800MB under valgrind, 0 with sanitizers)."));
58
59 static cl::opt<bool>
60 UseValgrind("enable-valgrind",
61 cl::desc("Run optimizations through valgrind"));
62
63 // The AnalysesList is automatically populated with registered Passes by the
64 // PassNameParser.
65 //
66 static cl::list<const PassInfo *, bool, PassNameParser>
67 PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
68
69 static cl::opt<bool>
70 StandardLinkOpts("std-link-opts",
71 cl::desc("Include the standard link time optimizations"));
72
73 static cl::opt<bool>
74 OptLevelO1("O1", cl::desc("Optimization level 1. Identical to 'opt -O1'"));
75
76 static cl::opt<bool>
77 OptLevelO2("O2", cl::desc("Optimization level 2. Identical to 'opt -O2'"));
78
79 static cl::opt<bool> OptLevelOs(
80 "Os",
81 cl::desc(
82 "Like -O2 with extra optimizations for size. Similar to clang -Os"));
83
84 static cl::opt<bool>
85 OptLevelO3("O3", cl::desc("Optimization level 3. Identical to 'opt -O3'"));
86
87 static cl::opt<std::string>
88 OverrideTriple("mtriple", cl::desc("Override target triple for module"));
89
90 /// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
91 bool llvm::BugpointIsInterrupted = false;
92
93 #ifndef DEBUG_BUGPOINT
BugpointInterruptFunction()94 static void BugpointInterruptFunction() { BugpointIsInterrupted = true; }
95 #endif
96
97 // Hack to capture a pass list.
98 namespace {
99 class AddToDriver : public legacy::FunctionPassManager {
100 BugDriver &D;
101
102 public:
AddToDriver(BugDriver & _D)103 AddToDriver(BugDriver &_D) : FunctionPassManager(nullptr), D(_D) {}
104
add(Pass * P)105 void add(Pass *P) override {
106 const void *ID = P->getPassID();
107 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
108 D.addPass(PI->getPassArgument());
109 }
110 };
111 }
112
113 #ifdef LINK_POLLY_INTO_TOOLS
114 namespace polly {
115 void initializePollyPasses(llvm::PassRegistry &Registry);
116 }
117 #endif
118
main(int argc,char ** argv)119 int main(int argc, char **argv) {
120 #ifndef DEBUG_BUGPOINT
121 InitLLVM X(argc, argv);
122 #endif
123
124 // Initialize passes
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 initializeAggressiveInstCombine(Registry);
135 initializeInstrumentation(Registry);
136 initializeTarget(Registry);
137
138 #ifdef LINK_POLLY_INTO_TOOLS
139 polly::initializePollyPasses(Registry);
140 #endif
141
142 if (std::getenv("bar") == (char*) -1) {
143 InitializeAllTargets();
144 InitializeAllTargetMCs();
145 InitializeAllAsmPrinters();
146 InitializeAllAsmParsers();
147 }
148
149 cl::ParseCommandLineOptions(argc, argv,
150 "LLVM automatic testcase reducer. See\nhttp://"
151 "llvm.org/cmds/bugpoint.html"
152 " for more information.\n");
153 #ifndef DEBUG_BUGPOINT
154 sys::SetInterruptFunction(BugpointInterruptFunction);
155 #endif
156
157 LLVMContext Context;
158 // If we have an override, set it and then track the triple we want Modules
159 // to use.
160 if (!OverrideTriple.empty()) {
161 TargetTriple.setTriple(Triple::normalize(OverrideTriple));
162 outs() << "Override triple set to '" << TargetTriple.getTriple() << "'\n";
163 }
164
165 if (MemoryLimit < 0) {
166 // Set the default MemoryLimit. Be sure to update the flag's description if
167 // you change this.
168 if (sys::RunningOnValgrind() || UseValgrind)
169 MemoryLimit = 800;
170 else
171 MemoryLimit = 400;
172 #if (LLVM_ADDRESS_SANITIZER_BUILD || LLVM_MEMORY_SANITIZER_BUILD || \
173 LLVM_THREAD_SANITIZER_BUILD)
174 // Starting from kernel 4.9 memory allocated with mmap is counted against
175 // RLIMIT_DATA. Sanitizers need to allocate tens of terabytes for shadow.
176 MemoryLimit = 0;
177 #endif
178 }
179
180 BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit, UseValgrind,
181 Context);
182 if (D.addSources(InputFilenames))
183 return 1;
184
185 AddToDriver PM(D);
186
187 if (StandardLinkOpts) {
188 PassManagerBuilder Builder;
189 Builder.Inliner = createFunctionInliningPass();
190 Builder.populateLTOPassManager(PM);
191 }
192
193 if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
194 PassManagerBuilder Builder;
195 if (OptLevelO1)
196 Builder.Inliner = createAlwaysInlinerLegacyPass();
197 else if (OptLevelOs || OptLevelO2)
198 Builder.Inliner = createFunctionInliningPass(
199 2, OptLevelOs ? 1 : 0, false);
200 else
201 Builder.Inliner = createFunctionInliningPass(275);
202 Builder.populateFunctionPassManager(PM);
203 Builder.populateModulePassManager(PM);
204 }
205
206 for (const PassInfo *PI : PassList)
207 D.addPass(PI->getPassArgument());
208
209 // Bugpoint has the ability of generating a plethora of core files, so to
210 // avoid filling up the disk, we prevent it
211 #ifndef DEBUG_BUGPOINT
212 sys::Process::PreventCoreFiles();
213 #endif
214
215 if (Error E = D.run()) {
216 errs() << toString(std::move(E));
217 return 1;
218 }
219 return 0;
220 }
221