mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-11-25 12:12:47 +01:00
5c3f34f10b
Summary: First, we need to explain the core of the vulnerability. Note that this is a very incomplete description, please see the Project Zero blog post for details: https://googleprojectzero.blogspot.com/2018/01/reading-privileged-memory-with-side.html The basis for branch target injection is to direct speculative execution of the processor to some "gadget" of executable code by poisoning the prediction of indirect branches with the address of that gadget. The gadget in turn contains an operation that provides a side channel for reading data. Most commonly, this will look like a load of secret data followed by a branch on the loaded value and then a load of some predictable cache line. The attacker then uses timing of the processors cache to determine which direction the branch took *in the speculative execution*, and in turn what one bit of the loaded value was. Due to the nature of these timing side channels and the branch predictor on Intel processors, this allows an attacker to leak data only accessible to a privileged domain (like the kernel) back into an unprivileged domain. The goal is simple: avoid generating code which contains an indirect branch that could have its prediction poisoned by an attacker. In many cases, the compiler can simply use directed conditional branches and a small search tree. LLVM already has support for lowering switches in this way and the first step of this patch is to disable jump-table lowering of switches and introduce a pass to rewrite explicit indirectbr sequences into a switch over integers. However, there is no fully general alternative to indirect calls. We introduce a new construct we call a "retpoline" to implement indirect calls in a non-speculatable way. It can be thought of loosely as a trampoline for indirect calls which uses the RET instruction on x86. Further, we arrange for a specific call->ret sequence which ensures the processor predicts the return to go to a controlled, known location. The retpoline then "smashes" the return address pushed onto the stack by the call with the desired target of the original indirect call. The result is a predicted return to the next instruction after a call (which can be used to trap speculative execution within an infinite loop) and an actual indirect branch to an arbitrary address. On 64-bit x86 ABIs, this is especially easily done in the compiler by using a guaranteed scratch register to pass the target into this device. For 32-bit ABIs there isn't a guaranteed scratch register and so several different retpoline variants are introduced to use a scratch register if one is available in the calling convention and to otherwise use direct stack push/pop sequences to pass the target address. This "retpoline" mitigation is fully described in the following blog post: https://support.google.com/faqs/answer/7625886 We also support a target feature that disables emission of the retpoline thunk by the compiler to allow for custom thunks if users want them. These are particularly useful in environments like kernels that routinely do hot-patching on boot and want to hot-patch their thunk to different code sequences. They can write this custom thunk and use `-mretpoline-external-thunk` *in addition* to `-mretpoline`. In this case, on x86-64 thu thunk names must be: ``` __llvm_external_retpoline_r11 ``` or on 32-bit: ``` __llvm_external_retpoline_eax __llvm_external_retpoline_ecx __llvm_external_retpoline_edx __llvm_external_retpoline_push ``` And the target of the retpoline is passed in the named register, or in the case of the `push` suffix on the top of the stack via a `pushl` instruction. There is one other important source of indirect branches in x86 ELF binaries: the PLT. These patches also include support for LLD to generate PLT entries that perform a retpoline-style indirection. The only other indirect branches remaining that we are aware of are from precompiled runtimes (such as crt0.o and similar). The ones we have found are not really attackable, and so we have not focused on them here, but eventually these runtimes should also be replicated for retpoline-ed configurations for completeness. For kernels or other freestanding or fully static executables, the compiler switch `-mretpoline` is sufficient to fully mitigate this particular attack. For dynamic executables, you must compile *all* libraries with `-mretpoline` and additionally link the dynamic executable and all shared libraries with LLD and pass `-z retpolineplt` (or use similar functionality from some other linker). We strongly recommend also using `-z now` as non-lazy binding allows the retpoline-mitigated PLT to be substantially smaller. When manually apply similar transformations to `-mretpoline` to the Linux kernel we observed very small performance hits to applications running typical workloads, and relatively minor hits (approximately 2%) even for extremely syscall-heavy applications. This is largely due to the small number of indirect branches that occur in performance sensitive paths of the kernel. When using these patches on statically linked applications, especially C++ applications, you should expect to see a much more dramatic performance hit. For microbenchmarks that are switch, indirect-, or virtual-call heavy we have seen overheads ranging from 10% to 50%. However, real-world workloads exhibit substantially lower performance impact. Notably, techniques such as PGO and ThinLTO dramatically reduce the impact of hot indirect calls (by speculatively promoting them to direct calls) and allow optimized search trees to be used to lower switches. If you need to deploy these techniques in C++ applications, we *strongly* recommend that you ensure all hot call targets are statically linked (avoiding PLT indirection) and use both PGO and ThinLTO. Well tuned servers using all of these techniques saw 5% - 10% overhead from the use of retpoline. We will add detailed documentation covering these components in subsequent patches, but wanted to make the core functionality available as soon as possible. Happy for more code review, but we'd really like to get these patches landed and backported ASAP for obvious reasons. We're planning to backport this to both 6.0 and 5.0 release streams and get a 5.0 release with just this cherry picked ASAP for distros and vendors. This patch is the work of a number of people over the past month: Eric, Reid, Rui, and myself. I'm mailing it out as a single commit due to the time sensitive nature of landing this and the need to backport it. Huge thanks to everyone who helped out here, and everyone at Intel who helped out in discussions about how to craft this. Also, credit goes to Paul Turner (at Google, but not an LLVM contributor) for much of the underlying retpoline design. Reviewers: echristo, rnk, ruiu, craig.topper, DavidKreitzer Subscribers: sanjoy, emaste, mcrosier, mgorny, mehdi_amini, hiraditya, llvm-commits Differential Revision: https://reviews.llvm.org/D41723 llvm-svn: 323155
795 lines
26 KiB
C++
795 lines
26 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/Analysis/TargetTransformInfo.h"
|
|
#include "llvm/Bitcode/BitcodeWriterPass.h"
|
|
#include "llvm/CodeGen/CommandFlags.def"
|
|
#include "llvm/CodeGen/TargetPassConfig.h"
|
|
#include "llvm/IR/DataLayout.h"
|
|
#include "llvm/IR/DebugInfo.h"
|
|
#include "llvm/IR/IRPrintingPasses.h"
|
|
#include "llvm/IR/LLVMContext.h"
|
|
#include "llvm/IR/LegacyPassManager.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/Support/Debug.h"
|
|
#include "llvm/Support/FileSystem.h"
|
|
#include "llvm/Support/Host.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/Support/YAMLTraits.h"
|
|
#include "llvm/Target/TargetMachine.h"
|
|
#include "llvm/Transforms/Coroutines.h"
|
|
#include "llvm/Transforms/IPO/AlwaysInliner.h"
|
|
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
|
|
#include "llvm/Transforms/Utils/Cloning.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>
|
|
OutputThinLTOBC("thinlto-bc",
|
|
cl::desc("Write output as ThinLTO-ready bitcode"));
|
|
|
|
static cl::opt<std::string> ThinLinkBitcodeFile(
|
|
"thin-link-bitcode-file", cl::value_desc("filename"),
|
|
cl::desc(
|
|
"A file in which to write minimized bitcode for the thin link only"));
|
|
|
|
static cl::opt<bool>
|
|
NoVerify("disable-verify", cl::desc("Do not run the verifier"), cl::Hidden);
|
|
|
|
static cl::opt<bool>
|
|
VerifyEach("verify-each", cl::desc("Verify after each transform"));
|
|
|
|
static cl::opt<bool>
|
|
DisableDITypeMap("disable-debug-info-type-map",
|
|
cl::desc("Don't use a uniquing type map for debug info"));
|
|
|
|
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>
|
|
OptLevelO0("O0",
|
|
cl::desc("Optimization level 0. Similar to clang -O0"));
|
|
|
|
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<unsigned>
|
|
CodeGenOptLevel("codegen-opt-level",
|
|
cl::desc("Override optimization level for codegen hooks"));
|
|
|
|
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> EmitSummaryIndex("module-summary",
|
|
cl::desc("Emit module summary index"),
|
|
cl::init(false));
|
|
|
|
static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"),
|
|
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> ClDataLayout("data-layout",
|
|
cl::desc("data layout string to use"),
|
|
cl::value_desc("layout-string"),
|
|
cl::init(""));
|
|
|
|
static cl::opt<bool> PreserveBitcodeUseListOrder(
|
|
"preserve-bc-uselistorder",
|
|
cl::desc("Preserve use-list order when writing LLVM bitcode."),
|
|
cl::init(true), cl::Hidden);
|
|
|
|
static cl::opt<bool> PreserveAssemblyUseListOrder(
|
|
"preserve-ll-uselistorder",
|
|
cl::desc("Preserve use-list order when writing LLVM assembly."),
|
|
cl::init(false), cl::Hidden);
|
|
|
|
static cl::opt<bool>
|
|
RunTwice("run-twice",
|
|
cl::desc("Run all passes twice, re-using the same pass manager."),
|
|
cl::init(false), cl::Hidden);
|
|
|
|
static cl::opt<bool> DiscardValueNames(
|
|
"discard-value-names",
|
|
cl::desc("Discard names from Value (other than GlobalValue)."),
|
|
cl::init(false), cl::Hidden);
|
|
|
|
static cl::opt<bool> Coroutines(
|
|
"enable-coroutines",
|
|
cl::desc("Enable coroutine passes."),
|
|
cl::init(false), cl::Hidden);
|
|
|
|
static cl::opt<bool> PassRemarksWithHotness(
|
|
"pass-remarks-with-hotness",
|
|
cl::desc("With PGO, include profile count in optimization remarks"),
|
|
cl::Hidden);
|
|
|
|
static cl::opt<unsigned> PassRemarksHotnessThreshold(
|
|
"pass-remarks-hotness-threshold",
|
|
cl::desc("Minimum profile count required for an optimization remark to be output"),
|
|
cl::Hidden);
|
|
|
|
static cl::opt<std::string>
|
|
RemarksFilename("pass-remarks-output",
|
|
cl::desc("YAML output filename for pass remarks"),
|
|
cl::value_desc("filename"));
|
|
|
|
static inline void addPass(legacy::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());
|
|
}
|
|
|
|
/// This routine adds optimization passes based on selected optimization level,
|
|
/// OptLevel.
|
|
///
|
|
/// OptLevel - Optimization Level
|
|
static void AddOptimizationPasses(legacy::PassManagerBase &MPM,
|
|
legacy::FunctionPassManager &FPM,
|
|
TargetMachine *TM, unsigned OptLevel,
|
|
unsigned SizeLevel) {
|
|
if (!NoVerify || VerifyEach)
|
|
FPM.add(createVerifierPass()); // Verify that input is correct
|
|
|
|
PassManagerBuilder Builder;
|
|
Builder.OptLevel = OptLevel;
|
|
Builder.SizeLevel = SizeLevel;
|
|
|
|
if (DisableInline) {
|
|
// No inlining pass
|
|
} else if (OptLevel > 1) {
|
|
Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false);
|
|
} else {
|
|
Builder.Inliner = createAlwaysInlinerLegacyPass();
|
|
}
|
|
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;
|
|
|
|
if (TM)
|
|
TM->adjustPassManager(Builder);
|
|
|
|
if (Coroutines)
|
|
addCoroutinePassesToExtensionPoints(Builder);
|
|
|
|
Builder.populateFunctionPassManager(FPM);
|
|
Builder.populateModulePassManager(MPM);
|
|
}
|
|
|
|
static void AddStandardLinkPasses(legacy::PassManagerBase &PM) {
|
|
PassManagerBuilder Builder;
|
|
Builder.VerifyInput = true;
|
|
if (DisableOptimizations)
|
|
Builder.OptLevel = 0;
|
|
|
|
if (!DisableInline)
|
|
Builder.Inliner = createFunctionInliningPass();
|
|
Builder.populateLTOPassManager(PM);
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// CodeGen-related helper functions.
|
|
//
|
|
|
|
static CodeGenOpt::Level GetCodeGenOptLevel() {
|
|
if (CodeGenOptLevel.getNumOccurrences())
|
|
return static_cast<CodeGenOpt::Level>(unsigned(CodeGenOptLevel));
|
|
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, StringRef CPUStr,
|
|
StringRef FeaturesStr,
|
|
const TargetOptions &Options) {
|
|
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;
|
|
}
|
|
|
|
return TheTarget->createTargetMachine(TheTriple.getTriple(), CPUStr,
|
|
FeaturesStr, Options, getRelocModel(),
|
|
getCodeModel(), 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(argv[0]);
|
|
llvm::PrettyStackTraceProgram X(argc, argv);
|
|
|
|
// Enable debug stream buffering.
|
|
EnableDebugBuffering = true;
|
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
|
LLVMContext Context;
|
|
|
|
InitializeAllTargets();
|
|
InitializeAllTargetMCs();
|
|
InitializeAllAsmPrinters();
|
|
InitializeAllAsmParsers();
|
|
|
|
// Initialize passes
|
|
PassRegistry &Registry = *PassRegistry::getPassRegistry();
|
|
initializeCore(Registry);
|
|
initializeCoroutines(Registry);
|
|
initializeScalarOpts(Registry);
|
|
initializeObjCARCOpts(Registry);
|
|
initializeVectorization(Registry);
|
|
initializeIPO(Registry);
|
|
initializeAnalysis(Registry);
|
|
initializeTransformUtils(Registry);
|
|
initializeInstCombine(Registry);
|
|
initializeInstrumentation(Registry);
|
|
initializeTarget(Registry);
|
|
// For codegen passes, only passes that do IR to IR transformation are
|
|
// supported.
|
|
initializeExpandMemCmpPassPass(Registry);
|
|
initializeScalarizeMaskedMemIntrinPass(Registry);
|
|
initializeCodeGenPreparePass(Registry);
|
|
initializeAtomicExpandPass(Registry);
|
|
initializeRewriteSymbolsLegacyPassPass(Registry);
|
|
initializeWinEHPreparePass(Registry);
|
|
initializeDwarfEHPreparePass(Registry);
|
|
initializeSafeStackLegacyPassPass(Registry);
|
|
initializeSjLjEHPreparePass(Registry);
|
|
initializePreISelIntrinsicLoweringLegacyPassPass(Registry);
|
|
initializeGlobalMergePass(Registry);
|
|
initializeIndirectBrExpandPassPass(Registry);
|
|
initializeInterleavedAccessPass(Registry);
|
|
initializeEntryExitInstrumenterPass(Registry);
|
|
initializePostInlineEntryExitInstrumenterPass(Registry);
|
|
initializeUnreachableBlockElimLegacyPassPass(Registry);
|
|
initializeExpandReductionsPass(Registry);
|
|
initializeWriteBitcodePassPass(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;
|
|
|
|
Context.setDiscardValueNames(DiscardValueNames);
|
|
if (!DisableDITypeMap)
|
|
Context.enableDebugTypeODRUniquing();
|
|
|
|
if (PassRemarksWithHotness)
|
|
Context.setDiagnosticsHotnessRequested(true);
|
|
|
|
if (PassRemarksHotnessThreshold)
|
|
Context.setDiagnosticsHotnessThreshold(PassRemarksHotnessThreshold);
|
|
|
|
std::unique_ptr<ToolOutputFile> OptRemarkFile;
|
|
if (RemarksFilename != "") {
|
|
std::error_code EC;
|
|
OptRemarkFile =
|
|
llvm::make_unique<ToolOutputFile>(RemarksFilename, EC, sys::fs::F_None);
|
|
if (EC) {
|
|
errs() << EC.message() << '\n';
|
|
return 1;
|
|
}
|
|
Context.setDiagnosticsOutputFile(
|
|
llvm::make_unique<yaml::Output>(OptRemarkFile->os()));
|
|
}
|
|
|
|
// Load the input module...
|
|
std::unique_ptr<Module> M =
|
|
parseIRFile(InputFilename, Err, Context, !NoVerify);
|
|
|
|
if (!M) {
|
|
Err.print(argv[0], errs());
|
|
return 1;
|
|
}
|
|
|
|
// Strip debug info before running the verifier.
|
|
if (StripDebug)
|
|
StripDebugInfo(*M);
|
|
|
|
// Immediately run the verifier to catch any problems before starting up the
|
|
// pass pipelines. Otherwise we can crash on broken code during
|
|
// doInitialization().
|
|
if (!NoVerify && verifyModule(*M, &errs())) {
|
|
errs() << argv[0] << ": " << InputFilename
|
|
<< ": error: input module is broken!\n";
|
|
return 1;
|
|
}
|
|
|
|
// If we are supposed to override the target triple or data layout, do so now.
|
|
if (!TargetTriple.empty())
|
|
M->setTargetTriple(Triple::normalize(TargetTriple));
|
|
if (!ClDataLayout.empty())
|
|
M->setDataLayout(ClDataLayout);
|
|
|
|
// Figure out what stream we are supposed to write to...
|
|
std::unique_ptr<ToolOutputFile> Out;
|
|
std::unique_ptr<ToolOutputFile> ThinLinkOut;
|
|
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 ToolOutputFile(OutputFilename, EC, sys::fs::F_None));
|
|
if (EC) {
|
|
errs() << EC.message() << '\n';
|
|
return 1;
|
|
}
|
|
|
|
if (!ThinLinkBitcodeFile.empty()) {
|
|
ThinLinkOut.reset(
|
|
new ToolOutputFile(ThinLinkBitcodeFile, EC, sys::fs::F_None));
|
|
if (EC) {
|
|
errs() << EC.message() << '\n';
|
|
return 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
Triple ModuleTriple(M->getTargetTriple());
|
|
std::string CPUStr, FeaturesStr;
|
|
TargetMachine *Machine = nullptr;
|
|
const TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
|
|
|
|
if (ModuleTriple.getArch()) {
|
|
CPUStr = getCPUStr();
|
|
FeaturesStr = getFeaturesStr();
|
|
Machine = GetTargetMachine(ModuleTriple, CPUStr, FeaturesStr, Options);
|
|
}
|
|
|
|
std::unique_ptr<TargetMachine> TM(Machine);
|
|
|
|
// Override function attributes based on CPUStr, FeaturesStr, and command line
|
|
// flags.
|
|
setFunctionAttributes(CPUStr, FeaturesStr, *M);
|
|
|
|
// 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
|
|
: (OutputThinLTOBC ? OK_OutputThinLTOBitcode : 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], *M, TM.get(), Out.get(), ThinLinkOut.get(),
|
|
OptRemarkFile.get(), PassPipeline, OK, VK,
|
|
PreserveAssemblyUseListOrder,
|
|
PreserveBitcodeUseListOrder, EmitSummaryIndex,
|
|
EmitModuleHash)
|
|
? 0
|
|
: 1;
|
|
}
|
|
|
|
// Create a PassManager to hold and optimize the collection of passes we are
|
|
// about to build.
|
|
//
|
|
legacy::PassManager Passes;
|
|
|
|
// Add an appropriate TargetLibraryInfo pass for the module's triple.
|
|
TargetLibraryInfoImpl TLII(ModuleTriple);
|
|
|
|
// The -disable-simplify-libcalls flag actually disables all builtin optzns.
|
|
if (DisableSimplifyLibCalls)
|
|
TLII.disableAllFunctions();
|
|
Passes.add(new TargetLibraryInfoWrapperPass(TLII));
|
|
|
|
// Add internal analysis passes from the target machine.
|
|
Passes.add(createTargetTransformInfoWrapperPass(TM ? TM->getTargetIRAnalysis()
|
|
: TargetIRAnalysis()));
|
|
|
|
std::unique_ptr<legacy::FunctionPassManager> FPasses;
|
|
if (OptLevelO0 || OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz ||
|
|
OptLevelO3) {
|
|
FPasses.reset(new legacy::FunctionPassManager(M.get()));
|
|
FPasses->add(createTargetTransformInfoWrapperPass(
|
|
TM ? TM->getTargetIRAnalysis() : TargetIRAnalysis()));
|
|
}
|
|
|
|
if (PrintBreakpoints) {
|
|
// Default to standard output.
|
|
if (!Out) {
|
|
if (OutputFilename.empty())
|
|
OutputFilename = "-";
|
|
|
|
std::error_code EC;
|
|
Out = llvm::make_unique<ToolOutputFile>(OutputFilename, EC,
|
|
sys::fs::F_None);
|
|
if (EC) {
|
|
errs() << EC.message() << '\n';
|
|
return 1;
|
|
}
|
|
}
|
|
Passes.add(createBreakpointPrinter(Out->os()));
|
|
NoOutput = true;
|
|
}
|
|
|
|
if (TM) {
|
|
// FIXME: We should dyn_cast this when supported.
|
|
auto <M = static_cast<LLVMTargetMachine &>(*TM);
|
|
Pass *TPC = LTM.createPassConfig(Passes);
|
|
Passes.add(TPC);
|
|
}
|
|
|
|
// 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 (OptLevelO0 && OptLevelO0.getPosition() < PassList.getPosition(i)) {
|
|
AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0);
|
|
OptLevelO0 = false;
|
|
}
|
|
|
|
if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
|
|
AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0);
|
|
OptLevelO1 = false;
|
|
}
|
|
|
|
if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
|
|
AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0);
|
|
OptLevelO2 = false;
|
|
}
|
|
|
|
if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) {
|
|
AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1);
|
|
OptLevelOs = false;
|
|
}
|
|
|
|
if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) {
|
|
AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2);
|
|
OptLevelOz = false;
|
|
}
|
|
|
|
if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
|
|
AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0);
|
|
OptLevelO3 = false;
|
|
}
|
|
|
|
const PassInfo *PassInf = PassList[i];
|
|
Pass *P = nullptr;
|
|
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(), "", PreserveAssemblyUseListOrder));
|
|
}
|
|
|
|
if (StandardLinkOpts) {
|
|
AddStandardLinkPasses(Passes);
|
|
StandardLinkOpts = false;
|
|
}
|
|
|
|
if (OptLevelO0)
|
|
AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0);
|
|
|
|
if (OptLevelO1)
|
|
AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0);
|
|
|
|
if (OptLevelO2)
|
|
AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0);
|
|
|
|
if (OptLevelOs)
|
|
AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1);
|
|
|
|
if (OptLevelOz)
|
|
AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2);
|
|
|
|
if (OptLevelO3)
|
|
AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0);
|
|
|
|
if (FPasses) {
|
|
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());
|
|
|
|
// In run twice mode, we want to make sure the output is bit-by-bit
|
|
// equivalent if we run the pass manager again, so setup two buffers and
|
|
// a stream to write to them. Note that llc does something similar and it
|
|
// may be worth to abstract this out in the future.
|
|
SmallVector<char, 0> Buffer;
|
|
SmallVector<char, 0> CompileTwiceBuffer;
|
|
std::unique_ptr<raw_svector_ostream> BOS;
|
|
raw_ostream *OS = nullptr;
|
|
|
|
// Write bitcode or assembly to the output as the last step...
|
|
if (!NoOutput && !AnalyzeOnly) {
|
|
assert(Out);
|
|
OS = &Out->os();
|
|
if (RunTwice) {
|
|
BOS = make_unique<raw_svector_ostream>(Buffer);
|
|
OS = BOS.get();
|
|
}
|
|
if (OutputAssembly) {
|
|
if (EmitSummaryIndex)
|
|
report_fatal_error("Text output is incompatible with -module-summary");
|
|
if (EmitModuleHash)
|
|
report_fatal_error("Text output is incompatible with -module-hash");
|
|
Passes.add(createPrintModulePass(*OS, "", PreserveAssemblyUseListOrder));
|
|
} else if (OutputThinLTOBC)
|
|
Passes.add(createWriteThinLTOBitcodePass(
|
|
*OS, ThinLinkOut ? &ThinLinkOut->os() : nullptr));
|
|
else
|
|
Passes.add(createBitcodeWriterPass(*OS, PreserveBitcodeUseListOrder,
|
|
EmitSummaryIndex, EmitModuleHash));
|
|
}
|
|
|
|
// Before executing passes, print the final values of the LLVM options.
|
|
cl::PrintOptionValues();
|
|
|
|
// If requested, run all passes again with the same pass manager to catch
|
|
// bugs caused by persistent state in the passes
|
|
if (RunTwice) {
|
|
std::unique_ptr<Module> M2(CloneModule(M.get()));
|
|
Passes.run(*M2);
|
|
CompileTwiceBuffer = Buffer;
|
|
Buffer.clear();
|
|
}
|
|
|
|
// Now that we have all of the passes ready, run them.
|
|
Passes.run(*M);
|
|
|
|
// Compare the two outputs and make sure they're the same
|
|
if (RunTwice) {
|
|
assert(Out);
|
|
if (Buffer.size() != CompileTwiceBuffer.size() ||
|
|
(memcmp(Buffer.data(), CompileTwiceBuffer.data(), Buffer.size()) !=
|
|
0)) {
|
|
errs() << "Running the pass manager twice changed the output.\n"
|
|
"Writing the result of the second run to the specified output.\n"
|
|
"To generate the one-run comparison binary, just run without\n"
|
|
"the compile-twice option\n";
|
|
Out->os() << BOS->str();
|
|
Out->keep();
|
|
if (OptRemarkFile)
|
|
OptRemarkFile->keep();
|
|
return 1;
|
|
}
|
|
Out->os() << BOS->str();
|
|
}
|
|
|
|
// Declare success.
|
|
if (!NoOutput || PrintBreakpoints)
|
|
Out->keep();
|
|
|
|
if (OptRemarkFile)
|
|
OptRemarkFile->keep();
|
|
|
|
if (ThinLinkOut)
|
|
ThinLinkOut->keep();
|
|
|
|
return 0;
|
|
}
|