blob: 5e8fdd145eef50e70148eb4f8594c701c3a0ea0b [file] [log] [blame]
Chris Lattnerebec8032003-10-20 17:57:13 +00001//===- bugpoint.cpp - The LLVM Bugpoint utility ---------------------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswell09344dc2003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner345353d2007-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 Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswell09344dc2003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner73a6bdd2002-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 Lattnerffac2862006-06-06 22:30:59 +000017#include "ToolRunner.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/LLVMContext.h"
Jakub Staszak63e77d52013-01-10 21:56:40 +000019#include "llvm/LinkAllIR.h"
Chandler Carruth1fe21fc2013-01-19 08:03:47 +000020#include "llvm/LinkAllPasses.h"
Rafael Espindola3ea478b2011-08-02 21:50:27 +000021#include "llvm/PassManager.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000022#include "llvm/Support/CommandLine.h"
Chris Lattner76d46322006-12-06 01:18:01 +000023#include "llvm/Support/ManagedStatic.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000024#include "llvm/Support/PassNameParser.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000025#include "llvm/Support/PluginLoader.h"
Chris Lattnere3fc2d12009-03-06 05:34:10 +000026#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000027#include "llvm/Support/Process.h"
28#include "llvm/Support/Signals.h"
29#include "llvm/Support/Valgrind.h"
Rafael Espindola591eaa42011-08-02 21:50:24 +000030#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Devang Patel331df542011-01-13 19:48:54 +000031
Devang Patel610c41e72011-01-14 15:55:50 +000032//Enable this macro to debug bugpoint itself.
33//#define DEBUG_BUGPOINT 1
Devang Patel331df542011-01-13 19:48:54 +000034
Brian Gaeke960707c2003-11-11 22:41:34 +000035using namespace llvm;
36
Patrick Jenkinsc46c0382006-08-15 16:40:49 +000037static cl::opt<bool>
Dan Gohman02d2bc82008-02-18 17:15:45 +000038FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
Patrick Jenkinsc46c0382006-08-15 16:40:49 +000039 "on program to find bugs"), cl::init(false));
Reid Spencer842118c2005-12-22 20:02:55 +000040
Chris Lattner73a6bdd2002-11-20 22:28:10 +000041static cl::list<std::string>
42InputFilenames(cl::Positional, cl::OneOrMore,
43 cl::desc("<input llvm ll/bc files>"));
44
Chris Lattner20ec1652006-06-13 03:10:48 +000045static cl::opt<unsigned>
46TimeoutValue("timeout", cl::init(300), cl::value_desc("seconds"),
47 cl::desc("Number of seconds program is allowed to run before it "
48 "is killed (default is 300s), 0 disables timeout"));
49
Jeffrey Yasskin71bd0f42010-03-19 00:09:28 +000050static cl::opt<int>
51MemoryLimit("mlimit", cl::init(-1), cl::value_desc("MBytes"),
52 cl::desc("Maximum amount of memory to use. 0 disables check."
53 " Defaults to 100MB (800MB under valgrind)."));
54
55static cl::opt<bool>
56UseValgrind("enable-valgrind",
57 cl::desc("Run optimizations through valgrind"));
Anton Korobeynikovd01defe2007-02-16 19:11:07 +000058
Chris Lattner73a6bdd2002-11-20 22:28:10 +000059// The AnalysesList is automatically populated with registered Passes by the
60// PassNameParser.
61//
Owen Anderson81781222010-07-20 08:26:15 +000062static cl::list<const PassInfo*, bool, PassNameParser>
Misha Brukmand792c9b2003-07-24 18:17:43 +000063PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
Chris Lattner73a6bdd2002-11-20 22:28:10 +000064
Daniel Dunbar73699512009-07-20 07:01:01 +000065static cl::opt<bool>
66StandardCompileOpts("std-compile-opts",
67 cl::desc("Include the standard compile time optimizations"));
68
69static cl::opt<bool>
70StandardLinkOpts("std-link-opts",
71 cl::desc("Include the standard link time optimizations"));
72
Eli Friedman2378cac2011-06-06 22:45:46 +000073static cl::opt<bool>
74OptLevelO1("O1",
75 cl::desc("Optimization level 1. Similar to llvm-gcc -O1"));
76
77static cl::opt<bool>
78OptLevelO2("O2",
79 cl::desc("Optimization level 2. Similar to llvm-gcc -O2"));
80
81static cl::opt<bool>
82OptLevelO3("O3",
83 cl::desc("Optimization level 3. Similar to llvm-gcc -O3"));
84
Daniel Dunbar8575a602009-08-18 03:35:57 +000085static cl::opt<std::string>
86OverrideTriple("mtriple", cl::desc("Override target triple for module"));
87
Chris Lattnerbeb01fa2005-08-02 02:16:17 +000088/// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
89bool llvm::BugpointIsInterrupted = false;
90
Devang Patel331df542011-01-13 19:48:54 +000091#ifndef DEBUG_BUGPOINT
Chris Lattnerbeb01fa2005-08-02 02:16:17 +000092static void BugpointInterruptFunction() {
93 BugpointIsInterrupted = true;
94}
Devang Patel331df542011-01-13 19:48:54 +000095#endif
Chris Lattnerbeb01fa2005-08-02 02:16:17 +000096
Daniel Dunbar73699512009-07-20 07:01:01 +000097// Hack to capture a pass list.
98namespace {
Eli Friedman2378cac2011-06-06 22:45:46 +000099 class AddToDriver : public FunctionPassManager {
Daniel Dunbar73699512009-07-20 07:01:01 +0000100 BugDriver &D;
101 public:
Eli Friedman2378cac2011-06-06 22:45:46 +0000102 AddToDriver(BugDriver &_D) : FunctionPassManager(0), D(_D) {}
Daniel Dunbar73699512009-07-20 07:01:01 +0000103
104 virtual void add(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000105 const void *ID = P->getPassID();
106 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
Rafael Espindola33e81a82010-08-08 03:55:08 +0000107 D.addPass(PI->getPassArgument());
Daniel Dunbar73699512009-07-20 07:01:01 +0000108 }
109 };
110}
111
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000112int main(int argc, char **argv) {
Devang Patel331df542011-01-13 19:48:54 +0000113#ifndef DEBUG_BUGPOINT
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000114 llvm::sys::PrintStackTraceOnErrorSignal();
115 llvm::PrettyStackTraceProgram X(argc, argv);
116 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Devang Patel331df542011-01-13 19:48:54 +0000117#endif
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000118
119 // Initialize passes
120 PassRegistry &Registry = *PassRegistry::getPassRegistry();
121 initializeCore(Registry);
122 initializeScalarOpts(Registry);
Michael Gottesman79d8d812013-01-28 01:35:51 +0000123 initializeObjCARCOpts(Registry);
Hal Finkel61f210b2012-02-07 21:11:12 +0000124 initializeVectorization(Registry);
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000125 initializeIPO(Registry);
126 initializeAnalysis(Registry);
127 initializeIPA(Registry);
128 initializeTransformUtils(Registry);
129 initializeInstCombine(Registry);
130 initializeInstrumentation(Registry);
131 initializeTarget(Registry);
132
Chris Lattner849619e2003-10-18 21:55:35 +0000133 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman2c6a8212007-10-08 15:45:12 +0000134 "LLVM automatic testcase reducer. See\nhttp://"
Chris Lattnerc3aa8f72009-02-07 18:56:30 +0000135 "llvm.org/cmds/bugpoint.html"
Chris Lattner849619e2003-10-18 21:55:35 +0000136 " for more information.\n");
Devang Patel331df542011-01-13 19:48:54 +0000137#ifndef DEBUG_BUGPOINT
Chris Lattnerbeb01fa2005-08-02 02:16:17 +0000138 sys::SetInterruptFunction(BugpointInterruptFunction);
Devang Patel331df542011-01-13 19:48:54 +0000139#endif
Owen Anderson6773d382009-07-01 16:58:40 +0000140
Owen Anderson19251ec2009-07-15 22:16:10 +0000141 LLVMContext& Context = getGlobalContext();
Daniel Dunbar8575a602009-08-18 03:35:57 +0000142 // If we have an override, set it and then track the triple we want Modules
143 // to use.
Chris Lattneredcbfe82009-08-31 03:22:35 +0000144 if (!OverrideTriple.empty()) {
Duncan Sands3bd97fe2010-08-28 01:30:02 +0000145 TargetTriple.setTriple(Triple::normalize(OverrideTriple));
146 outs() << "Override triple set to '" << TargetTriple.getTriple() << "'\n";
Chris Lattneredcbfe82009-08-31 03:22:35 +0000147 }
Daniel Dunbar8575a602009-08-18 03:35:57 +0000148
Jeffrey Yasskin71bd0f42010-03-19 00:09:28 +0000149 if (MemoryLimit < 0) {
150 // Set the default MemoryLimit. Be sure to update the flag's description if
151 // you change this.
152 if (sys::RunningOnValgrind() || UseValgrind)
153 MemoryLimit = 800;
154 else
155 MemoryLimit = 100;
156 }
157
Rafael Espindolabbdce492010-08-07 23:03:21 +0000158 BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit,
Jeffrey Yasskin71bd0f42010-03-19 00:09:28 +0000159 UseValgrind, Context);
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000160 if (D.addSources(InputFilenames)) return 1;
Daniel Dunbar8575a602009-08-18 03:35:57 +0000161
Daniel Dunbar73699512009-07-20 07:01:01 +0000162 AddToDriver PM(D);
163 if (StandardCompileOpts) {
Chris Lattnerf200a0a2011-05-22 00:20:07 +0000164 PassManagerBuilder Builder;
165 Builder.OptLevel = 3;
166 Builder.Inliner = createFunctionInliningPass();
167 Builder.populateModulePassManager(PM);
Daniel Dunbar73699512009-07-20 07:01:01 +0000168 }
169
Chris Lattnerf200a0a2011-05-22 00:20:07 +0000170 if (StandardLinkOpts) {
171 PassManagerBuilder Builder;
172 Builder.populateLTOPassManager(PM, /*Internalize=*/true,
173 /*RunInliner=*/true);
174 }
Daniel Dunbar73699512009-07-20 07:01:01 +0000175
Eli Friedman2378cac2011-06-06 22:45:46 +0000176 if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
177 PassManagerBuilder Builder;
178 if (OptLevelO1)
179 Builder.Inliner = createAlwaysInlinerPass();
180 else if (OptLevelO2)
181 Builder.Inliner = createFunctionInliningPass(225);
182 else
183 Builder.Inliner = createFunctionInliningPass(275);
184
185 // Note that although clang/llvm-gcc use two separate passmanagers
186 // here, it shouldn't normally make a difference.
187 Builder.populateFunctionPassManager(PM);
188 Builder.populateModulePassManager(PM);
189 }
Rafael Espindola33e81a82010-08-08 03:55:08 +0000190
191 for (std::vector<const PassInfo*>::iterator I = PassList.begin(),
192 E = PassList.end();
193 I != E; ++I) {
194 const PassInfo* PI = *I;
195 D.addPass(PI->getPassArgument());
196 }
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000197
Misha Brukman46e18d42003-09-12 20:42:57 +0000198 // Bugpoint has the ability of generating a plethora of core files, so to
Reid Spencer7c16caa2004-09-01 22:55:40 +0000199 // avoid filling up the disk, we prevent it
Devang Patel331df542011-01-13 19:48:54 +0000200#ifndef DEBUG_BUGPOINT
Reid Spencera079b692004-12-27 06:18:02 +0000201 sys::Process::PreventCoreFiles();
Devang Patel331df542011-01-13 19:48:54 +0000202#endif
Misha Brukman46e18d42003-09-12 20:42:57 +0000203
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000204 std::string Error;
205 bool Failure = D.run(Error);
206 if (!Error.empty()) {
207 errs() << Error;
208 return 1;
Chris Lattnereb89dba2004-02-18 17:32:54 +0000209 }
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000210 return Failure;
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000211}