blob: e14f31e67d8517b540beecb8c75183038e9791d9 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- bugpoint.cpp - The LLVM Bugpoint utility ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5f5a5732007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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"
Owen Anderson25209b42009-07-01 16:58:40 +000019#include "llvm/LLVMContext.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/Support/PassNameParser.h"
21#include "llvm/Support/CommandLine.h"
22#include "llvm/Support/ManagedStatic.h"
23#include "llvm/Support/PluginLoader.h"
Chris Lattnere6012df2009-03-06 05:34:10 +000024#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbard94e4672009-07-20 07:01:01 +000025#include "llvm/Support/StandardPasses.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/System/Process.h"
27#include "llvm/System/Signals.h"
Jeffrey Yasskin13baf9e2010-03-19 00:09:28 +000028#include "llvm/System/Valgrind.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029#include "llvm/LinkAllVMCore.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030using namespace llvm;
31
32// AsChild - Specifies that this invocation of bugpoint is being generated
33// from a parent process. It is not intended to be used by users so the
34// option is hidden.
35static cl::opt<bool>
36AsChild("as-child", cl::desc("Run bugpoint as child process"),
37 cl::ReallyHidden);
38
39static cl::opt<bool>
Dan Gohmand56bb2f2008-02-18 17:15:45 +000040FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041 "on program to find bugs"), cl::init(false));
42
43static cl::list<std::string>
44InputFilenames(cl::Positional, cl::OneOrMore,
45 cl::desc("<input llvm ll/bc files>"));
46
47static cl::opt<unsigned>
48TimeoutValue("timeout", cl::init(300), cl::value_desc("seconds"),
49 cl::desc("Number of seconds program is allowed to run before it "
50 "is killed (default is 300s), 0 disables timeout"));
51
Jeffrey Yasskin13baf9e2010-03-19 00:09:28 +000052static cl::opt<int>
53MemoryLimit("mlimit", cl::init(-1), cl::value_desc("MBytes"),
54 cl::desc("Maximum amount of memory to use. 0 disables check."
55 " Defaults to 100MB (800MB under valgrind)."));
56
57static cl::opt<bool>
58UseValgrind("enable-valgrind",
59 cl::desc("Run optimizations through valgrind"));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060
61// The AnalysesList is automatically populated with registered Passes by the
62// PassNameParser.
63//
64static cl::list<const PassInfo*, bool, PassNameParser>
65PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
66
Daniel Dunbard94e4672009-07-20 07:01:01 +000067static cl::opt<bool>
68StandardCompileOpts("std-compile-opts",
69 cl::desc("Include the standard compile time optimizations"));
70
71static cl::opt<bool>
72StandardLinkOpts("std-link-opts",
73 cl::desc("Include the standard link time optimizations"));
74
Daniel Dunbard8590af2009-08-18 03:35:57 +000075static cl::opt<std::string>
76OverrideTriple("mtriple", cl::desc("Override target triple for module"));
77
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078/// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
79bool llvm::BugpointIsInterrupted = false;
80
81static void BugpointInterruptFunction() {
82 BugpointIsInterrupted = true;
83}
84
Daniel Dunbard94e4672009-07-20 07:01:01 +000085// Hack to capture a pass list.
86namespace {
87 class AddToDriver : public PassManager {
88 BugDriver &D;
89 public:
90 AddToDriver(BugDriver &_D) : D(_D) {}
91
92 virtual void add(Pass *P) {
93 const PassInfo *PI = P->getPassInfo();
94 D.addPasses(&PI, &PI + 1);
95 }
96 };
97}
98
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099int main(int argc, char **argv) {
Chris Lattnere6012df2009-03-06 05:34:10 +0000100 llvm::sys::PrintStackTraceOnErrorSignal();
101 llvm::PrettyStackTraceProgram X(argc, argv);
102 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman6099df82007-10-08 15:45:12 +0000104 "LLVM automatic testcase reducer. See\nhttp://"
Chris Lattnerc95317e2009-02-07 18:56:30 +0000105 "llvm.org/cmds/bugpoint.html"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 " for more information.\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 sys::SetInterruptFunction(BugpointInterruptFunction);
Owen Anderson25209b42009-07-01 16:58:40 +0000108
Owen Andersone84b8b32009-07-15 22:16:10 +0000109 LLVMContext& Context = getGlobalContext();
Daniel Dunbard8590af2009-08-18 03:35:57 +0000110 // If we have an override, set it and then track the triple we want Modules
111 // to use.
Chris Lattner3e30f912009-08-31 03:22:35 +0000112 if (!OverrideTriple.empty()) {
Daniel Dunbard8590af2009-08-18 03:35:57 +0000113 TargetTriple.setTriple(OverrideTriple);
Chris Lattner3e30f912009-08-31 03:22:35 +0000114 outs() << "Override triple set to '" << OverrideTriple << "'\n";
115 }
Daniel Dunbard8590af2009-08-18 03:35:57 +0000116
Jeffrey Yasskin13baf9e2010-03-19 00:09:28 +0000117 if (MemoryLimit < 0) {
118 // Set the default MemoryLimit. Be sure to update the flag's description if
119 // you change this.
120 if (sys::RunningOnValgrind() || UseValgrind)
121 MemoryLimit = 800;
122 else
123 MemoryLimit = 100;
124 }
125
126 BugDriver D(argv[0], AsChild, FindBugs, TimeoutValue, MemoryLimit,
127 UseValgrind, Context);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128 if (D.addSources(InputFilenames)) return 1;
Daniel Dunbard8590af2009-08-18 03:35:57 +0000129
Daniel Dunbard94e4672009-07-20 07:01:01 +0000130 AddToDriver PM(D);
131 if (StandardCompileOpts) {
132 createStandardModulePasses(&PM, 3,
133 /*OptimizeSize=*/ false,
134 /*UnitAtATime=*/ true,
135 /*UnrollLoops=*/ true,
136 /*SimplifyLibCalls=*/ true,
137 /*HaveExceptions=*/ true,
138 createFunctionInliningPass());
139 }
140
141 if (StandardLinkOpts)
Dan Gohman9f723132009-07-27 23:23:47 +0000142 createStandardLTOPasses(&PM, /*Internalize=*/true,
Daniel Dunbard94e4672009-07-20 07:01:01 +0000143 /*RunInliner=*/true,
144 /*VerifyEach=*/false);
145
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 D.addPasses(PassList.begin(), PassList.end());
147
148 // Bugpoint has the ability of generating a plethora of core files, so to
149 // avoid filling up the disk, we prevent it
150 sys::Process::PreventCoreFiles();
151
152 try {
153 return D.run();
154 } catch (ToolExecutionError &TEE) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000155 errs() << "Tool execution error: " << TEE.what() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 } catch (const std::string& msg) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000157 errs() << argv[0] << ": " << msg << "\n";
Daniel Dunbar04de3242009-08-07 20:50:09 +0000158 } catch (const std::bad_alloc&) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000159 errs() << "Oh no, a bugpoint process ran out of memory!\n"
160 "To increase the allocation limits for bugpoint child\n"
161 "processes, use the -mlimit option.\n";
Dan Gohman27679392009-04-27 01:30:37 +0000162 } catch (const std::exception &e) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000163 errs() << "Whoops, a std::exception leaked out of bugpoint: "
164 << e.what() << "\n"
165 << "This is a bug in bugpoint!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 } catch (...) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000167 errs() << "Whoops, an exception leaked out of bugpoint. "
168 << "This is a bug in bugpoint!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 }
170 return 1;
171}