opt.cpp 38 KB

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