blob: 565f3f9a70876864bfcf7e0904e3cbea8bbc1971 [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"
Daniel Dunbar14886702009-07-20 07:01:01 +000025#include "llvm/Support/StandardPasses.h"
Reid Spencer51163302004-12-27 06:18:02 +000026#include "llvm/System/Process.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000027#include "llvm/System/Signals.h"
Reid Spencere3f05612006-06-07 23:06:50 +000028#include "llvm/LinkAllVMCore.h"
Brian Gaeked0fde302003-11-11 22:41:34 +000029using namespace llvm;
30
Reid Spencerc4bb0522005-12-22 20:02:55 +000031// AsChild - Specifies that this invocation of bugpoint is being generated
32// from a parent process. It is not intended to be used by users so the
33// option is hidden.
34static cl::opt<bool>
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +000035AsChild("as-child", cl::desc("Run bugpoint as child process"),
36 cl::ReallyHidden);
37
38static cl::opt<bool>
Dan Gohman39564492008-02-18 17:15:45 +000039FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +000040 "on program to find bugs"), cl::init(false));
Reid Spencerc4bb0522005-12-22 20:02:55 +000041
Chris Lattnerafade922002-11-20 22:28:10 +000042static cl::list<std::string>
43InputFilenames(cl::Positional, cl::OneOrMore,
44 cl::desc("<input llvm ll/bc files>"));
45
Chris Lattner9686ae72006-06-13 03:10:48 +000046static cl::opt<unsigned>
47TimeoutValue("timeout", cl::init(300), cl::value_desc("seconds"),
48 cl::desc("Number of seconds program is allowed to run before it "
49 "is killed (default is 300s), 0 disables timeout"));
50
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +000051static cl::opt<unsigned>
52MemoryLimit("mlimit", cl::init(100), cl::value_desc("MBytes"),
53 cl::desc("Maximum amount of memory to use. 0 disables check."));
54
Chris Lattnerafade922002-11-20 22:28:10 +000055// The AnalysesList is automatically populated with registered Passes by the
56// PassNameParser.
57//
58static cl::list<const PassInfo*, bool, PassNameParser>
Misha Brukman50733362003-07-24 18:17:43 +000059PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
Chris Lattnerafade922002-11-20 22:28:10 +000060
Daniel Dunbar14886702009-07-20 07:01:01 +000061static cl::opt<bool>
62StandardCompileOpts("std-compile-opts",
63 cl::desc("Include the standard compile time optimizations"));
64
65static cl::opt<bool>
66StandardLinkOpts("std-link-opts",
67 cl::desc("Include the standard link time optimizations"));
68
Daniel Dunbarca740962009-08-18 03:35:57 +000069static cl::opt<std::string>
70OverrideTriple("mtriple", cl::desc("Override target triple for module"));
71
Chris Lattnerf9aaae02005-08-02 02:16:17 +000072/// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
73bool llvm::BugpointIsInterrupted = false;
74
75static void BugpointInterruptFunction() {
76 BugpointIsInterrupted = true;
77}
78
Daniel Dunbar14886702009-07-20 07:01:01 +000079// Hack to capture a pass list.
80namespace {
81 class AddToDriver : public PassManager {
82 BugDriver &D;
83 public:
84 AddToDriver(BugDriver &_D) : D(_D) {}
85
86 virtual void add(Pass *P) {
87 const PassInfo *PI = P->getPassInfo();
88 D.addPasses(&PI, &PI + 1);
89 }
90 };
91}
92
Chris Lattnerafade922002-11-20 22:28:10 +000093int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +000094 llvm::sys::PrintStackTraceOnErrorSignal();
95 llvm::PrettyStackTraceProgram X(argc, argv);
96 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Chris Lattner670406d2003-10-18 21:55:35 +000097 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman82a13c92007-10-08 15:45:12 +000098 "LLVM automatic testcase reducer. See\nhttp://"
Chris Lattner3a4baf12009-02-07 18:56:30 +000099 "llvm.org/cmds/bugpoint.html"
Chris Lattner670406d2003-10-18 21:55:35 +0000100 " for more information.\n");
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000101 sys::SetInterruptFunction(BugpointInterruptFunction);
Owen Anderson8b477ed2009-07-01 16:58:40 +0000102
Owen Anderson0d7c6952009-07-15 22:16:10 +0000103 LLVMContext& Context = getGlobalContext();
Daniel Dunbarca740962009-08-18 03:35:57 +0000104 // If we have an override, set it and then track the triple we want Modules
105 // to use.
Chris Lattner30611082009-08-31 03:22:35 +0000106 if (!OverrideTriple.empty()) {
Daniel Dunbarca740962009-08-18 03:35:57 +0000107 TargetTriple.setTriple(OverrideTriple);
Chris Lattner30611082009-08-31 03:22:35 +0000108 outs() << "Override triple set to '" << OverrideTriple << "'\n";
109 }
Daniel Dunbarca740962009-08-18 03:35:57 +0000110
Owen Anderson31895e72009-07-01 21:22:36 +0000111 BugDriver D(argv[0], AsChild, FindBugs, TimeoutValue, MemoryLimit, Context);
Chris Lattnerafade922002-11-20 22:28:10 +0000112 if (D.addSources(InputFilenames)) return 1;
Daniel Dunbarca740962009-08-18 03:35:57 +0000113
Daniel Dunbar14886702009-07-20 07:01:01 +0000114 AddToDriver PM(D);
115 if (StandardCompileOpts) {
116 createStandardModulePasses(&PM, 3,
117 /*OptimizeSize=*/ false,
118 /*UnitAtATime=*/ true,
119 /*UnrollLoops=*/ true,
120 /*SimplifyLibCalls=*/ true,
121 /*HaveExceptions=*/ true,
122 createFunctionInliningPass());
123 }
124
125 if (StandardLinkOpts)
Dan Gohmanb6dec1b2009-07-27 23:23:47 +0000126 createStandardLTOPasses(&PM, /*Internalize=*/true,
Daniel Dunbar14886702009-07-20 07:01:01 +0000127 /*RunInliner=*/true,
128 /*VerifyEach=*/false);
129
Chris Lattnerafade922002-11-20 22:28:10 +0000130 D.addPasses(PassList.begin(), PassList.end());
131
Misha Brukman67b36e42003-09-12 20:42:57 +0000132 // Bugpoint has the ability of generating a plethora of core files, so to
Reid Spencer551ccae2004-09-01 22:55:40 +0000133 // avoid filling up the disk, we prevent it
Reid Spencer51163302004-12-27 06:18:02 +0000134 sys::Process::PreventCoreFiles();
Misha Brukman67b36e42003-09-12 20:42:57 +0000135
Chris Lattner74d45272004-02-18 17:32:54 +0000136 try {
137 return D.run();
Chris Lattner230fef82004-02-18 20:22:11 +0000138 } catch (ToolExecutionError &TEE) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000139 errs() << "Tool execution error: " << TEE.what() << '\n';
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000140 } catch (const std::string& msg) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000141 errs() << argv[0] << ": " << msg << "\n";
Daniel Dunbar482cccd2009-08-07 20:50:09 +0000142 } catch (const std::bad_alloc&) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000143 errs() << "Oh no, a bugpoint process ran out of memory!\n"
144 "To increase the allocation limits for bugpoint child\n"
145 "processes, use the -mlimit option.\n";
Dan Gohman03242052009-04-27 01:30:37 +0000146 } catch (const std::exception &e) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000147 errs() << "Whoops, a std::exception leaked out of bugpoint: "
148 << e.what() << "\n"
149 << "This is a bug in bugpoint!\n";
Chris Lattner74d45272004-02-18 17:32:54 +0000150 } catch (...) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000151 errs() << "Whoops, an exception leaked out of bugpoint. "
152 << "This is a bug in bugpoint!\n";
Chris Lattner74d45272004-02-18 17:32:54 +0000153 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000154 return 1;
Chris Lattnerafade922002-11-20 22:28:10 +0000155}