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