2003-10-20 19:57:13 +02:00
|
|
|
//===- bugpoint.cpp - The LLVM Bugpoint utility ---------------------------===//
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2003-10-20 19:47:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-11-20 23:28:10 +01:00
|
|
|
//
|
|
|
|
// This program is an automated compiler debugger tool. It is used to narrow
|
|
|
|
// down miscompilations and crash problems to a specific pass in the compiler,
|
|
|
|
// and the specific Module or Function input that is causing the problem.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "BugDriver.h"
|
2006-06-07 00:30:59 +02:00
|
|
|
#include "ToolRunner.h"
|
2018-04-30 16:59:11 +02:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2015-02-13 11:01:29 +01:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2014-03-04 13:32:42 +01:00
|
|
|
#include "llvm/IR/LegacyPassNameParser.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-13 22:15:01 +01:00
|
|
|
#include "llvm/InitializePasses.h"
|
2013-01-10 22:56:40 +01:00
|
|
|
#include "llvm/LinkAllIR.h"
|
2013-01-19 09:03:47 +01:00
|
|
|
#include "llvm/LinkAllPasses.h"
|
2019-06-08 17:37:47 +02:00
|
|
|
#include "llvm/Passes/PassPlugin.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2018-04-13 20:26:06 +02:00
|
|
|
#include "llvm/Support/InitLLVM.h"
|
2006-12-06 02:18:01 +01:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/Support/PluginLoader.h"
|
2009-03-06 06:34:10 +01:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2010-11-29 19:16:10 +01:00
|
|
|
#include "llvm/Support/Process.h"
|
2017-06-24 10:09:33 +02:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2010-11-29 19:16:10 +01:00
|
|
|
#include "llvm/Support/Valgrind.h"
|
[PM] Port the always inliner to the new pass manager in a much more
minimal and boring form than the old pass manager's version.
This pass does the very minimal amount of work necessary to inline
functions declared as always-inline. It doesn't support a wide array of
things that the legacy pass manager did support, but is alse ... about
20 lines of code. So it has that going for it. Notably things this
doesn't support:
- Array alloca merging
- To support the above, bottom-up inlining with careful history
tracking and call graph updates
- DCE of the functions that become dead after this inlining.
- Inlining through call instructions with the always_inline attribute.
Instead, it focuses on inlining functions with that attribute.
The first I've omitted because I'm hoping to just turn it off for the
primary pass manager. If that doesn't pan out, I can add it here but it
will be reasonably expensive to do so.
The second should really be handled by running global-dce after the
inliner. I don't want to re-implement the non-trivial logic necessary to
do comdat-correct DCE of functions. This means the -O0 pipeline will
have to be at least 'always-inline,global-dce', but that seems
reasonable to me. If others are seriously worried about this I'd like to
hear about it and understand why. Again, this is all solveable by
factoring that logic into a utility and calling it here, but I'd like to
wait to do that until there is a clear reason why the existing
pass-based factoring won't work.
The final point is a serious one. I can fairly easily add support for
this, but it seems both costly and a confusing construct for the use
case of the always inliner running at -O0. This attribute can of course
still impact the normal inliner easily (although I find that
a questionable re-use of the same attribute). I've started a discussion
to sort out what semantics we want here and based on that can figure out
if it makes sense ta have this complexity at O0 or not.
One other advantage of this design is that it should be quite a bit
faster due to checking for whether the function is a viable candidate
for inlining exactly once per function instead of doing it for each call
site.
Anyways, hopefully a reasonable starting point for this pass.
Differential Revision: https://reviews.llvm.org/D23299
llvm-svn: 278896
2016-08-17 04:56:20 +02:00
|
|
|
#include "llvm/Transforms/IPO/AlwaysInliner.h"
|
2011-08-02 23:50:24 +02:00
|
|
|
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
|
2011-01-13 20:48:54 +01:00
|
|
|
|
2016-09-02 03:21:37 +02:00
|
|
|
// Enable this macro to debug bugpoint itself.
|
2011-01-14 16:55:50 +01:00
|
|
|
//#define DEBUG_BUGPOINT 1
|
2011-01-13 20:48:54 +01:00
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-01-15 09:21:38 +01:00
|
|
|
static cl::opt<bool>
|
2016-09-02 03:21:37 +02:00
|
|
|
FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
|
|
|
|
"on program to find bugs"),
|
|
|
|
cl::init(false));
|
2005-12-22 21:02:55 +01:00
|
|
|
|
2002-11-20 23:28:10 +01:00
|
|
|
static cl::list<std::string>
|
2016-09-02 03:21:37 +02:00
|
|
|
InputFilenames(cl::Positional, cl::OneOrMore,
|
|
|
|
cl::desc("<input llvm ll/bc files>"));
|
2002-11-20 23:28:10 +01:00
|
|
|
|
2016-09-02 03:21:37 +02:00
|
|
|
static cl::opt<unsigned> TimeoutValue(
|
|
|
|
"timeout", cl::init(300), cl::value_desc("seconds"),
|
|
|
|
cl::desc("Number of seconds program is allowed to run before it "
|
|
|
|
"is killed (default is 300s), 0 disables timeout"));
|
2006-06-13 05:10:48 +02:00
|
|
|
|
2017-09-01 03:47:34 +02:00
|
|
|
static cl::opt<int> MemoryLimit(
|
|
|
|
"mlimit", cl::init(-1), cl::value_desc("MBytes"),
|
|
|
|
cl::desc("Maximum amount of memory to use. 0 disables check. Defaults to "
|
|
|
|
"400MB (800MB under valgrind, 0 with sanitizers)."));
|
2010-03-19 01:09:28 +01:00
|
|
|
|
|
|
|
static cl::opt<bool>
|
2016-09-02 03:21:37 +02:00
|
|
|
UseValgrind("enable-valgrind",
|
|
|
|
cl::desc("Run optimizations through valgrind"));
|
2007-02-16 20:11:07 +01:00
|
|
|
|
2002-11-20 23:28:10 +01:00
|
|
|
// The AnalysesList is automatically populated with registered Passes by the
|
|
|
|
// PassNameParser.
|
|
|
|
//
|
2016-09-02 03:21:37 +02:00
|
|
|
static cl::list<const PassInfo *, bool, PassNameParser>
|
|
|
|
PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
|
2002-11-20 23:28:10 +01:00
|
|
|
|
2009-07-20 09:01:01 +02:00
|
|
|
static cl::opt<bool>
|
2016-09-02 03:21:37 +02:00
|
|
|
StandardLinkOpts("std-link-opts",
|
|
|
|
cl::desc("Include the standard link time optimizations"));
|
2009-07-20 09:01:01 +02:00
|
|
|
|
2011-06-07 00:45:46 +02:00
|
|
|
static cl::opt<bool>
|
2016-09-02 03:21:37 +02:00
|
|
|
OptLevelO1("O1", cl::desc("Optimization level 1. Identical to 'opt -O1'"));
|
2011-06-07 00:45:46 +02:00
|
|
|
|
|
|
|
static cl::opt<bool>
|
2016-09-02 03:21:37 +02:00
|
|
|
OptLevelO2("O2", cl::desc("Optimization level 2. Identical to 'opt -O2'"));
|
2011-06-07 00:45:46 +02:00
|
|
|
|
2016-09-02 03:21:37 +02:00
|
|
|
static cl::opt<bool> OptLevelOs(
|
|
|
|
"Os",
|
|
|
|
cl::desc(
|
|
|
|
"Like -O2 with extra optimizations for size. Similar to clang -Os"));
|
2016-07-31 21:25:16 +02:00
|
|
|
|
2019-09-17 10:14:09 +02:00
|
|
|
static cl::opt<bool>
|
|
|
|
OptLevelOz("Oz",
|
|
|
|
cl::desc("Like -Os but reduces code size further. Similar to clang -Oz"));
|
|
|
|
|
2011-06-07 00:45:46 +02:00
|
|
|
static cl::opt<bool>
|
2016-09-02 03:21:37 +02:00
|
|
|
OptLevelO3("O3", cl::desc("Optimization level 3. Identical to 'opt -O3'"));
|
2011-06-07 00:45:46 +02:00
|
|
|
|
2009-08-18 05:35:57 +02:00
|
|
|
static cl::opt<std::string>
|
2016-09-02 03:21:37 +02:00
|
|
|
OverrideTriple("mtriple", cl::desc("Override target triple for module"));
|
2009-08-18 05:35:57 +02:00
|
|
|
|
2005-08-02 04:16:17 +02:00
|
|
|
/// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
|
|
|
|
bool llvm::BugpointIsInterrupted = false;
|
|
|
|
|
2011-01-13 20:48:54 +01:00
|
|
|
#ifndef DEBUG_BUGPOINT
|
2016-09-02 03:21:37 +02:00
|
|
|
static void BugpointInterruptFunction() { BugpointIsInterrupted = true; }
|
2011-01-13 20:48:54 +01:00
|
|
|
#endif
|
2005-08-02 04:16:17 +02:00
|
|
|
|
2009-07-20 09:01:01 +02:00
|
|
|
// Hack to capture a pass list.
|
|
|
|
namespace {
|
2016-09-02 03:21:37 +02:00
|
|
|
class AddToDriver : public legacy::FunctionPassManager {
|
|
|
|
BugDriver &D;
|
|
|
|
|
|
|
|
public:
|
|
|
|
AddToDriver(BugDriver &_D) : FunctionPassManager(nullptr), D(_D) {}
|
|
|
|
|
|
|
|
void add(Pass *P) override {
|
|
|
|
const void *ID = P->getPassID();
|
|
|
|
const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
|
2020-01-28 20:23:46 +01:00
|
|
|
D.addPass(std::string(PI->getPassArgument()));
|
2016-09-02 03:21:37 +02:00
|
|
|
}
|
|
|
|
};
|
2009-07-20 09:01:01 +02:00
|
|
|
}
|
|
|
|
|
2019-09-17 10:14:09 +02:00
|
|
|
// This routine adds optimization passes based on selected optimization level,
|
|
|
|
// OptLevel.
|
|
|
|
//
|
|
|
|
// OptLevel - Optimization Level
|
|
|
|
static void AddOptimizationPasses(legacy::FunctionPassManager &FPM,
|
|
|
|
unsigned OptLevel,
|
|
|
|
unsigned SizeLevel) {
|
|
|
|
PassManagerBuilder Builder;
|
|
|
|
Builder.OptLevel = OptLevel;
|
|
|
|
Builder.SizeLevel = SizeLevel;
|
|
|
|
|
|
|
|
if (OptLevel > 1)
|
|
|
|
Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false);
|
|
|
|
else
|
|
|
|
Builder.Inliner = createAlwaysInlinerLegacyPass();
|
|
|
|
|
|
|
|
Builder.populateFunctionPassManager(FPM);
|
|
|
|
Builder.populateModulePassManager(FPM);
|
|
|
|
}
|
|
|
|
|
2019-06-08 17:37:47 +02:00
|
|
|
#define HANDLE_EXTENSION(Ext) \
|
|
|
|
llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
|
|
|
|
#include "llvm/Support/Extension.def"
|
2014-03-14 05:04:14 +01:00
|
|
|
|
2002-11-20 23:28:10 +01:00
|
|
|
int main(int argc, char **argv) {
|
2011-01-13 20:48:54 +01:00
|
|
|
#ifndef DEBUG_BUGPOINT
|
2018-04-13 20:26:06 +02:00
|
|
|
InitLLVM X(argc, argv);
|
2011-01-13 20:48:54 +01:00
|
|
|
#endif
|
2014-01-15 09:21:38 +01:00
|
|
|
|
2010-10-19 19:21:58 +02:00
|
|
|
// Initialize passes
|
|
|
|
PassRegistry &Registry = *PassRegistry::getPassRegistry();
|
|
|
|
initializeCore(Registry);
|
|
|
|
initializeScalarOpts(Registry);
|
2013-01-28 02:35:51 +01:00
|
|
|
initializeObjCARCOpts(Registry);
|
2012-02-07 22:11:12 +01:00
|
|
|
initializeVectorization(Registry);
|
2010-10-19 19:21:58 +02:00
|
|
|
initializeIPO(Registry);
|
|
|
|
initializeAnalysis(Registry);
|
|
|
|
initializeTransformUtils(Registry);
|
|
|
|
initializeInstCombine(Registry);
|
2018-04-24 02:05:21 +02:00
|
|
|
initializeAggressiveInstCombine(Registry);
|
2010-10-19 19:21:58 +02:00
|
|
|
initializeInstrumentation(Registry);
|
|
|
|
initializeTarget(Registry);
|
2014-01-15 09:21:38 +01:00
|
|
|
|
2017-06-24 10:09:33 +02:00
|
|
|
if (std::getenv("bar") == (char*) -1) {
|
|
|
|
InitializeAllTargets();
|
|
|
|
InitializeAllTargetMCs();
|
|
|
|
InitializeAllAsmPrinters();
|
|
|
|
InitializeAllAsmParsers();
|
|
|
|
}
|
|
|
|
|
2003-10-18 23:55:35 +02:00
|
|
|
cl::ParseCommandLineOptions(argc, argv,
|
2007-10-08 17:45:12 +02:00
|
|
|
"LLVM automatic testcase reducer. See\nhttp://"
|
2009-02-07 19:56:30 +01:00
|
|
|
"llvm.org/cmds/bugpoint.html"
|
2003-10-18 23:55:35 +02:00
|
|
|
" for more information.\n");
|
2011-01-13 20:48:54 +01:00
|
|
|
#ifndef DEBUG_BUGPOINT
|
2005-08-02 04:16:17 +02:00
|
|
|
sys::SetInterruptFunction(BugpointInterruptFunction);
|
2011-01-13 20:48:54 +01:00
|
|
|
#endif
|
2009-07-01 18:58:40 +02:00
|
|
|
|
2016-04-14 23:59:01 +02:00
|
|
|
LLVMContext Context;
|
2009-08-18 05:35:57 +02:00
|
|
|
// If we have an override, set it and then track the triple we want Modules
|
|
|
|
// to use.
|
2009-08-31 05:22:35 +02:00
|
|
|
if (!OverrideTriple.empty()) {
|
2010-08-28 03:30:02 +02:00
|
|
|
TargetTriple.setTriple(Triple::normalize(OverrideTriple));
|
|
|
|
outs() << "Override triple set to '" << TargetTriple.getTriple() << "'\n";
|
2009-08-31 05:22:35 +02:00
|
|
|
}
|
2009-08-18 05:35:57 +02:00
|
|
|
|
2010-03-19 01:09:28 +01:00
|
|
|
if (MemoryLimit < 0) {
|
|
|
|
// Set the default MemoryLimit. Be sure to update the flag's description if
|
|
|
|
// you change this.
|
|
|
|
if (sys::RunningOnValgrind() || UseValgrind)
|
|
|
|
MemoryLimit = 800;
|
|
|
|
else
|
2015-05-05 18:29:40 +02:00
|
|
|
MemoryLimit = 400;
|
2017-09-01 03:47:34 +02:00
|
|
|
#if (LLVM_ADDRESS_SANITIZER_BUILD || LLVM_MEMORY_SANITIZER_BUILD || \
|
|
|
|
LLVM_THREAD_SANITIZER_BUILD)
|
|
|
|
// Starting from kernel 4.9 memory allocated with mmap is counted against
|
|
|
|
// RLIMIT_DATA. Sanitizers need to allocate tens of terabytes for shadow.
|
|
|
|
MemoryLimit = 0;
|
|
|
|
#endif
|
2010-03-19 01:09:28 +01:00
|
|
|
}
|
|
|
|
|
2016-09-02 03:21:37 +02:00
|
|
|
BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit, UseValgrind,
|
|
|
|
Context);
|
|
|
|
if (D.addSources(InputFilenames))
|
|
|
|
return 1;
|
2014-01-15 09:21:38 +01:00
|
|
|
|
2009-07-20 09:01:01 +02:00
|
|
|
AddToDriver PM(D);
|
2014-01-15 09:21:38 +01:00
|
|
|
|
2011-05-22 02:20:07 +02:00
|
|
|
if (StandardLinkOpts) {
|
|
|
|
PassManagerBuilder Builder;
|
2014-08-21 15:35:30 +02:00
|
|
|
Builder.Inliner = createFunctionInliningPass();
|
|
|
|
Builder.populateLTOPassManager(PM);
|
2011-05-22 02:20:07 +02:00
|
|
|
}
|
2009-07-20 09:01:01 +02:00
|
|
|
|
2019-09-17 10:14:09 +02:00
|
|
|
if (OptLevelO1)
|
|
|
|
AddOptimizationPasses(PM, 1, 0);
|
|
|
|
else if (OptLevelO2)
|
|
|
|
AddOptimizationPasses(PM, 2, 0);
|
|
|
|
else if (OptLevelO3)
|
|
|
|
AddOptimizationPasses(PM, 3, 0);
|
|
|
|
else if (OptLevelOs)
|
|
|
|
AddOptimizationPasses(PM, 2, 1);
|
|
|
|
else if (OptLevelOz)
|
|
|
|
AddOptimizationPasses(PM, 2, 2);
|
2010-08-08 05:55:08 +02:00
|
|
|
|
2015-10-15 03:12:01 +02:00
|
|
|
for (const PassInfo *PI : PassList)
|
2020-01-28 20:23:46 +01:00
|
|
|
D.addPass(std::string(PI->getPassArgument()));
|
2002-11-20 23:28:10 +01:00
|
|
|
|
2016-09-02 03:21:37 +02:00
|
|
|
// Bugpoint has the ability of generating a plethora of core files, so to
|
|
|
|
// avoid filling up the disk, we prevent it
|
2011-01-13 20:48:54 +01:00
|
|
|
#ifndef DEBUG_BUGPOINT
|
2004-12-27 07:18:02 +01:00
|
|
|
sys::Process::PreventCoreFiles();
|
2011-01-13 20:48:54 +01:00
|
|
|
#endif
|
2003-09-12 22:42:57 +02:00
|
|
|
|
2019-06-08 17:37:47 +02:00
|
|
|
// Needed to pull in symbols from statically linked extensions, including static
|
|
|
|
// registration. It is unused otherwise because bugpoint has no support for
|
|
|
|
// NewPM.
|
|
|
|
#define HANDLE_EXTENSION(Ext) \
|
|
|
|
(void)get##Ext##PluginInfo();
|
|
|
|
#include "llvm/Support/Extension.def"
|
|
|
|
|
2016-09-06 19:18:22 +02:00
|
|
|
if (Error E = D.run()) {
|
|
|
|
errs() << toString(std::move(E));
|
2016-09-06 06:45:37 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2016-09-06 19:18:22 +02:00
|
|
|
return 0;
|
2002-11-20 23:28:10 +01:00
|
|
|
}
|