41156f92ce8c36d54bf0fd1bc2d05ae727958656
[oota-llvm.git] / tools / bugpoint / bugpoint.cpp
1 //===- bugpoint.cpp - The LLVM Bugpoint utility ---------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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"
19 #include "llvm/LLVMContext.h"
20 #include "llvm/Support/PassNameParser.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/ManagedStatic.h"
23 #include "llvm/Support/PluginLoader.h"
24 #include "llvm/Support/PrettyStackTrace.h"
25 #include "llvm/Support/StandardPasses.h"
26 #include "llvm/System/Process.h"
27 #include "llvm/System/Signals.h"
28 #include "llvm/System/Valgrind.h"
29 #include "llvm/LinkAllVMCore.h"
30 using namespace llvm;
31
32 static cl::opt<bool> 
33 FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
34                                "on program to find bugs"), cl::init(false));
35
36 static cl::list<std::string>
37 InputFilenames(cl::Positional, cl::OneOrMore,
38                cl::desc("<input llvm ll/bc files>"));
39
40 static cl::opt<unsigned>
41 TimeoutValue("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
45 static cl::opt<int>
46 MemoryLimit("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
50 static cl::opt<bool>
51 UseValgrind("enable-valgrind",
52             cl::desc("Run optimizations through valgrind"));
53
54 // The AnalysesList is automatically populated with registered Passes by the
55 // PassNameParser.
56 //
57 static cl::list<const PassInfo*, bool, PassNameParser>
58 PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
59
60 static cl::opt<bool>
61 StandardCompileOpts("std-compile-opts", 
62                    cl::desc("Include the standard compile time optimizations"));
63
64 static cl::opt<bool>
65 StandardLinkOpts("std-link-opts", 
66                  cl::desc("Include the standard link time optimizations"));
67
68 static cl::opt<std::string>
69 OverrideTriple("mtriple", cl::desc("Override target triple for module"));
70
71 /// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
72 bool llvm::BugpointIsInterrupted = false;
73
74 static void BugpointInterruptFunction() {
75   BugpointIsInterrupted = true;
76 }
77
78 // Hack to capture a pass list.
79 namespace {
80   class AddToDriver : public PassManager {
81     BugDriver &D;
82   public:
83     AddToDriver(BugDriver &_D) : D(_D) {}
84     
85     virtual void add(Pass *P) {
86       const PassInfo *PI = P->getPassInfo();
87       D.addPasses(&PI, &PI + 1);
88     }
89   };
90 }
91
92 int main(int argc, char **argv) {
93   llvm::sys::PrintStackTraceOnErrorSignal();
94   llvm::PrettyStackTraceProgram X(argc, argv);
95   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
96   cl::ParseCommandLineOptions(argc, argv,
97                               "LLVM automatic testcase reducer. See\nhttp://"
98                               "llvm.org/cmds/bugpoint.html"
99                               " for more information.\n");
100   sys::SetInterruptFunction(BugpointInterruptFunction);
101
102   LLVMContext& Context = getGlobalContext();
103   // If we have an override, set it and then track the triple we want Modules
104   // to use.
105   if (!OverrideTriple.empty()) {
106     TargetTriple.setTriple(OverrideTriple);
107     outs() << "Override triple set to '" << OverrideTriple << "'\n";
108   }
109
110   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
119   BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit,
120               UseValgrind, Context);
121   if (D.addSources(InputFilenames)) return 1;
122   
123   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)
135     createStandardLTOPasses(&PM, /*Internalize=*/true,
136                             /*RunInliner=*/true,
137                             /*VerifyEach=*/false);
138
139   D.addPasses(PassList.begin(), PassList.end());
140
141   // Bugpoint has the ability of generating a plethora of core files, so to
142   // avoid filling up the disk, we prevent it
143   sys::Process::PreventCoreFiles();
144
145   std::string Error;
146   bool Failure = D.run(Error);
147   if (!Error.empty()) {
148     errs() << Error;
149     return 1;
150   }
151   return Failure;
152 }