opt.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. //===- opt.cpp - The LLVM Modular Optimizer -------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Optimizations may be specified an arbitrary number of times on the command
  10. // line, They are run in the order specified.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "BreakpointPrinter.h"
  14. #include "NewPMDriver.h"
  15. #include "llvm/ADT/Triple.h"
  16. #include "llvm/Analysis/CallGraph.h"
  17. #include "llvm/Analysis/CallGraphSCCPass.h"
  18. #include "llvm/Analysis/LoopPass.h"
  19. #include "llvm/Analysis/RegionPass.h"
  20. #include "llvm/Analysis/TargetLibraryInfo.h"
  21. #include "llvm/Analysis/TargetTransformInfo.h"
  22. #include "llvm/AsmParser/Parser.h"
  23. #include "llvm/CodeGen/CommandFlags.h"
  24. #include "llvm/CodeGen/TargetPassConfig.h"
  25. #include "llvm/Config/llvm-config.h"
  26. #include "llvm/IR/DataLayout.h"
  27. #include "llvm/IR/DebugInfo.h"
  28. #include "llvm/IR/LLVMContext.h"
  29. #include "llvm/IR/LLVMRemarkStreamer.h"
  30. #include "llvm/IR/LegacyPassManager.h"
  31. #include "llvm/IR/LegacyPassNameParser.h"
  32. #include "llvm/IR/Module.h"
  33. #include "llvm/IR/ModuleSummaryIndex.h"
  34. #include "llvm/IR/Verifier.h"
  35. #include "llvm/IRReader/IRReader.h"
  36. #include "llvm/InitializePasses.h"
  37. #include "llvm/LinkAllIR.h"
  38. #include "llvm/LinkAllPasses.h"
  39. #include "llvm/MC/SubtargetFeature.h"
  40. #include "llvm/MC/TargetRegistry.h"
  41. #include "llvm/Passes/PassPlugin.h"
  42. #include "llvm/Remarks/HotnessThresholdParser.h"
  43. #include "llvm/Support/Debug.h"
  44. #include "llvm/Support/FileSystem.h"
  45. #include "llvm/Support/Host.h"
  46. #include "llvm/Support/InitLLVM.h"
  47. #include "llvm/Support/PluginLoader.h"
  48. #include "llvm/Support/SourceMgr.h"
  49. #include "llvm/Support/SystemUtils.h"
  50. #include "llvm/Support/TargetSelect.h"
  51. #include "llvm/Support/ToolOutputFile.h"
  52. #include "llvm/Support/YAMLTraits.h"
  53. #include "llvm/Target/TargetMachine.h"
  54. #include "llvm/Transforms/IPO/WholeProgramDevirt.h"
  55. #include "llvm/Transforms/Utils/Cloning.h"
  56. #include "llvm/Transforms/Utils/Debugify.h"
  57. #include <algorithm>
  58. #include <memory>
  59. #include <optional>
  60. using namespace llvm;
  61. using namespace opt_tool;
  62. static codegen::RegisterCodeGenFlags CFG;
  63. // The OptimizationList is automatically populated with registered Passes by the
  64. // PassNameParser.
  65. static cl::list<const PassInfo *, bool, PassNameParser> PassList(cl::desc(
  66. "Optimizations available (use '-passes=' for the new pass manager)"));
  67. static cl::opt<bool> EnableNewPassManager(
  68. "enable-new-pm",
  69. cl::desc("Enable the new pass manager, translating "
  70. "'opt -foo' to 'opt -passes=foo'. This is strictly for the new PM "
  71. "migration, use '-passes=' when possible."),
  72. cl::init(true));
  73. // This flag specifies a textual description of the optimization pass pipeline
  74. // to run over the module. This flag switches opt to use the new pass manager
  75. // infrastructure, completely disabling all of the flags specific to the old
  76. // pass management.
  77. static cl::opt<std::string> PassPipeline(
  78. "passes",
  79. cl::desc(
  80. "A textual description of the pass pipeline. To have analysis passes "
  81. "available before a certain pass, add 'require<foo-analysis>'."));
  82. static cl::alias PassPipeline2("p", cl::aliasopt(PassPipeline),
  83. cl::desc("Alias for -passes"));
  84. static cl::opt<bool> PrintPasses("print-passes",
  85. cl::desc("Print available passes that can be "
  86. "specified in -passes=foo and exit"));
  87. static cl::opt<std::string>
  88. InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
  89. cl::init("-"), cl::value_desc("filename"));
  90. static cl::opt<std::string>
  91. OutputFilename("o", cl::desc("Override output filename"),
  92. cl::value_desc("filename"));
  93. static cl::opt<bool>
  94. Force("f", cl::desc("Enable binary output on terminals"));
  95. static cl::opt<bool>
  96. NoOutput("disable-output",
  97. cl::desc("Do not write result bitcode file"), cl::Hidden);
  98. static cl::opt<bool>
  99. OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
  100. static cl::opt<bool>
  101. OutputThinLTOBC("thinlto-bc",
  102. cl::desc("Write output as ThinLTO-ready bitcode"));
  103. static cl::opt<bool>
  104. SplitLTOUnit("thinlto-split-lto-unit",
  105. cl::desc("Enable splitting of a ThinLTO LTOUnit"));
  106. static cl::opt<std::string> ThinLinkBitcodeFile(
  107. "thin-link-bitcode-file", cl::value_desc("filename"),
  108. cl::desc(
  109. "A file in which to write minimized bitcode for the thin link only"));
  110. static cl::opt<bool>
  111. NoVerify("disable-verify", cl::desc("Do not run the verifier"), cl::Hidden);
  112. static cl::opt<bool> NoUpgradeDebugInfo("disable-upgrade-debug-info",
  113. cl::desc("Generate invalid output"),
  114. cl::ReallyHidden);
  115. static cl::opt<bool> VerifyEach("verify-each",
  116. cl::desc("Verify after each transform"));
  117. static cl::opt<bool>
  118. DisableDITypeMap("disable-debug-info-type-map",
  119. cl::desc("Don't use a uniquing type map for debug info"));
  120. static cl::opt<bool>
  121. StripDebug("strip-debug",
  122. cl::desc("Strip debugger symbol info from translation unit"));
  123. static cl::opt<bool>
  124. StripNamedMetadata("strip-named-metadata",
  125. cl::desc("Strip module-level named metadata"));
  126. static cl::opt<bool>
  127. OptLevelO0("O0", cl::desc("Optimization level 0. Similar to clang -O0. "
  128. "Use -passes='default<O0>' for the new PM"));
  129. static cl::opt<bool>
  130. OptLevelO1("O1", cl::desc("Optimization level 1. Similar to clang -O1. "
  131. "Use -passes='default<O1>' for the new PM"));
  132. static cl::opt<bool>
  133. OptLevelO2("O2", cl::desc("Optimization level 2. Similar to clang -O2. "
  134. "Use -passes='default<O2>' for the new PM"));
  135. static cl::opt<bool>
  136. OptLevelOs("Os", cl::desc("Like -O2 but size-conscious. Similar to clang "
  137. "-Os. Use -passes='default<Os>' for the new PM"));
  138. static cl::opt<bool> OptLevelOz(
  139. "Oz",
  140. cl::desc("Like -O2 but optimize for code size above all else. Similar to "
  141. "clang -Oz. Use -passes='default<Oz>' for the new PM"));
  142. static cl::opt<bool>
  143. OptLevelO3("O3", cl::desc("Optimization level 3. Similar to clang -O3. "
  144. "Use -passes='default<O3>' for the new PM"));
  145. static cl::opt<unsigned> CodeGenOptLevel(
  146. "codegen-opt-level",
  147. cl::desc("Override optimization level for codegen hooks, legacy PM only"));
  148. static cl::opt<std::string>
  149. TargetTriple("mtriple", cl::desc("Override target triple for module"));
  150. static cl::opt<bool> EmitSummaryIndex("module-summary",
  151. cl::desc("Emit module summary index"),
  152. cl::init(false));
  153. static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"),
  154. cl::init(false));
  155. static cl::opt<bool>
  156. DisableSimplifyLibCalls("disable-simplify-libcalls",
  157. cl::desc("Disable simplify-libcalls"));
  158. static cl::list<std::string> DisableBuiltins(
  159. "disable-builtin",
  160. cl::desc("Disable specific target library builtin function"));
  161. static cl::opt<bool> EnableDebugify(
  162. "enable-debugify",
  163. cl::desc(
  164. "Start the pipeline with debugify and end it with check-debugify"));
  165. static cl::opt<bool> VerifyDebugInfoPreserve(
  166. "verify-debuginfo-preserve",
  167. cl::desc("Start the pipeline with collecting and end it with checking of "
  168. "debug info preservation."));
  169. static cl::opt<bool>
  170. PrintBreakpoints("print-breakpoints-for-testing",
  171. cl::desc("Print select breakpoints location for testing"));
  172. static cl::opt<std::string> ClDataLayout("data-layout",
  173. cl::desc("data layout string to use"),
  174. cl::value_desc("layout-string"),
  175. cl::init(""));
  176. static cl::opt<bool> PreserveBitcodeUseListOrder(
  177. "preserve-bc-uselistorder",
  178. cl::desc("Preserve use-list order when writing LLVM bitcode."),
  179. cl::init(true), cl::Hidden);
  180. static cl::opt<bool> PreserveAssemblyUseListOrder(
  181. "preserve-ll-uselistorder",
  182. cl::desc("Preserve use-list order when writing LLVM assembly."),
  183. cl::init(false), cl::Hidden);
  184. static cl::opt<bool> RunTwice("run-twice",
  185. cl::desc("Run all passes twice, re-using the "
  186. "same pass manager (legacy PM only)."),
  187. cl::init(false), cl::Hidden);
  188. static cl::opt<bool> DiscardValueNames(
  189. "discard-value-names",
  190. cl::desc("Discard names from Value (other than GlobalValue)."),
  191. cl::init(false), cl::Hidden);
  192. static cl::opt<bool> TimeTrace(
  193. "time-trace",
  194. cl::desc("Record time trace"));
  195. static cl::opt<unsigned> TimeTraceGranularity(
  196. "time-trace-granularity",
  197. cl::desc("Minimum time granularity (in microseconds) traced by time profiler"),
  198. cl::init(500), cl::Hidden);
  199. static cl::opt<std::string>
  200. TimeTraceFile("time-trace-file",
  201. cl::desc("Specify time trace file destination"),
  202. cl::value_desc("filename"));
  203. static cl::opt<bool> RemarksWithHotness(
  204. "pass-remarks-with-hotness",
  205. cl::desc("With PGO, include profile count in optimization remarks"),
  206. cl::Hidden);
  207. static cl::opt<std::optional<uint64_t>, false, remarks::HotnessThresholdParser>
  208. RemarksHotnessThreshold(
  209. "pass-remarks-hotness-threshold",
  210. cl::desc("Minimum profile count required for "
  211. "an optimization remark to be output. "
  212. "Use 'auto' to apply the threshold from profile summary"),
  213. cl::value_desc("N or 'auto'"), cl::init(0), cl::Hidden);
  214. static cl::opt<std::string>
  215. RemarksFilename("pass-remarks-output",
  216. cl::desc("Output filename for pass remarks"),
  217. cl::value_desc("filename"));
  218. static cl::opt<std::string>
  219. RemarksPasses("pass-remarks-filter",
  220. cl::desc("Only record optimization remarks from passes whose "
  221. "names match the given regular expression"),
  222. cl::value_desc("regex"));
  223. static cl::opt<std::string> RemarksFormat(
  224. "pass-remarks-format",
  225. cl::desc("The format used for serializing remarks (default: YAML)"),
  226. cl::value_desc("format"), cl::init("yaml"));
  227. static cl::list<std::string>
  228. PassPlugins("load-pass-plugin",
  229. cl::desc("Load passes from plugin library"));
  230. static inline void addPass(legacy::PassManagerBase &PM, Pass *P) {
  231. // Add the pass to the pass manager...
  232. PM.add(P);
  233. // If we are verifying all of the intermediate steps, add the verifier...
  234. if (VerifyEach)
  235. PM.add(createVerifierPass());
  236. }
  237. //===----------------------------------------------------------------------===//
  238. // CodeGen-related helper functions.
  239. //
  240. static CodeGenOpt::Level GetCodeGenOptLevel() {
  241. return static_cast<CodeGenOpt::Level>(unsigned(CodeGenOptLevel));
  242. }
  243. // Returns the TargetMachine instance or zero if no triple is provided.
  244. static TargetMachine* GetTargetMachine(Triple TheTriple, StringRef CPUStr,
  245. StringRef FeaturesStr,
  246. const TargetOptions &Options) {
  247. std::string Error;
  248. const Target *TheTarget =
  249. TargetRegistry::lookupTarget(codegen::getMArch(), TheTriple, Error);
  250. // Some modules don't specify a triple, and this is okay.
  251. if (!TheTarget) {
  252. return nullptr;
  253. }
  254. return TheTarget->createTargetMachine(
  255. TheTriple.getTriple(), codegen::getCPUStr(), codegen::getFeaturesStr(),
  256. Options, codegen::getExplicitRelocModel(),
  257. codegen::getExplicitCodeModel(), GetCodeGenOptLevel());
  258. }
  259. struct TimeTracerRAII {
  260. TimeTracerRAII(StringRef ProgramName) {
  261. if (TimeTrace)
  262. timeTraceProfilerInitialize(TimeTraceGranularity, ProgramName);
  263. }
  264. ~TimeTracerRAII() {
  265. if (TimeTrace) {
  266. if (auto E = timeTraceProfilerWrite(TimeTraceFile, OutputFilename)) {
  267. handleAllErrors(std::move(E), [&](const StringError &SE) {
  268. errs() << SE.getMessage() << "\n";
  269. });
  270. return;
  271. }
  272. timeTraceProfilerCleanup();
  273. }
  274. }
  275. };
  276. // For use in NPM transition. Currently this contains most codegen-specific
  277. // passes. Remove passes from here when porting to the NPM.
  278. // TODO: use a codegen version of PassRegistry.def/PassBuilder::is*Pass() once
  279. // it exists.
  280. static bool shouldPinPassToLegacyPM(StringRef Pass) {
  281. std::vector<StringRef> PassNameExactToIgnore = {
  282. "nvvm-reflect",
  283. "nvvm-intr-range",
  284. "amdgpu-simplifylib",
  285. "amdgpu-usenative",
  286. "amdgpu-promote-alloca",
  287. "amdgpu-promote-alloca-to-vector",
  288. "amdgpu-lower-kernel-attributes",
  289. "amdgpu-propagate-attributes-early",
  290. "amdgpu-propagate-attributes-late",
  291. "amdgpu-unify-metadata",
  292. "amdgpu-printf-runtime-binding",
  293. "amdgpu-always-inline"};
  294. if (llvm::is_contained(PassNameExactToIgnore, Pass))
  295. return false;
  296. std::vector<StringRef> PassNamePrefix = {
  297. "x86-", "xcore-", "wasm-", "systemz-", "ppc-", "nvvm-",
  298. "nvptx-", "mips-", "lanai-", "hexagon-", "bpf-", "avr-",
  299. "thumb2-", "arm-", "si-", "gcn-", "amdgpu-", "aarch64-",
  300. "amdgcn-", "polly-", "riscv-", "dxil-"};
  301. std::vector<StringRef> PassNameContain = {"ehprepare"};
  302. std::vector<StringRef> PassNameExact = {
  303. "safe-stack",
  304. "cost-model",
  305. "codegenprepare",
  306. "interleaved-load-combine",
  307. "unreachableblockelim",
  308. "verify-safepoint-ir",
  309. "atomic-expand",
  310. "expandvp",
  311. "hardware-loops",
  312. "mve-tail-predication",
  313. "interleaved-access",
  314. "global-merge",
  315. "pre-isel-intrinsic-lowering",
  316. "expand-reductions",
  317. "indirectbr-expand",
  318. "generic-to-nvvm",
  319. "expandmemcmp",
  320. "loop-reduce",
  321. "lower-amx-type",
  322. "pre-amx-config",
  323. "lower-amx-intrinsics",
  324. "polyhedral-info",
  325. "print-polyhedral-info",
  326. "replace-with-veclib",
  327. "jmc-instrument",
  328. "dot-regions",
  329. "dot-regions-only",
  330. "view-regions",
  331. "view-regions-only",
  332. "select-optimize",
  333. "expand-large-div-rem",
  334. "structurizecfg",
  335. "fix-irreducible",
  336. "expand-large-fp-convert"
  337. };
  338. for (const auto &P : PassNamePrefix)
  339. if (Pass.startswith(P))
  340. return true;
  341. for (const auto &P : PassNameContain)
  342. if (Pass.contains(P))
  343. return true;
  344. return llvm::is_contained(PassNameExact, Pass);
  345. }
  346. // For use in NPM transition.
  347. static bool shouldForceLegacyPM() {
  348. for (const auto &P : PassList) {
  349. StringRef Arg = P->getPassArgument();
  350. if (shouldPinPassToLegacyPM(Arg))
  351. return true;
  352. }
  353. return false;
  354. }
  355. //===----------------------------------------------------------------------===//
  356. // main for opt
  357. //
  358. int main(int argc, char **argv) {
  359. InitLLVM X(argc, argv);
  360. // Enable debug stream buffering.
  361. EnableDebugBuffering = true;
  362. InitializeAllTargets();
  363. InitializeAllTargetMCs();
  364. InitializeAllAsmPrinters();
  365. InitializeAllAsmParsers();
  366. // Initialize passes
  367. PassRegistry &Registry = *PassRegistry::getPassRegistry();
  368. initializeCore(Registry);
  369. initializeScalarOpts(Registry);
  370. initializeVectorization(Registry);
  371. initializeIPO(Registry);
  372. initializeAnalysis(Registry);
  373. initializeTransformUtils(Registry);
  374. initializeInstCombine(Registry);
  375. initializeTarget(Registry);
  376. // For codegen passes, only passes that do IR to IR transformation are
  377. // supported.
  378. initializeExpandLargeDivRemLegacyPassPass(Registry);
  379. initializeExpandLargeFpConvertLegacyPassPass(Registry);
  380. initializeExpandMemCmpPassPass(Registry);
  381. initializeScalarizeMaskedMemIntrinLegacyPassPass(Registry);
  382. initializeSelectOptimizePass(Registry);
  383. initializeCodeGenPreparePass(Registry);
  384. initializeAtomicExpandPass(Registry);
  385. initializeRewriteSymbolsLegacyPassPass(Registry);
  386. initializeWinEHPreparePass(Registry);
  387. initializeDwarfEHPrepareLegacyPassPass(Registry);
  388. initializeSafeStackLegacyPassPass(Registry);
  389. initializeSjLjEHPreparePass(Registry);
  390. initializePreISelIntrinsicLoweringLegacyPassPass(Registry);
  391. initializeGlobalMergePass(Registry);
  392. initializeIndirectBrExpandPassPass(Registry);
  393. initializeInterleavedLoadCombinePass(Registry);
  394. initializeInterleavedAccessPass(Registry);
  395. initializeUnreachableBlockElimLegacyPassPass(Registry);
  396. initializeExpandReductionsPass(Registry);
  397. initializeExpandVectorPredicationPass(Registry);
  398. initializeWasmEHPreparePass(Registry);
  399. initializeWriteBitcodePassPass(Registry);
  400. initializeHardwareLoopsPass(Registry);
  401. initializeReplaceWithVeclibLegacyPass(Registry);
  402. initializeJMCInstrumenterPass(Registry);
  403. SmallVector<PassPlugin, 1> PluginList;
  404. PassPlugins.setCallback([&](const std::string &PluginPath) {
  405. auto Plugin = PassPlugin::Load(PluginPath);
  406. if (!Plugin) {
  407. errs() << "Failed to load passes from '" << PluginPath
  408. << "'. Request ignored.\n";
  409. return;
  410. }
  411. PluginList.emplace_back(Plugin.get());
  412. });
  413. // Register the Target and CPU printer for --version.
  414. cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU);
  415. cl::ParseCommandLineOptions(argc, argv,
  416. "llvm .bc -> .bc modular optimizer and analysis printer\n");
  417. LLVMContext Context;
  418. // If `-passes=` is specified, use NPM.
  419. // If `-enable-new-pm` is specified and there are no codegen passes, use NPM.
  420. // e.g. `-enable-new-pm -sroa` will use NPM.
  421. // but `-enable-new-pm -codegenprepare` will still revert to legacy PM.
  422. const bool UseNPM = (EnableNewPassManager && !shouldForceLegacyPM()) ||
  423. PassPipeline.getNumOccurrences() > 0;
  424. if (UseNPM && !PassList.empty()) {
  425. errs() << "The `opt -passname` syntax for the new pass manager is "
  426. "not supported, please use `opt -passes=<pipeline>` (or the `-p` "
  427. "alias for a more concise version).\n";
  428. errs() << "See https://llvm.org/docs/NewPassManager.html#invoking-opt "
  429. "for more details on the pass pipeline syntax.\n\n";
  430. return 1;
  431. }
  432. if (!UseNPM && PluginList.size()) {
  433. errs() << argv[0] << ": " << PassPlugins.ArgStr
  434. << " specified with legacy PM.\n";
  435. return 1;
  436. }
  437. // FIXME: once the legacy PM code is deleted, move runPassPipeline() here and
  438. // construct the PassBuilder before parsing IR so we can reuse the same
  439. // PassBuilder for print passes.
  440. if (PrintPasses) {
  441. printPasses(outs());
  442. return 0;
  443. }
  444. TimeTracerRAII TimeTracer(argv[0]);
  445. SMDiagnostic Err;
  446. Context.setDiscardValueNames(DiscardValueNames);
  447. if (!DisableDITypeMap)
  448. Context.enableDebugTypeODRUniquing();
  449. Expected<std::unique_ptr<ToolOutputFile>> RemarksFileOrErr =
  450. setupLLVMOptimizationRemarks(Context, RemarksFilename, RemarksPasses,
  451. RemarksFormat, RemarksWithHotness,
  452. RemarksHotnessThreshold);
  453. if (Error E = RemarksFileOrErr.takeError()) {
  454. errs() << toString(std::move(E)) << '\n';
  455. return 1;
  456. }
  457. std::unique_ptr<ToolOutputFile> RemarksFile = std::move(*RemarksFileOrErr);
  458. // Load the input module...
  459. auto SetDataLayout = [](StringRef, StringRef) -> std::optional<std::string> {
  460. if (ClDataLayout.empty())
  461. return std::nullopt;
  462. return ClDataLayout;
  463. };
  464. std::unique_ptr<Module> M;
  465. if (NoUpgradeDebugInfo)
  466. M = parseAssemblyFileWithIndexNoUpgradeDebugInfo(
  467. InputFilename, Err, Context, nullptr, SetDataLayout)
  468. .Mod;
  469. else
  470. M = parseIRFile(InputFilename, Err, Context,
  471. ParserCallbacks(SetDataLayout));
  472. if (!M) {
  473. Err.print(argv[0], errs());
  474. return 1;
  475. }
  476. // Strip debug info before running the verifier.
  477. if (StripDebug)
  478. StripDebugInfo(*M);
  479. // Erase module-level named metadata, if requested.
  480. if (StripNamedMetadata) {
  481. while (!M->named_metadata_empty()) {
  482. NamedMDNode *NMD = &*M->named_metadata_begin();
  483. M->eraseNamedMetadata(NMD);
  484. }
  485. }
  486. // If we are supposed to override the target triple or data layout, do so now.
  487. if (!TargetTriple.empty())
  488. M->setTargetTriple(Triple::normalize(TargetTriple));
  489. // Immediately run the verifier to catch any problems before starting up the
  490. // pass pipelines. Otherwise we can crash on broken code during
  491. // doInitialization().
  492. if (!NoVerify && verifyModule(*M, &errs())) {
  493. errs() << argv[0] << ": " << InputFilename
  494. << ": error: input module is broken!\n";
  495. return 1;
  496. }
  497. // Enable testing of whole program devirtualization on this module by invoking
  498. // the facility for updating public visibility to linkage unit visibility when
  499. // specified by an internal option. This is normally done during LTO which is
  500. // not performed via opt.
  501. updateVCallVisibilityInModule(*M,
  502. /* WholeProgramVisibilityEnabledInLTO */ false,
  503. /* DynamicExportSymbols */ {});
  504. // Figure out what stream we are supposed to write to...
  505. std::unique_ptr<ToolOutputFile> Out;
  506. std::unique_ptr<ToolOutputFile> ThinLinkOut;
  507. if (NoOutput) {
  508. if (!OutputFilename.empty())
  509. errs() << "WARNING: The -o (output filename) option is ignored when\n"
  510. "the --disable-output option is used.\n";
  511. } else {
  512. // Default to standard output.
  513. if (OutputFilename.empty())
  514. OutputFilename = "-";
  515. std::error_code EC;
  516. sys::fs::OpenFlags Flags =
  517. OutputAssembly ? sys::fs::OF_TextWithCRLF : sys::fs::OF_None;
  518. Out.reset(new ToolOutputFile(OutputFilename, EC, Flags));
  519. if (EC) {
  520. errs() << EC.message() << '\n';
  521. return 1;
  522. }
  523. if (!ThinLinkBitcodeFile.empty()) {
  524. ThinLinkOut.reset(
  525. new ToolOutputFile(ThinLinkBitcodeFile, EC, sys::fs::OF_None));
  526. if (EC) {
  527. errs() << EC.message() << '\n';
  528. return 1;
  529. }
  530. }
  531. }
  532. Triple ModuleTriple(M->getTargetTriple());
  533. std::string CPUStr, FeaturesStr;
  534. TargetMachine *Machine = nullptr;
  535. const TargetOptions Options =
  536. codegen::InitTargetOptionsFromCodeGenFlags(ModuleTriple);
  537. if (ModuleTriple.getArch()) {
  538. CPUStr = codegen::getCPUStr();
  539. FeaturesStr = codegen::getFeaturesStr();
  540. Machine = GetTargetMachine(ModuleTriple, CPUStr, FeaturesStr, Options);
  541. } else if (ModuleTriple.getArchName() != "unknown" &&
  542. ModuleTriple.getArchName() != "") {
  543. errs() << argv[0] << ": unrecognized architecture '"
  544. << ModuleTriple.getArchName() << "' provided.\n";
  545. return 1;
  546. }
  547. std::unique_ptr<TargetMachine> TM(Machine);
  548. // Override function attributes based on CPUStr, FeaturesStr, and command line
  549. // flags.
  550. codegen::setFunctionAttributes(CPUStr, FeaturesStr, *M);
  551. // If the output is set to be emitted to standard out, and standard out is a
  552. // console, print out a warning message and refuse to do it. We don't
  553. // impress anyone by spewing tons of binary goo to a terminal.
  554. if (!Force && !NoOutput && !OutputAssembly)
  555. if (CheckBitcodeOutputToConsole(Out->os()))
  556. NoOutput = true;
  557. if (OutputThinLTOBC)
  558. M->addModuleFlag(Module::Error, "EnableSplitLTOUnit", SplitLTOUnit);
  559. // Add an appropriate TargetLibraryInfo pass for the module's triple.
  560. TargetLibraryInfoImpl TLII(ModuleTriple);
  561. // The -disable-simplify-libcalls flag actually disables all builtin optzns.
  562. if (DisableSimplifyLibCalls)
  563. TLII.disableAllFunctions();
  564. else {
  565. // Disable individual builtin functions in TargetLibraryInfo.
  566. LibFunc F;
  567. for (auto &FuncName : DisableBuiltins)
  568. if (TLII.getLibFunc(FuncName, F))
  569. TLII.setUnavailable(F);
  570. else {
  571. errs() << argv[0] << ": cannot disable nonexistent builtin function "
  572. << FuncName << '\n';
  573. return 1;
  574. }
  575. }
  576. if (UseNPM) {
  577. if (legacy::debugPassSpecified()) {
  578. errs()
  579. << "-debug-pass does not work with the new PM, either use "
  580. "-debug-pass-manager, or use the legacy PM (-enable-new-pm=0)\n";
  581. return 1;
  582. }
  583. auto NumOLevel = OptLevelO0 + OptLevelO1 + OptLevelO2 + OptLevelO3 +
  584. OptLevelOs + OptLevelOz;
  585. if (NumOLevel > 1) {
  586. errs() << "Cannot specify multiple -O#\n";
  587. return 1;
  588. }
  589. if (NumOLevel > 0 && (PassPipeline.getNumOccurrences() > 0)) {
  590. errs() << "Cannot specify -O# and --passes=/--foo-pass, use "
  591. "-passes='default<O#>,other-pass'\n";
  592. return 1;
  593. }
  594. std::string Pipeline = PassPipeline;
  595. if (OptLevelO0)
  596. Pipeline = "default<O0>";
  597. if (OptLevelO1)
  598. Pipeline = "default<O1>";
  599. if (OptLevelO2)
  600. Pipeline = "default<O2>";
  601. if (OptLevelO3)
  602. Pipeline = "default<O3>";
  603. if (OptLevelOs)
  604. Pipeline = "default<Os>";
  605. if (OptLevelOz)
  606. Pipeline = "default<Oz>";
  607. OutputKind OK = OK_NoOutput;
  608. if (!NoOutput)
  609. OK = OutputAssembly
  610. ? OK_OutputAssembly
  611. : (OutputThinLTOBC ? OK_OutputThinLTOBitcode : OK_OutputBitcode);
  612. VerifierKind VK = VK_VerifyOut;
  613. if (NoVerify)
  614. VK = VK_NoVerifier;
  615. else if (VerifyEach)
  616. VK = VK_VerifyEachPass;
  617. // The user has asked to use the new pass manager and provided a pipeline
  618. // string. Hand off the rest of the functionality to the new code for that
  619. // layer.
  620. return runPassPipeline(argv[0], *M, TM.get(), &TLII, Out.get(),
  621. ThinLinkOut.get(), RemarksFile.get(), Pipeline,
  622. PluginList, OK, VK, PreserveAssemblyUseListOrder,
  623. PreserveBitcodeUseListOrder, EmitSummaryIndex,
  624. EmitModuleHash, EnableDebugify,
  625. VerifyDebugInfoPreserve)
  626. ? 0
  627. : 1;
  628. }
  629. if (OptLevelO0 || OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz ||
  630. OptLevelO3) {
  631. errs() << "Cannot use -O# with legacy PM.\n";
  632. return 1;
  633. }
  634. if (EmitSummaryIndex) {
  635. errs() << "Cannot use -module-summary with legacy PM.\n";
  636. return 1;
  637. }
  638. if (EmitModuleHash) {
  639. errs() << "Cannot use -module-hash with legacy PM.\n";
  640. return 1;
  641. }
  642. if (OutputThinLTOBC) {
  643. errs() << "Cannot use -thinlto-bc with legacy PM.\n";
  644. return 1;
  645. }
  646. // Create a PassManager to hold and optimize the collection of passes we are
  647. // about to build. If the -debugify-each option is set, wrap each pass with
  648. // the (-check)-debugify passes.
  649. DebugifyCustomPassManager Passes;
  650. DebugifyStatsMap DIStatsMap;
  651. DebugInfoPerPass DebugInfoBeforePass;
  652. if (DebugifyEach) {
  653. Passes.setDebugifyMode(DebugifyMode::SyntheticDebugInfo);
  654. Passes.setDIStatsMap(DIStatsMap);
  655. } else if (VerifyEachDebugInfoPreserve) {
  656. Passes.setDebugifyMode(DebugifyMode::OriginalDebugInfo);
  657. Passes.setDebugInfoBeforePass(DebugInfoBeforePass);
  658. if (!VerifyDIPreserveExport.empty())
  659. Passes.setOrigDIVerifyBugsReportFilePath(VerifyDIPreserveExport);
  660. }
  661. bool AddOneTimeDebugifyPasses =
  662. (EnableDebugify && !DebugifyEach) ||
  663. (VerifyDebugInfoPreserve && !VerifyEachDebugInfoPreserve);
  664. Passes.add(new TargetLibraryInfoWrapperPass(TLII));
  665. // Add internal analysis passes from the target machine.
  666. Passes.add(createTargetTransformInfoWrapperPass(TM ? TM->getTargetIRAnalysis()
  667. : TargetIRAnalysis()));
  668. if (AddOneTimeDebugifyPasses) {
  669. if (EnableDebugify) {
  670. Passes.setDIStatsMap(DIStatsMap);
  671. Passes.add(createDebugifyModulePass());
  672. } else if (VerifyDebugInfoPreserve) {
  673. Passes.setDebugInfoBeforePass(DebugInfoBeforePass);
  674. Passes.add(createDebugifyModulePass(
  675. DebugifyMode::OriginalDebugInfo, "",
  676. &(Passes.getDebugInfoPerPass())));
  677. }
  678. }
  679. std::unique_ptr<legacy::FunctionPassManager> FPasses;
  680. if (PrintBreakpoints) {
  681. // Default to standard output.
  682. if (!Out) {
  683. if (OutputFilename.empty())
  684. OutputFilename = "-";
  685. std::error_code EC;
  686. Out = std::make_unique<ToolOutputFile>(OutputFilename, EC,
  687. sys::fs::OF_None);
  688. if (EC) {
  689. errs() << EC.message() << '\n';
  690. return 1;
  691. }
  692. }
  693. Passes.add(createBreakpointPrinter(Out->os()));
  694. NoOutput = true;
  695. }
  696. if (TM) {
  697. // FIXME: We should dyn_cast this when supported.
  698. auto &LTM = static_cast<LLVMTargetMachine &>(*TM);
  699. Pass *TPC = LTM.createPassConfig(Passes);
  700. Passes.add(TPC);
  701. }
  702. // Create a new optimization pass for each one specified on the command line
  703. for (unsigned i = 0; i < PassList.size(); ++i) {
  704. const PassInfo *PassInf = PassList[i];
  705. Pass *P = nullptr;
  706. if (PassInf->getNormalCtor())
  707. P = PassInf->getNormalCtor()();
  708. else
  709. errs() << argv[0] << ": cannot create pass: "
  710. << PassInf->getPassName() << "\n";
  711. if (P)
  712. addPass(Passes, P);
  713. }
  714. if (FPasses) {
  715. FPasses->doInitialization();
  716. for (Function &F : *M)
  717. FPasses->run(F);
  718. FPasses->doFinalization();
  719. }
  720. // Check that the module is well formed on completion of optimization
  721. if (!NoVerify && !VerifyEach)
  722. Passes.add(createVerifierPass());
  723. if (AddOneTimeDebugifyPasses) {
  724. if (EnableDebugify)
  725. Passes.add(createCheckDebugifyModulePass(false));
  726. else if (VerifyDebugInfoPreserve) {
  727. if (!VerifyDIPreserveExport.empty())
  728. Passes.setOrigDIVerifyBugsReportFilePath(VerifyDIPreserveExport);
  729. Passes.add(createCheckDebugifyModulePass(
  730. false, "", nullptr, DebugifyMode::OriginalDebugInfo,
  731. &(Passes.getDebugInfoPerPass()), VerifyDIPreserveExport));
  732. }
  733. }
  734. // In run twice mode, we want to make sure the output is bit-by-bit
  735. // equivalent if we run the pass manager again, so setup two buffers and
  736. // a stream to write to them. Note that llc does something similar and it
  737. // may be worth to abstract this out in the future.
  738. SmallVector<char, 0> Buffer;
  739. SmallVector<char, 0> FirstRunBuffer;
  740. std::unique_ptr<raw_svector_ostream> BOS;
  741. raw_ostream *OS = nullptr;
  742. const bool ShouldEmitOutput = !NoOutput;
  743. // Write bitcode or assembly to the output as the last step...
  744. if (ShouldEmitOutput || RunTwice) {
  745. assert(Out);
  746. OS = &Out->os();
  747. if (RunTwice) {
  748. BOS = std::make_unique<raw_svector_ostream>(Buffer);
  749. OS = BOS.get();
  750. }
  751. if (OutputAssembly)
  752. Passes.add(createPrintModulePass(*OS, "", PreserveAssemblyUseListOrder));
  753. else
  754. Passes.add(createBitcodeWriterPass(*OS, PreserveBitcodeUseListOrder));
  755. }
  756. // Before executing passes, print the final values of the LLVM options.
  757. cl::PrintOptionValues();
  758. if (!RunTwice) {
  759. // Now that we have all of the passes ready, run them.
  760. Passes.run(*M);
  761. } else {
  762. // If requested, run all passes twice with the same pass manager to catch
  763. // bugs caused by persistent state in the passes.
  764. std::unique_ptr<Module> M2(CloneModule(*M));
  765. // Run all passes on the original module first, so the second run processes
  766. // the clone to catch CloneModule bugs.
  767. Passes.run(*M);
  768. FirstRunBuffer = Buffer;
  769. Buffer.clear();
  770. Passes.run(*M2);
  771. // Compare the two outputs and make sure they're the same
  772. assert(Out);
  773. if (Buffer.size() != FirstRunBuffer.size() ||
  774. (memcmp(Buffer.data(), FirstRunBuffer.data(), Buffer.size()) != 0)) {
  775. errs()
  776. << "Running the pass manager twice changed the output.\n"
  777. "Writing the result of the second run to the specified output.\n"
  778. "To generate the one-run comparison binary, just run without\n"
  779. "the compile-twice option\n";
  780. if (ShouldEmitOutput) {
  781. Out->os() << BOS->str();
  782. Out->keep();
  783. }
  784. if (RemarksFile)
  785. RemarksFile->keep();
  786. return 1;
  787. }
  788. if (ShouldEmitOutput)
  789. Out->os() << BOS->str();
  790. }
  791. if (DebugifyEach && !DebugifyExport.empty())
  792. exportDebugifyStats(DebugifyExport, Passes.getDebugifyStatsMap());
  793. // Declare success.
  794. if (!NoOutput || PrintBreakpoints)
  795. Out->keep();
  796. if (RemarksFile)
  797. RemarksFile->keep();
  798. if (ThinLinkOut)
  799. ThinLinkOut->keep();
  800. return 0;
  801. }