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/LinkAllPasses.h"
19 #include "llvm/LLVMContext.h"
20 #include "llvm/Support/PassNameParser.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/ManagedStatic.h"
23 #include "llvm/Support/PluginLoader.h"
24 #include "llvm/Support/PrettyStackTrace.h"
25 #include "llvm/Support/PassManagerBuilder.h"
26 #include "llvm/Support/Process.h"
27 #include "llvm/Support/Signals.h"
28 #include "llvm/Support/Valgrind.h"
29 #include "llvm/LinkAllVMCore.h"
31 //Enable this macro to debug bugpoint itself.
32 //#define DEBUG_BUGPOINT 1
37 FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
38 "on program to find bugs"), cl::init(false));
40 static cl::list
<std::string
>
41 InputFilenames(cl::Positional
, cl::OneOrMore
,
42 cl::desc("<input llvm ll/bc files>"));
44 static cl::opt
<unsigned>
45 TimeoutValue("timeout", cl::init(300), cl::value_desc("seconds"),
46 cl::desc("Number of seconds program is allowed to run before it "
47 "is killed (default is 300s), 0 disables timeout"));
50 MemoryLimit("mlimit", cl::init(-1), cl::value_desc("MBytes"),
51 cl::desc("Maximum amount of memory to use. 0 disables check."
52 " Defaults to 100MB (800MB under valgrind)."));
55 UseValgrind("enable-valgrind",
56 cl::desc("Run optimizations through valgrind"));
58 // The AnalysesList is automatically populated with registered Passes by the
61 static cl::list
<const PassInfo
*, bool, PassNameParser
>
62 PassList(cl::desc("Passes available:"), cl::ZeroOrMore
);
65 StandardCompileOpts("std-compile-opts",
66 cl::desc("Include the standard compile time optimizations"));
69 StandardLinkOpts("std-link-opts",
70 cl::desc("Include the standard link time optimizations"));
74 cl::desc("Optimization level 1. Similar to llvm-gcc -O1"));
78 cl::desc("Optimization level 2. Similar to llvm-gcc -O2"));
82 cl::desc("Optimization level 3. Similar to llvm-gcc -O3"));
84 static cl::opt
<std::string
>
85 OverrideTriple("mtriple", cl::desc("Override target triple for module"));
87 /// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
88 bool llvm::BugpointIsInterrupted
= false;
90 #ifndef DEBUG_BUGPOINT
91 static void BugpointInterruptFunction() {
92 BugpointIsInterrupted
= true;
96 // Hack to capture a pass list.
98 class AddToDriver
: public FunctionPassManager
{
101 AddToDriver(BugDriver
&_D
) : FunctionPassManager(0), D(_D
) {}
103 virtual void add(Pass
*P
) {
104 const void *ID
= P
->getPassID();
105 const PassInfo
*PI
= PassRegistry::getPassRegistry()->getPassInfo(ID
);
106 D
.addPass(PI
->getPassArgument());
111 int main(int argc
, char **argv
) {
112 #ifndef DEBUG_BUGPOINT
113 llvm::sys::PrintStackTraceOnErrorSignal();
114 llvm::PrettyStackTraceProgram
X(argc
, argv
);
115 llvm_shutdown_obj Y
; // Call llvm_shutdown() on exit.
119 PassRegistry
&Registry
= *PassRegistry::getPassRegistry();
120 initializeCore(Registry
);
121 initializeScalarOpts(Registry
);
122 initializeIPO(Registry
);
123 initializeAnalysis(Registry
);
124 initializeIPA(Registry
);
125 initializeTransformUtils(Registry
);
126 initializeInstCombine(Registry
);
127 initializeInstrumentation(Registry
);
128 initializeTarget(Registry
);
130 cl::ParseCommandLineOptions(argc
, argv
,
131 "LLVM automatic testcase reducer. See\nhttp://"
132 "llvm.org/cmds/bugpoint.html"
133 " for more information.\n");
134 #ifndef DEBUG_BUGPOINT
135 sys::SetInterruptFunction(BugpointInterruptFunction
);
138 LLVMContext
& Context
= getGlobalContext();
139 // If we have an override, set it and then track the triple we want Modules
141 if (!OverrideTriple
.empty()) {
142 TargetTriple
.setTriple(Triple::normalize(OverrideTriple
));
143 outs() << "Override triple set to '" << TargetTriple
.getTriple() << "'\n";
146 if (MemoryLimit
< 0) {
147 // Set the default MemoryLimit. Be sure to update the flag's description if
149 if (sys::RunningOnValgrind() || UseValgrind
)
155 BugDriver
D(argv
[0], FindBugs
, TimeoutValue
, MemoryLimit
,
156 UseValgrind
, Context
);
157 if (D
.addSources(InputFilenames
)) return 1;
160 if (StandardCompileOpts
) {
161 PassManagerBuilder Builder
;
162 Builder
.OptLevel
= 3;
163 Builder
.Inliner
= createFunctionInliningPass();
164 Builder
.populateModulePassManager(PM
);
167 if (StandardLinkOpts
) {
168 PassManagerBuilder Builder
;
169 Builder
.populateLTOPassManager(PM
, /*Internalize=*/true,
170 /*RunInliner=*/true);
173 if (OptLevelO1
|| OptLevelO2
|| OptLevelO3
) {
174 PassManagerBuilder Builder
;
176 Builder
.Inliner
= createAlwaysInlinerPass();
178 Builder
.Inliner
= createFunctionInliningPass(225);
180 Builder
.Inliner
= createFunctionInliningPass(275);
182 // Note that although clang/llvm-gcc use two separate passmanagers
183 // here, it shouldn't normally make a difference.
184 Builder
.populateFunctionPassManager(PM
);
185 Builder
.populateModulePassManager(PM
);
188 for (std::vector
<const PassInfo
*>::iterator I
= PassList
.begin(),
191 const PassInfo
* PI
= *I
;
192 D
.addPass(PI
->getPassArgument());
195 // Bugpoint has the ability of generating a plethora of core files, so to
196 // avoid filling up the disk, we prevent it
197 #ifndef DEBUG_BUGPOINT
198 sys::Process::PreventCoreFiles();
202 bool Failure
= D
.run(Error
);
203 if (!Error
.empty()) {