blob: 025beec0bd86b5081e25c66104f450ff278d78f3 [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
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032static cl::opt<bool>
Dan Gohmand56bb2f2008-02-18 17:15:45 +000033FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034 "on program to find bugs"), cl::init(false));
35
36static cl::list<std::string>
37InputFilenames(cl::Positional, cl::OneOrMore,
38 cl::desc("<input llvm ll/bc files>"));
39
40static 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 Yasskin13baf9e2010-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"));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053
54// The AnalysesList is automatically populated with registered Passes by the
55// PassNameParser.
56//
Owen Andersoncb14cb82010-07-20 08:26:15 +000057static cl::list<const PassInfo*, bool, PassNameParser>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
59
Daniel Dunbard94e4672009-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 Dunbard8590af2009-08-18 03:35:57 +000068static cl::opt<std::string>
69OverrideTriple("mtriple", cl::desc("Override target triple for module"));
70
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Dunbard94e4672009-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 Anderson75693222010-08-06 18:33:48 +000086 const void *ID = P->getPassID();
87 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
Rafael Espindola3f1a8f02010-08-08 03:55:08 +000088 D.addPass(PI->getPassArgument());
Daniel Dunbard94e4672009-07-20 07:01:01 +000089 }
90 };
91}
92
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093int main(int argc, char **argv) {
Chris Lattnere6012df2009-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.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman6099df82007-10-08 15:45:12 +000098 "LLVM automatic testcase reducer. See\nhttp://"
Chris Lattnerc95317e2009-02-07 18:56:30 +000099 "llvm.org/cmds/bugpoint.html"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100 " for more information.\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 sys::SetInterruptFunction(BugpointInterruptFunction);
Owen Anderson25209b42009-07-01 16:58:40 +0000102
Owen Andersone84b8b32009-07-15 22:16:10 +0000103 LLVMContext& Context = getGlobalContext();
Daniel Dunbard8590af2009-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 Lattner3e30f912009-08-31 03:22:35 +0000106 if (!OverrideTriple.empty()) {
Daniel Dunbard8590af2009-08-18 03:35:57 +0000107 TargetTriple.setTriple(OverrideTriple);
Chris Lattner3e30f912009-08-31 03:22:35 +0000108 outs() << "Override triple set to '" << OverrideTriple << "'\n";
109 }
Daniel Dunbard8590af2009-08-18 03:35:57 +0000110
Jeffrey Yasskin13baf9e2010-03-19 00:09:28 +0000111 if (MemoryLimit < 0) {
112 // Set the default MemoryLimit. Be sure to update the flag's description if
113 // you change this.
114 if (sys::RunningOnValgrind() || UseValgrind)
115 MemoryLimit = 800;
116 else
117 MemoryLimit = 100;
118 }
119
Rafael Espindola216c6242010-08-07 23:03:21 +0000120 BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit,
Jeffrey Yasskin13baf9e2010-03-19 00:09:28 +0000121 UseValgrind, Context);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 if (D.addSources(InputFilenames)) return 1;
Daniel Dunbard8590af2009-08-18 03:35:57 +0000123
Daniel Dunbard94e4672009-07-20 07:01:01 +0000124 AddToDriver PM(D);
125 if (StandardCompileOpts) {
126 createStandardModulePasses(&PM, 3,
127 /*OptimizeSize=*/ false,
128 /*UnitAtATime=*/ true,
129 /*UnrollLoops=*/ true,
130 /*SimplifyLibCalls=*/ true,
131 /*HaveExceptions=*/ true,
132 createFunctionInliningPass());
133 }
134
135 if (StandardLinkOpts)
Dan Gohman9f723132009-07-27 23:23:47 +0000136 createStandardLTOPasses(&PM, /*Internalize=*/true,
Daniel Dunbard94e4672009-07-20 07:01:01 +0000137 /*RunInliner=*/true,
138 /*VerifyEach=*/false);
139
Rafael Espindola3f1a8f02010-08-08 03:55:08 +0000140
141 for (std::vector<const PassInfo*>::iterator I = PassList.begin(),
142 E = PassList.end();
143 I != E; ++I) {
144 const PassInfo* PI = *I;
145 D.addPass(PI->getPassArgument());
146 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147
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
Nick Lewycky3cefdd32010-04-12 05:08:25 +0000152 std::string Error;
153 bool Failure = D.run(Error);
154 if (!Error.empty()) {
155 errs() << Error;
156 return 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 }
Nick Lewycky3cefdd32010-04-12 05:08:25 +0000158 return Failure;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159}