]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/bugpoint/bugpoint.cpp
Merge ^/head r313896 through r314128.
[FreeBSD/FreeBSD.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/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"),
41              cl::init(false));
42
43 static cl::list<std::string>
44     InputFilenames(cl::Positional, cl::OneOrMore,
45                    cl::desc("<input llvm ll/bc files>"));
46
47 static cl::opt<unsigned> TimeoutValue(
48     "timeout", cl::init(300), cl::value_desc("seconds"),
49     cl::desc("Number of seconds program is allowed to run before it "
50              "is killed (default is 300s), 0 disables timeout"));
51
52 static cl::opt<int>
53     MemoryLimit("mlimit", cl::init(-1), cl::value_desc("MBytes"),
54                 cl::desc("Maximum amount of memory to use. 0 disables check."
55                          " Defaults to 400MB (800MB under valgrind)."));
56
57 static cl::opt<bool>
58     UseValgrind("enable-valgrind",
59                 cl::desc("Run optimizations through valgrind"));
60
61 // The AnalysesList is automatically populated with registered Passes by the
62 // PassNameParser.
63 //
64 static cl::list<const PassInfo *, bool, PassNameParser>
65     PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
66
67 static cl::opt<bool>
68     StandardLinkOpts("std-link-opts",
69                      cl::desc("Include the standard link time optimizations"));
70
71 static cl::opt<bool>
72     OptLevelO1("O1", cl::desc("Optimization level 1. Identical to 'opt -O1'"));
73
74 static cl::opt<bool>
75     OptLevelO2("O2", cl::desc("Optimization level 2. Identical to 'opt -O2'"));
76
77 static cl::opt<bool> OptLevelOs(
78     "Os",
79     cl::desc(
80         "Like -O2 with extra optimizations for size. Similar to clang -Os"));
81
82 static cl::opt<bool>
83     OptLevelO3("O3", 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() { BugpointIsInterrupted = true; }
93 #endif
94
95 // Hack to capture a pass list.
96 namespace {
97 class AddToDriver : public legacy::FunctionPassManager {
98   BugDriver &D;
99
100 public:
101   AddToDriver(BugDriver &_D) : FunctionPassManager(nullptr), D(_D) {}
102
103   void add(Pass *P) override {
104     const void *ID = P->getPassID();
105     const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
106     D.addPass(PI->getPassArgument());
107   }
108 };
109 }
110
111 #ifdef LINK_POLLY_INTO_TOOLS
112 namespace polly {
113 void initializePollyPasses(llvm::PassRegistry &Registry);
114 }
115 #endif
116
117 int main(int argc, char **argv) {
118 #ifndef DEBUG_BUGPOINT
119   llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
120   llvm::PrettyStackTraceProgram X(argc, argv);
121   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
122 #endif
123
124   // Initialize passes
125   PassRegistry &Registry = *PassRegistry::getPassRegistry();
126   initializeCore(Registry);
127   initializeScalarOpts(Registry);
128   initializeObjCARCOpts(Registry);
129   initializeVectorization(Registry);
130   initializeIPO(Registry);
131   initializeAnalysis(Registry);
132   initializeTransformUtils(Registry);
133   initializeInstCombine(Registry);
134   initializeInstrumentation(Registry);
135   initializeTarget(Registry);
136
137 #ifdef LINK_POLLY_INTO_TOOLS
138   polly::initializePollyPasses(Registry);
139 #endif
140
141   cl::ParseCommandLineOptions(argc, argv,
142                               "LLVM automatic testcase reducer. See\nhttp://"
143                               "llvm.org/cmds/bugpoint.html"
144                               " for more information.\n");
145 #ifndef DEBUG_BUGPOINT
146   sys::SetInterruptFunction(BugpointInterruptFunction);
147 #endif
148
149   LLVMContext Context;
150   // If we have an override, set it and then track the triple we want Modules
151   // to use.
152   if (!OverrideTriple.empty()) {
153     TargetTriple.setTriple(Triple::normalize(OverrideTriple));
154     outs() << "Override triple set to '" << TargetTriple.getTriple() << "'\n";
155   }
156
157   if (MemoryLimit < 0) {
158     // Set the default MemoryLimit.  Be sure to update the flag's description if
159     // you change this.
160     if (sys::RunningOnValgrind() || UseValgrind)
161       MemoryLimit = 800;
162     else
163       MemoryLimit = 400;
164   }
165
166   BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit, UseValgrind,
167               Context);
168   if (D.addSources(InputFilenames))
169     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 = createAlwaysInlinerLegacyPass();
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   if (Error E = D.run()) {
201     errs() << toString(std::move(E));
202     return 1;
203   }
204   return 0;
205 }