llvm-6502/tools/opt/opt.cpp
Chandler Carruth 6f409cbc05 [PM] Rework how the TargetLibraryInfo pass integrates with the new pass
manager to support the actual uses of it. =]

When I ported instcombine to the new pass manager I discover that it
didn't work because TLI wasn't available in the right places. This is
a somewhat surprising and/or subtle aspect of the new pass manager
design that came up before but I think is useful to be reminded of:

While the new pass manager *allows* a function pass to query a module
analysis, it requires that the module analysis is already run and cached
prior to the function pass manager starting up, possibly with
a 'require<foo>' style utility in the pass pipeline. This is an
intentional hurdle because using a module analysis from a function pass
*requires* that the module analysis is run prior to entering the
function pass manager. Otherwise the other functions in the module could
be in who-knows-what state, etc.

A somewhat surprising consequence of this design decision (at least to
me) is that you have to design a function pass that leverages
a module analysis to do so as an optional feature. Even if that means
your function pass does no work in the absence of the module analysis,
you have to handle that possibility and remain conservatively correct.
This is a natural consequence of things being able to invalidate the
module analysis and us being unable to re-run it. And it's a generally
good thing because it lets us reorder passes arbitrarily without
breaking correctness, etc.

This ends up causing problems in one case. What if we have a module
analysis that is *definitionally* impossible to invalidate. In the
places this might come up, the analysis is usually also definitionally
trivial to run even while other transformation passes run on the module,
regardless of the state of anything. And so, it follows that it is
natural to have a hard requirement on such analyses from a function
pass.

It turns out, that TargetLibraryInfo is just such an analysis, and
InstCombine has a hard requirement on it.

The approach I've taken here is to produce an analysis that models this
flexibility by making it both a module and a function analysis. This
exposes the fact that it is in fact safe to compute at any point. We can
even make it a valid CGSCC analysis at some point if that is useful.
However, we don't want to have a copy of the actual target library info
state for each function! This state is specific to the triple. The
somewhat direct and blunt approach here is to turn TLI into a pimpl,
with the state and mutators in the implementation class and the query
routines primarily in the wrapper. Then the analysis can lazily
construct and cache the implementations, keyed on the triple, and
on-demand produce wrappers of them for each function.

One minor annoyance is that we will end up with a wrapper for each
function in the module. While this is a bit wasteful (one pointer per
function) it seems tolerable. And it has the advantage of ensuring that
we pay the absolute minimum synchronization cost to access this
information should we end up with a nice parallel function pass manager
in the future. We could look into trying to mark when analysis results
are especially cheap to recompute and more eagerly GC-ing the cached
results, or we could look at supporting a variant of analyses whose
results are specifically *not* cached and expected to just be used and
discarded by the consumer. Either way, these seem like incremental
enhancements that should happen when we start profiling the memory and
CPU usage of the new pass manager and not before.

The other minor annoyance is that if we end up using the TLI in both
a module pass and a function pass, those will be produced by two
separate analyses, and thus will point to separate copies of the
implementation state. While a minor issue, I dislike this and would like
to find a way to cleanly allow a single analysis instance to be used
across multiple IR unit managers. But I don't have a good solution to
this today, and I don't want to hold up all of the work waiting to come
up with one. This too seems like a reasonable thing to incrementally
improve later.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@226981 91177308-0d34-0410-b5e6-96231b3b80d8
2015-01-24 02:06:09 +00:00

590 lines
18 KiB
C++

//===- opt.cpp - The LLVM Modular Optimizer -------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Optimizations may be specified an arbitrary number of times on the command
// line, They are run in the order specified.
//
//===----------------------------------------------------------------------===//
#include "BreakpointPrinter.h"
#include "NewPMDriver.h"
#include "PassPrinters.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Analysis/CallGraph.h"
#include "llvm/Analysis/CallGraphSCCPass.h"
#include "llvm/Analysis/LoopPass.h"
#include "llvm/Analysis/RegionPass.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/Bitcode/BitcodeWriterPass.h"
#include "llvm/CodeGen/CommandFlags.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/IRPrintingPasses.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/LegacyPassNameParser.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/InitializePasses.h"
#include "llvm/LinkAllIR.h"
#include "llvm/LinkAllPasses.h"
#include "llvm/MC/SubtargetFeature.h"
#include "llvm/PassManager.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/PluginLoader.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/SystemUtils.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
#include <algorithm>
#include <memory>
using namespace llvm;
using namespace opt_tool;
// The OptimizationList is automatically populated with registered Passes by the
// PassNameParser.
//
static cl::list<const PassInfo*, bool, PassNameParser>
PassList(cl::desc("Optimizations available:"));
// This flag specifies a textual description of the optimization pass pipeline
// to run over the module. This flag switches opt to use the new pass manager
// infrastructure, completely disabling all of the flags specific to the old
// pass management.
static cl::opt<std::string> PassPipeline(
"passes",
cl::desc("A textual description of the pass pipeline for optimizing"),
cl::Hidden);
// Other command line options...
//
static cl::opt<std::string>
InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
cl::init("-"), cl::value_desc("filename"));
static cl::opt<std::string>
OutputFilename("o", cl::desc("Override output filename"),
cl::value_desc("filename"));
static cl::opt<bool>
Force("f", cl::desc("Enable binary output on terminals"));
static cl::opt<bool>
PrintEachXForm("p", cl::desc("Print module after each transformation"));
static cl::opt<bool>
NoOutput("disable-output",
cl::desc("Do not write result bitcode file"), cl::Hidden);
static cl::opt<bool>
OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
static cl::opt<bool>
NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
static cl::opt<bool>
VerifyEach("verify-each", cl::desc("Verify after each transform"));
static cl::opt<bool>
StripDebug("strip-debug",
cl::desc("Strip debugger symbol info from translation unit"));
static cl::opt<bool>
DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
static cl::opt<bool>
DisableOptimizations("disable-opt",
cl::desc("Do not run any optimization passes"));
static cl::opt<bool>
StandardLinkOpts("std-link-opts",
cl::desc("Include the standard link time optimizations"));
static cl::opt<bool>
OptLevelO1("O1",
cl::desc("Optimization level 1. Similar to clang -O1"));
static cl::opt<bool>
OptLevelO2("O2",
cl::desc("Optimization level 2. Similar to clang -O2"));
static cl::opt<bool>
OptLevelOs("Os",
cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os"));
static cl::opt<bool>
OptLevelOz("Oz",
cl::desc("Like -Os but reduces code size further. Similar to clang -Oz"));
static cl::opt<bool>
OptLevelO3("O3",
cl::desc("Optimization level 3. Similar to clang -O3"));
static cl::opt<std::string>
TargetTriple("mtriple", cl::desc("Override target triple for module"));
static cl::opt<bool>
UnitAtATime("funit-at-a-time",
cl::desc("Enable IPO. This corresponds to gcc's -funit-at-a-time"),
cl::init(true));
static cl::opt<bool>
DisableLoopUnrolling("disable-loop-unrolling",
cl::desc("Disable loop unrolling in all relevant passes"),
cl::init(false));
static cl::opt<bool>
DisableLoopVectorization("disable-loop-vectorization",
cl::desc("Disable the loop vectorization pass"),
cl::init(false));
static cl::opt<bool>
DisableSLPVectorization("disable-slp-vectorization",
cl::desc("Disable the slp vectorization pass"),
cl::init(false));
static cl::opt<bool>
DisableSimplifyLibCalls("disable-simplify-libcalls",
cl::desc("Disable simplify-libcalls"));
static cl::opt<bool>
Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
static cl::alias
QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
static cl::opt<bool>
AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
static cl::opt<bool>
PrintBreakpoints("print-breakpoints-for-testing",
cl::desc("Print select breakpoints location for testing"));
static cl::opt<std::string>
DefaultDataLayout("default-data-layout",
cl::desc("data layout string to use if not specified by module"),
cl::value_desc("layout-string"), cl::init(""));
static inline void addPass(PassManagerBase &PM, Pass *P) {
// Add the pass to the pass manager...
PM.add(P);
// If we are verifying all of the intermediate steps, add the verifier...
if (VerifyEach) {
PM.add(createVerifierPass());
PM.add(createDebugInfoVerifierPass());
}
}
/// This routine adds optimization passes based on selected optimization level,
/// OptLevel.
///
/// OptLevel - Optimization Level
static void AddOptimizationPasses(PassManagerBase &MPM,FunctionPassManager &FPM,
unsigned OptLevel, unsigned SizeLevel) {
FPM.add(createVerifierPass()); // Verify that input is correct
MPM.add(createDebugInfoVerifierPass()); // Verify that debug info is correct
PassManagerBuilder Builder;
Builder.OptLevel = OptLevel;
Builder.SizeLevel = SizeLevel;
if (DisableInline) {
// No inlining pass
} else if (OptLevel > 1) {
Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel);
} else {
Builder.Inliner = createAlwaysInlinerPass();
}
Builder.DisableUnitAtATime = !UnitAtATime;
Builder.DisableUnrollLoops = (DisableLoopUnrolling.getNumOccurrences() > 0) ?
DisableLoopUnrolling : OptLevel == 0;
// This is final, unless there is a #pragma vectorize enable
if (DisableLoopVectorization)
Builder.LoopVectorize = false;
// If option wasn't forced via cmd line (-vectorize-loops, -loop-vectorize)
else if (!Builder.LoopVectorize)
Builder.LoopVectorize = OptLevel > 1 && SizeLevel < 2;
// When #pragma vectorize is on for SLP, do the same as above
Builder.SLPVectorize =
DisableSLPVectorization ? false : OptLevel > 1 && SizeLevel < 2;
Builder.populateFunctionPassManager(FPM);
Builder.populateModulePassManager(MPM);
}
static void AddStandardLinkPasses(PassManagerBase &PM) {
PassManagerBuilder Builder;
Builder.VerifyInput = true;
Builder.StripDebug = StripDebug;
if (DisableOptimizations)
Builder.OptLevel = 0;
if (!DisableInline)
Builder.Inliner = createFunctionInliningPass();
Builder.populateLTOPassManager(PM);
}
//===----------------------------------------------------------------------===//
// CodeGen-related helper functions.
//
CodeGenOpt::Level GetCodeGenOptLevel() {
if (OptLevelO1)
return CodeGenOpt::Less;
if (OptLevelO2)
return CodeGenOpt::Default;
if (OptLevelO3)
return CodeGenOpt::Aggressive;
return CodeGenOpt::None;
}
// Returns the TargetMachine instance or zero if no triple is provided.
static TargetMachine* GetTargetMachine(Triple TheTriple) {
std::string Error;
const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
Error);
// Some modules don't specify a triple, and this is okay.
if (!TheTarget) {
return nullptr;
}
// Package up features to be passed to target/subtarget
std::string FeaturesStr;
if (MAttrs.size()) {
SubtargetFeatures Features;
for (unsigned i = 0; i != MAttrs.size(); ++i)
Features.AddFeature(MAttrs[i]);
FeaturesStr = Features.getString();
}
return TheTarget->createTargetMachine(TheTriple.getTriple(),
MCPU, FeaturesStr,
InitTargetOptionsFromCodeGenFlags(),
RelocModel, CMModel,
GetCodeGenOptLevel());
}
#ifdef LINK_POLLY_INTO_TOOLS
namespace polly {
void initializePollyPasses(llvm::PassRegistry &Registry);
}
#endif
//===----------------------------------------------------------------------===//
// main for opt
//
int main(int argc, char **argv) {
sys::PrintStackTraceOnErrorSignal();
llvm::PrettyStackTraceProgram X(argc, argv);
// Enable debug stream buffering.
EnableDebugBuffering = true;
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
LLVMContext &Context = getGlobalContext();
InitializeAllTargets();
InitializeAllTargetMCs();
InitializeAllAsmPrinters();
// Initialize passes
PassRegistry &Registry = *PassRegistry::getPassRegistry();
initializeCore(Registry);
initializeScalarOpts(Registry);
initializeObjCARCOpts(Registry);
initializeVectorization(Registry);
initializeIPO(Registry);
initializeAnalysis(Registry);
initializeIPA(Registry);
initializeTransformUtils(Registry);
initializeInstCombine(Registry);
initializeInstrumentation(Registry);
initializeTarget(Registry);
// For codegen passes, only passes that do IR to IR transformation are
// supported.
initializeCodeGenPreparePass(Registry);
initializeAtomicExpandPass(Registry);
initializeRewriteSymbolsPass(Registry);
#ifdef LINK_POLLY_INTO_TOOLS
polly::initializePollyPasses(Registry);
#endif
cl::ParseCommandLineOptions(argc, argv,
"llvm .bc -> .bc modular optimizer and analysis printer\n");
if (AnalyzeOnly && NoOutput) {
errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
return 1;
}
SMDiagnostic Err;
// Load the input module...
std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
if (!M) {
Err.print(argv[0], errs());
return 1;
}
// If we are supposed to override the target triple, do so now.
if (!TargetTriple.empty())
M->setTargetTriple(Triple::normalize(TargetTriple));
// Figure out what stream we are supposed to write to...
std::unique_ptr<tool_output_file> Out;
if (NoOutput) {
if (!OutputFilename.empty())
errs() << "WARNING: The -o (output filename) option is ignored when\n"
"the --disable-output option is used.\n";
} else {
// Default to standard output.
if (OutputFilename.empty())
OutputFilename = "-";
std::error_code EC;
Out.reset(new tool_output_file(OutputFilename, EC, sys::fs::F_None));
if (EC) {
errs() << EC.message() << '\n';
return 1;
}
}
// If the output is set to be emitted to standard out, and standard out is a
// console, print out a warning message and refuse to do it. We don't
// impress anyone by spewing tons of binary goo to a terminal.
if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
NoOutput = true;
if (PassPipeline.getNumOccurrences() > 0) {
OutputKind OK = OK_NoOutput;
if (!NoOutput)
OK = OutputAssembly ? OK_OutputAssembly : OK_OutputBitcode;
VerifierKind VK = VK_VerifyInAndOut;
if (NoVerify)
VK = VK_NoVerifier;
else if (VerifyEach)
VK = VK_VerifyEachPass;
// The user has asked to use the new pass manager and provided a pipeline
// string. Hand off the rest of the functionality to the new code for that
// layer.
return runPassPipeline(argv[0], Context, *M, Out.get(), PassPipeline,
OK, VK)
? 0
: 1;
}
// Create a PassManager to hold and optimize the collection of passes we are
// about to build.
//
PassManager Passes;
// Add an appropriate TargetLibraryInfo pass for the module's triple.
TargetLibraryInfoImpl TLII(Triple(M->getTargetTriple()));
// The -disable-simplify-libcalls flag actually disables all builtin optzns.
if (DisableSimplifyLibCalls)
TLII.disableAllFunctions();
Passes.add(new TargetLibraryInfoWrapperPass(TLII));
// Add an appropriate DataLayout instance for this module.
const DataLayout *DL = M->getDataLayout();
if (!DL && !DefaultDataLayout.empty()) {
M->setDataLayout(DefaultDataLayout);
DL = M->getDataLayout();
}
if (DL)
Passes.add(new DataLayoutPass());
Triple ModuleTriple(M->getTargetTriple());
TargetMachine *Machine = nullptr;
if (ModuleTriple.getArch())
Machine = GetTargetMachine(Triple(ModuleTriple));
std::unique_ptr<TargetMachine> TM(Machine);
// Add internal analysis passes from the target machine.
if (TM)
TM->addAnalysisPasses(Passes);
std::unique_ptr<FunctionPassManager> FPasses;
if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
FPasses.reset(new FunctionPassManager(M.get()));
if (DL)
FPasses->add(new DataLayoutPass());
if (TM)
TM->addAnalysisPasses(*FPasses);
}
if (PrintBreakpoints) {
// Default to standard output.
if (!Out) {
if (OutputFilename.empty())
OutputFilename = "-";
std::error_code EC;
Out = llvm::make_unique<tool_output_file>(OutputFilename, EC,
sys::fs::F_None);
if (EC) {
errs() << EC.message() << '\n';
return 1;
}
}
Passes.add(createBreakpointPrinter(Out->os()));
NoOutput = true;
}
// If the -strip-debug command line option was specified, add it.
if (StripDebug)
addPass(Passes, createStripSymbolsPass(true));
// Create a new optimization pass for each one specified on the command line
for (unsigned i = 0; i < PassList.size(); ++i) {
if (StandardLinkOpts &&
StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
AddStandardLinkPasses(Passes);
StandardLinkOpts = false;
}
if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
AddOptimizationPasses(Passes, *FPasses, 1, 0);
OptLevelO1 = false;
}
if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
AddOptimizationPasses(Passes, *FPasses, 2, 0);
OptLevelO2 = false;
}
if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) {
AddOptimizationPasses(Passes, *FPasses, 2, 1);
OptLevelOs = false;
}
if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) {
AddOptimizationPasses(Passes, *FPasses, 2, 2);
OptLevelOz = false;
}
if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
AddOptimizationPasses(Passes, *FPasses, 3, 0);
OptLevelO3 = false;
}
const PassInfo *PassInf = PassList[i];
Pass *P = nullptr;
if (PassInf->getTargetMachineCtor())
P = PassInf->getTargetMachineCtor()(TM.get());
else if (PassInf->getNormalCtor())
P = PassInf->getNormalCtor()();
else
errs() << argv[0] << ": cannot create pass: "
<< PassInf->getPassName() << "\n";
if (P) {
PassKind Kind = P->getPassKind();
addPass(Passes, P);
if (AnalyzeOnly) {
switch (Kind) {
case PT_BasicBlock:
Passes.add(createBasicBlockPassPrinter(PassInf, Out->os(), Quiet));
break;
case PT_Region:
Passes.add(createRegionPassPrinter(PassInf, Out->os(), Quiet));
break;
case PT_Loop:
Passes.add(createLoopPassPrinter(PassInf, Out->os(), Quiet));
break;
case PT_Function:
Passes.add(createFunctionPassPrinter(PassInf, Out->os(), Quiet));
break;
case PT_CallGraphSCC:
Passes.add(createCallGraphPassPrinter(PassInf, Out->os(), Quiet));
break;
default:
Passes.add(createModulePassPrinter(PassInf, Out->os(), Quiet));
break;
}
}
}
if (PrintEachXForm)
Passes.add(createPrintModulePass(errs()));
}
if (StandardLinkOpts) {
AddStandardLinkPasses(Passes);
StandardLinkOpts = false;
}
if (OptLevelO1)
AddOptimizationPasses(Passes, *FPasses, 1, 0);
if (OptLevelO2)
AddOptimizationPasses(Passes, *FPasses, 2, 0);
if (OptLevelOs)
AddOptimizationPasses(Passes, *FPasses, 2, 1);
if (OptLevelOz)
AddOptimizationPasses(Passes, *FPasses, 2, 2);
if (OptLevelO3)
AddOptimizationPasses(Passes, *FPasses, 3, 0);
if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
FPasses->doInitialization();
for (Function &F : *M)
FPasses->run(F);
FPasses->doFinalization();
}
// Check that the module is well formed on completion of optimization
if (!NoVerify && !VerifyEach) {
Passes.add(createVerifierPass());
Passes.add(createDebugInfoVerifierPass());
}
// Write bitcode or assembly to the output as the last step...
if (!NoOutput && !AnalyzeOnly) {
if (OutputAssembly)
Passes.add(createPrintModulePass(Out->os()));
else
Passes.add(createBitcodeWriterPass(Out->os()));
}
// Before executing passes, print the final values of the LLVM options.
cl::PrintOptionValues();
// Now that we have all of the passes ready, run them.
Passes.run(*M);
// Declare success.
if (!NoOutput || PrintBreakpoints)
Out->keep();
return 0;
}