#include "BreakpointPrinter.h"
#include "NewPMDriver.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/AsmParser/Parser.h"
#include "llvm/CodeGen/CommandFlags.h"
#include "llvm/CodeGen/TargetPassConfig.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/LLVMRemarkStreamer.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/LegacyPassNameParser.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/ModuleSummaryIndex.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/MC/TargetRegistry.h"
#include "llvm/Passes/PassPlugin.h"
#include "llvm/Remarks/HotnessThresholdParser.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/PluginLoader.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/SystemUtils.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/YAMLTraits.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Transforms/IPO/AlwaysInliner.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/Transforms/Utils/Debugify.h"
#include <algorithm>
#include <memory>
using namespace llvm;
using namespace opt_tool;
static codegen::RegisterCodeGenFlags CFG;
static cl::list<const PassInfo *, bool, PassNameParser> PassList(cl::desc(
"Optimizations available (use '-passes=' for the new pass manager)"));
static cl::opt<bool> EnableNewPassManager(
"enable-new-pm",
cl::desc("Enable the new pass manager, translating "
"'opt -foo' to 'opt -passes=foo'. This is strictly for the new PM "
"migration, use '-passes=' when possible."),
cl::init(true));
static cl::opt<std::string> PassPipeline(
"passes",
cl::desc(
"A textual description of the pass pipeline. To have analysis passes "
"available before a certain pass, add 'require<foo-analysis>'."));
static cl::opt<bool> PrintPasses("print-passes",
cl::desc("Print available passes that can be "
"specified in -passes=foo and exit"));
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>
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<bool>
SplitLTOUnit("thinlto-split-lto-unit",
cl::desc("Enable splitting of a ThinLTO LTOUnit"));
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> NoUpgradeDebugInfo("disable-upgrade-debug-info",
cl::desc("Generate invalid output"),
cl::ReallyHidden);
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>
StripNamedMetadata("strip-named-metadata",
cl::desc("Strip module-level named metadata"));
static cl::opt<bool>
OptLevelO0("O0", cl::desc("Optimization level 0. Similar to clang -O0. "
"Use -passes='default<O0>' for the new PM"));
static cl::opt<bool>
OptLevelO1("O1", cl::desc("Optimization level 1. Similar to clang -O1. "
"Use -passes='default<O1>' for the new PM"));
static cl::opt<bool>
OptLevelO2("O2", cl::desc("Optimization level 2. Similar to clang -O2. "
"Use -passes='default<O2>' for the new PM"));
static cl::opt<bool>
OptLevelOs("Os", cl::desc("Like -O2 but size-conscious. Similar to clang "
"-Os. Use -passes='default<Os>' for the new PM"));
static cl::opt<bool> OptLevelOz(
"Oz",
cl::desc("Like -O2 but optimize for code size above all else. Similar to "
"clang -Oz. Use -passes='default<Oz>' for the new PM"));
static cl::opt<bool>
OptLevelO3("O3", cl::desc("Optimization level 3. Similar to clang -O3. "
"Use -passes='default<O3>' for the new PM"));
static cl::opt<unsigned> CodeGenOptLevel(
"codegen-opt-level",
cl::desc("Override optimization level for codegen hooks, legacy PM only"));
static cl::opt<std::string>
TargetTriple("mtriple", cl::desc("Override target triple for module"));
cl::opt<bool> DisableLoopUnrolling(
"disable-loop-unrolling",
cl::desc("Disable loop unrolling in all relevant passes"), 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::list<std::string> DisableBuiltins(
"disable-builtin",
cl::desc("Disable specific target library builtin function"));
static cl::opt<bool> EnableDebugify(
"enable-debugify",
cl::desc(
"Start the pipeline with debugify and end it with check-debugify"));
static cl::opt<bool> VerifyDebugInfoPreserve(
"verify-debuginfo-preserve",
cl::desc("Start the pipeline with collecting and end it with checking of "
"debug info preservation."));
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 (legacy PM only)."),
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> TimeTrace(
"time-trace",
cl::desc("Record time trace"));
static cl::opt<unsigned> TimeTraceGranularity(
"time-trace-granularity",
cl::desc("Minimum time granularity (in microseconds) traced by time profiler"),
cl::init(500), cl::Hidden);
static cl::opt<std::string>
TimeTraceFile("time-trace-file",
cl::desc("Specify time trace file destination"),
cl::value_desc("filename"));
static cl::opt<bool> RemarksWithHotness(
"pass-remarks-with-hotness",
cl::desc("With PGO, include profile count in optimization remarks"),
cl::Hidden);
static cl::opt<Optional<uint64_t>, false, remarks::HotnessThresholdParser>
RemarksHotnessThreshold(
"pass-remarks-hotness-threshold",
cl::desc("Minimum profile count required for "
"an optimization remark to be output. "
"Use 'auto' to apply the threshold from profile summary."),
cl::value_desc("N or 'auto'"), cl::init(0), cl::Hidden);
static cl::opt<std::string>
RemarksFilename("pass-remarks-output",
cl::desc("Output filename for pass remarks"),
cl::value_desc("filename"));
static cl::opt<std::string>
RemarksPasses("pass-remarks-filter",
cl::desc("Only record optimization remarks from passes whose "
"names match the given regular expression"),
cl::value_desc("regex"));
static cl::opt<std::string> RemarksFormat(
"pass-remarks-format",
cl::desc("The format used for serializing remarks (default: YAML)"),
cl::value_desc("format"), cl::init("yaml"));
static cl::list<std::string>
PassPlugins("load-pass-plugin",
cl::desc("Load passes from plugin library"));
namespace llvm {
cl::opt<PGOKind>
PGOKindFlag("pgo-kind", cl::init(NoPGO), cl::Hidden,
cl::desc("The kind of profile guided optimization"),
cl::values(clEnumValN(NoPGO, "nopgo", "Do not use PGO."),
clEnumValN(InstrGen, "pgo-instr-gen-pipeline",
"Instrument the IR to generate profile."),
clEnumValN(InstrUse, "pgo-instr-use-pipeline",
"Use instrumented profile to guide PGO."),
clEnumValN(SampleUse, "pgo-sample-use-pipeline",
"Use sampled profile to guide PGO.")));
cl::opt<std::string> ProfileFile("profile-file",
cl::desc("Path to the profile."), cl::Hidden);
cl::opt<CSPGOKind> CSPGOKindFlag(
"cspgo-kind", cl::init(NoCSPGO), cl::Hidden,
cl::desc("The kind of context sensitive profile guided optimization"),
cl::values(
clEnumValN(NoCSPGO, "nocspgo", "Do not use CSPGO."),
clEnumValN(
CSInstrGen, "cspgo-instr-gen-pipeline",
"Instrument (context sensitive) the IR to generate profile."),
clEnumValN(
CSInstrUse, "cspgo-instr-use-pipeline",
"Use instrumented (context sensitive) profile to guide PGO.")));
cl::opt<std::string> CSProfileGenFile(
"cs-profilegen-file",
cl::desc("Path to the instrumented context sensitive profile."),
cl::Hidden);
}
static inline void addPass(legacy::PassManagerBase &PM, Pass *P) {
PM.add(P);
if (VerifyEach)
PM.add(createVerifierPass());
}
static void AddOptimizationPasses(legacy::PassManagerBase &MPM,
legacy::FunctionPassManager &FPM,
TargetMachine *TM, unsigned OptLevel,
unsigned SizeLevel) {
if (!NoVerify || VerifyEach)
FPM.add(createVerifierPass());
PassManagerBuilder Builder;
Builder.OptLevel = OptLevel;
Builder.SizeLevel = SizeLevel;
if (OptLevel > 1) {
Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false);
} else {
Builder.Inliner = createAlwaysInlinerLegacyPass();
}
Builder.DisableUnrollLoops = (DisableLoopUnrolling.getNumOccurrences() > 0) ?
DisableLoopUnrolling : OptLevel == 0;
Builder.LoopVectorize = OptLevel > 1 && SizeLevel < 2;
Builder.SLPVectorize = OptLevel > 1 && SizeLevel < 2;
if (TM)
TM->adjustPassManager(Builder);
Builder.populateFunctionPassManager(FPM);
Builder.populateModulePassManager(MPM);
}
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;
}
static TargetMachine* GetTargetMachine(Triple TheTriple, StringRef CPUStr,
StringRef FeaturesStr,
const TargetOptions &Options) {
std::string Error;
const Target *TheTarget =
TargetRegistry::lookupTarget(codegen::getMArch(), TheTriple, Error);
if (!TheTarget) {
return nullptr;
}
return TheTarget->createTargetMachine(
TheTriple.getTriple(), codegen::getCPUStr(), codegen::getFeaturesStr(),
Options, codegen::getExplicitRelocModel(),
codegen::getExplicitCodeModel(), GetCodeGenOptLevel());
}
#ifdef BUILD_EXAMPLES
void initializeExampleIRTransforms(llvm::PassRegistry &Registry);
#endif
struct TimeTracerRAII {
TimeTracerRAII(StringRef ProgramName) {
if (TimeTrace)
timeTraceProfilerInitialize(TimeTraceGranularity, ProgramName);
}
~TimeTracerRAII() {
if (TimeTrace) {
if (auto E = timeTraceProfilerWrite(TimeTraceFile, OutputFilename)) {
handleAllErrors(std::move(E), [&](const StringError &SE) {
errs() << SE.getMessage() << "\n";
});
return;
}
timeTraceProfilerCleanup();
}
}
};
static bool shouldPinPassToLegacyPM(StringRef Pass) {
std::vector<StringRef> PassNameExactToIgnore = {
"nvvm-reflect",
"nvvm-intr-range",
"amdgpu-simplifylib",
"amdgpu-usenative",
"amdgpu-promote-alloca",
"amdgpu-promote-alloca-to-vector",
"amdgpu-lower-kernel-attributes",
"amdgpu-propagate-attributes-early",
"amdgpu-propagate-attributes-late",
"amdgpu-unify-metadata",
"amdgpu-printf-runtime-binding",
"amdgpu-always-inline"};
if (llvm::is_contained(PassNameExactToIgnore, Pass))
return false;
std::vector<StringRef> PassNamePrefix = {
"x86-", "xcore-", "wasm-", "systemz-", "ppc-", "nvvm-",
"nvptx-", "mips-", "lanai-", "hexagon-", "bpf-", "avr-",
"thumb2-", "arm-", "si-", "gcn-", "amdgpu-", "aarch64-",
"amdgcn-", "polly-", "riscv-", "dxil-"};
std::vector<StringRef> PassNameContain = {"ehprepare"};
std::vector<StringRef> PassNameExact = {
"safe-stack", "cost-model",
"codegenprepare", "interleaved-load-combine",
"unreachableblockelim", "verify-safepoint-ir",
"atomic-expand", "expandvp",
"hardware-loops", "type-promotion",
"mve-tail-predication", "interleaved-access",
"global-merge", "pre-isel-intrinsic-lowering",
"expand-reductions", "indirectbr-expand",
"generic-to-nvvm", "expandmemcmp",
"loop-reduce", "lower-amx-type",
"pre-amx-config", "lower-amx-intrinsics",
"polyhedral-info", "print-polyhedral-info",
"replace-with-veclib", "jmc-instrument",
"dot-regions", "dot-regions-only",
"view-regions", "view-regions-only",
"select-optimize"};
for (const auto &P : PassNamePrefix)
if (Pass.startswith(P))
return true;
for (const auto &P : PassNameContain)
if (Pass.contains(P))
return true;
return llvm::is_contained(PassNameExact, Pass);
}
static bool shouldForceLegacyPM() {
for (const auto &P : PassList) {
StringRef Arg = P->getPassArgument();
if (shouldPinPassToLegacyPM(Arg))
return true;
}
return false;
}
int main(int argc, char **argv) {
InitLLVM X(argc, argv);
EnableDebugBuffering = true;
InitializeAllTargets();
InitializeAllTargetMCs();
InitializeAllAsmPrinters();
InitializeAllAsmParsers();
PassRegistry &Registry = *PassRegistry::getPassRegistry();
initializeCore(Registry);
initializeScalarOpts(Registry);
initializeObjCARCOpts(Registry);
initializeVectorization(Registry);
initializeIPO(Registry);
initializeAnalysis(Registry);
initializeTransformUtils(Registry);
initializeInstCombine(Registry);
initializeAggressiveInstCombine(Registry);
initializeInstrumentation(Registry);
initializeTarget(Registry);
initializeExpandMemCmpPassPass(Registry);
initializeScalarizeMaskedMemIntrinLegacyPassPass(Registry);
initializeSelectOptimizePass(Registry);
initializeCodeGenPreparePass(Registry);
initializeAtomicExpandPass(Registry);
initializeRewriteSymbolsLegacyPassPass(Registry);
initializeWinEHPreparePass(Registry);
initializeDwarfEHPrepareLegacyPassPass(Registry);
initializeSafeStackLegacyPassPass(Registry);
initializeSjLjEHPreparePass(Registry);
initializePreISelIntrinsicLoweringLegacyPassPass(Registry);
initializeGlobalMergePass(Registry);
initializeIndirectBrExpandPassPass(Registry);
initializeInterleavedLoadCombinePass(Registry);
initializeInterleavedAccessPass(Registry);
initializeUnreachableBlockElimLegacyPassPass(Registry);
initializeExpandReductionsPass(Registry);
initializeExpandVectorPredicationPass(Registry);
initializeWasmEHPreparePass(Registry);
initializeWriteBitcodePassPass(Registry);
initializeHardwareLoopsPass(Registry);
initializeTypePromotionPass(Registry);
initializeReplaceWithVeclibLegacyPass(Registry);
initializeJMCInstrumenterPass(Registry);
#ifdef BUILD_EXAMPLES
initializeExampleIRTransforms(Registry);
#endif
SmallVector<PassPlugin, 1> PluginList;
PassPlugins.setCallback([&](const std::string &PluginPath) {
auto Plugin = PassPlugin::Load(PluginPath);
if (!Plugin) {
errs() << "Failed to load passes from '" << PluginPath
<< "'. Request ignored.\n";
return;
}
PluginList.emplace_back(Plugin.get());
});
cl::ParseCommandLineOptions(argc, argv,
"llvm .bc -> .bc modular optimizer and analysis printer\n");
LLVMContext Context;
const bool UseNPM = (EnableNewPassManager && !shouldForceLegacyPM()) ||
PassPipeline.getNumOccurrences() > 0;
if (!UseNPM && PluginList.size()) {
errs() << argv[0] << ": " << PassPlugins.ArgStr
<< " specified with legacy PM.\n";
return 1;
}
if (PrintPasses) {
printPasses(outs());
return 0;
}
TimeTracerRAII TimeTracer(argv[0]);
SMDiagnostic Err;
Context.setDiscardValueNames(DiscardValueNames);
if (!DisableDITypeMap)
Context.enableDebugTypeODRUniquing();
Expected<std::unique_ptr<ToolOutputFile>> RemarksFileOrErr =
setupLLVMOptimizationRemarks(Context, RemarksFilename, RemarksPasses,
RemarksFormat, RemarksWithHotness,
RemarksHotnessThreshold);
if (Error E = RemarksFileOrErr.takeError()) {
errs() << toString(std::move(E)) << '\n';
return 1;
}
std::unique_ptr<ToolOutputFile> RemarksFile = std::move(*RemarksFileOrErr);
auto SetDataLayout = [](StringRef) -> Optional<std::string> {
if (ClDataLayout.empty())
return None;
return ClDataLayout;
};
std::unique_ptr<Module> M;
if (NoUpgradeDebugInfo)
M = parseAssemblyFileWithIndexNoUpgradeDebugInfo(
InputFilename, Err, Context, nullptr, SetDataLayout)
.Mod;
else
M = parseIRFile(InputFilename, Err, Context, SetDataLayout);
if (!M) {
Err.print(argv[0], errs());
return 1;
}
if (StripDebug)
StripDebugInfo(*M);
if (StripNamedMetadata) {
while (!M->named_metadata_empty()) {
NamedMDNode *NMD = &*M->named_metadata_begin();
M->eraseNamedMetadata(NMD);
}
}
if (!TargetTriple.empty())
M->setTargetTriple(Triple::normalize(TargetTriple));
if (!NoVerify && verifyModule(*M, &errs())) {
errs() << argv[0] << ": " << InputFilename
<< ": error: input module is broken!\n";
return 1;
}
updateVCallVisibilityInModule(*M,
false,
{});
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 {
if (OutputFilename.empty())
OutputFilename = "-";
std::error_code EC;
sys::fs::OpenFlags Flags =
OutputAssembly ? sys::fs::OF_TextWithCRLF : sys::fs::OF_None;
Out.reset(new ToolOutputFile(OutputFilename, EC, Flags));
if (EC) {
errs() << EC.message() << '\n';
return 1;
}
if (!ThinLinkBitcodeFile.empty()) {
ThinLinkOut.reset(
new ToolOutputFile(ThinLinkBitcodeFile, EC, sys::fs::OF_None));
if (EC) {
errs() << EC.message() << '\n';
return 1;
}
}
}
Triple ModuleTriple(M->getTargetTriple());
std::string CPUStr, FeaturesStr;
TargetMachine *Machine = nullptr;
const TargetOptions Options =
codegen::InitTargetOptionsFromCodeGenFlags(ModuleTriple);
if (ModuleTriple.getArch()) {
CPUStr = codegen::getCPUStr();
FeaturesStr = codegen::getFeaturesStr();
Machine = GetTargetMachine(ModuleTriple, CPUStr, FeaturesStr, Options);
} else if (ModuleTriple.getArchName() != "unknown" &&
ModuleTriple.getArchName() != "") {
errs() << argv[0] << ": unrecognized architecture '"
<< ModuleTriple.getArchName() << "' provided.\n";
return 1;
}
std::unique_ptr<TargetMachine> TM(Machine);
codegen::setFunctionAttributes(CPUStr, FeaturesStr, *M);
if (!Force && !NoOutput && !OutputAssembly)
if (CheckBitcodeOutputToConsole(Out->os()))
NoOutput = true;
if (OutputThinLTOBC)
M->addModuleFlag(Module::Error, "EnableSplitLTOUnit", SplitLTOUnit);
TargetLibraryInfoImpl TLII(ModuleTriple);
if (DisableSimplifyLibCalls)
TLII.disableAllFunctions();
else {
LibFunc F;
for (auto &FuncName : DisableBuiltins)
if (TLII.getLibFunc(FuncName, F))
TLII.setUnavailable(F);
else {
errs() << argv[0] << ": cannot disable nonexistent builtin function "
<< FuncName << '\n';
return 1;
}
}
if (UseNPM) {
if (legacy::debugPassSpecified()) {
errs()
<< "-debug-pass does not work with the new PM, either use "
"-debug-pass-manager, or use the legacy PM (-enable-new-pm=0)\n";
return 1;
}
if (PassPipeline.getNumOccurrences() > 0 && PassList.size() > 0) {
errs()
<< "Cannot specify passes via both -foo-pass and --passes=foo-pass\n";
return 1;
}
auto NumOLevel = OptLevelO0 + OptLevelO1 + OptLevelO2 + OptLevelO3 +
OptLevelOs + OptLevelOz;
if (NumOLevel > 1) {
errs() << "Cannot specify multiple -O#\n";
return 1;
}
if (NumOLevel > 0 &&
(PassPipeline.getNumOccurrences() > 0 || PassList.size() > 0)) {
errs() << "Cannot specify -O# and --passes=/--foo-pass, use "
"-passes='default<O#>,other-pass'\n";
return 1;
}
std::string Pipeline = PassPipeline;
SmallVector<StringRef, 4> Passes;
if (OptLevelO0)
Pipeline = "default<O0>";
if (OptLevelO1)
Pipeline = "default<O1>";
if (OptLevelO2)
Pipeline = "default<O2>";
if (OptLevelO3)
Pipeline = "default<O3>";
if (OptLevelOs)
Pipeline = "default<Os>";
if (OptLevelOz)
Pipeline = "default<Oz>";
for (const auto &P : PassList)
Passes.push_back(P->getPassArgument());
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;
return runPassPipeline(argv[0], *M, TM.get(), &TLII, Out.get(),
ThinLinkOut.get(), RemarksFile.get(), Pipeline,
Passes, PluginList, OK, VK, PreserveAssemblyUseListOrder,
PreserveBitcodeUseListOrder, EmitSummaryIndex,
EmitModuleHash, EnableDebugify,
VerifyDebugInfoPreserve)
? 0
: 1;
}
DebugifyCustomPassManager Passes;
DebugifyStatsMap DIStatsMap;
DebugInfoPerPass DebugInfoBeforePass;
if (DebugifyEach) {
Passes.setDebugifyMode(DebugifyMode::SyntheticDebugInfo);
Passes.setDIStatsMap(DIStatsMap);
} else if (VerifyEachDebugInfoPreserve) {
Passes.setDebugifyMode(DebugifyMode::OriginalDebugInfo);
Passes.setDebugInfoBeforePass(DebugInfoBeforePass);
if (!VerifyDIPreserveExport.empty())
Passes.setOrigDIVerifyBugsReportFilePath(VerifyDIPreserveExport);
}
bool AddOneTimeDebugifyPasses =
(EnableDebugify && !DebugifyEach) ||
(VerifyDebugInfoPreserve && !VerifyEachDebugInfoPreserve);
Passes.add(new TargetLibraryInfoWrapperPass(TLII));
Passes.add(createTargetTransformInfoWrapperPass(TM ? TM->getTargetIRAnalysis()
: TargetIRAnalysis()));
if (AddOneTimeDebugifyPasses) {
if (EnableDebugify) {
Passes.setDIStatsMap(DIStatsMap);
Passes.add(createDebugifyModulePass());
} else if (VerifyDebugInfoPreserve) {
Passes.setDebugInfoBeforePass(DebugInfoBeforePass);
Passes.add(createDebugifyModulePass(
DebugifyMode::OriginalDebugInfo, "",
&(Passes.getDebugInfoPerPass())));
}
}
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) {
if (!Out) {
if (OutputFilename.empty())
OutputFilename = "-";
std::error_code EC;
Out = std::make_unique<ToolOutputFile>(OutputFilename, EC,
sys::fs::OF_None);
if (EC) {
errs() << EC.message() << '\n';
return 1;
}
}
Passes.add(createBreakpointPrinter(Out->os()));
NoOutput = true;
}
if (TM) {
auto <M = static_cast<LLVMTargetMachine &>(*TM);
Pass *TPC = LTM.createPassConfig(Passes);
Passes.add(TPC);
}
for (unsigned i = 0; i < PassList.size(); ++i) {
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)
addPass(Passes, P);
}
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();
}
if (!NoVerify && !VerifyEach)
Passes.add(createVerifierPass());
if (AddOneTimeDebugifyPasses) {
if (EnableDebugify)
Passes.add(createCheckDebugifyModulePass(false));
else if (VerifyDebugInfoPreserve) {
if (!VerifyDIPreserveExport.empty())
Passes.setOrigDIVerifyBugsReportFilePath(VerifyDIPreserveExport);
Passes.add(createCheckDebugifyModulePass(
false, "", nullptr, DebugifyMode::OriginalDebugInfo,
&(Passes.getDebugInfoPerPass()), VerifyDIPreserveExport));
}
}
SmallVector<char, 0> Buffer;
SmallVector<char, 0> FirstRunBuffer;
std::unique_ptr<raw_svector_ostream> BOS;
raw_ostream *OS = nullptr;
const bool ShouldEmitOutput = !NoOutput;
if (ShouldEmitOutput || RunTwice) {
assert(Out);
OS = &Out->os();
if (RunTwice) {
BOS = std::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));
}
cl::PrintOptionValues();
if (!RunTwice) {
Passes.run(*M);
} else {
std::unique_ptr<Module> M2(CloneModule(*M));
Passes.run(*M);
FirstRunBuffer = Buffer;
Buffer.clear();
Passes.run(*M2);
assert(Out);
if (Buffer.size() != FirstRunBuffer.size() ||
(memcmp(Buffer.data(), FirstRunBuffer.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";
if (ShouldEmitOutput) {
Out->os() << BOS->str();
Out->keep();
}
if (RemarksFile)
RemarksFile->keep();
return 1;
}
if (ShouldEmitOutput)
Out->os() << BOS->str();
}
if (DebugifyEach && !DebugifyExport.empty())
exportDebugifyStats(DebugifyExport, Passes.getDebugifyStatsMap());
if (!NoOutput || PrintBreakpoints)
Out->keep();
if (RemarksFile)
RemarksFile->keep();
if (ThinLinkOut)
ThinLinkOut->keep();
return 0;
}