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/Support/Process.h"
27 #include "llvm/Support/Signals.h"
28 #include "llvm/Support/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.
99 PassRegistry &Registry = *PassRegistry::getPassRegistry();
100 initializeCore(Registry);
101 initializeScalarOpts(Registry);
102 initializeIPO(Registry);
103 initializeAnalysis(Registry);
104 initializeIPA(Registry);
105 initializeTransformUtils(Registry);
106 initializeInstCombine(Registry);
107 initializeInstrumentation(Registry);
108 initializeTarget(Registry);
110 cl::ParseCommandLineOptions(argc, argv,
111 "LLVM automatic testcase reducer. See\nhttp://"
112 "llvm.org/cmds/bugpoint.html"
113 " for more information.\n");
114 sys::SetInterruptFunction(BugpointInterruptFunction);
116 LLVMContext& Context = getGlobalContext();
117 // If we have an override, set it and then track the triple we want Modules
119 if (!OverrideTriple.empty()) {
120 TargetTriple.setTriple(Triple::normalize(OverrideTriple));
121 outs() << "Override triple set to '" << TargetTriple.getTriple() << "'\n";
124 if (MemoryLimit < 0) {
125 // Set the default MemoryLimit. Be sure to update the flag's description if
127 if (sys::RunningOnValgrind() || UseValgrind)
133 BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit,
134 UseValgrind, Context);
135 if (D.addSources(InputFilenames)) return 1;
138 if (StandardCompileOpts) {
139 createStandardModulePasses(&PM, 3,
140 /*OptimizeSize=*/ false,
141 /*UnitAtATime=*/ true,
142 /*UnrollLoops=*/ true,
143 /*SimplifyLibCalls=*/ true,
144 /*HaveExceptions=*/ true,
145 createFunctionInliningPass());
148 if (StandardLinkOpts)
149 createStandardLTOPasses(&PM, /*Internalize=*/true,
151 /*VerifyEach=*/false);
154 for (std::vector<const PassInfo*>::iterator I = PassList.begin(),
157 const PassInfo* PI = *I;
158 D.addPass(PI->getPassArgument());
161 // Bugpoint has the ability of generating a plethora of core files, so to
162 // avoid filling up the disk, we prevent it
163 sys::Process::PreventCoreFiles();
166 bool Failure = D.run(Error);
167 if (!Error.empty()) {