Merge System into Support.
[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/Support/Process.h"
27 #include "llvm/Support/Signals.h"
28 #include "llvm/Support/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 void *ID = P->getPassID();
87       const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
88       D.addPass(PI->getPassArgument());
89     }
90   };
91 }
92
93 int main(int argc, char **argv) {
94   llvm::sys::PrintStackTraceOnErrorSignal();
95   llvm::PrettyStackTraceProgram X(argc, argv);
96   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
97   
98   // Initialize passes
99   PassRegistry &Registry = *PassRegistry::getPassRegistry();
100   initializeCore(Registry);
101   initializeScalarOpts(Registry);
102   initializeIPO(Registry);
103   initializeAnalysis(Registry);
104   initializeIPA(Registry);
105   initializeTransformUtils(Registry);
106   initializeInstCombine(Registry);
107   initializeInstrumentation(Registry);
108   initializeTarget(Registry);
109   
110   cl::ParseCommandLineOptions(argc, argv,
111                               "LLVM automatic testcase reducer. See\nhttp://"
112                               "llvm.org/cmds/bugpoint.html"
113                               " for more information.\n");
114   sys::SetInterruptFunction(BugpointInterruptFunction);
115
116   LLVMContext& Context = getGlobalContext();
117   // If we have an override, set it and then track the triple we want Modules
118   // to use.
119   if (!OverrideTriple.empty()) {
120     TargetTriple.setTriple(Triple::normalize(OverrideTriple));
121     outs() << "Override triple set to '" << TargetTriple.getTriple() << "'\n";
122   }
123
124   if (MemoryLimit < 0) {
125     // Set the default MemoryLimit.  Be sure to update the flag's description if
126     // you change this.
127     if (sys::RunningOnValgrind() || UseValgrind)
128       MemoryLimit = 800;
129     else
130       MemoryLimit = 100;
131   }
132
133   BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit,
134               UseValgrind, Context);
135   if (D.addSources(InputFilenames)) return 1;
136   
137   AddToDriver PM(D);
138   if (StandardCompileOpts) {
139     createStandardModulePasses(&PM, 3,
140                                /*OptimizeSize=*/ false,
141                                /*UnitAtATime=*/ true,
142                                /*UnrollLoops=*/ true,
143                                /*SimplifyLibCalls=*/ true,
144                                /*HaveExceptions=*/ true,
145                                createFunctionInliningPass());
146   }
147       
148   if (StandardLinkOpts)
149     createStandardLTOPasses(&PM, /*Internalize=*/true,
150                             /*RunInliner=*/true,
151                             /*VerifyEach=*/false);
152
153
154   for (std::vector<const PassInfo*>::iterator I = PassList.begin(),
155          E = PassList.end();
156        I != E; ++I) {
157     const PassInfo* PI = *I;
158     D.addPass(PI->getPassArgument());
159   }
160
161   // Bugpoint has the ability of generating a plethora of core files, so to
162   // avoid filling up the disk, we prevent it
163   sys::Process::PreventCoreFiles();
164
165   std::string Error;
166   bool Failure = D.run(Error);
167   if (!Error.empty()) {
168     errs() << Error;
169     return 1;
170   }
171   return Failure;
172 }