2003-10-21 01:57:13 +08:00
|
|
|
//===- bugpoint.cpp - The LLVM Bugpoint utility ---------------------------===//
|
2005-04-22 08:00:37 +08:00
|
|
|
//
|
2003-10-21 01:47:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:44:31 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 08:00:37 +08:00
|
|
|
//
|
2003-10-21 01:47:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-11-21 06:28:10 +08: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 06:30:59 +08:00
|
|
|
#include "ToolRunner.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2015-02-13 18:01:29 +08:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2014-03-04 20:32:42 +08:00
|
|
|
#include "llvm/IR/LegacyPassNameParser.h"
|
2013-01-11 05:56:40 +08:00
|
|
|
#include "llvm/LinkAllIR.h"
|
2013-01-19 16:03:47 +08:00
|
|
|
#include "llvm/LinkAllPasses.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2006-12-06 09:18:01 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/PluginLoader.h"
|
2009-03-06 13:34:10 +08:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2010-11-30 02:16:10 +08:00
|
|
|
#include "llvm/Support/Process.h"
|
|
|
|
#include "llvm/Support/Signals.h"
|
2017-06-24 16:09:33 +08:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2010-11-30 02:16:10 +08: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 10:56:20 +08:00
|
|
|
#include "llvm/Transforms/IPO/AlwaysInliner.h"
|
2011-08-03 05:50:24 +08:00
|
|
|
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
|
2011-01-14 03:48:54 +08:00
|
|
|
|
2016-09-02 09:21:37 +08:00
|
|
|
// Enable this macro to debug bugpoint itself.
|
2011-01-14 23:55:50 +08:00
|
|
|
//#define DEBUG_BUGPOINT 1
|
2011-01-14 03:48:54 +08:00
|
|
|
|
2003-11-12 06:41:34 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-01-15 16:21:38 +08:00
|
|
|
static cl::opt<bool>
|
2016-09-02 09:21:37 +08:00
|
|
|
FindBugs("find-bugs", cl::desc("Run many different optimization sequences "
|
|
|
|
"on program to find bugs"),
|
|
|
|
cl::init(false));
|
2005-12-23 04:02:55 +08:00
|
|
|
|
2002-11-21 06:28:10 +08:00
|
|
|
static cl::list<std::string>
|
2016-09-02 09:21:37 +08:00
|
|
|
InputFilenames(cl::Positional, cl::OneOrMore,
|
|
|
|
cl::desc("<input llvm ll/bc files>"));
|
2002-11-21 06:28:10 +08:00
|
|
|
|
2016-09-02 09:21:37 +08: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 11:10:48 +08:00
|
|
|
|
2017-09-01 09:47:34 +08: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 08:09:28 +08:00
|
|
|
|
|
|
|
static cl::opt<bool>
|
2016-09-02 09:21:37 +08:00
|
|
|
UseValgrind("enable-valgrind",
|
|
|
|
cl::desc("Run optimizations through valgrind"));
|
2007-02-17 03:11:07 +08:00
|
|
|
|
2002-11-21 06:28:10 +08:00
|
|
|
// The AnalysesList is automatically populated with registered Passes by the
|
|
|
|
// PassNameParser.
|
|
|
|
//
|
2016-09-02 09:21:37 +08:00
|
|
|
static cl::list<const PassInfo *, bool, PassNameParser>
|
|
|
|
PassList(cl::desc("Passes available:"), cl::ZeroOrMore);
|
2002-11-21 06:28:10 +08:00
|
|
|
|
2009-07-20 15:01:01 +08:00
|
|
|
static cl::opt<bool>
|
2016-09-02 09:21:37 +08:00
|
|
|
StandardLinkOpts("std-link-opts",
|
|
|
|
cl::desc("Include the standard link time optimizations"));
|
2009-07-20 15:01:01 +08:00
|
|
|
|
2011-06-07 06:45:46 +08:00
|
|
|
static cl::opt<bool>
|
2016-09-02 09:21:37 +08:00
|
|
|
OptLevelO1("O1", cl::desc("Optimization level 1. Identical to 'opt -O1'"));
|
2011-06-07 06:45:46 +08:00
|
|
|
|
|
|
|
static cl::opt<bool>
|
2016-09-02 09:21:37 +08:00
|
|
|
OptLevelO2("O2", cl::desc("Optimization level 2. Identical to 'opt -O2'"));
|
2011-06-07 06:45:46 +08:00
|
|
|
|
2016-09-02 09:21:37 +08:00
|
|
|
static cl::opt<bool> OptLevelOs(
|
|
|
|
"Os",
|
|
|
|
cl::desc(
|
|
|
|
"Like -O2 with extra optimizations for size. Similar to clang -Os"));
|
2016-08-01 03:25:16 +08:00
|
|
|
|
2011-06-07 06:45:46 +08:00
|
|
|
static cl::opt<bool>
|
2016-09-02 09:21:37 +08:00
|
|
|
OptLevelO3("O3", cl::desc("Optimization level 3. Identical to 'opt -O3'"));
|
2011-06-07 06:45:46 +08:00
|
|
|
|
2009-08-18 11:35:57 +08:00
|
|
|
static cl::opt<std::string>
|
2016-09-02 09:21:37 +08:00
|
|
|
OverrideTriple("mtriple", cl::desc("Override target triple for module"));
|
2009-08-18 11:35:57 +08:00
|
|
|
|
2005-08-02 10:16:17 +08:00
|
|
|
/// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
|
|
|
|
bool llvm::BugpointIsInterrupted = false;
|
|
|
|
|
2011-01-14 03:48:54 +08:00
|
|
|
#ifndef DEBUG_BUGPOINT
|
2016-09-02 09:21:37 +08:00
|
|
|
static void BugpointInterruptFunction() { BugpointIsInterrupted = true; }
|
2011-01-14 03:48:54 +08:00
|
|
|
#endif
|
2005-08-02 10:16:17 +08:00
|
|
|
|
2009-07-20 15:01:01 +08:00
|
|
|
// Hack to capture a pass list.
|
|
|
|
namespace {
|
2016-09-02 09:21:37 +08: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);
|
|
|
|
D.addPass(PI->getPassArgument());
|
|
|
|
}
|
|
|
|
};
|
2009-07-20 15:01:01 +08:00
|
|
|
}
|
|
|
|
|
2014-03-14 12:04:14 +08:00
|
|
|
#ifdef LINK_POLLY_INTO_TOOLS
|
|
|
|
namespace polly {
|
|
|
|
void initializePollyPasses(llvm::PassRegistry &Registry);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-11-21 06:28:10 +08:00
|
|
|
int main(int argc, char **argv) {
|
2011-01-14 03:48:54 +08:00
|
|
|
#ifndef DEBUG_BUGPOINT
|
2016-06-09 08:53:21 +08:00
|
|
|
llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
|
2009-03-06 13:34:10 +08:00
|
|
|
llvm::PrettyStackTraceProgram X(argc, argv);
|
2016-09-02 09:21:37 +08:00
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
2011-01-14 03:48:54 +08:00
|
|
|
#endif
|
2014-01-15 16:21:38 +08:00
|
|
|
|
2010-10-20 01:21:58 +08:00
|
|
|
// Initialize passes
|
|
|
|
PassRegistry &Registry = *PassRegistry::getPassRegistry();
|
|
|
|
initializeCore(Registry);
|
|
|
|
initializeScalarOpts(Registry);
|
2013-01-28 09:35:51 +08:00
|
|
|
initializeObjCARCOpts(Registry);
|
2012-02-08 05:11:12 +08:00
|
|
|
initializeVectorization(Registry);
|
2010-10-20 01:21:58 +08:00
|
|
|
initializeIPO(Registry);
|
|
|
|
initializeAnalysis(Registry);
|
|
|
|
initializeTransformUtils(Registry);
|
|
|
|
initializeInstCombine(Registry);
|
|
|
|
initializeInstrumentation(Registry);
|
|
|
|
initializeTarget(Registry);
|
2014-01-15 16:21:38 +08:00
|
|
|
|
2014-03-14 12:04:14 +08:00
|
|
|
#ifdef LINK_POLLY_INTO_TOOLS
|
|
|
|
polly::initializePollyPasses(Registry);
|
|
|
|
#endif
|
|
|
|
|
2017-06-24 16:09:33 +08:00
|
|
|
if (std::getenv("bar") == (char*) -1) {
|
|
|
|
InitializeAllTargets();
|
|
|
|
InitializeAllTargetMCs();
|
|
|
|
InitializeAllAsmPrinters();
|
|
|
|
InitializeAllAsmParsers();
|
|
|
|
}
|
|
|
|
|
2003-10-19 05:55:35 +08:00
|
|
|
cl::ParseCommandLineOptions(argc, argv,
|
2007-10-08 23:45:12 +08:00
|
|
|
"LLVM automatic testcase reducer. See\nhttp://"
|
2009-02-08 02:56:30 +08:00
|
|
|
"llvm.org/cmds/bugpoint.html"
|
2003-10-19 05:55:35 +08:00
|
|
|
" for more information.\n");
|
2011-01-14 03:48:54 +08:00
|
|
|
#ifndef DEBUG_BUGPOINT
|
2005-08-02 10:16:17 +08:00
|
|
|
sys::SetInterruptFunction(BugpointInterruptFunction);
|
2011-01-14 03:48:54 +08:00
|
|
|
#endif
|
2009-07-02 00:58:40 +08:00
|
|
|
|
2016-04-15 05:59:01 +08:00
|
|
|
LLVMContext Context;
|
2009-08-18 11:35:57 +08:00
|
|
|
// If we have an override, set it and then track the triple we want Modules
|
|
|
|
// to use.
|
2009-08-31 11:22:35 +08:00
|
|
|
if (!OverrideTriple.empty()) {
|
2010-08-28 09:30:02 +08:00
|
|
|
TargetTriple.setTriple(Triple::normalize(OverrideTriple));
|
|
|
|
outs() << "Override triple set to '" << TargetTriple.getTriple() << "'\n";
|
2009-08-31 11:22:35 +08:00
|
|
|
}
|
2009-08-18 11:35:57 +08:00
|
|
|
|
2010-03-19 08:09:28 +08: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-06 00:29:40 +08:00
|
|
|
MemoryLimit = 400;
|
2017-09-01 09:47:34 +08: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 08:09:28 +08:00
|
|
|
}
|
|
|
|
|
2016-09-02 09:21:37 +08:00
|
|
|
BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit, UseValgrind,
|
|
|
|
Context);
|
|
|
|
if (D.addSources(InputFilenames))
|
|
|
|
return 1;
|
2014-01-15 16:21:38 +08:00
|
|
|
|
2009-07-20 15:01:01 +08:00
|
|
|
AddToDriver PM(D);
|
2014-01-15 16:21:38 +08:00
|
|
|
|
2011-05-22 08:20:07 +08:00
|
|
|
if (StandardLinkOpts) {
|
|
|
|
PassManagerBuilder Builder;
|
2014-08-21 21:35:30 +08:00
|
|
|
Builder.Inliner = createFunctionInliningPass();
|
|
|
|
Builder.populateLTOPassManager(PM);
|
2011-05-22 08:20:07 +08:00
|
|
|
}
|
2009-07-20 15:01:01 +08:00
|
|
|
|
2011-06-07 06:45:46 +08:00
|
|
|
if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
|
|
|
|
PassManagerBuilder Builder;
|
|
|
|
if (OptLevelO1)
|
[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 10:56:20 +08:00
|
|
|
Builder.Inliner = createAlwaysInlinerLegacyPass();
|
2016-08-01 03:25:16 +08:00
|
|
|
else if (OptLevelOs || OptLevelO2)
|
2017-03-22 03:55:36 +08:00
|
|
|
Builder.Inliner = createFunctionInliningPass(
|
|
|
|
2, OptLevelOs ? 1 : 0, false);
|
2011-06-07 06:45:46 +08:00
|
|
|
else
|
|
|
|
Builder.Inliner = createFunctionInliningPass(275);
|
|
|
|
Builder.populateFunctionPassManager(PM);
|
|
|
|
Builder.populateModulePassManager(PM);
|
|
|
|
}
|
2010-08-08 11:55:08 +08:00
|
|
|
|
2015-10-15 09:12:01 +08:00
|
|
|
for (const PassInfo *PI : PassList)
|
2010-08-08 11:55:08 +08:00
|
|
|
D.addPass(PI->getPassArgument());
|
2002-11-21 06:28:10 +08:00
|
|
|
|
2016-09-02 09:21:37 +08:00
|
|
|
// Bugpoint has the ability of generating a plethora of core files, so to
|
|
|
|
// avoid filling up the disk, we prevent it
|
2011-01-14 03:48:54 +08:00
|
|
|
#ifndef DEBUG_BUGPOINT
|
2004-12-27 14:18:02 +08:00
|
|
|
sys::Process::PreventCoreFiles();
|
2011-01-14 03:48:54 +08:00
|
|
|
#endif
|
2003-09-13 04:42:57 +08:00
|
|
|
|
2016-09-07 01:18:22 +08:00
|
|
|
if (Error E = D.run()) {
|
|
|
|
errs() << toString(std::move(E));
|
2016-09-06 12:45:37 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2016-09-07 01:18:22 +08:00
|
|
|
return 0;
|
2002-11-21 06:28:10 +08:00
|
|
|
}
|