blob: 41156f92ce8c36d54bf0fd1bc2d05ae727958656 [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"
Reid Spencera3223662006-08-21 05:34:03 +000018#include "llvm/LinkAllPasses.h"
Owen Anderson6773d382009-07-01 16:58:40 +000019#include "llvm/LLVMContext.h"
Chris Lattner73a6bdd2002-11-20 22:28:10 +000020#include "llvm/Support/PassNameParser.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000021#include "llvm/Support/CommandLine.h"
Chris Lattner76d46322006-12-06 01:18:01 +000022#include "llvm/Support/ManagedStatic.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000023#include "llvm/Support/PluginLoader.h"
Chris Lattnere3fc2d12009-03-06 05:34:10 +000024#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbar73699512009-07-20 07:01:01 +000025#include "llvm/Support/StandardPasses.h"
Reid Spencera079b692004-12-27 06:18:02 +000026#include "llvm/System/Process.h"
Chris Lattner278f5152004-05-27 05:41:36 +000027#include "llvm/System/Signals.h"
Jeffrey Yasskin71bd0f42010-03-19 00:09:28 +000028#include "llvm/System/Valgrind.h"
Reid Spencerc1588f62006-06-07 23:06:50 +000029#include "llvm/LinkAllVMCore.h"
Brian Gaeke960707c2003-11-11 22:41:34 +000030using namespace llvm;
31
Patrick Jenkinsc46c0382006-08-15 16:40:49 +000032static cl::opt<bool>
Dan Gohman02d2bc82008-02-18 17:15:45 +000033FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
Patrick Jenkinsc46c0382006-08-15 16:40:49 +000034 "on program to find bugs"), cl::init(false));
Reid Spencer842118c2005-12-22 20:02:55 +000035
Chris Lattner73a6bdd2002-11-20 22:28:10 +000036static cl::list<std::string>
37InputFilenames(cl::Positional, cl::OneOrMore,
38 cl::desc("<input llvm ll/bc files>"));
39
Chris Lattner20ec1652006-06-13 03:10:48 +000040static cl::opt<unsigned>
41TimeoutValue("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"));
44
Jeffrey Yasskin71bd0f42010-03-19 00:09:28 +000045static cl::opt<int>
46MemoryLimit("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)."));
49
50static cl::opt<bool>
51UseValgrind("enable-valgrind",
52 cl::desc("Run optimizations through valgrind"));
Anton Korobeynikovd01defe2007-02-16 19:11:07 +000053
Chris Lattner73a6bdd2002-11-20 22:28:10 +000054// The AnalysesList is automatically populated with registered Passes by the
55// PassNameParser.
56//
Owen Anderson81781222010-07-20 08:26:15 +000057static cl::list<const PassInfo*, bool, PassNameParser>
Misha Brukmand792c9b2003-07-24 18:17:43 +000058PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
Chris Lattner73a6bdd2002-11-20 22:28:10 +000059
Daniel Dunbar73699512009-07-20 07:01:01 +000060static cl::opt<bool>
61StandardCompileOpts("std-compile-opts",
62 cl::desc("Include the standard compile time optimizations"));
63
64static cl::opt<bool>
65StandardLinkOpts("std-link-opts",
66 cl::desc("Include the standard link time optimizations"));
67
Daniel Dunbar8575a602009-08-18 03:35:57 +000068static cl::opt<std::string>
69OverrideTriple("mtriple", cl::desc("Override target triple for module"));
70
Chris Lattnerbeb01fa2005-08-02 02:16:17 +000071/// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
72bool llvm::BugpointIsInterrupted = false;
73
74static void BugpointInterruptFunction() {
75 BugpointIsInterrupted = true;
76}
77
Daniel Dunbar73699512009-07-20 07:01:01 +000078// Hack to capture a pass list.
79namespace {
80 class AddToDriver : public PassManager {
81 BugDriver &D;
82 public:
83 AddToDriver(BugDriver &_D) : D(_D) {}
84
85 virtual void add(Pass *P) {
Owen Anderson81781222010-07-20 08:26:15 +000086 const PassInfo *PI = P->getPassInfo();
Daniel Dunbar73699512009-07-20 07:01:01 +000087 D.addPasses(&PI, &PI + 1);
88 }
89 };
90}
91
Chris Lattner73a6bdd2002-11-20 22:28:10 +000092int main(int argc, char **argv) {
Chris Lattnere3fc2d12009-03-06 05:34:10 +000093 llvm::sys::PrintStackTraceOnErrorSignal();
94 llvm::PrettyStackTraceProgram X(argc, argv);
95 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Chris Lattner849619e2003-10-18 21:55:35 +000096 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman2c6a8212007-10-08 15:45:12 +000097 "LLVM automatic testcase reducer. See\nhttp://"
Chris Lattnerc3aa8f72009-02-07 18:56:30 +000098 "llvm.org/cmds/bugpoint.html"
Chris Lattner849619e2003-10-18 21:55:35 +000099 " for more information.\n");
Chris Lattnerbeb01fa2005-08-02 02:16:17 +0000100 sys::SetInterruptFunction(BugpointInterruptFunction);
Owen Anderson6773d382009-07-01 16:58:40 +0000101
Owen Anderson19251ec2009-07-15 22:16:10 +0000102 LLVMContext& Context = getGlobalContext();
Daniel Dunbar8575a602009-08-18 03:35:57 +0000103 // If we have an override, set it and then track the triple we want Modules
104 // to use.
Chris Lattneredcbfe82009-08-31 03:22:35 +0000105 if (!OverrideTriple.empty()) {
Daniel Dunbar8575a602009-08-18 03:35:57 +0000106 TargetTriple.setTriple(OverrideTriple);
Chris Lattneredcbfe82009-08-31 03:22:35 +0000107 outs() << "Override triple set to '" << OverrideTriple << "'\n";
108 }
Daniel Dunbar8575a602009-08-18 03:35:57 +0000109
Jeffrey Yasskin71bd0f42010-03-19 00:09:28 +0000110 if (MemoryLimit < 0) {
111 // Set the default MemoryLimit. Be sure to update the flag's description if
112 // you change this.
113 if (sys::RunningOnValgrind() || UseValgrind)
114 MemoryLimit = 800;
115 else
116 MemoryLimit = 100;
117 }
118
Rafael Espindola86d70952010-08-05 15:25:38 +0000119 BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit,
Jeffrey Yasskin71bd0f42010-03-19 00:09:28 +0000120 UseValgrind, Context);
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000121 if (D.addSources(InputFilenames)) return 1;
Daniel Dunbar8575a602009-08-18 03:35:57 +0000122
Daniel Dunbar73699512009-07-20 07:01:01 +0000123 AddToDriver PM(D);
124 if (StandardCompileOpts) {
125 createStandardModulePasses(&PM, 3,
126 /*OptimizeSize=*/ false,
127 /*UnitAtATime=*/ true,
128 /*UnrollLoops=*/ true,
129 /*SimplifyLibCalls=*/ true,
130 /*HaveExceptions=*/ true,
131 createFunctionInliningPass());
132 }
133
134 if (StandardLinkOpts)
Dan Gohmana30c0a12009-07-27 23:23:47 +0000135 createStandardLTOPasses(&PM, /*Internalize=*/true,
Daniel Dunbar73699512009-07-20 07:01:01 +0000136 /*RunInliner=*/true,
137 /*VerifyEach=*/false);
138
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000139 D.addPasses(PassList.begin(), PassList.end());
140
Misha Brukman46e18d42003-09-12 20:42:57 +0000141 // Bugpoint has the ability of generating a plethora of core files, so to
Reid Spencer7c16caa2004-09-01 22:55:40 +0000142 // avoid filling up the disk, we prevent it
Reid Spencera079b692004-12-27 06:18:02 +0000143 sys::Process::PreventCoreFiles();
Misha Brukman46e18d42003-09-12 20:42:57 +0000144
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000145 std::string Error;
146 bool Failure = D.run(Error);
147 if (!Error.empty()) {
148 errs() << Error;
149 return 1;
Chris Lattnereb89dba2004-02-18 17:32:54 +0000150 }
Nick Lewycky6ba630b2010-04-12 05:08:25 +0000151 return Failure;
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000152}