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/IR/LegacyPassManager.h"
20 #include "llvm/IR/LegacyPassNameParser.h"
21 #include "llvm/LinkAllIR.h"
22 #include "llvm/LinkAllPasses.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/ManagedStatic.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 400MB (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 StandardLinkOpts("std-link-opts",
67                  cl::desc("Include the standard link time optimizations"));
68 
69 static cl::opt<bool>
70 OptLevelO1("O1",
71            cl::desc("Optimization level 1. Identical to 'opt -O1'"));
72 
73 static cl::opt<bool>
74 OptLevelO2("O2",
75            cl::desc("Optimization level 2. Identical to 'opt -O2'"));
76 
77 static cl::opt<bool>
78 OptLevelOs("Os",
79            cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os"));
80 
81 static cl::opt<bool>
82 OptLevelO3("O3",
83            cl::desc("Optimization level 3. Identical to 'opt -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 legacy::FunctionPassManager {
100     BugDriver &D;
101   public:
102     AddToDriver(BugDriver &_D) : FunctionPassManager(nullptr), D(_D) {}
103 
104     void add(Pass *P) override {
105       const void *ID = P->getPassID();
106       const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
107       D.addPass(PI->getPassArgument());
108     }
109   };
110 }
111 
112 #ifdef LINK_POLLY_INTO_TOOLS
113 namespace polly {
114 void initializePollyPasses(llvm::PassRegistry &Registry);
115 }
116 #endif
117 
118 int main(int argc, char **argv) {
119 #ifndef DEBUG_BUGPOINT
120   llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
121   llvm::PrettyStackTraceProgram X(argc, argv);
122   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
123 #endif
124 
125   // Initialize passes
126   PassRegistry &Registry = *PassRegistry::getPassRegistry();
127   initializeCore(Registry);
128   initializeScalarOpts(Registry);
129   initializeObjCARCOpts(Registry);
130   initializeVectorization(Registry);
131   initializeIPO(Registry);
132   initializeAnalysis(Registry);
133   initializeTransformUtils(Registry);
134   initializeInstCombine(Registry);
135   initializeInstrumentation(Registry);
136   initializeTarget(Registry);
137 
138 #ifdef LINK_POLLY_INTO_TOOLS
139   polly::initializePollyPasses(Registry);
140 #endif
141 
142   cl::ParseCommandLineOptions(argc, argv,
143                               "LLVM automatic testcase reducer. See\nhttp://"
144                               "llvm.org/cmds/bugpoint.html"
145                               " for more information.\n");
146 #ifndef DEBUG_BUGPOINT
147   sys::SetInterruptFunction(BugpointInterruptFunction);
148 #endif
149 
150   LLVMContext Context;
151   // If we have an override, set it and then track the triple we want Modules
152   // to use.
153   if (!OverrideTriple.empty()) {
154     TargetTriple.setTriple(Triple::normalize(OverrideTriple));
155     outs() << "Override triple set to '" << TargetTriple.getTriple() << "'\n";
156   }
157 
158   if (MemoryLimit < 0) {
159     // Set the default MemoryLimit.  Be sure to update the flag's description if
160     // you change this.
161     if (sys::RunningOnValgrind() || UseValgrind)
162       MemoryLimit = 800;
163     else
164       MemoryLimit = 400;
165   }
166 
167   BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit,
168               UseValgrind, Context);
169   if (D.addSources(InputFilenames)) return 1;
170 
171   AddToDriver PM(D);
172 
173   if (StandardLinkOpts) {
174     PassManagerBuilder Builder;
175     Builder.Inliner = createFunctionInliningPass();
176     Builder.populateLTOPassManager(PM);
177   }
178 
179   if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
180     PassManagerBuilder Builder;
181     if (OptLevelO1)
182       Builder.Inliner = createAlwaysInlinerPass();
183     else if (OptLevelOs || OptLevelO2)
184       Builder.Inliner = createFunctionInliningPass(2, OptLevelOs ? 1 : 0);
185     else
186       Builder.Inliner = createFunctionInliningPass(275);
187     Builder.populateFunctionPassManager(PM);
188     Builder.populateModulePassManager(PM);
189   }
190 
191   for (const PassInfo *PI : PassList)
192     D.addPass(PI->getPassArgument());
193 
194   // Bugpoint has the ability of generating a plethora of core files, so to
195   // avoid filling up the disk, we prevent it
196 #ifndef DEBUG_BUGPOINT
197   sys::Process::PreventCoreFiles();
198 #endif
199 
200   std::string Error;
201   bool Failure = D.run(Error);
202   if (!Error.empty()) {
203     errs() << Error;
204     return 1;
205   }
206   return Failure;
207 }
208