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/StandardPasses.h"
26 #include "llvm/System/Process.h"
27 #include "llvm/System/Signals.h"
28 #include "llvm/System/Valgrind.h"
29 #include "llvm/LinkAllVMCore.h"
32 // AsChild - Specifies that this invocation of bugpoint is being generated
33 // from a parent process. It is not intended to be used by users so the
36 AsChild("as-child", cl::desc("Run bugpoint as child process"),
40 FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
41 "on program to find bugs"), cl::init(false));
43 static cl::list<std::string>
44 InputFilenames(cl::Positional, cl::OneOrMore,
45 cl::desc("<input llvm ll/bc files>"));
47 static cl::opt<unsigned>
48 TimeoutValue("timeout", cl::init(300), cl::value_desc("seconds"),
49 cl::desc("Number of seconds program is allowed to run before it "
50 "is killed (default is 300s), 0 disables timeout"));
53 MemoryLimit("mlimit", cl::init(-1), cl::value_desc("MBytes"),
54 cl::desc("Maximum amount of memory to use. 0 disables check."
55 " Defaults to 100MB (800MB under valgrind)."));
58 UseValgrind("enable-valgrind",
59 cl::desc("Run optimizations through valgrind"));
61 // The AnalysesList is automatically populated with registered Passes by the
64 static cl::list<const PassInfo*, bool, PassNameParser>
65 PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
68 StandardCompileOpts("std-compile-opts",
69 cl::desc("Include the standard compile time optimizations"));
72 StandardLinkOpts("std-link-opts",
73 cl::desc("Include the standard link time optimizations"));
75 static cl::opt<std::string>
76 OverrideTriple("mtriple", cl::desc("Override target triple for module"));
78 /// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
79 bool llvm::BugpointIsInterrupted = false;
81 static void BugpointInterruptFunction() {
82 BugpointIsInterrupted = true;
85 // Hack to capture a pass list.
87 class AddToDriver : public PassManager {
90 AddToDriver(BugDriver &_D) : D(_D) {}
92 virtual void add(Pass *P) {
93 const void *ID = P->getPassID();
94 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
95 D.addPasses(&PI, &PI + 1);
100 int main(int argc, char **argv) {
101 llvm::sys::PrintStackTraceOnErrorSignal();
102 llvm::PrettyStackTraceProgram X(argc, argv);
103 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
104 cl::ParseCommandLineOptions(argc, argv,
105 "LLVM automatic testcase reducer. See\nhttp://"
106 "llvm.org/cmds/bugpoint.html"
107 " for more information.\n");
108 sys::SetInterruptFunction(BugpointInterruptFunction);
110 LLVMContext& Context = getGlobalContext();
111 // If we have an override, set it and then track the triple we want Modules
113 if (!OverrideTriple.empty()) {
114 TargetTriple.setTriple(OverrideTriple);
115 outs() << "Override triple set to '" << OverrideTriple << "'\n";
118 if (MemoryLimit < 0) {
119 // Set the default MemoryLimit. Be sure to update the flag's description if
121 if (sys::RunningOnValgrind() || UseValgrind)
127 BugDriver D(argv[0], AsChild, FindBugs, TimeoutValue, MemoryLimit,
128 UseValgrind, Context);
129 if (D.addSources(InputFilenames)) return 1;
132 if (StandardCompileOpts) {
133 createStandardModulePasses(&PM, 3,
134 /*OptimizeSize=*/ false,
135 /*UnitAtATime=*/ true,
136 /*UnrollLoops=*/ true,
137 /*SimplifyLibCalls=*/ true,
138 /*HaveExceptions=*/ true,
139 createFunctionInliningPass());
142 if (StandardLinkOpts)
143 createStandardLTOPasses(&PM, /*Internalize=*/true,
145 /*VerifyEach=*/false);
147 D.addPasses(PassList.begin(), PassList.end());
149 // Bugpoint has the ability of generating a plethora of core files, so to
150 // avoid filling up the disk, we prevent it
151 sys::Process::PreventCoreFiles();
154 bool Failure = D.run(Error);
155 if (!Error.empty()) {