1 //===- bugpoint.cpp - The LLVM Bugpoint utility ---------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
13 //
14 //===----------------------------------------------------------------------===//
15
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"
30
31 //Enable this macro to debug bugpoint itself.
32 //#define DEBUG_BUGPOINT 1
33
34 using namespace llvm;
35
36 static cl::opt<bool>
37 FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
38 "on program to find bugs"), cl::init(false));
39
40 static cl::list<std::string>
41 InputFilenames(cl::Positional, cl::OneOrMore,
42 cl::desc("<input llvm ll/bc files>"));
43
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"));
48
49 static cl::opt<int>
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)."));
53
54 static cl::opt<bool>
55 UseValgrind("enable-valgrind",
56 cl::desc("Run optimizations through valgrind"));
57
58 // The AnalysesList is automatically populated with registered Passes by the
59 // PassNameParser.
60 //
61 static cl::list<const PassInfo*, bool, PassNameParser>
62 PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
63
64 static cl::opt<bool>
65 StandardCompileOpts("std-compile-opts",
66 cl::desc("Include the standard compile time optimizations"));
67
68 static cl::opt<bool>
69 StandardLinkOpts("std-link-opts",
70 cl::desc("Include the standard link time optimizations"));
71
72 static cl::opt<bool>
73 OptLevelO1("O1",
74 cl::desc("Optimization level 1. Similar to llvm-gcc -O1"));
75
76 static cl::opt<bool>
77 OptLevelO2("O2",
78 cl::desc("Optimization level 2. Similar to llvm-gcc -O2"));
79
80 static cl::opt<bool>
81 OptLevelO3("O3",
82 cl::desc("Optimization level 3. Similar to llvm-gcc -O3"));
83
84 static cl::opt<std::string>
85 OverrideTriple("mtriple", cl::desc("Override target triple for module"));
86
87 /// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
88 bool llvm::BugpointIsInterrupted = false;
89
90 #ifndef DEBUG_BUGPOINT
BugpointInterruptFunction()91 static void BugpointInterruptFunction() {
92 BugpointIsInterrupted = true;
93 }
94 #endif
95
96 // Hack to capture a pass list.
97 namespace {
98 class AddToDriver : public FunctionPassManager {
99 BugDriver &D;
100 public:
AddToDriver(BugDriver & _D)101 AddToDriver(BugDriver &_D) : FunctionPassManager(0), D(_D) {}
102
add(Pass * P)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());
107 }
108 };
109 }
110
main(int argc,char ** argv)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.
116 #endif
117
118 // Initialize passes
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);
129
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);
136 #endif
137
138 LLVMContext& Context = getGlobalContext();
139 // If we have an override, set it and then track the triple we want Modules
140 // to use.
141 if (!OverrideTriple.empty()) {
142 TargetTriple.setTriple(Triple::normalize(OverrideTriple));
143 outs() << "Override triple set to '" << TargetTriple.getTriple() << "'\n";
144 }
145
146 if (MemoryLimit < 0) {
147 // Set the default MemoryLimit. Be sure to update the flag's description if
148 // you change this.
149 if (sys::RunningOnValgrind() || UseValgrind)
150 MemoryLimit = 800;
151 else
152 MemoryLimit = 100;
153 }
154
155 BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit,
156 UseValgrind, Context);
157 if (D.addSources(InputFilenames)) return 1;
158
159 AddToDriver PM(D);
160 if (StandardCompileOpts) {
161 PassManagerBuilder Builder;
162 Builder.OptLevel = 3;
163 Builder.Inliner = createFunctionInliningPass();
164 Builder.populateModulePassManager(PM);
165 }
166
167 if (StandardLinkOpts) {
168 PassManagerBuilder Builder;
169 Builder.populateLTOPassManager(PM, /*Internalize=*/true,
170 /*RunInliner=*/true);
171 }
172
173 if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
174 PassManagerBuilder Builder;
175 if (OptLevelO1)
176 Builder.Inliner = createAlwaysInlinerPass();
177 else if (OptLevelO2)
178 Builder.Inliner = createFunctionInliningPass(225);
179 else
180 Builder.Inliner = createFunctionInliningPass(275);
181
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);
186 }
187
188 for (std::vector<const PassInfo*>::iterator I = PassList.begin(),
189 E = PassList.end();
190 I != E; ++I) {
191 const PassInfo* PI = *I;
192 D.addPass(PI->getPassArgument());
193 }
194
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();
199 #endif
200
201 std::string Error;
202 bool Failure = D.run(Error);
203 if (!Error.empty()) {
204 errs() << Error;
205 return 1;
206 }
207 return Failure;
208 }
209