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/InitLLVM.h"
25 #include "llvm/Support/ManagedStatic.h"
26 #include "llvm/Support/PluginLoader.h"
27 #include "llvm/Support/PrettyStackTrace.h"
28 #include "llvm/Support/Process.h"
29 #include "llvm/Support/TargetSelect.h"
30 #include "llvm/Support/Valgrind.h"
31 #include "llvm/Transforms/IPO/AlwaysInliner.h"
32 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
33 
34 // Enable this macro to debug bugpoint itself.
35 //#define DEBUG_BUGPOINT 1
36 
37 using namespace llvm;
38 
39 static cl::opt<bool>
40     FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
41                                    "on program to find bugs"),
42              cl::init(false));
43 
44 static cl::list<std::string>
45     InputFilenames(cl::Positional, cl::OneOrMore,
46                    cl::desc("<input llvm ll/bc files>"));
47 
48 static cl::opt<unsigned> TimeoutValue(
49     "timeout", cl::init(300), cl::value_desc("seconds"),
50     cl::desc("Number of seconds program is allowed to run before it "
51              "is killed (default is 300s), 0 disables timeout"));
52 
53 static cl::opt<int> MemoryLimit(
54     "mlimit", cl::init(-1), cl::value_desc("MBytes"),
55     cl::desc("Maximum amount of memory to use. 0 disables check. Defaults to "
56              "400MB (800MB under valgrind, 0 with sanitizers)."));
57 
58 static cl::opt<bool>
59     UseValgrind("enable-valgrind",
60                 cl::desc("Run optimizations through valgrind"));
61 
62 // The AnalysesList is automatically populated with registered Passes by the
63 // PassNameParser.
64 //
65 static cl::list<const PassInfo *, bool, PassNameParser>
66     PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
67 
68 static cl::opt<bool>
69     StandardLinkOpts("std-link-opts",
70                      cl::desc("Include the standard link time optimizations"));
71 
72 static cl::opt<bool>
73     OptLevelO1("O1", cl::desc("Optimization level 1. Identical to 'opt -O1'"));
74 
75 static cl::opt<bool>
76     OptLevelO2("O2", cl::desc("Optimization level 2. Identical to 'opt -O2'"));
77 
78 static cl::opt<bool> OptLevelOs(
79     "Os",
80     cl::desc(
81         "Like -O2 with extra optimizations for size. Similar to clang -Os"));
82 
83 static cl::opt<bool>
84     OptLevelO3("O3", 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() { BugpointIsInterrupted = true; }
94 #endif
95 
96 // Hack to capture a pass list.
97 namespace {
98 class AddToDriver : public legacy::FunctionPassManager {
99   BugDriver &D;
100 
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   InitLLVM X(argc, argv);
121 #endif
122 
123   // Initialize passes
124   PassRegistry &Registry = *PassRegistry::getPassRegistry();
125   initializeCore(Registry);
126   initializeScalarOpts(Registry);
127   initializeObjCARCOpts(Registry);
128   initializeVectorization(Registry);
129   initializeIPO(Registry);
130   initializeAnalysis(Registry);
131   initializeTransformUtils(Registry);
132   initializeInstCombine(Registry);
133   initializeInstrumentation(Registry);
134   initializeTarget(Registry);
135 
136 #ifdef LINK_POLLY_INTO_TOOLS
137   polly::initializePollyPasses(Registry);
138 #endif
139 
140   if (std::getenv("bar") == (char*) -1) {
141     InitializeAllTargets();
142     InitializeAllTargetMCs();
143     InitializeAllAsmPrinters();
144     InitializeAllAsmParsers();
145   }
146 
147   cl::ParseCommandLineOptions(argc, argv,
148                               "LLVM automatic testcase reducer. See\nhttp://"
149                               "llvm.org/cmds/bugpoint.html"
150                               " for more information.\n");
151 #ifndef DEBUG_BUGPOINT
152   sys::SetInterruptFunction(BugpointInterruptFunction);
153 #endif
154 
155   LLVMContext Context;
156   // If we have an override, set it and then track the triple we want Modules
157   // to use.
158   if (!OverrideTriple.empty()) {
159     TargetTriple.setTriple(Triple::normalize(OverrideTriple));
160     outs() << "Override triple set to '" << TargetTriple.getTriple() << "'\n";
161   }
162 
163   if (MemoryLimit < 0) {
164     // Set the default MemoryLimit.  Be sure to update the flag's description if
165     // you change this.
166     if (sys::RunningOnValgrind() || UseValgrind)
167       MemoryLimit = 800;
168     else
169       MemoryLimit = 400;
170 #if (LLVM_ADDRESS_SANITIZER_BUILD || LLVM_MEMORY_SANITIZER_BUILD ||            \
171      LLVM_THREAD_SANITIZER_BUILD)
172     // Starting from kernel 4.9 memory allocated with mmap is counted against
173     // RLIMIT_DATA. Sanitizers need to allocate tens of terabytes for shadow.
174     MemoryLimit = 0;
175 #endif
176   }
177 
178   BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit, UseValgrind,
179               Context);
180   if (D.addSources(InputFilenames))
181     return 1;
182 
183   AddToDriver PM(D);
184 
185   if (StandardLinkOpts) {
186     PassManagerBuilder Builder;
187     Builder.Inliner = createFunctionInliningPass();
188     Builder.populateLTOPassManager(PM);
189   }
190 
191   if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
192     PassManagerBuilder Builder;
193     if (OptLevelO1)
194       Builder.Inliner = createAlwaysInlinerLegacyPass();
195     else if (OptLevelOs || OptLevelO2)
196       Builder.Inliner = createFunctionInliningPass(
197           2, OptLevelOs ? 1 : 0, false);
198     else
199       Builder.Inliner = createFunctionInliningPass(275);
200     Builder.populateFunctionPassManager(PM);
201     Builder.populateModulePassManager(PM);
202   }
203 
204   for (const PassInfo *PI : PassList)
205     D.addPass(PI->getPassArgument());
206 
207 // Bugpoint has the ability of generating a plethora of core files, so to
208 // avoid filling up the disk, we prevent it
209 #ifndef DEBUG_BUGPOINT
210   sys::Process::PreventCoreFiles();
211 #endif
212 
213   if (Error E = D.run()) {
214     errs() << toString(std::move(E));
215     return 1;
216   }
217   return 0;
218 }
219