blob: f6b7d08455d438f2eadf35aec37123afe6dddc76 [file] [log] [blame]
Chris Lattner94321112003-10-20 17:57:13 +00001//===- bugpoint.cpp - The LLVM Bugpoint utility ---------------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerafade922002-11-20 22:28:10 +00009//
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"
Chris Lattnerf1b20d82006-06-06 22:30:59 +000017#include "ToolRunner.h"
Nico Weber0f38c602018-04-30 14:59:11 +000018#include "llvm/Config/llvm-config.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000019#include "llvm/IR/LLVMContext.h"
Chandler Carruth417c5c12015-02-13 10:01:29 +000020#include "llvm/IR/LegacyPassManager.h"
Chandler Carruthf7591dd2014-03-04 12:32:42 +000021#include "llvm/IR/LegacyPassNameParser.h"
Jakub Staszak446991d2013-01-10 21:56:40 +000022#include "llvm/LinkAllIR.h"
Chandler Carruth90230c82013-01-19 08:03:47 +000023#include "llvm/LinkAllPasses.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/Support/CommandLine.h"
Rui Ueyama0b9d56a2018-04-13 18:26:06 +000025#include "llvm/Support/InitLLVM.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000026#include "llvm/Support/ManagedStatic.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000027#include "llvm/Support/PluginLoader.h"
Chris Lattnercc14d252009-03-06 05:34:10 +000028#include "llvm/Support/PrettyStackTrace.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000029#include "llvm/Support/Process.h"
Tobias Grosser7f3bf012017-06-24 08:09:33 +000030#include "llvm/Support/TargetSelect.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000031#include "llvm/Support/Valgrind.h"
Chandler Carruthb699f7b2016-08-17 02:56:20 +000032#include "llvm/Transforms/IPO/AlwaysInliner.h"
Rafael Espindola3d453ac2011-08-02 21:50:24 +000033#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Devang Patelbc8d5f12011-01-13 19:48:54 +000034
Justin Bogner388e8b92016-09-02 01:21:37 +000035// Enable this macro to debug bugpoint itself.
Devang Patel3f84a452011-01-14 15:55:50 +000036//#define DEBUG_BUGPOINT 1
Devang Patelbc8d5f12011-01-13 19:48:54 +000037
Brian Gaeked0fde302003-11-11 22:41:34 +000038using namespace llvm;
39
NAKAMURA Takumi78713092014-01-15 08:21:38 +000040static cl::opt<bool>
Justin Bogner388e8b92016-09-02 01:21:37 +000041 FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
42 "on program to find bugs"),
43 cl::init(false));
Reid Spencerc4bb0522005-12-22 20:02:55 +000044
Chris Lattnerafade922002-11-20 22:28:10 +000045static cl::list<std::string>
Justin Bogner388e8b92016-09-02 01:21:37 +000046 InputFilenames(cl::Positional, cl::OneOrMore,
47 cl::desc("<input llvm ll/bc files>"));
Chris Lattnerafade922002-11-20 22:28:10 +000048
Justin Bogner388e8b92016-09-02 01:21:37 +000049static cl::opt<unsigned> TimeoutValue(
50 "timeout", cl::init(300), cl::value_desc("seconds"),
51 cl::desc("Number of seconds program is allowed to run before it "
52 "is killed (default is 300s), 0 disables timeout"));
Chris Lattner9686ae72006-06-13 03:10:48 +000053
Vitaly Bukafe70e7d2017-09-01 01:47:34 +000054static cl::opt<int> MemoryLimit(
55 "mlimit", cl::init(-1), cl::value_desc("MBytes"),
56 cl::desc("Maximum amount of memory to use. 0 disables check. Defaults to "
57 "400MB (800MB under valgrind, 0 with sanitizers)."));
Jeffrey Yasskinc3e68592010-03-19 00:09:28 +000058
59static cl::opt<bool>
Justin Bogner388e8b92016-09-02 01:21:37 +000060 UseValgrind("enable-valgrind",
61 cl::desc("Run optimizations through valgrind"));
Anton Korobeynikov9ba8a762007-02-16 19:11:07 +000062
Chris Lattnerafade922002-11-20 22:28:10 +000063// The AnalysesList is automatically populated with registered Passes by the
64// PassNameParser.
65//
Justin Bogner388e8b92016-09-02 01:21:37 +000066static cl::list<const PassInfo *, bool, PassNameParser>
67 PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
Chris Lattnerafade922002-11-20 22:28:10 +000068
Daniel Dunbar14886702009-07-20 07:01:01 +000069static cl::opt<bool>
Justin Bogner388e8b92016-09-02 01:21:37 +000070 StandardLinkOpts("std-link-opts",
71 cl::desc("Include the standard link time optimizations"));
Daniel Dunbar14886702009-07-20 07:01:01 +000072
Eli Friedmanbe2d1232011-06-06 22:45:46 +000073static cl::opt<bool>
Justin Bogner388e8b92016-09-02 01:21:37 +000074 OptLevelO1("O1", cl::desc("Optimization level 1. Identical to 'opt -O1'"));
Eli Friedmanbe2d1232011-06-06 22:45:46 +000075
76static cl::opt<bool>
Justin Bogner388e8b92016-09-02 01:21:37 +000077 OptLevelO2("O2", cl::desc("Optimization level 2. Identical to 'opt -O2'"));
78
79static cl::opt<bool> OptLevelOs(
80 "Os",
81 cl::desc(
82 "Like -O2 with extra optimizations for size. Similar to clang -Os"));
Eli Friedmanbe2d1232011-06-06 22:45:46 +000083
84static cl::opt<bool>
Justin Bogner388e8b92016-09-02 01:21:37 +000085 OptLevelO3("O3", cl::desc("Optimization level 3. Identical to 'opt -O3'"));
Eli Friedmanbe2d1232011-06-06 22:45:46 +000086
Daniel Dunbarca740962009-08-18 03:35:57 +000087static cl::opt<std::string>
Justin Bogner388e8b92016-09-02 01:21:37 +000088 OverrideTriple("mtriple", cl::desc("Override target triple for module"));
Daniel Dunbarca740962009-08-18 03:35:57 +000089
Chris Lattnerf9aaae02005-08-02 02:16:17 +000090/// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
91bool llvm::BugpointIsInterrupted = false;
92
Devang Patelbc8d5f12011-01-13 19:48:54 +000093#ifndef DEBUG_BUGPOINT
Justin Bogner388e8b92016-09-02 01:21:37 +000094static void BugpointInterruptFunction() { BugpointIsInterrupted = true; }
Devang Patelbc8d5f12011-01-13 19:48:54 +000095#endif
Chris Lattnerf9aaae02005-08-02 02:16:17 +000096
Daniel Dunbar14886702009-07-20 07:01:01 +000097// Hack to capture a pass list.
98namespace {
Justin Bogner388e8b92016-09-02 01:21:37 +000099class AddToDriver : public legacy::FunctionPassManager {
100 BugDriver &D;
NAKAMURA Takumi78713092014-01-15 08:21:38 +0000101
Justin Bogner388e8b92016-09-02 01:21:37 +0000102public:
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};
Daniel Dunbar14886702009-07-20 07:01:01 +0000111}
112
Sebastian Popbeaa95d2014-03-14 04:04:14 +0000113#ifdef LINK_POLLY_INTO_TOOLS
114namespace polly {
115void initializePollyPasses(llvm::PassRegistry &Registry);
116}
117#endif
118
Chris Lattnerafade922002-11-20 22:28:10 +0000119int main(int argc, char **argv) {
Devang Patelbc8d5f12011-01-13 19:48:54 +0000120#ifndef DEBUG_BUGPOINT
Rui Ueyama0b9d56a2018-04-13 18:26:06 +0000121 InitLLVM X(argc, argv);
Devang Patelbc8d5f12011-01-13 19:48:54 +0000122#endif
NAKAMURA Takumi78713092014-01-15 08:21:38 +0000123
Owen Anderson081c34b2010-10-19 17:21:58 +0000124 // Initialize passes
125 PassRegistry &Registry = *PassRegistry::getPassRegistry();
126 initializeCore(Registry);
127 initializeScalarOpts(Registry);
Michael Gottesman24c48982013-01-28 01:35:51 +0000128 initializeObjCARCOpts(Registry);
Hal Finkel0ae25102012-02-07 21:11:12 +0000129 initializeVectorization(Registry);
Owen Anderson081c34b2010-10-19 17:21:58 +0000130 initializeIPO(Registry);
131 initializeAnalysis(Registry);
Owen Anderson081c34b2010-10-19 17:21:58 +0000132 initializeTransformUtils(Registry);
133 initializeInstCombine(Registry);
Craig Topper043b2352018-04-24 00:05:21 +0000134 initializeAggressiveInstCombine(Registry);
Owen Anderson081c34b2010-10-19 17:21:58 +0000135 initializeInstrumentation(Registry);
136 initializeTarget(Registry);
NAKAMURA Takumi78713092014-01-15 08:21:38 +0000137
Sebastian Popbeaa95d2014-03-14 04:04:14 +0000138#ifdef LINK_POLLY_INTO_TOOLS
139 polly::initializePollyPasses(Registry);
140#endif
141
Tobias Grosser7f3bf012017-06-24 08:09:33 +0000142 if (std::getenv("bar") == (char*) -1) {
143 InitializeAllTargets();
144 InitializeAllTargetMCs();
145 InitializeAllAsmPrinters();
146 InitializeAllAsmParsers();
Tobias Grosser7f3bf012017-06-24 08:09:33 +0000147 }
148
Chris Lattner670406d2003-10-18 21:55:35 +0000149 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman82a13c92007-10-08 15:45:12 +0000150 "LLVM automatic testcase reducer. See\nhttp://"
Chris Lattner3a4baf12009-02-07 18:56:30 +0000151 "llvm.org/cmds/bugpoint.html"
Chris Lattner670406d2003-10-18 21:55:35 +0000152 " for more information.\n");
Devang Patelbc8d5f12011-01-13 19:48:54 +0000153#ifndef DEBUG_BUGPOINT
Chris Lattnerf9aaae02005-08-02 02:16:17 +0000154 sys::SetInterruptFunction(BugpointInterruptFunction);
Devang Patelbc8d5f12011-01-13 19:48:54 +0000155#endif
Owen Anderson8b477ed2009-07-01 16:58:40 +0000156
Mehdi Amini8be77072016-04-14 21:59:01 +0000157 LLVMContext Context;
Daniel Dunbarca740962009-08-18 03:35:57 +0000158 // If we have an override, set it and then track the triple we want Modules
159 // to use.
Chris Lattner30611082009-08-31 03:22:35 +0000160 if (!OverrideTriple.empty()) {
Duncan Sands75ebbce2010-08-28 01:30:02 +0000161 TargetTriple.setTriple(Triple::normalize(OverrideTriple));
162 outs() << "Override triple set to '" << TargetTriple.getTriple() << "'\n";
Chris Lattner30611082009-08-31 03:22:35 +0000163 }
Daniel Dunbarca740962009-08-18 03:35:57 +0000164
Jeffrey Yasskinc3e68592010-03-19 00:09:28 +0000165 if (MemoryLimit < 0) {
166 // Set the default MemoryLimit. Be sure to update the flag's description if
167 // you change this.
168 if (sys::RunningOnValgrind() || UseValgrind)
169 MemoryLimit = 800;
170 else
Daniel Sanders18481aa2015-05-05 16:29:40 +0000171 MemoryLimit = 400;
Vitaly Bukafe70e7d2017-09-01 01:47:34 +0000172#if (LLVM_ADDRESS_SANITIZER_BUILD || LLVM_MEMORY_SANITIZER_BUILD || \
173 LLVM_THREAD_SANITIZER_BUILD)
174 // Starting from kernel 4.9 memory allocated with mmap is counted against
175 // RLIMIT_DATA. Sanitizers need to allocate tens of terabytes for shadow.
176 MemoryLimit = 0;
177#endif
Jeffrey Yasskinc3e68592010-03-19 00:09:28 +0000178 }
179
Justin Bogner388e8b92016-09-02 01:21:37 +0000180 BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit, UseValgrind,
181 Context);
182 if (D.addSources(InputFilenames))
183 return 1;
NAKAMURA Takumi78713092014-01-15 08:21:38 +0000184
Daniel Dunbar14886702009-07-20 07:01:01 +0000185 AddToDriver PM(D);
NAKAMURA Takumi78713092014-01-15 08:21:38 +0000186
Chris Lattner817a01f2011-05-22 00:20:07 +0000187 if (StandardLinkOpts) {
188 PassManagerBuilder Builder;
Rafael Espindola0b994a702014-08-21 13:35:30 +0000189 Builder.Inliner = createFunctionInliningPass();
190 Builder.populateLTOPassManager(PM);
Chris Lattner817a01f2011-05-22 00:20:07 +0000191 }
Daniel Dunbar14886702009-07-20 07:01:01 +0000192
Eli Friedmanbe2d1232011-06-06 22:45:46 +0000193 if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
194 PassManagerBuilder Builder;
195 if (OptLevelO1)
Chandler Carruthb699f7b2016-08-17 02:56:20 +0000196 Builder.Inliner = createAlwaysInlinerLegacyPass();
David Majnemer5494cee2016-07-31 19:25:16 +0000197 else if (OptLevelOs || OptLevelO2)
Dehao Chen287fe252017-03-21 19:55:36 +0000198 Builder.Inliner = createFunctionInliningPass(
199 2, OptLevelOs ? 1 : 0, false);
Eli Friedmanbe2d1232011-06-06 22:45:46 +0000200 else
201 Builder.Inliner = createFunctionInliningPass(275);
Eli Friedmanbe2d1232011-06-06 22:45:46 +0000202 Builder.populateFunctionPassManager(PM);
203 Builder.populateModulePassManager(PM);
204 }
Rafael Espindola8261dfe2010-08-08 03:55:08 +0000205
Davide Italianobd133e62015-10-15 01:12:01 +0000206 for (const PassInfo *PI : PassList)
Rafael Espindola8261dfe2010-08-08 03:55:08 +0000207 D.addPass(PI->getPassArgument());
Chris Lattnerafade922002-11-20 22:28:10 +0000208
Justin Bogner388e8b92016-09-02 01:21:37 +0000209// Bugpoint has the ability of generating a plethora of core files, so to
210// avoid filling up the disk, we prevent it
Devang Patelbc8d5f12011-01-13 19:48:54 +0000211#ifndef DEBUG_BUGPOINT
Reid Spencer51163302004-12-27 06:18:02 +0000212 sys::Process::PreventCoreFiles();
Devang Patelbc8d5f12011-01-13 19:48:54 +0000213#endif
Misha Brukman67b36e42003-09-12 20:42:57 +0000214
Justin Bognerd8090ae2016-09-06 17:18:22 +0000215 if (Error E = D.run()) {
216 errs() << toString(std::move(E));
Justin Bogner79a93a62016-09-06 04:45:37 +0000217 return 1;
218 }
Justin Bognerd8090ae2016-09-06 17:18:22 +0000219 return 0;
Chris Lattnerafade922002-11-20 22:28:10 +0000220}