]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/llvm/tools/bugpoint/bugpoint.cpp
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / llvm / 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/IR/LLVMContext.h"
19 #include "llvm/LinkAllIR.h"
20 #include "llvm/LinkAllPasses.h"
21 #include "llvm/PassManager.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/ManagedStatic.h"
24 #include "llvm/Support/PassNameParser.h"
25 #include "llvm/Support/PluginLoader.h"
26 #include "llvm/Support/PrettyStackTrace.h"
27 #include "llvm/Support/Process.h"
28 #include "llvm/Support/Signals.h"
29 #include "llvm/Support/Valgrind.h"
30 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
31
32 //Enable this macro to debug bugpoint itself.
33 //#define DEBUG_BUGPOINT 1
34
35 using namespace llvm;
36
37 static cl::opt<bool> 
38 FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
39                                "on program to find bugs"), cl::init(false));
40
41 static cl::list<std::string>
42 InputFilenames(cl::Positional, cl::OneOrMore,
43                cl::desc("<input llvm ll/bc files>"));
44
45 static cl::opt<unsigned>
46 TimeoutValue("timeout", cl::init(300), cl::value_desc("seconds"),
47              cl::desc("Number of seconds program is allowed to run before it "
48                       "is killed (default is 300s), 0 disables timeout"));
49
50 static cl::opt<int>
51 MemoryLimit("mlimit", cl::init(-1), cl::value_desc("MBytes"),
52              cl::desc("Maximum amount of memory to use. 0 disables check."
53                       " Defaults to 100MB (800MB under valgrind)."));
54
55 static cl::opt<bool>
56 UseValgrind("enable-valgrind",
57             cl::desc("Run optimizations through valgrind"));
58
59 // The AnalysesList is automatically populated with registered Passes by the
60 // PassNameParser.
61 //
62 static cl::list<const PassInfo*, bool, PassNameParser>
63 PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
64
65 static cl::opt<bool>
66 StandardCompileOpts("std-compile-opts", 
67                    cl::desc("Include the standard compile time optimizations"));
68
69 static cl::opt<bool>
70 StandardLinkOpts("std-link-opts", 
71                  cl::desc("Include the standard link time optimizations"));
72
73 static cl::opt<bool>
74 OptLevelO1("O1",
75            cl::desc("Optimization level 1. Similar to llvm-gcc -O1"));
76
77 static cl::opt<bool>
78 OptLevelO2("O2",
79            cl::desc("Optimization level 2. Similar to llvm-gcc -O2"));
80
81 static cl::opt<bool>
82 OptLevelO3("O3",
83            cl::desc("Optimization level 3. Similar to llvm-gcc -O3"));
84
85 static cl::opt<std::string>
86 OverrideTriple("mtriple", cl::desc("Override target triple for module"));
87
88 /// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
89 bool llvm::BugpointIsInterrupted = false;
90
91 #ifndef DEBUG_BUGPOINT
92 static void BugpointInterruptFunction() {
93   BugpointIsInterrupted = true;
94 }
95 #endif
96
97 // Hack to capture a pass list.
98 namespace {
99   class AddToDriver : public FunctionPassManager {
100     BugDriver &D;
101   public:
102     AddToDriver(BugDriver &_D) : FunctionPassManager(0), D(_D) {}
103     
104     virtual void add(Pass *P) {
105       const void *ID = P->getPassID();
106       const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
107       D.addPass(PI->getPassArgument());
108     }
109   };
110 }
111
112 int main(int argc, char **argv) {
113 #ifndef DEBUG_BUGPOINT
114   llvm::sys::PrintStackTraceOnErrorSignal();
115   llvm::PrettyStackTraceProgram X(argc, argv);
116   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
117 #endif
118   
119   // Initialize passes
120   PassRegistry &Registry = *PassRegistry::getPassRegistry();
121   initializeCore(Registry);
122   initializeScalarOpts(Registry);
123   initializeObjCARCOpts(Registry);
124   initializeVectorization(Registry);
125   initializeIPO(Registry);
126   initializeAnalysis(Registry);
127   initializeIPA(Registry);
128   initializeTransformUtils(Registry);
129   initializeInstCombine(Registry);
130   initializeInstrumentation(Registry);
131   initializeTarget(Registry);
132   
133   cl::ParseCommandLineOptions(argc, argv,
134                               "LLVM automatic testcase reducer. See\nhttp://"
135                               "llvm.org/cmds/bugpoint.html"
136                               " for more information.\n");
137 #ifndef DEBUG_BUGPOINT
138   sys::SetInterruptFunction(BugpointInterruptFunction);
139 #endif
140
141   LLVMContext& Context = getGlobalContext();
142   // If we have an override, set it and then track the triple we want Modules
143   // to use.
144   if (!OverrideTriple.empty()) {
145     TargetTriple.setTriple(Triple::normalize(OverrideTriple));
146     outs() << "Override triple set to '" << TargetTriple.getTriple() << "'\n";
147   }
148
149   if (MemoryLimit < 0) {
150     // Set the default MemoryLimit.  Be sure to update the flag's description if
151     // you change this.
152     if (sys::RunningOnValgrind() || UseValgrind)
153       MemoryLimit = 800;
154     else
155       MemoryLimit = 100;
156   }
157
158   BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit,
159               UseValgrind, Context);
160   if (D.addSources(InputFilenames)) return 1;
161   
162   AddToDriver PM(D);
163   if (StandardCompileOpts) {
164     PassManagerBuilder Builder;
165     Builder.OptLevel = 3;
166     Builder.Inliner = createFunctionInliningPass();
167     Builder.populateModulePassManager(PM);
168   }
169       
170   if (StandardLinkOpts) {
171     PassManagerBuilder Builder;
172     Builder.populateLTOPassManager(PM, /*Internalize=*/true,
173                                    /*RunInliner=*/true);
174   }
175
176   if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
177     PassManagerBuilder Builder;
178     if (OptLevelO1)
179       Builder.Inliner = createAlwaysInlinerPass();
180     else if (OptLevelO2)
181       Builder.Inliner = createFunctionInliningPass(225);
182     else
183       Builder.Inliner = createFunctionInliningPass(275);
184
185     // Note that although clang/llvm-gcc use two separate passmanagers
186     // here, it shouldn't normally make a difference.
187     Builder.populateFunctionPassManager(PM);
188     Builder.populateModulePassManager(PM);
189   }
190
191   for (std::vector<const PassInfo*>::iterator I = PassList.begin(),
192          E = PassList.end();
193        I != E; ++I) {
194     const PassInfo* PI = *I;
195     D.addPass(PI->getPassArgument());
196   }
197
198   // Bugpoint has the ability of generating a plethora of core files, so to
199   // avoid filling up the disk, we prevent it
200 #ifndef DEBUG_BUGPOINT
201   sys::Process::PreventCoreFiles();
202 #endif
203
204   std::string Error;
205   bool Failure = D.run(Error);
206   if (!Error.empty()) {
207     errs() << Error;
208     return 1;
209   }
210   return Failure;
211 }