blob: e25414fa4265b470974e0d14c5f3de2001fc298c [file] [log] [blame]
Chris Lattner94321112003-10-20 17:57:13 +00001//===- bugpoint.cpp - The LLVM Bugpoint utility ---------------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerafade922002-11-20 22:28:10 +00009//
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"
Chris Lattnerf1b20d82006-06-06 22:30:59 +000017#include "ToolRunner.h"
Reid Spencer62c51052006-08-21 05:34:03 +000018#include "llvm/LinkAllPasses.h"
Owen Anderson8b477ed2009-07-01 16:58:40 +000019#include "llvm/LLVMContext.h"
Chris Lattnerafade922002-11-20 22:28:10 +000020#include "llvm/Support/PassNameParser.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000022#include "llvm/Support/ManagedStatic.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/Support/PluginLoader.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000024#include "llvm/Support/PrettyStackTrace.h"
Chris Lattner817a01f2011-05-22 00:20:07 +000025#include "llvm/Support/PassManagerBuilder.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000026#include "llvm/Support/Process.h"
27#include "llvm/Support/Signals.h"
28#include "llvm/Support/Valgrind.h"
Reid Spencere3f05612006-06-07 23:06:50 +000029#include "llvm/LinkAllVMCore.h"
Devang Patelbc8d5f12011-01-13 19:48:54 +000030
Devang Patel3f84a452011-01-14 15:55:50 +000031//Enable this macro to debug bugpoint itself.
32//#define DEBUG_BUGPOINT 1
Devang Patelbc8d5f12011-01-13 19:48:54 +000033
Brian Gaeked0fde302003-11-11 22:41:34 +000034using namespace llvm;
35
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +000036static cl::opt<bool>
Dan Gohman39564492008-02-18 17:15:45 +000037FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +000038 "on program to find bugs"), cl::init(false));
Reid Spencerc4bb0522005-12-22 20:02:55 +000039
Chris Lattnerafade922002-11-20 22:28:10 +000040static cl::list<std::string>
41InputFilenames(cl::Positional, cl::OneOrMore,
42 cl::desc("<input llvm ll/bc files>"));
43
Chris Lattner9686ae72006-06-13 03:10:48 +000044static cl::opt<unsigned>
45TimeoutValue("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
Jeffrey Yasskinc3e68592010-03-19 00:09:28 +000049static cl::opt<int>
50MemoryLimit("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
54static cl::opt<bool>
55UseValgrind("enable-valgrind",
56 cl::desc("Run optimizations through valgrind"));
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +000057
Chris Lattnerafade922002-11-20 22:28:10 +000058// The AnalysesList is automatically populated with registered Passes by the
59// PassNameParser.
60//
Owen Anderson8be32912010-07-20 08:26:15 +000061static cl::list<const PassInfo*, bool, PassNameParser>
Misha Brukman50733362003-07-24 18:17:43 +000062PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
Chris Lattnerafade922002-11-20 22:28:10 +000063
Daniel Dunbar14886702009-07-20 07:01:01 +000064static cl::opt<bool>
65StandardCompileOpts("std-compile-opts",
66 cl::desc("Include the standard compile time optimizations"));
67
68static cl::opt<bool>
69StandardLinkOpts("std-link-opts",
70 cl::desc("Include the standard link time optimizations"));
71
Eli Friedmanbe2d1232011-06-06 22:45:46 +000072static cl::opt<bool>
73OptLevelO1("O1",
74 cl::desc("Optimization level 1. Similar to llvm-gcc -O1"));
75
76static cl::opt<bool>
77OptLevelO2("O2",
78 cl::desc("Optimization level 2. Similar to llvm-gcc -O2"));
79
80static cl::opt<bool>
81OptLevelO3("O3",
82 cl::desc("Optimization level 3. Similar to llvm-gcc -O3"));
83
Daniel Dunbarca740962009-08-18 03:35:57 +000084static cl::opt<std::string>
85OverrideTriple("mtriple", cl::desc("Override target triple for module"));
86
Chris Lattnerf9aaae02005-08-02 02:16:17 +000087/// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
88bool llvm::BugpointIsInterrupted = false;
89
Devang Patelbc8d5f12011-01-13 19:48:54 +000090#ifndef DEBUG_BUGPOINT
Chris Lattnerf9aaae02005-08-02 02:16:17 +000091static void BugpointInterruptFunction() {
92 BugpointIsInterrupted = true;
93}
Devang Patelbc8d5f12011-01-13 19:48:54 +000094#endif
Chris Lattnerf9aaae02005-08-02 02:16:17 +000095
Daniel Dunbar14886702009-07-20 07:01:01 +000096// Hack to capture a pass list.
97namespace {
Eli Friedmanbe2d1232011-06-06 22:45:46 +000098 class AddToDriver : public FunctionPassManager {
Daniel Dunbar14886702009-07-20 07:01:01 +000099 BugDriver &D;
100 public:
Eli Friedmanbe2d1232011-06-06 22:45:46 +0000101 AddToDriver(BugDriver &_D) : FunctionPassManager(0), D(_D) {}
Daniel Dunbar14886702009-07-20 07:01:01 +0000102
103 virtual void add(Pass *P) {
Owen Anderson90c579d2010-08-06 18:33:48 +0000104 const void *ID = P->getPassID();
105 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
Rafael Espindola8261dfe2010-08-08 03:55:08 +0000106 D.addPass(PI->getPassArgument());
Daniel Dunbar14886702009-07-20 07:01:01 +0000107 }
108 };
109}
110
Chris Lattnerafade922002-11-20 22:28:10 +0000111int main(int argc, char **argv) {
Devang Patelbc8d5f12011-01-13 19:48:54 +0000112#ifndef DEBUG_BUGPOINT
Chris Lattnercc14d252009-03-06 05:34:10 +0000113 llvm::sys::PrintStackTraceOnErrorSignal();
114 llvm::PrettyStackTraceProgram X(argc, argv);
115 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Devang Patelbc8d5f12011-01-13 19:48:54 +0000116#endif
Owen Anderson081c34b2010-10-19 17:21:58 +0000117
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
Chris Lattner670406d2003-10-18 21:55:35 +0000130 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman82a13c92007-10-08 15:45:12 +0000131 "LLVM automatic testcase reducer. See\nhttp://"
Chris Lattner3a4baf12009-02-07 18:56:30 +0000132 "llvm.org/cmds/bugpoint.html"
Chris Lattner670406d2003-10-18 21:55:35 +0000133 " for more information.\n");
Devang Patelbc8d5f12011-01-13 19:48:54 +0000134#ifndef DEBUG_BUGPOINT
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000135 sys::SetInterruptFunction(BugpointInterruptFunction);
Devang Patelbc8d5f12011-01-13 19:48:54 +0000136#endif
Owen Anderson8b477ed2009-07-01 16:58:40 +0000137
Owen Anderson0d7c6952009-07-15 22:16:10 +0000138 LLVMContext& Context = getGlobalContext();
Daniel Dunbarca740962009-08-18 03:35:57 +0000139 // If we have an override, set it and then track the triple we want Modules
140 // to use.
Chris Lattner30611082009-08-31 03:22:35 +0000141 if (!OverrideTriple.empty()) {
Duncan Sands75ebbce2010-08-28 01:30:02 +0000142 TargetTriple.setTriple(Triple::normalize(OverrideTriple));
143 outs() << "Override triple set to '" << TargetTriple.getTriple() << "'\n";
Chris Lattner30611082009-08-31 03:22:35 +0000144 }
Daniel Dunbarca740962009-08-18 03:35:57 +0000145
Jeffrey Yasskinc3e68592010-03-19 00:09:28 +0000146 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
Rafael Espindola7f99f742010-08-07 23:03:21 +0000155 BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit,
Jeffrey Yasskinc3e68592010-03-19 00:09:28 +0000156 UseValgrind, Context);
Chris Lattnerafade922002-11-20 22:28:10 +0000157 if (D.addSources(InputFilenames)) return 1;
Daniel Dunbarca740962009-08-18 03:35:57 +0000158
Daniel Dunbar14886702009-07-20 07:01:01 +0000159 AddToDriver PM(D);
160 if (StandardCompileOpts) {
Chris Lattner817a01f2011-05-22 00:20:07 +0000161 PassManagerBuilder Builder;
162 Builder.OptLevel = 3;
163 Builder.Inliner = createFunctionInliningPass();
164 Builder.populateModulePassManager(PM);
Daniel Dunbar14886702009-07-20 07:01:01 +0000165 }
166
Chris Lattner817a01f2011-05-22 00:20:07 +0000167 if (StandardLinkOpts) {
168 PassManagerBuilder Builder;
169 Builder.populateLTOPassManager(PM, /*Internalize=*/true,
170 /*RunInliner=*/true);
171 }
Daniel Dunbar14886702009-07-20 07:01:01 +0000172
Eli Friedmanbe2d1232011-06-06 22:45:46 +0000173 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 }
Rafael Espindola8261dfe2010-08-08 03:55:08 +0000187
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 }
Chris Lattnerafade922002-11-20 22:28:10 +0000194
Misha Brukman67b36e42003-09-12 20:42:57 +0000195 // Bugpoint has the ability of generating a plethora of core files, so to
Reid Spencer551ccae2004-09-01 22:55:40 +0000196 // avoid filling up the disk, we prevent it
Devang Patelbc8d5f12011-01-13 19:48:54 +0000197#ifndef DEBUG_BUGPOINT
Reid Spencer51163302004-12-27 06:18:02 +0000198 sys::Process::PreventCoreFiles();
Devang Patelbc8d5f12011-01-13 19:48:54 +0000199#endif
Misha Brukman67b36e42003-09-12 20:42:57 +0000200
Nick Lewycky22ff7482010-04-12 05:08:25 +0000201 std::string Error;
202 bool Failure = D.run(Error);
203 if (!Error.empty()) {
204 errs() << Error;
205 return 1;
Chris Lattner74d45272004-02-18 17:32:54 +0000206 }
Nick Lewycky22ff7482010-04-12 05:08:25 +0000207 return Failure;
Chris Lattnerafade922002-11-20 22:28:10 +0000208}