Straighten out any triple strings passed on the command line before
[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 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   cl::ParseCommandLineOptions(argc, argv,
98                               "LLVM automatic testcase reducer. See\nhttp://"
99                               "llvm.org/cmds/bugpoint.html"
100                               " for more information.\n");
101   sys::SetInterruptFunction(BugpointInterruptFunction);
102
103   LLVMContext& Context = getGlobalContext();
104   // If we have an override, set it and then track the triple we want Modules
105   // to use.
106   if (!OverrideTriple.empty()) {
107     TargetTriple.setTriple(Triple::normalize(OverrideTriple));
108     outs() << "Override triple set to '" << TargetTriple.getTriple() << "'\n";
109   }
110
111   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
120   BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit,
121               UseValgrind, Context);
122   if (D.addSources(InputFilenames)) return 1;
123   
124   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)
136     createStandardLTOPasses(&PM, /*Internalize=*/true,
137                             /*RunInliner=*/true,
138                             /*VerifyEach=*/false);
139
140
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   }
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   std::string Error;
153   bool Failure = D.run(Error);
154   if (!Error.empty()) {
155     errs() << Error;
156     return 1;
157   }
158   return Failure;
159 }