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