1*0b57cec5SDimitry Andric //===- opt.cpp - The LLVM Modular Optimizer -------------------------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // Optimizations may be specified an arbitrary number of times on the command 10*0b57cec5SDimitry Andric // line, They are run in the order specified. 11*0b57cec5SDimitry Andric // 12*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 13*0b57cec5SDimitry Andric 14*0b57cec5SDimitry Andric #include "BreakpointPrinter.h" 15*0b57cec5SDimitry Andric #include "Debugify.h" 16*0b57cec5SDimitry Andric #include "NewPMDriver.h" 17*0b57cec5SDimitry Andric #include "PassPrinters.h" 18*0b57cec5SDimitry Andric #include "llvm/ADT/Triple.h" 19*0b57cec5SDimitry Andric #include "llvm/Analysis/CallGraph.h" 20*0b57cec5SDimitry Andric #include "llvm/Analysis/CallGraphSCCPass.h" 21*0b57cec5SDimitry Andric #include "llvm/Analysis/LoopPass.h" 22*0b57cec5SDimitry Andric #include "llvm/Analysis/RegionPass.h" 23*0b57cec5SDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h" 24*0b57cec5SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h" 25*0b57cec5SDimitry Andric #include "llvm/Bitcode/BitcodeWriterPass.h" 26*0b57cec5SDimitry Andric #include "llvm/CodeGen/CommandFlags.inc" 27*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h" 28*0b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h" 29*0b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 30*0b57cec5SDimitry Andric #include "llvm/IR/DebugInfo.h" 31*0b57cec5SDimitry Andric #include "llvm/IR/IRPrintingPasses.h" 32*0b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h" 33*0b57cec5SDimitry Andric #include "llvm/IR/LegacyPassManager.h" 34*0b57cec5SDimitry Andric #include "llvm/IR/LegacyPassNameParser.h" 35*0b57cec5SDimitry Andric #include "llvm/IR/Module.h" 36*0b57cec5SDimitry Andric #include "llvm/IR/RemarkStreamer.h" 37*0b57cec5SDimitry Andric #include "llvm/IR/Verifier.h" 38*0b57cec5SDimitry Andric #include "llvm/IRReader/IRReader.h" 39*0b57cec5SDimitry Andric #include "llvm/InitializePasses.h" 40*0b57cec5SDimitry Andric #include "llvm/LinkAllIR.h" 41*0b57cec5SDimitry Andric #include "llvm/LinkAllPasses.h" 42*0b57cec5SDimitry Andric #include "llvm/MC/SubtargetFeature.h" 43*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 44*0b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h" 45*0b57cec5SDimitry Andric #include "llvm/Support/Host.h" 46*0b57cec5SDimitry Andric #include "llvm/Support/InitLLVM.h" 47*0b57cec5SDimitry Andric #include "llvm/Support/PluginLoader.h" 48*0b57cec5SDimitry Andric #include "llvm/Support/SourceMgr.h" 49*0b57cec5SDimitry Andric #include "llvm/Support/SystemUtils.h" 50*0b57cec5SDimitry Andric #include "llvm/Support/TargetRegistry.h" 51*0b57cec5SDimitry Andric #include "llvm/Support/TargetSelect.h" 52*0b57cec5SDimitry Andric #include "llvm/Support/ToolOutputFile.h" 53*0b57cec5SDimitry Andric #include "llvm/Support/YAMLTraits.h" 54*0b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 55*0b57cec5SDimitry Andric #include "llvm/Transforms/Coroutines.h" 56*0b57cec5SDimitry Andric #include "llvm/Transforms/IPO/AlwaysInliner.h" 57*0b57cec5SDimitry Andric #include "llvm/Transforms/IPO/PassManagerBuilder.h" 58*0b57cec5SDimitry Andric #include "llvm/Transforms/Utils/Cloning.h" 59*0b57cec5SDimitry Andric #include <algorithm> 60*0b57cec5SDimitry Andric #include <memory> 61*0b57cec5SDimitry Andric using namespace llvm; 62*0b57cec5SDimitry Andric using namespace opt_tool; 63*0b57cec5SDimitry Andric 64*0b57cec5SDimitry Andric // The OptimizationList is automatically populated with registered Passes by the 65*0b57cec5SDimitry Andric // PassNameParser. 66*0b57cec5SDimitry Andric // 67*0b57cec5SDimitry Andric static cl::list<const PassInfo*, bool, PassNameParser> 68*0b57cec5SDimitry Andric PassList(cl::desc("Optimizations available:")); 69*0b57cec5SDimitry Andric 70*0b57cec5SDimitry Andric // This flag specifies a textual description of the optimization pass pipeline 71*0b57cec5SDimitry Andric // to run over the module. This flag switches opt to use the new pass manager 72*0b57cec5SDimitry Andric // infrastructure, completely disabling all of the flags specific to the old 73*0b57cec5SDimitry Andric // pass management. 74*0b57cec5SDimitry Andric static cl::opt<std::string> PassPipeline( 75*0b57cec5SDimitry Andric "passes", 76*0b57cec5SDimitry Andric cl::desc("A textual description of the pass pipeline for optimizing"), 77*0b57cec5SDimitry Andric cl::Hidden); 78*0b57cec5SDimitry Andric 79*0b57cec5SDimitry Andric // Other command line options... 80*0b57cec5SDimitry Andric // 81*0b57cec5SDimitry Andric static cl::opt<std::string> 82*0b57cec5SDimitry Andric InputFilename(cl::Positional, cl::desc("<input bitcode file>"), 83*0b57cec5SDimitry Andric cl::init("-"), cl::value_desc("filename")); 84*0b57cec5SDimitry Andric 85*0b57cec5SDimitry Andric static cl::opt<std::string> 86*0b57cec5SDimitry Andric OutputFilename("o", cl::desc("Override output filename"), 87*0b57cec5SDimitry Andric cl::value_desc("filename")); 88*0b57cec5SDimitry Andric 89*0b57cec5SDimitry Andric static cl::opt<bool> 90*0b57cec5SDimitry Andric Force("f", cl::desc("Enable binary output on terminals")); 91*0b57cec5SDimitry Andric 92*0b57cec5SDimitry Andric static cl::opt<bool> 93*0b57cec5SDimitry Andric PrintEachXForm("p", cl::desc("Print module after each transformation")); 94*0b57cec5SDimitry Andric 95*0b57cec5SDimitry Andric static cl::opt<bool> 96*0b57cec5SDimitry Andric NoOutput("disable-output", 97*0b57cec5SDimitry Andric cl::desc("Do not write result bitcode file"), cl::Hidden); 98*0b57cec5SDimitry Andric 99*0b57cec5SDimitry Andric static cl::opt<bool> 100*0b57cec5SDimitry Andric OutputAssembly("S", cl::desc("Write output as LLVM assembly")); 101*0b57cec5SDimitry Andric 102*0b57cec5SDimitry Andric static cl::opt<bool> 103*0b57cec5SDimitry Andric OutputThinLTOBC("thinlto-bc", 104*0b57cec5SDimitry Andric cl::desc("Write output as ThinLTO-ready bitcode")); 105*0b57cec5SDimitry Andric 106*0b57cec5SDimitry Andric static cl::opt<bool> 107*0b57cec5SDimitry Andric SplitLTOUnit("thinlto-split-lto-unit", 108*0b57cec5SDimitry Andric cl::desc("Enable splitting of a ThinLTO LTOUnit")); 109*0b57cec5SDimitry Andric 110*0b57cec5SDimitry Andric static cl::opt<std::string> ThinLinkBitcodeFile( 111*0b57cec5SDimitry Andric "thin-link-bitcode-file", cl::value_desc("filename"), 112*0b57cec5SDimitry Andric cl::desc( 113*0b57cec5SDimitry Andric "A file in which to write minimized bitcode for the thin link only")); 114*0b57cec5SDimitry Andric 115*0b57cec5SDimitry Andric static cl::opt<bool> 116*0b57cec5SDimitry Andric NoVerify("disable-verify", cl::desc("Do not run the verifier"), cl::Hidden); 117*0b57cec5SDimitry Andric 118*0b57cec5SDimitry Andric static cl::opt<bool> 119*0b57cec5SDimitry Andric VerifyEach("verify-each", cl::desc("Verify after each transform")); 120*0b57cec5SDimitry Andric 121*0b57cec5SDimitry Andric static cl::opt<bool> 122*0b57cec5SDimitry Andric DisableDITypeMap("disable-debug-info-type-map", 123*0b57cec5SDimitry Andric cl::desc("Don't use a uniquing type map for debug info")); 124*0b57cec5SDimitry Andric 125*0b57cec5SDimitry Andric static cl::opt<bool> 126*0b57cec5SDimitry Andric StripDebug("strip-debug", 127*0b57cec5SDimitry Andric cl::desc("Strip debugger symbol info from translation unit")); 128*0b57cec5SDimitry Andric 129*0b57cec5SDimitry Andric static cl::opt<bool> 130*0b57cec5SDimitry Andric StripNamedMetadata("strip-named-metadata", 131*0b57cec5SDimitry Andric cl::desc("Strip module-level named metadata")); 132*0b57cec5SDimitry Andric 133*0b57cec5SDimitry Andric static cl::opt<bool> DisableInline("disable-inlining", 134*0b57cec5SDimitry Andric cl::desc("Do not run the inliner pass")); 135*0b57cec5SDimitry Andric 136*0b57cec5SDimitry Andric static cl::opt<bool> 137*0b57cec5SDimitry Andric DisableOptimizations("disable-opt", 138*0b57cec5SDimitry Andric cl::desc("Do not run any optimization passes")); 139*0b57cec5SDimitry Andric 140*0b57cec5SDimitry Andric static cl::opt<bool> 141*0b57cec5SDimitry Andric StandardLinkOpts("std-link-opts", 142*0b57cec5SDimitry Andric cl::desc("Include the standard link time optimizations")); 143*0b57cec5SDimitry Andric 144*0b57cec5SDimitry Andric static cl::opt<bool> 145*0b57cec5SDimitry Andric OptLevelO0("O0", 146*0b57cec5SDimitry Andric cl::desc("Optimization level 0. Similar to clang -O0")); 147*0b57cec5SDimitry Andric 148*0b57cec5SDimitry Andric static cl::opt<bool> 149*0b57cec5SDimitry Andric OptLevelO1("O1", 150*0b57cec5SDimitry Andric cl::desc("Optimization level 1. Similar to clang -O1")); 151*0b57cec5SDimitry Andric 152*0b57cec5SDimitry Andric static cl::opt<bool> 153*0b57cec5SDimitry Andric OptLevelO2("O2", 154*0b57cec5SDimitry Andric cl::desc("Optimization level 2. Similar to clang -O2")); 155*0b57cec5SDimitry Andric 156*0b57cec5SDimitry Andric static cl::opt<bool> 157*0b57cec5SDimitry Andric OptLevelOs("Os", 158*0b57cec5SDimitry Andric cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os")); 159*0b57cec5SDimitry Andric 160*0b57cec5SDimitry Andric static cl::opt<bool> 161*0b57cec5SDimitry Andric OptLevelOz("Oz", 162*0b57cec5SDimitry Andric cl::desc("Like -Os but reduces code size further. Similar to clang -Oz")); 163*0b57cec5SDimitry Andric 164*0b57cec5SDimitry Andric static cl::opt<bool> 165*0b57cec5SDimitry Andric OptLevelO3("O3", 166*0b57cec5SDimitry Andric cl::desc("Optimization level 3. Similar to clang -O3")); 167*0b57cec5SDimitry Andric 168*0b57cec5SDimitry Andric static cl::opt<unsigned> 169*0b57cec5SDimitry Andric CodeGenOptLevel("codegen-opt-level", 170*0b57cec5SDimitry Andric cl::desc("Override optimization level for codegen hooks")); 171*0b57cec5SDimitry Andric 172*0b57cec5SDimitry Andric static cl::opt<std::string> 173*0b57cec5SDimitry Andric TargetTriple("mtriple", cl::desc("Override target triple for module")); 174*0b57cec5SDimitry Andric 175*0b57cec5SDimitry Andric static cl::opt<bool> 176*0b57cec5SDimitry Andric DisableLoopUnrolling("disable-loop-unrolling", 177*0b57cec5SDimitry Andric cl::desc("Disable loop unrolling in all relevant passes"), 178*0b57cec5SDimitry Andric cl::init(false)); 179*0b57cec5SDimitry Andric 180*0b57cec5SDimitry Andric static cl::opt<bool> 181*0b57cec5SDimitry Andric DisableSLPVectorization("disable-slp-vectorization", 182*0b57cec5SDimitry Andric cl::desc("Disable the slp vectorization pass"), 183*0b57cec5SDimitry Andric cl::init(false)); 184*0b57cec5SDimitry Andric 185*0b57cec5SDimitry Andric static cl::opt<bool> EmitSummaryIndex("module-summary", 186*0b57cec5SDimitry Andric cl::desc("Emit module summary index"), 187*0b57cec5SDimitry Andric cl::init(false)); 188*0b57cec5SDimitry Andric 189*0b57cec5SDimitry Andric static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"), 190*0b57cec5SDimitry Andric cl::init(false)); 191*0b57cec5SDimitry Andric 192*0b57cec5SDimitry Andric static cl::opt<bool> 193*0b57cec5SDimitry Andric DisableSimplifyLibCalls("disable-simplify-libcalls", 194*0b57cec5SDimitry Andric cl::desc("Disable simplify-libcalls")); 195*0b57cec5SDimitry Andric 196*0b57cec5SDimitry Andric static cl::opt<bool> 197*0b57cec5SDimitry Andric Quiet("q", cl::desc("Obsolete option"), cl::Hidden); 198*0b57cec5SDimitry Andric 199*0b57cec5SDimitry Andric static cl::alias 200*0b57cec5SDimitry Andric QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet)); 201*0b57cec5SDimitry Andric 202*0b57cec5SDimitry Andric static cl::opt<bool> 203*0b57cec5SDimitry Andric AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization")); 204*0b57cec5SDimitry Andric 205*0b57cec5SDimitry Andric static cl::opt<bool> EnableDebugify( 206*0b57cec5SDimitry Andric "enable-debugify", 207*0b57cec5SDimitry Andric cl::desc( 208*0b57cec5SDimitry Andric "Start the pipeline with debugify and end it with check-debugify")); 209*0b57cec5SDimitry Andric 210*0b57cec5SDimitry Andric static cl::opt<bool> DebugifyEach( 211*0b57cec5SDimitry Andric "debugify-each", 212*0b57cec5SDimitry Andric cl::desc( 213*0b57cec5SDimitry Andric "Start each pass with debugify and end it with check-debugify")); 214*0b57cec5SDimitry Andric 215*0b57cec5SDimitry Andric static cl::opt<std::string> 216*0b57cec5SDimitry Andric DebugifyExport("debugify-export", 217*0b57cec5SDimitry Andric cl::desc("Export per-pass debugify statistics to this file"), 218*0b57cec5SDimitry Andric cl::value_desc("filename"), cl::init("")); 219*0b57cec5SDimitry Andric 220*0b57cec5SDimitry Andric static cl::opt<bool> 221*0b57cec5SDimitry Andric PrintBreakpoints("print-breakpoints-for-testing", 222*0b57cec5SDimitry Andric cl::desc("Print select breakpoints location for testing")); 223*0b57cec5SDimitry Andric 224*0b57cec5SDimitry Andric static cl::opt<std::string> ClDataLayout("data-layout", 225*0b57cec5SDimitry Andric cl::desc("data layout string to use"), 226*0b57cec5SDimitry Andric cl::value_desc("layout-string"), 227*0b57cec5SDimitry Andric cl::init("")); 228*0b57cec5SDimitry Andric 229*0b57cec5SDimitry Andric static cl::opt<bool> PreserveBitcodeUseListOrder( 230*0b57cec5SDimitry Andric "preserve-bc-uselistorder", 231*0b57cec5SDimitry Andric cl::desc("Preserve use-list order when writing LLVM bitcode."), 232*0b57cec5SDimitry Andric cl::init(true), cl::Hidden); 233*0b57cec5SDimitry Andric 234*0b57cec5SDimitry Andric static cl::opt<bool> PreserveAssemblyUseListOrder( 235*0b57cec5SDimitry Andric "preserve-ll-uselistorder", 236*0b57cec5SDimitry Andric cl::desc("Preserve use-list order when writing LLVM assembly."), 237*0b57cec5SDimitry Andric cl::init(false), cl::Hidden); 238*0b57cec5SDimitry Andric 239*0b57cec5SDimitry Andric static cl::opt<bool> 240*0b57cec5SDimitry Andric RunTwice("run-twice", 241*0b57cec5SDimitry Andric cl::desc("Run all passes twice, re-using the same pass manager."), 242*0b57cec5SDimitry Andric cl::init(false), cl::Hidden); 243*0b57cec5SDimitry Andric 244*0b57cec5SDimitry Andric static cl::opt<bool> DiscardValueNames( 245*0b57cec5SDimitry Andric "discard-value-names", 246*0b57cec5SDimitry Andric cl::desc("Discard names from Value (other than GlobalValue)."), 247*0b57cec5SDimitry Andric cl::init(false), cl::Hidden); 248*0b57cec5SDimitry Andric 249*0b57cec5SDimitry Andric static cl::opt<bool> Coroutines( 250*0b57cec5SDimitry Andric "enable-coroutines", 251*0b57cec5SDimitry Andric cl::desc("Enable coroutine passes."), 252*0b57cec5SDimitry Andric cl::init(false), cl::Hidden); 253*0b57cec5SDimitry Andric 254*0b57cec5SDimitry Andric static cl::opt<bool> RemarksWithHotness( 255*0b57cec5SDimitry Andric "pass-remarks-with-hotness", 256*0b57cec5SDimitry Andric cl::desc("With PGO, include profile count in optimization remarks"), 257*0b57cec5SDimitry Andric cl::Hidden); 258*0b57cec5SDimitry Andric 259*0b57cec5SDimitry Andric static cl::opt<unsigned> 260*0b57cec5SDimitry Andric RemarksHotnessThreshold("pass-remarks-hotness-threshold", 261*0b57cec5SDimitry Andric cl::desc("Minimum profile count required for " 262*0b57cec5SDimitry Andric "an optimization remark to be output"), 263*0b57cec5SDimitry Andric cl::Hidden); 264*0b57cec5SDimitry Andric 265*0b57cec5SDimitry Andric static cl::opt<std::string> 266*0b57cec5SDimitry Andric RemarksFilename("pass-remarks-output", 267*0b57cec5SDimitry Andric cl::desc("Output filename for pass remarks"), 268*0b57cec5SDimitry Andric cl::value_desc("filename")); 269*0b57cec5SDimitry Andric 270*0b57cec5SDimitry Andric static cl::opt<std::string> 271*0b57cec5SDimitry Andric RemarksPasses("pass-remarks-filter", 272*0b57cec5SDimitry Andric cl::desc("Only record optimization remarks from passes whose " 273*0b57cec5SDimitry Andric "names match the given regular expression"), 274*0b57cec5SDimitry Andric cl::value_desc("regex")); 275*0b57cec5SDimitry Andric 276*0b57cec5SDimitry Andric static cl::opt<std::string> RemarksFormat( 277*0b57cec5SDimitry Andric "pass-remarks-format", 278*0b57cec5SDimitry Andric cl::desc("The format used for serializing remarks (default: YAML)"), 279*0b57cec5SDimitry Andric cl::value_desc("format"), cl::init("yaml")); 280*0b57cec5SDimitry Andric 281*0b57cec5SDimitry Andric cl::opt<PGOKind> 282*0b57cec5SDimitry Andric PGOKindFlag("pgo-kind", cl::init(NoPGO), cl::Hidden, 283*0b57cec5SDimitry Andric cl::desc("The kind of profile guided optimization"), 284*0b57cec5SDimitry Andric cl::values(clEnumValN(NoPGO, "nopgo", "Do not use PGO."), 285*0b57cec5SDimitry Andric clEnumValN(InstrGen, "pgo-instr-gen-pipeline", 286*0b57cec5SDimitry Andric "Instrument the IR to generate profile."), 287*0b57cec5SDimitry Andric clEnumValN(InstrUse, "pgo-instr-use-pipeline", 288*0b57cec5SDimitry Andric "Use instrumented profile to guide PGO."), 289*0b57cec5SDimitry Andric clEnumValN(SampleUse, "pgo-sample-use-pipeline", 290*0b57cec5SDimitry Andric "Use sampled profile to guide PGO."))); 291*0b57cec5SDimitry Andric cl::opt<std::string> ProfileFile("profile-file", 292*0b57cec5SDimitry Andric cl::desc("Path to the profile."), cl::Hidden); 293*0b57cec5SDimitry Andric 294*0b57cec5SDimitry Andric cl::opt<CSPGOKind> CSPGOKindFlag( 295*0b57cec5SDimitry Andric "cspgo-kind", cl::init(NoCSPGO), cl::Hidden, 296*0b57cec5SDimitry Andric cl::desc("The kind of context sensitive profile guided optimization"), 297*0b57cec5SDimitry Andric cl::values( 298*0b57cec5SDimitry Andric clEnumValN(NoCSPGO, "nocspgo", "Do not use CSPGO."), 299*0b57cec5SDimitry Andric clEnumValN( 300*0b57cec5SDimitry Andric CSInstrGen, "cspgo-instr-gen-pipeline", 301*0b57cec5SDimitry Andric "Instrument (context sensitive) the IR to generate profile."), 302*0b57cec5SDimitry Andric clEnumValN( 303*0b57cec5SDimitry Andric CSInstrUse, "cspgo-instr-use-pipeline", 304*0b57cec5SDimitry Andric "Use instrumented (context sensitive) profile to guide PGO."))); 305*0b57cec5SDimitry Andric cl::opt<std::string> CSProfileGenFile( 306*0b57cec5SDimitry Andric "cs-profilegen-file", 307*0b57cec5SDimitry Andric cl::desc("Path to the instrumented context sensitive profile."), 308*0b57cec5SDimitry Andric cl::Hidden); 309*0b57cec5SDimitry Andric 310*0b57cec5SDimitry Andric class OptCustomPassManager : public legacy::PassManager { 311*0b57cec5SDimitry Andric DebugifyStatsMap DIStatsMap; 312*0b57cec5SDimitry Andric 313*0b57cec5SDimitry Andric public: 314*0b57cec5SDimitry Andric using super = legacy::PassManager; 315*0b57cec5SDimitry Andric 316*0b57cec5SDimitry Andric void add(Pass *P) override { 317*0b57cec5SDimitry Andric // Wrap each pass with (-check)-debugify passes if requested, making 318*0b57cec5SDimitry Andric // exceptions for passes which shouldn't see -debugify instrumentation. 319*0b57cec5SDimitry Andric bool WrapWithDebugify = DebugifyEach && !P->getAsImmutablePass() && 320*0b57cec5SDimitry Andric !isIRPrintingPass(P) && !isBitcodeWriterPass(P); 321*0b57cec5SDimitry Andric if (!WrapWithDebugify) { 322*0b57cec5SDimitry Andric super::add(P); 323*0b57cec5SDimitry Andric return; 324*0b57cec5SDimitry Andric } 325*0b57cec5SDimitry Andric 326*0b57cec5SDimitry Andric // Apply -debugify/-check-debugify before/after each pass and collect 327*0b57cec5SDimitry Andric // debug info loss statistics. 328*0b57cec5SDimitry Andric PassKind Kind = P->getPassKind(); 329*0b57cec5SDimitry Andric StringRef Name = P->getPassName(); 330*0b57cec5SDimitry Andric 331*0b57cec5SDimitry Andric // TODO: Implement Debugify for BasicBlockPass, LoopPass. 332*0b57cec5SDimitry Andric switch (Kind) { 333*0b57cec5SDimitry Andric case PT_Function: 334*0b57cec5SDimitry Andric super::add(createDebugifyFunctionPass()); 335*0b57cec5SDimitry Andric super::add(P); 336*0b57cec5SDimitry Andric super::add(createCheckDebugifyFunctionPass(true, Name, &DIStatsMap)); 337*0b57cec5SDimitry Andric break; 338*0b57cec5SDimitry Andric case PT_Module: 339*0b57cec5SDimitry Andric super::add(createDebugifyModulePass()); 340*0b57cec5SDimitry Andric super::add(P); 341*0b57cec5SDimitry Andric super::add(createCheckDebugifyModulePass(true, Name, &DIStatsMap)); 342*0b57cec5SDimitry Andric break; 343*0b57cec5SDimitry Andric default: 344*0b57cec5SDimitry Andric super::add(P); 345*0b57cec5SDimitry Andric break; 346*0b57cec5SDimitry Andric } 347*0b57cec5SDimitry Andric } 348*0b57cec5SDimitry Andric 349*0b57cec5SDimitry Andric const DebugifyStatsMap &getDebugifyStatsMap() const { return DIStatsMap; } 350*0b57cec5SDimitry Andric }; 351*0b57cec5SDimitry Andric 352*0b57cec5SDimitry Andric static inline void addPass(legacy::PassManagerBase &PM, Pass *P) { 353*0b57cec5SDimitry Andric // Add the pass to the pass manager... 354*0b57cec5SDimitry Andric PM.add(P); 355*0b57cec5SDimitry Andric 356*0b57cec5SDimitry Andric // If we are verifying all of the intermediate steps, add the verifier... 357*0b57cec5SDimitry Andric if (VerifyEach) 358*0b57cec5SDimitry Andric PM.add(createVerifierPass()); 359*0b57cec5SDimitry Andric } 360*0b57cec5SDimitry Andric 361*0b57cec5SDimitry Andric /// This routine adds optimization passes based on selected optimization level, 362*0b57cec5SDimitry Andric /// OptLevel. 363*0b57cec5SDimitry Andric /// 364*0b57cec5SDimitry Andric /// OptLevel - Optimization Level 365*0b57cec5SDimitry Andric static void AddOptimizationPasses(legacy::PassManagerBase &MPM, 366*0b57cec5SDimitry Andric legacy::FunctionPassManager &FPM, 367*0b57cec5SDimitry Andric TargetMachine *TM, unsigned OptLevel, 368*0b57cec5SDimitry Andric unsigned SizeLevel) { 369*0b57cec5SDimitry Andric if (!NoVerify || VerifyEach) 370*0b57cec5SDimitry Andric FPM.add(createVerifierPass()); // Verify that input is correct 371*0b57cec5SDimitry Andric 372*0b57cec5SDimitry Andric PassManagerBuilder Builder; 373*0b57cec5SDimitry Andric Builder.OptLevel = OptLevel; 374*0b57cec5SDimitry Andric Builder.SizeLevel = SizeLevel; 375*0b57cec5SDimitry Andric 376*0b57cec5SDimitry Andric if (DisableInline) { 377*0b57cec5SDimitry Andric // No inlining pass 378*0b57cec5SDimitry Andric } else if (OptLevel > 1) { 379*0b57cec5SDimitry Andric Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false); 380*0b57cec5SDimitry Andric } else { 381*0b57cec5SDimitry Andric Builder.Inliner = createAlwaysInlinerLegacyPass(); 382*0b57cec5SDimitry Andric } 383*0b57cec5SDimitry Andric Builder.DisableUnrollLoops = (DisableLoopUnrolling.getNumOccurrences() > 0) ? 384*0b57cec5SDimitry Andric DisableLoopUnrolling : OptLevel == 0; 385*0b57cec5SDimitry Andric 386*0b57cec5SDimitry Andric // Check if vectorization is explicitly disabled via -vectorize-loops=false. 387*0b57cec5SDimitry Andric // The flag enables vectorization in the LoopVectorize pass, it is on by 388*0b57cec5SDimitry Andric // default, and if it was disabled, leave it disabled here. 389*0b57cec5SDimitry Andric // Another flag that exists: -loop-vectorize, controls adding the pass to the 390*0b57cec5SDimitry Andric // pass manager. If set, the pass is added, and there is no additional check 391*0b57cec5SDimitry Andric // here for it. 392*0b57cec5SDimitry Andric if (Builder.LoopVectorize) 393*0b57cec5SDimitry Andric Builder.LoopVectorize = OptLevel > 1 && SizeLevel < 2; 394*0b57cec5SDimitry Andric 395*0b57cec5SDimitry Andric // When #pragma vectorize is on for SLP, do the same as above 396*0b57cec5SDimitry Andric Builder.SLPVectorize = 397*0b57cec5SDimitry Andric DisableSLPVectorization ? false : OptLevel > 1 && SizeLevel < 2; 398*0b57cec5SDimitry Andric 399*0b57cec5SDimitry Andric if (TM) 400*0b57cec5SDimitry Andric TM->adjustPassManager(Builder); 401*0b57cec5SDimitry Andric 402*0b57cec5SDimitry Andric if (Coroutines) 403*0b57cec5SDimitry Andric addCoroutinePassesToExtensionPoints(Builder); 404*0b57cec5SDimitry Andric 405*0b57cec5SDimitry Andric switch (PGOKindFlag) { 406*0b57cec5SDimitry Andric case InstrGen: 407*0b57cec5SDimitry Andric Builder.EnablePGOInstrGen = true; 408*0b57cec5SDimitry Andric Builder.PGOInstrGen = ProfileFile; 409*0b57cec5SDimitry Andric break; 410*0b57cec5SDimitry Andric case InstrUse: 411*0b57cec5SDimitry Andric Builder.PGOInstrUse = ProfileFile; 412*0b57cec5SDimitry Andric break; 413*0b57cec5SDimitry Andric case SampleUse: 414*0b57cec5SDimitry Andric Builder.PGOSampleUse = ProfileFile; 415*0b57cec5SDimitry Andric break; 416*0b57cec5SDimitry Andric default: 417*0b57cec5SDimitry Andric break; 418*0b57cec5SDimitry Andric } 419*0b57cec5SDimitry Andric 420*0b57cec5SDimitry Andric switch (CSPGOKindFlag) { 421*0b57cec5SDimitry Andric case CSInstrGen: 422*0b57cec5SDimitry Andric Builder.EnablePGOCSInstrGen = true; 423*0b57cec5SDimitry Andric break; 424*0b57cec5SDimitry Andric case CSInstrUse: 425*0b57cec5SDimitry Andric Builder.EnablePGOCSInstrUse = true; 426*0b57cec5SDimitry Andric break; 427*0b57cec5SDimitry Andric default: 428*0b57cec5SDimitry Andric break; 429*0b57cec5SDimitry Andric } 430*0b57cec5SDimitry Andric 431*0b57cec5SDimitry Andric Builder.populateFunctionPassManager(FPM); 432*0b57cec5SDimitry Andric Builder.populateModulePassManager(MPM); 433*0b57cec5SDimitry Andric } 434*0b57cec5SDimitry Andric 435*0b57cec5SDimitry Andric static void AddStandardLinkPasses(legacy::PassManagerBase &PM) { 436*0b57cec5SDimitry Andric PassManagerBuilder Builder; 437*0b57cec5SDimitry Andric Builder.VerifyInput = true; 438*0b57cec5SDimitry Andric if (DisableOptimizations) 439*0b57cec5SDimitry Andric Builder.OptLevel = 0; 440*0b57cec5SDimitry Andric 441*0b57cec5SDimitry Andric if (!DisableInline) 442*0b57cec5SDimitry Andric Builder.Inliner = createFunctionInliningPass(); 443*0b57cec5SDimitry Andric Builder.populateLTOPassManager(PM); 444*0b57cec5SDimitry Andric } 445*0b57cec5SDimitry Andric 446*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 447*0b57cec5SDimitry Andric // CodeGen-related helper functions. 448*0b57cec5SDimitry Andric // 449*0b57cec5SDimitry Andric 450*0b57cec5SDimitry Andric static CodeGenOpt::Level GetCodeGenOptLevel() { 451*0b57cec5SDimitry Andric if (CodeGenOptLevel.getNumOccurrences()) 452*0b57cec5SDimitry Andric return static_cast<CodeGenOpt::Level>(unsigned(CodeGenOptLevel)); 453*0b57cec5SDimitry Andric if (OptLevelO1) 454*0b57cec5SDimitry Andric return CodeGenOpt::Less; 455*0b57cec5SDimitry Andric if (OptLevelO2) 456*0b57cec5SDimitry Andric return CodeGenOpt::Default; 457*0b57cec5SDimitry Andric if (OptLevelO3) 458*0b57cec5SDimitry Andric return CodeGenOpt::Aggressive; 459*0b57cec5SDimitry Andric return CodeGenOpt::None; 460*0b57cec5SDimitry Andric } 461*0b57cec5SDimitry Andric 462*0b57cec5SDimitry Andric // Returns the TargetMachine instance or zero if no triple is provided. 463*0b57cec5SDimitry Andric static TargetMachine* GetTargetMachine(Triple TheTriple, StringRef CPUStr, 464*0b57cec5SDimitry Andric StringRef FeaturesStr, 465*0b57cec5SDimitry Andric const TargetOptions &Options) { 466*0b57cec5SDimitry Andric std::string Error; 467*0b57cec5SDimitry Andric const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple, 468*0b57cec5SDimitry Andric Error); 469*0b57cec5SDimitry Andric // Some modules don't specify a triple, and this is okay. 470*0b57cec5SDimitry Andric if (!TheTarget) { 471*0b57cec5SDimitry Andric return nullptr; 472*0b57cec5SDimitry Andric } 473*0b57cec5SDimitry Andric 474*0b57cec5SDimitry Andric return TheTarget->createTargetMachine(TheTriple.getTriple(), CPUStr, 475*0b57cec5SDimitry Andric FeaturesStr, Options, getRelocModel(), 476*0b57cec5SDimitry Andric getCodeModel(), GetCodeGenOptLevel()); 477*0b57cec5SDimitry Andric } 478*0b57cec5SDimitry Andric 479*0b57cec5SDimitry Andric #ifdef LINK_POLLY_INTO_TOOLS 480*0b57cec5SDimitry Andric namespace polly { 481*0b57cec5SDimitry Andric void initializePollyPasses(llvm::PassRegistry &Registry); 482*0b57cec5SDimitry Andric } 483*0b57cec5SDimitry Andric #endif 484*0b57cec5SDimitry Andric 485*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 486*0b57cec5SDimitry Andric // main for opt 487*0b57cec5SDimitry Andric // 488*0b57cec5SDimitry Andric int main(int argc, char **argv) { 489*0b57cec5SDimitry Andric InitLLVM X(argc, argv); 490*0b57cec5SDimitry Andric 491*0b57cec5SDimitry Andric // Enable debug stream buffering. 492*0b57cec5SDimitry Andric EnableDebugBuffering = true; 493*0b57cec5SDimitry Andric 494*0b57cec5SDimitry Andric LLVMContext Context; 495*0b57cec5SDimitry Andric 496*0b57cec5SDimitry Andric InitializeAllTargets(); 497*0b57cec5SDimitry Andric InitializeAllTargetMCs(); 498*0b57cec5SDimitry Andric InitializeAllAsmPrinters(); 499*0b57cec5SDimitry Andric InitializeAllAsmParsers(); 500*0b57cec5SDimitry Andric 501*0b57cec5SDimitry Andric // Initialize passes 502*0b57cec5SDimitry Andric PassRegistry &Registry = *PassRegistry::getPassRegistry(); 503*0b57cec5SDimitry Andric initializeCore(Registry); 504*0b57cec5SDimitry Andric initializeCoroutines(Registry); 505*0b57cec5SDimitry Andric initializeScalarOpts(Registry); 506*0b57cec5SDimitry Andric initializeObjCARCOpts(Registry); 507*0b57cec5SDimitry Andric initializeVectorization(Registry); 508*0b57cec5SDimitry Andric initializeIPO(Registry); 509*0b57cec5SDimitry Andric initializeAnalysis(Registry); 510*0b57cec5SDimitry Andric initializeTransformUtils(Registry); 511*0b57cec5SDimitry Andric initializeInstCombine(Registry); 512*0b57cec5SDimitry Andric initializeAggressiveInstCombine(Registry); 513*0b57cec5SDimitry Andric initializeInstrumentation(Registry); 514*0b57cec5SDimitry Andric initializeTarget(Registry); 515*0b57cec5SDimitry Andric // For codegen passes, only passes that do IR to IR transformation are 516*0b57cec5SDimitry Andric // supported. 517*0b57cec5SDimitry Andric initializeExpandMemCmpPassPass(Registry); 518*0b57cec5SDimitry Andric initializeScalarizeMaskedMemIntrinPass(Registry); 519*0b57cec5SDimitry Andric initializeCodeGenPreparePass(Registry); 520*0b57cec5SDimitry Andric initializeAtomicExpandPass(Registry); 521*0b57cec5SDimitry Andric initializeRewriteSymbolsLegacyPassPass(Registry); 522*0b57cec5SDimitry Andric initializeWinEHPreparePass(Registry); 523*0b57cec5SDimitry Andric initializeDwarfEHPreparePass(Registry); 524*0b57cec5SDimitry Andric initializeSafeStackLegacyPassPass(Registry); 525*0b57cec5SDimitry Andric initializeSjLjEHPreparePass(Registry); 526*0b57cec5SDimitry Andric initializeStackProtectorPass(Registry); 527*0b57cec5SDimitry Andric initializePreISelIntrinsicLoweringLegacyPassPass(Registry); 528*0b57cec5SDimitry Andric initializeGlobalMergePass(Registry); 529*0b57cec5SDimitry Andric initializeIndirectBrExpandPassPass(Registry); 530*0b57cec5SDimitry Andric initializeInterleavedLoadCombinePass(Registry); 531*0b57cec5SDimitry Andric initializeInterleavedAccessPass(Registry); 532*0b57cec5SDimitry Andric initializeEntryExitInstrumenterPass(Registry); 533*0b57cec5SDimitry Andric initializePostInlineEntryExitInstrumenterPass(Registry); 534*0b57cec5SDimitry Andric initializeUnreachableBlockElimLegacyPassPass(Registry); 535*0b57cec5SDimitry Andric initializeExpandReductionsPass(Registry); 536*0b57cec5SDimitry Andric initializeWasmEHPreparePass(Registry); 537*0b57cec5SDimitry Andric initializeWriteBitcodePassPass(Registry); 538*0b57cec5SDimitry Andric initializeHardwareLoopsPass(Registry); 539*0b57cec5SDimitry Andric 540*0b57cec5SDimitry Andric #ifdef LINK_POLLY_INTO_TOOLS 541*0b57cec5SDimitry Andric polly::initializePollyPasses(Registry); 542*0b57cec5SDimitry Andric #endif 543*0b57cec5SDimitry Andric 544*0b57cec5SDimitry Andric cl::ParseCommandLineOptions(argc, argv, 545*0b57cec5SDimitry Andric "llvm .bc -> .bc modular optimizer and analysis printer\n"); 546*0b57cec5SDimitry Andric 547*0b57cec5SDimitry Andric if (AnalyzeOnly && NoOutput) { 548*0b57cec5SDimitry Andric errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n"; 549*0b57cec5SDimitry Andric return 1; 550*0b57cec5SDimitry Andric } 551*0b57cec5SDimitry Andric 552*0b57cec5SDimitry Andric SMDiagnostic Err; 553*0b57cec5SDimitry Andric 554*0b57cec5SDimitry Andric Context.setDiscardValueNames(DiscardValueNames); 555*0b57cec5SDimitry Andric if (!DisableDITypeMap) 556*0b57cec5SDimitry Andric Context.enableDebugTypeODRUniquing(); 557*0b57cec5SDimitry Andric 558*0b57cec5SDimitry Andric Expected<std::unique_ptr<ToolOutputFile>> RemarksFileOrErr = 559*0b57cec5SDimitry Andric setupOptimizationRemarks(Context, RemarksFilename, RemarksPasses, 560*0b57cec5SDimitry Andric RemarksFormat, RemarksWithHotness, 561*0b57cec5SDimitry Andric RemarksHotnessThreshold); 562*0b57cec5SDimitry Andric if (Error E = RemarksFileOrErr.takeError()) { 563*0b57cec5SDimitry Andric errs() << toString(std::move(E)) << '\n'; 564*0b57cec5SDimitry Andric return 1; 565*0b57cec5SDimitry Andric } 566*0b57cec5SDimitry Andric std::unique_ptr<ToolOutputFile> RemarksFile = std::move(*RemarksFileOrErr); 567*0b57cec5SDimitry Andric 568*0b57cec5SDimitry Andric // Load the input module... 569*0b57cec5SDimitry Andric std::unique_ptr<Module> M = 570*0b57cec5SDimitry Andric parseIRFile(InputFilename, Err, Context, !NoVerify, ClDataLayout); 571*0b57cec5SDimitry Andric 572*0b57cec5SDimitry Andric if (!M) { 573*0b57cec5SDimitry Andric Err.print(argv[0], errs()); 574*0b57cec5SDimitry Andric return 1; 575*0b57cec5SDimitry Andric } 576*0b57cec5SDimitry Andric 577*0b57cec5SDimitry Andric // Strip debug info before running the verifier. 578*0b57cec5SDimitry Andric if (StripDebug) 579*0b57cec5SDimitry Andric StripDebugInfo(*M); 580*0b57cec5SDimitry Andric 581*0b57cec5SDimitry Andric // Erase module-level named metadata, if requested. 582*0b57cec5SDimitry Andric if (StripNamedMetadata) { 583*0b57cec5SDimitry Andric while (!M->named_metadata_empty()) { 584*0b57cec5SDimitry Andric NamedMDNode *NMD = &*M->named_metadata_begin(); 585*0b57cec5SDimitry Andric M->eraseNamedMetadata(NMD); 586*0b57cec5SDimitry Andric } 587*0b57cec5SDimitry Andric } 588*0b57cec5SDimitry Andric 589*0b57cec5SDimitry Andric // If we are supposed to override the target triple or data layout, do so now. 590*0b57cec5SDimitry Andric if (!TargetTriple.empty()) 591*0b57cec5SDimitry Andric M->setTargetTriple(Triple::normalize(TargetTriple)); 592*0b57cec5SDimitry Andric 593*0b57cec5SDimitry Andric // Immediately run the verifier to catch any problems before starting up the 594*0b57cec5SDimitry Andric // pass pipelines. Otherwise we can crash on broken code during 595*0b57cec5SDimitry Andric // doInitialization(). 596*0b57cec5SDimitry Andric if (!NoVerify && verifyModule(*M, &errs())) { 597*0b57cec5SDimitry Andric errs() << argv[0] << ": " << InputFilename 598*0b57cec5SDimitry Andric << ": error: input module is broken!\n"; 599*0b57cec5SDimitry Andric return 1; 600*0b57cec5SDimitry Andric } 601*0b57cec5SDimitry Andric 602*0b57cec5SDimitry Andric // Figure out what stream we are supposed to write to... 603*0b57cec5SDimitry Andric std::unique_ptr<ToolOutputFile> Out; 604*0b57cec5SDimitry Andric std::unique_ptr<ToolOutputFile> ThinLinkOut; 605*0b57cec5SDimitry Andric if (NoOutput) { 606*0b57cec5SDimitry Andric if (!OutputFilename.empty()) 607*0b57cec5SDimitry Andric errs() << "WARNING: The -o (output filename) option is ignored when\n" 608*0b57cec5SDimitry Andric "the --disable-output option is used.\n"; 609*0b57cec5SDimitry Andric } else { 610*0b57cec5SDimitry Andric // Default to standard output. 611*0b57cec5SDimitry Andric if (OutputFilename.empty()) 612*0b57cec5SDimitry Andric OutputFilename = "-"; 613*0b57cec5SDimitry Andric 614*0b57cec5SDimitry Andric std::error_code EC; 615*0b57cec5SDimitry Andric Out.reset(new ToolOutputFile(OutputFilename, EC, sys::fs::F_None)); 616*0b57cec5SDimitry Andric if (EC) { 617*0b57cec5SDimitry Andric errs() << EC.message() << '\n'; 618*0b57cec5SDimitry Andric return 1; 619*0b57cec5SDimitry Andric } 620*0b57cec5SDimitry Andric 621*0b57cec5SDimitry Andric if (!ThinLinkBitcodeFile.empty()) { 622*0b57cec5SDimitry Andric ThinLinkOut.reset( 623*0b57cec5SDimitry Andric new ToolOutputFile(ThinLinkBitcodeFile, EC, sys::fs::F_None)); 624*0b57cec5SDimitry Andric if (EC) { 625*0b57cec5SDimitry Andric errs() << EC.message() << '\n'; 626*0b57cec5SDimitry Andric return 1; 627*0b57cec5SDimitry Andric } 628*0b57cec5SDimitry Andric } 629*0b57cec5SDimitry Andric } 630*0b57cec5SDimitry Andric 631*0b57cec5SDimitry Andric Triple ModuleTriple(M->getTargetTriple()); 632*0b57cec5SDimitry Andric std::string CPUStr, FeaturesStr; 633*0b57cec5SDimitry Andric TargetMachine *Machine = nullptr; 634*0b57cec5SDimitry Andric const TargetOptions Options = InitTargetOptionsFromCodeGenFlags(); 635*0b57cec5SDimitry Andric 636*0b57cec5SDimitry Andric if (ModuleTriple.getArch()) { 637*0b57cec5SDimitry Andric CPUStr = getCPUStr(); 638*0b57cec5SDimitry Andric FeaturesStr = getFeaturesStr(); 639*0b57cec5SDimitry Andric Machine = GetTargetMachine(ModuleTriple, CPUStr, FeaturesStr, Options); 640*0b57cec5SDimitry Andric } else if (ModuleTriple.getArchName() != "unknown" && 641*0b57cec5SDimitry Andric ModuleTriple.getArchName() != "") { 642*0b57cec5SDimitry Andric errs() << argv[0] << ": unrecognized architecture '" 643*0b57cec5SDimitry Andric << ModuleTriple.getArchName() << "' provided.\n"; 644*0b57cec5SDimitry Andric return 1; 645*0b57cec5SDimitry Andric } 646*0b57cec5SDimitry Andric 647*0b57cec5SDimitry Andric std::unique_ptr<TargetMachine> TM(Machine); 648*0b57cec5SDimitry Andric 649*0b57cec5SDimitry Andric // Override function attributes based on CPUStr, FeaturesStr, and command line 650*0b57cec5SDimitry Andric // flags. 651*0b57cec5SDimitry Andric setFunctionAttributes(CPUStr, FeaturesStr, *M); 652*0b57cec5SDimitry Andric 653*0b57cec5SDimitry Andric // If the output is set to be emitted to standard out, and standard out is a 654*0b57cec5SDimitry Andric // console, print out a warning message and refuse to do it. We don't 655*0b57cec5SDimitry Andric // impress anyone by spewing tons of binary goo to a terminal. 656*0b57cec5SDimitry Andric if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly) 657*0b57cec5SDimitry Andric if (CheckBitcodeOutputToConsole(Out->os(), !Quiet)) 658*0b57cec5SDimitry Andric NoOutput = true; 659*0b57cec5SDimitry Andric 660*0b57cec5SDimitry Andric if (OutputThinLTOBC) 661*0b57cec5SDimitry Andric M->addModuleFlag(Module::Error, "EnableSplitLTOUnit", SplitLTOUnit); 662*0b57cec5SDimitry Andric 663*0b57cec5SDimitry Andric if (PassPipeline.getNumOccurrences() > 0) { 664*0b57cec5SDimitry Andric OutputKind OK = OK_NoOutput; 665*0b57cec5SDimitry Andric if (!NoOutput) 666*0b57cec5SDimitry Andric OK = OutputAssembly 667*0b57cec5SDimitry Andric ? OK_OutputAssembly 668*0b57cec5SDimitry Andric : (OutputThinLTOBC ? OK_OutputThinLTOBitcode : OK_OutputBitcode); 669*0b57cec5SDimitry Andric 670*0b57cec5SDimitry Andric VerifierKind VK = VK_VerifyInAndOut; 671*0b57cec5SDimitry Andric if (NoVerify) 672*0b57cec5SDimitry Andric VK = VK_NoVerifier; 673*0b57cec5SDimitry Andric else if (VerifyEach) 674*0b57cec5SDimitry Andric VK = VK_VerifyEachPass; 675*0b57cec5SDimitry Andric 676*0b57cec5SDimitry Andric // The user has asked to use the new pass manager and provided a pipeline 677*0b57cec5SDimitry Andric // string. Hand off the rest of the functionality to the new code for that 678*0b57cec5SDimitry Andric // layer. 679*0b57cec5SDimitry Andric return runPassPipeline(argv[0], *M, TM.get(), Out.get(), ThinLinkOut.get(), 680*0b57cec5SDimitry Andric RemarksFile.get(), PassPipeline, OK, VK, 681*0b57cec5SDimitry Andric PreserveAssemblyUseListOrder, 682*0b57cec5SDimitry Andric PreserveBitcodeUseListOrder, EmitSummaryIndex, 683*0b57cec5SDimitry Andric EmitModuleHash, EnableDebugify) 684*0b57cec5SDimitry Andric ? 0 685*0b57cec5SDimitry Andric : 1; 686*0b57cec5SDimitry Andric } 687*0b57cec5SDimitry Andric 688*0b57cec5SDimitry Andric // Create a PassManager to hold and optimize the collection of passes we are 689*0b57cec5SDimitry Andric // about to build. 690*0b57cec5SDimitry Andric OptCustomPassManager Passes; 691*0b57cec5SDimitry Andric bool AddOneTimeDebugifyPasses = EnableDebugify && !DebugifyEach; 692*0b57cec5SDimitry Andric 693*0b57cec5SDimitry Andric // Add an appropriate TargetLibraryInfo pass for the module's triple. 694*0b57cec5SDimitry Andric TargetLibraryInfoImpl TLII(ModuleTriple); 695*0b57cec5SDimitry Andric 696*0b57cec5SDimitry Andric // The -disable-simplify-libcalls flag actually disables all builtin optzns. 697*0b57cec5SDimitry Andric if (DisableSimplifyLibCalls) 698*0b57cec5SDimitry Andric TLII.disableAllFunctions(); 699*0b57cec5SDimitry Andric Passes.add(new TargetLibraryInfoWrapperPass(TLII)); 700*0b57cec5SDimitry Andric 701*0b57cec5SDimitry Andric // Add internal analysis passes from the target machine. 702*0b57cec5SDimitry Andric Passes.add(createTargetTransformInfoWrapperPass(TM ? TM->getTargetIRAnalysis() 703*0b57cec5SDimitry Andric : TargetIRAnalysis())); 704*0b57cec5SDimitry Andric 705*0b57cec5SDimitry Andric if (AddOneTimeDebugifyPasses) 706*0b57cec5SDimitry Andric Passes.add(createDebugifyModulePass()); 707*0b57cec5SDimitry Andric 708*0b57cec5SDimitry Andric std::unique_ptr<legacy::FunctionPassManager> FPasses; 709*0b57cec5SDimitry Andric if (OptLevelO0 || OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || 710*0b57cec5SDimitry Andric OptLevelO3) { 711*0b57cec5SDimitry Andric FPasses.reset(new legacy::FunctionPassManager(M.get())); 712*0b57cec5SDimitry Andric FPasses->add(createTargetTransformInfoWrapperPass( 713*0b57cec5SDimitry Andric TM ? TM->getTargetIRAnalysis() : TargetIRAnalysis())); 714*0b57cec5SDimitry Andric } 715*0b57cec5SDimitry Andric 716*0b57cec5SDimitry Andric if (PrintBreakpoints) { 717*0b57cec5SDimitry Andric // Default to standard output. 718*0b57cec5SDimitry Andric if (!Out) { 719*0b57cec5SDimitry Andric if (OutputFilename.empty()) 720*0b57cec5SDimitry Andric OutputFilename = "-"; 721*0b57cec5SDimitry Andric 722*0b57cec5SDimitry Andric std::error_code EC; 723*0b57cec5SDimitry Andric Out = llvm::make_unique<ToolOutputFile>(OutputFilename, EC, 724*0b57cec5SDimitry Andric sys::fs::F_None); 725*0b57cec5SDimitry Andric if (EC) { 726*0b57cec5SDimitry Andric errs() << EC.message() << '\n'; 727*0b57cec5SDimitry Andric return 1; 728*0b57cec5SDimitry Andric } 729*0b57cec5SDimitry Andric } 730*0b57cec5SDimitry Andric Passes.add(createBreakpointPrinter(Out->os())); 731*0b57cec5SDimitry Andric NoOutput = true; 732*0b57cec5SDimitry Andric } 733*0b57cec5SDimitry Andric 734*0b57cec5SDimitry Andric if (TM) { 735*0b57cec5SDimitry Andric // FIXME: We should dyn_cast this when supported. 736*0b57cec5SDimitry Andric auto <M = static_cast<LLVMTargetMachine &>(*TM); 737*0b57cec5SDimitry Andric Pass *TPC = LTM.createPassConfig(Passes); 738*0b57cec5SDimitry Andric Passes.add(TPC); 739*0b57cec5SDimitry Andric } 740*0b57cec5SDimitry Andric 741*0b57cec5SDimitry Andric // Create a new optimization pass for each one specified on the command line 742*0b57cec5SDimitry Andric for (unsigned i = 0; i < PassList.size(); ++i) { 743*0b57cec5SDimitry Andric if (StandardLinkOpts && 744*0b57cec5SDimitry Andric StandardLinkOpts.getPosition() < PassList.getPosition(i)) { 745*0b57cec5SDimitry Andric AddStandardLinkPasses(Passes); 746*0b57cec5SDimitry Andric StandardLinkOpts = false; 747*0b57cec5SDimitry Andric } 748*0b57cec5SDimitry Andric 749*0b57cec5SDimitry Andric if (OptLevelO0 && OptLevelO0.getPosition() < PassList.getPosition(i)) { 750*0b57cec5SDimitry Andric AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0); 751*0b57cec5SDimitry Andric OptLevelO0 = false; 752*0b57cec5SDimitry Andric } 753*0b57cec5SDimitry Andric 754*0b57cec5SDimitry Andric if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) { 755*0b57cec5SDimitry Andric AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0); 756*0b57cec5SDimitry Andric OptLevelO1 = false; 757*0b57cec5SDimitry Andric } 758*0b57cec5SDimitry Andric 759*0b57cec5SDimitry Andric if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) { 760*0b57cec5SDimitry Andric AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0); 761*0b57cec5SDimitry Andric OptLevelO2 = false; 762*0b57cec5SDimitry Andric } 763*0b57cec5SDimitry Andric 764*0b57cec5SDimitry Andric if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) { 765*0b57cec5SDimitry Andric AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1); 766*0b57cec5SDimitry Andric OptLevelOs = false; 767*0b57cec5SDimitry Andric } 768*0b57cec5SDimitry Andric 769*0b57cec5SDimitry Andric if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) { 770*0b57cec5SDimitry Andric AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2); 771*0b57cec5SDimitry Andric OptLevelOz = false; 772*0b57cec5SDimitry Andric } 773*0b57cec5SDimitry Andric 774*0b57cec5SDimitry Andric if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) { 775*0b57cec5SDimitry Andric AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0); 776*0b57cec5SDimitry Andric OptLevelO3 = false; 777*0b57cec5SDimitry Andric } 778*0b57cec5SDimitry Andric 779*0b57cec5SDimitry Andric const PassInfo *PassInf = PassList[i]; 780*0b57cec5SDimitry Andric Pass *P = nullptr; 781*0b57cec5SDimitry Andric if (PassInf->getNormalCtor()) 782*0b57cec5SDimitry Andric P = PassInf->getNormalCtor()(); 783*0b57cec5SDimitry Andric else 784*0b57cec5SDimitry Andric errs() << argv[0] << ": cannot create pass: " 785*0b57cec5SDimitry Andric << PassInf->getPassName() << "\n"; 786*0b57cec5SDimitry Andric if (P) { 787*0b57cec5SDimitry Andric PassKind Kind = P->getPassKind(); 788*0b57cec5SDimitry Andric addPass(Passes, P); 789*0b57cec5SDimitry Andric 790*0b57cec5SDimitry Andric if (AnalyzeOnly) { 791*0b57cec5SDimitry Andric switch (Kind) { 792*0b57cec5SDimitry Andric case PT_BasicBlock: 793*0b57cec5SDimitry Andric Passes.add(createBasicBlockPassPrinter(PassInf, Out->os(), Quiet)); 794*0b57cec5SDimitry Andric break; 795*0b57cec5SDimitry Andric case PT_Region: 796*0b57cec5SDimitry Andric Passes.add(createRegionPassPrinter(PassInf, Out->os(), Quiet)); 797*0b57cec5SDimitry Andric break; 798*0b57cec5SDimitry Andric case PT_Loop: 799*0b57cec5SDimitry Andric Passes.add(createLoopPassPrinter(PassInf, Out->os(), Quiet)); 800*0b57cec5SDimitry Andric break; 801*0b57cec5SDimitry Andric case PT_Function: 802*0b57cec5SDimitry Andric Passes.add(createFunctionPassPrinter(PassInf, Out->os(), Quiet)); 803*0b57cec5SDimitry Andric break; 804*0b57cec5SDimitry Andric case PT_CallGraphSCC: 805*0b57cec5SDimitry Andric Passes.add(createCallGraphPassPrinter(PassInf, Out->os(), Quiet)); 806*0b57cec5SDimitry Andric break; 807*0b57cec5SDimitry Andric default: 808*0b57cec5SDimitry Andric Passes.add(createModulePassPrinter(PassInf, Out->os(), Quiet)); 809*0b57cec5SDimitry Andric break; 810*0b57cec5SDimitry Andric } 811*0b57cec5SDimitry Andric } 812*0b57cec5SDimitry Andric } 813*0b57cec5SDimitry Andric 814*0b57cec5SDimitry Andric if (PrintEachXForm) 815*0b57cec5SDimitry Andric Passes.add( 816*0b57cec5SDimitry Andric createPrintModulePass(errs(), "", PreserveAssemblyUseListOrder)); 817*0b57cec5SDimitry Andric } 818*0b57cec5SDimitry Andric 819*0b57cec5SDimitry Andric if (StandardLinkOpts) { 820*0b57cec5SDimitry Andric AddStandardLinkPasses(Passes); 821*0b57cec5SDimitry Andric StandardLinkOpts = false; 822*0b57cec5SDimitry Andric } 823*0b57cec5SDimitry Andric 824*0b57cec5SDimitry Andric if (OptLevelO0) 825*0b57cec5SDimitry Andric AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0); 826*0b57cec5SDimitry Andric 827*0b57cec5SDimitry Andric if (OptLevelO1) 828*0b57cec5SDimitry Andric AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0); 829*0b57cec5SDimitry Andric 830*0b57cec5SDimitry Andric if (OptLevelO2) 831*0b57cec5SDimitry Andric AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0); 832*0b57cec5SDimitry Andric 833*0b57cec5SDimitry Andric if (OptLevelOs) 834*0b57cec5SDimitry Andric AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1); 835*0b57cec5SDimitry Andric 836*0b57cec5SDimitry Andric if (OptLevelOz) 837*0b57cec5SDimitry Andric AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2); 838*0b57cec5SDimitry Andric 839*0b57cec5SDimitry Andric if (OptLevelO3) 840*0b57cec5SDimitry Andric AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0); 841*0b57cec5SDimitry Andric 842*0b57cec5SDimitry Andric if (FPasses) { 843*0b57cec5SDimitry Andric FPasses->doInitialization(); 844*0b57cec5SDimitry Andric for (Function &F : *M) 845*0b57cec5SDimitry Andric FPasses->run(F); 846*0b57cec5SDimitry Andric FPasses->doFinalization(); 847*0b57cec5SDimitry Andric } 848*0b57cec5SDimitry Andric 849*0b57cec5SDimitry Andric // Check that the module is well formed on completion of optimization 850*0b57cec5SDimitry Andric if (!NoVerify && !VerifyEach) 851*0b57cec5SDimitry Andric Passes.add(createVerifierPass()); 852*0b57cec5SDimitry Andric 853*0b57cec5SDimitry Andric if (AddOneTimeDebugifyPasses) 854*0b57cec5SDimitry Andric Passes.add(createCheckDebugifyModulePass(false)); 855*0b57cec5SDimitry Andric 856*0b57cec5SDimitry Andric // In run twice mode, we want to make sure the output is bit-by-bit 857*0b57cec5SDimitry Andric // equivalent if we run the pass manager again, so setup two buffers and 858*0b57cec5SDimitry Andric // a stream to write to them. Note that llc does something similar and it 859*0b57cec5SDimitry Andric // may be worth to abstract this out in the future. 860*0b57cec5SDimitry Andric SmallVector<char, 0> Buffer; 861*0b57cec5SDimitry Andric SmallVector<char, 0> FirstRunBuffer; 862*0b57cec5SDimitry Andric std::unique_ptr<raw_svector_ostream> BOS; 863*0b57cec5SDimitry Andric raw_ostream *OS = nullptr; 864*0b57cec5SDimitry Andric 865*0b57cec5SDimitry Andric // Write bitcode or assembly to the output as the last step... 866*0b57cec5SDimitry Andric if (!NoOutput && !AnalyzeOnly) { 867*0b57cec5SDimitry Andric assert(Out); 868*0b57cec5SDimitry Andric OS = &Out->os(); 869*0b57cec5SDimitry Andric if (RunTwice) { 870*0b57cec5SDimitry Andric BOS = make_unique<raw_svector_ostream>(Buffer); 871*0b57cec5SDimitry Andric OS = BOS.get(); 872*0b57cec5SDimitry Andric } 873*0b57cec5SDimitry Andric if (OutputAssembly) { 874*0b57cec5SDimitry Andric if (EmitSummaryIndex) 875*0b57cec5SDimitry Andric report_fatal_error("Text output is incompatible with -module-summary"); 876*0b57cec5SDimitry Andric if (EmitModuleHash) 877*0b57cec5SDimitry Andric report_fatal_error("Text output is incompatible with -module-hash"); 878*0b57cec5SDimitry Andric Passes.add(createPrintModulePass(*OS, "", PreserveAssemblyUseListOrder)); 879*0b57cec5SDimitry Andric } else if (OutputThinLTOBC) 880*0b57cec5SDimitry Andric Passes.add(createWriteThinLTOBitcodePass( 881*0b57cec5SDimitry Andric *OS, ThinLinkOut ? &ThinLinkOut->os() : nullptr)); 882*0b57cec5SDimitry Andric else 883*0b57cec5SDimitry Andric Passes.add(createBitcodeWriterPass(*OS, PreserveBitcodeUseListOrder, 884*0b57cec5SDimitry Andric EmitSummaryIndex, EmitModuleHash)); 885*0b57cec5SDimitry Andric } 886*0b57cec5SDimitry Andric 887*0b57cec5SDimitry Andric // Before executing passes, print the final values of the LLVM options. 888*0b57cec5SDimitry Andric cl::PrintOptionValues(); 889*0b57cec5SDimitry Andric 890*0b57cec5SDimitry Andric if (!RunTwice) { 891*0b57cec5SDimitry Andric // Now that we have all of the passes ready, run them. 892*0b57cec5SDimitry Andric Passes.run(*M); 893*0b57cec5SDimitry Andric } else { 894*0b57cec5SDimitry Andric // If requested, run all passes twice with the same pass manager to catch 895*0b57cec5SDimitry Andric // bugs caused by persistent state in the passes. 896*0b57cec5SDimitry Andric std::unique_ptr<Module> M2(CloneModule(*M)); 897*0b57cec5SDimitry Andric // Run all passes on the original module first, so the second run processes 898*0b57cec5SDimitry Andric // the clone to catch CloneModule bugs. 899*0b57cec5SDimitry Andric Passes.run(*M); 900*0b57cec5SDimitry Andric FirstRunBuffer = Buffer; 901*0b57cec5SDimitry Andric Buffer.clear(); 902*0b57cec5SDimitry Andric 903*0b57cec5SDimitry Andric Passes.run(*M2); 904*0b57cec5SDimitry Andric 905*0b57cec5SDimitry Andric // Compare the two outputs and make sure they're the same 906*0b57cec5SDimitry Andric assert(Out); 907*0b57cec5SDimitry Andric if (Buffer.size() != FirstRunBuffer.size() || 908*0b57cec5SDimitry Andric (memcmp(Buffer.data(), FirstRunBuffer.data(), Buffer.size()) != 0)) { 909*0b57cec5SDimitry Andric errs() 910*0b57cec5SDimitry Andric << "Running the pass manager twice changed the output.\n" 911*0b57cec5SDimitry Andric "Writing the result of the second run to the specified output.\n" 912*0b57cec5SDimitry Andric "To generate the one-run comparison binary, just run without\n" 913*0b57cec5SDimitry Andric "the compile-twice option\n"; 914*0b57cec5SDimitry Andric Out->os() << BOS->str(); 915*0b57cec5SDimitry Andric Out->keep(); 916*0b57cec5SDimitry Andric if (RemarksFile) 917*0b57cec5SDimitry Andric RemarksFile->keep(); 918*0b57cec5SDimitry Andric return 1; 919*0b57cec5SDimitry Andric } 920*0b57cec5SDimitry Andric Out->os() << BOS->str(); 921*0b57cec5SDimitry Andric } 922*0b57cec5SDimitry Andric 923*0b57cec5SDimitry Andric if (DebugifyEach && !DebugifyExport.empty()) 924*0b57cec5SDimitry Andric exportDebugifyStats(DebugifyExport, Passes.getDebugifyStatsMap()); 925*0b57cec5SDimitry Andric 926*0b57cec5SDimitry Andric // Declare success. 927*0b57cec5SDimitry Andric if (!NoOutput || PrintBreakpoints) 928*0b57cec5SDimitry Andric Out->keep(); 929*0b57cec5SDimitry Andric 930*0b57cec5SDimitry Andric if (RemarksFile) 931*0b57cec5SDimitry Andric RemarksFile->keep(); 932*0b57cec5SDimitry Andric 933*0b57cec5SDimitry Andric if (ThinLinkOut) 934*0b57cec5SDimitry Andric ThinLinkOut->keep(); 935*0b57cec5SDimitry Andric 936*0b57cec5SDimitry Andric return 0; 937*0b57cec5SDimitry Andric } 938