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"
33 FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
34 "on program to find bugs"), cl::init(false));
36 static cl::list<std::string>
37 InputFilenames(cl::Positional, cl::OneOrMore,
38 cl::desc("<input llvm ll/bc files>"));
40 static cl::opt<unsigned>
41 TimeoutValue("timeout", cl::init(300), cl::value_desc("seconds"),
42 cl::desc("Number of seconds program is allowed to run before it "
43 "is killed (default is 300s), 0 disables timeout"));
46 MemoryLimit("mlimit", cl::init(-1), cl::value_desc("MBytes"),
47 cl::desc("Maximum amount of memory to use. 0 disables check."
48 " Defaults to 100MB (800MB under valgrind)."));
51 UseValgrind("enable-valgrind",
52 cl::desc("Run optimizations through valgrind"));
54 // The AnalysesList is automatically populated with registered Passes by the
57 static cl::list<const PassInfo*, bool, PassNameParser>
58 PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
61 StandardCompileOpts("std-compile-opts",
62 cl::desc("Include the standard compile time optimizations"));
65 StandardLinkOpts("std-link-opts",
66 cl::desc("Include the standard link time optimizations"));
68 static cl::opt<std::string>
69 OverrideTriple("mtriple", cl::desc("Override target triple for module"));
71 /// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
72 bool llvm::BugpointIsInterrupted = false;
74 static void BugpointInterruptFunction() {
75 BugpointIsInterrupted = true;
78 // Hack to capture a pass list.
80 class AddToDriver : public PassManager {
83 AddToDriver(BugDriver &_D) : D(_D) {}
85 virtual void add(Pass *P) {
86 const void *ID = P->getPassID();
87 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
88 D.addPass(PI->getPassArgument());
93 int main(int argc, char **argv) {
94 llvm::sys::PrintStackTraceOnErrorSignal();
95 llvm::PrettyStackTraceProgram X(argc, argv);
96 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
97 cl::ParseCommandLineOptions(argc, argv,
98 "LLVM automatic testcase reducer. See\nhttp://"
99 "llvm.org/cmds/bugpoint.html"
100 " for more information.\n");
101 sys::SetInterruptFunction(BugpointInterruptFunction);
103 LLVMContext& Context = getGlobalContext();
104 // If we have an override, set it and then track the triple we want Modules
106 if (!OverrideTriple.empty()) {
107 TargetTriple.setTriple(OverrideTriple);
108 outs() << "Override triple set to '" << OverrideTriple << "'\n";
111 if (MemoryLimit < 0) {
112 // Set the default MemoryLimit. Be sure to update the flag's description if
114 if (sys::RunningOnValgrind() || UseValgrind)
120 BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit,
121 UseValgrind, Context);
122 if (D.addSources(InputFilenames)) return 1;
125 if (StandardCompileOpts) {
126 createStandardModulePasses(&PM, 3,
127 /*OptimizeSize=*/ false,
128 /*UnitAtATime=*/ true,
129 /*UnrollLoops=*/ true,
130 /*SimplifyLibCalls=*/ true,
131 /*HaveExceptions=*/ true,
132 createFunctionInliningPass());
135 if (StandardLinkOpts)
136 createStandardLTOPasses(&PM, /*Internalize=*/true,
138 /*VerifyEach=*/false);
141 for (std::vector<const PassInfo*>::iterator I = PassList.begin(),
144 const PassInfo* PI = *I;
145 D.addPass(PI->getPassArgument());
148 // Bugpoint has the ability of generating a plethora of core files, so to
149 // avoid filling up the disk, we prevent it
150 sys::Process::PreventCoreFiles();
153 bool Failure = D.run(Error);
154 if (!Error.empty()) {