opt.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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/Remarks/HotnessThresholdParser.h"
  41. #include "llvm/Support/Debug.h"
  42. #include "llvm/Support/FileSystem.h"
  43. #include "llvm/Support/Host.h"
  44. #include "llvm/Support/InitLLVM.h"
  45. #include "llvm/Support/PluginLoader.h"
  46. #include "llvm/Support/SourceMgr.h"
  47. #include "llvm/Support/SystemUtils.h"
  48. #include "llvm/Support/TargetRegistry.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. //
  67. static cl::list<const PassInfo*, bool, PassNameParser>
  68. PassList(cl::desc("Optimizations available:"));
  69. static cl::opt<bool>
  70. EnableNewPassManager("enable-new-pm",
  71. cl::desc("Enable the new pass manager"),
  72. cl::init(LLVM_ENABLE_NEW_PASS_MANAGER));
  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("A textual description of the pass pipeline for optimizing"),
  80. cl::Hidden);
  81. // Other command line options...
  82. //
  83. static cl::opt<std::string>
  84. InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
  85. cl::init("-"), cl::value_desc("filename"));
  86. static cl::opt<std::string>
  87. OutputFilename("o", cl::desc("Override output filename"),
  88. cl::value_desc("filename"));
  89. static cl::opt<bool>
  90. Force("f", cl::desc("Enable binary output on terminals"));
  91. static cl::opt<bool>
  92. PrintEachXForm("p", cl::desc("Print module after each transformation"));
  93. static cl::opt<bool>
  94. NoOutput("disable-output",
  95. cl::desc("Do not write result bitcode file"), cl::Hidden);
  96. static cl::opt<bool>
  97. OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
  98. static cl::opt<bool>
  99. OutputThinLTOBC("thinlto-bc",
  100. cl::desc("Write output as ThinLTO-ready bitcode"));
  101. static cl::opt<bool>
  102. SplitLTOUnit("thinlto-split-lto-unit",
  103. cl::desc("Enable splitting of a ThinLTO LTOUnit"));
  104. static cl::opt<std::string> ThinLinkBitcodeFile(
  105. "thin-link-bitcode-file", cl::value_desc("filename"),
  106. cl::desc(
  107. "A file in which to write minimized bitcode for the thin link only"));
  108. static cl::opt<bool>
  109. NoVerify("disable-verify", cl::desc("Do not run the verifier"), cl::Hidden);
  110. static cl::opt<bool> NoUpgradeDebugInfo("disable-upgrade-debug-info",
  111. cl::desc("Generate invalid output"),
  112. cl::ReallyHidden);
  113. static cl::opt<bool> VerifyEach("verify-each",
  114. cl::desc("Verify after each transform"));
  115. static cl::opt<bool>
  116. DisableDITypeMap("disable-debug-info-type-map",
  117. cl::desc("Don't use a uniquing type map for debug info"));
  118. static cl::opt<bool>
  119. StripDebug("strip-debug",
  120. cl::desc("Strip debugger symbol info from translation unit"));
  121. static cl::opt<bool>
  122. StripNamedMetadata("strip-named-metadata",
  123. cl::desc("Strip module-level named metadata"));
  124. static cl::opt<bool> DisableInline("disable-inlining",
  125. cl::desc("Do not run the inliner pass"));
  126. static cl::opt<bool>
  127. DisableOptimizations("disable-opt",
  128. cl::desc("Do not run any optimization passes"));
  129. static cl::opt<bool>
  130. StandardLinkOpts("std-link-opts",
  131. cl::desc("Include the standard link time optimizations"));
  132. static cl::opt<bool>
  133. OptLevelO0("O0",
  134. cl::desc("Optimization level 0. Similar to clang -O0"));
  135. static cl::opt<bool>
  136. OptLevelO1("O1",
  137. cl::desc("Optimization level 1. Similar to clang -O1"));
  138. static cl::opt<bool>
  139. OptLevelO2("O2",
  140. cl::desc("Optimization level 2. Similar to clang -O2"));
  141. static cl::opt<bool>
  142. OptLevelOs("Os",
  143. cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os"));
  144. static cl::opt<bool>
  145. OptLevelOz("Oz",
  146. cl::desc("Like -Os but reduces code size further. Similar to clang -Oz"));
  147. static cl::opt<bool>
  148. OptLevelO3("O3",
  149. cl::desc("Optimization level 3. Similar to clang -O3"));
  150. static cl::opt<unsigned>
  151. CodeGenOptLevel("codegen-opt-level",
  152. cl::desc("Override optimization level for codegen hooks"));
  153. static cl::opt<std::string>
  154. TargetTriple("mtriple", cl::desc("Override target triple for module"));
  155. cl::opt<bool> DisableLoopUnrolling(
  156. "disable-loop-unrolling",
  157. cl::desc("Disable loop unrolling in all relevant passes"), cl::init(false));
  158. static cl::opt<bool> EmitSummaryIndex("module-summary",
  159. cl::desc("Emit module summary index"),
  160. cl::init(false));
  161. static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"),
  162. cl::init(false));
  163. static cl::opt<bool>
  164. DisableSimplifyLibCalls("disable-simplify-libcalls",
  165. cl::desc("Disable simplify-libcalls"));
  166. static cl::list<std::string>
  167. DisableBuiltins("disable-builtin",
  168. cl::desc("Disable specific target library builtin function"),
  169. cl::ZeroOrMore);
  170. static cl::opt<bool>
  171. AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
  172. static cl::opt<bool> EnableDebugify(
  173. "enable-debugify",
  174. cl::desc(
  175. "Start the pipeline with debugify and end it with check-debugify"));
  176. static cl::opt<bool>
  177. PrintBreakpoints("print-breakpoints-for-testing",
  178. cl::desc("Print select breakpoints location for testing"));
  179. static cl::opt<std::string> ClDataLayout("data-layout",
  180. cl::desc("data layout string to use"),
  181. cl::value_desc("layout-string"),
  182. cl::init(""));
  183. static cl::opt<bool> PreserveBitcodeUseListOrder(
  184. "preserve-bc-uselistorder",
  185. cl::desc("Preserve use-list order when writing LLVM bitcode."),
  186. cl::init(true), cl::Hidden);
  187. static cl::opt<bool> PreserveAssemblyUseListOrder(
  188. "preserve-ll-uselistorder",
  189. cl::desc("Preserve use-list order when writing LLVM assembly."),
  190. cl::init(false), cl::Hidden);
  191. static cl::opt<bool>
  192. RunTwice("run-twice",
  193. cl::desc("Run all passes twice, re-using the same pass manager."),
  194. cl::init(false), cl::Hidden);
  195. static cl::opt<bool> DiscardValueNames(
  196. "discard-value-names",
  197. cl::desc("Discard names from Value (other than GlobalValue)."),
  198. cl::init(false), cl::Hidden);
  199. static cl::opt<bool> Coroutines(
  200. "enable-coroutines",
  201. cl::desc("Enable coroutine passes."),
  202. cl::init(false), cl::Hidden);
  203. static cl::opt<bool> TimeTrace(
  204. "time-trace",
  205. cl::desc("Record time trace"));
  206. static cl::opt<unsigned> TimeTraceGranularity(
  207. "time-trace-granularity",
  208. cl::desc("Minimum time granularity (in microseconds) traced by time profiler"),
  209. cl::init(500), cl::Hidden);
  210. static cl::opt<std::string>
  211. TimeTraceFile("time-trace-file",
  212. cl::desc("Specify time trace file destination"),
  213. cl::value_desc("filename"));
  214. static cl::opt<bool> RemarksWithHotness(
  215. "pass-remarks-with-hotness",
  216. cl::desc("With PGO, include profile count in optimization remarks"),
  217. cl::Hidden);
  218. static cl::opt<Optional<uint64_t>, false, remarks::HotnessThresholdParser>
  219. RemarksHotnessThreshold(
  220. "pass-remarks-hotness-threshold",
  221. cl::desc("Minimum profile count required for "
  222. "an optimization remark to be output. "
  223. "Use 'auto' to apply the threshold from profile summary."),
  224. cl::value_desc("N or 'auto'"), cl::init(0), cl::Hidden);
  225. static cl::opt<std::string>
  226. RemarksFilename("pass-remarks-output",
  227. cl::desc("Output filename for pass remarks"),
  228. cl::value_desc("filename"));
  229. static cl::opt<std::string>
  230. RemarksPasses("pass-remarks-filter",
  231. cl::desc("Only record optimization remarks from passes whose "
  232. "names match the given regular expression"),
  233. cl::value_desc("regex"));
  234. static cl::opt<std::string> RemarksFormat(
  235. "pass-remarks-format",
  236. cl::desc("The format used for serializing remarks (default: YAML)"),
  237. cl::value_desc("format"), cl::init("yaml"));
  238. cl::opt<PGOKind>
  239. PGOKindFlag("pgo-kind", cl::init(NoPGO), cl::Hidden,
  240. cl::desc("The kind of profile guided optimization"),
  241. cl::values(clEnumValN(NoPGO, "nopgo", "Do not use PGO."),
  242. clEnumValN(InstrGen, "pgo-instr-gen-pipeline",
  243. "Instrument the IR to generate profile."),
  244. clEnumValN(InstrUse, "pgo-instr-use-pipeline",
  245. "Use instrumented profile to guide PGO."),
  246. clEnumValN(SampleUse, "pgo-sample-use-pipeline",
  247. "Use sampled profile to guide PGO.")));
  248. cl::opt<std::string> ProfileFile("profile-file",
  249. cl::desc("Path to the profile."), cl::Hidden);
  250. cl::opt<CSPGOKind> CSPGOKindFlag(
  251. "cspgo-kind", cl::init(NoCSPGO), cl::Hidden,
  252. cl::desc("The kind of context sensitive profile guided optimization"),
  253. cl::values(
  254. clEnumValN(NoCSPGO, "nocspgo", "Do not use CSPGO."),
  255. clEnumValN(
  256. CSInstrGen, "cspgo-instr-gen-pipeline",
  257. "Instrument (context sensitive) the IR to generate profile."),
  258. clEnumValN(
  259. CSInstrUse, "cspgo-instr-use-pipeline",
  260. "Use instrumented (context sensitive) profile to guide PGO.")));
  261. cl::opt<std::string> CSProfileGenFile(
  262. "cs-profilegen-file",
  263. cl::desc("Path to the instrumented context sensitive profile."),
  264. cl::Hidden);
  265. static inline void addPass(legacy::PassManagerBase &PM, Pass *P) {
  266. // Add the pass to the pass manager...
  267. PM.add(P);
  268. // If we are verifying all of the intermediate steps, add the verifier...
  269. if (VerifyEach)
  270. PM.add(createVerifierPass());
  271. }
  272. /// This routine adds optimization passes based on selected optimization level,
  273. /// OptLevel.
  274. ///
  275. /// OptLevel - Optimization Level
  276. static void AddOptimizationPasses(legacy::PassManagerBase &MPM,
  277. legacy::FunctionPassManager &FPM,
  278. TargetMachine *TM, unsigned OptLevel,
  279. unsigned SizeLevel) {
  280. if (!NoVerify || VerifyEach)
  281. FPM.add(createVerifierPass()); // Verify that input is correct
  282. PassManagerBuilder Builder;
  283. Builder.OptLevel = OptLevel;
  284. Builder.SizeLevel = SizeLevel;
  285. if (DisableInline) {
  286. // No inlining pass
  287. } else if (OptLevel > 1) {
  288. Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false);
  289. } else {
  290. Builder.Inliner = createAlwaysInlinerLegacyPass();
  291. }
  292. Builder.DisableUnrollLoops = (DisableLoopUnrolling.getNumOccurrences() > 0) ?
  293. DisableLoopUnrolling : OptLevel == 0;
  294. Builder.LoopVectorize = OptLevel > 1 && SizeLevel < 2;
  295. Builder.SLPVectorize = OptLevel > 1 && SizeLevel < 2;
  296. if (TM)
  297. TM->adjustPassManager(Builder);
  298. if (Coroutines)
  299. addCoroutinePassesToExtensionPoints(Builder);
  300. switch (PGOKindFlag) {
  301. case InstrGen:
  302. Builder.EnablePGOInstrGen = true;
  303. Builder.PGOInstrGen = ProfileFile;
  304. break;
  305. case InstrUse:
  306. Builder.PGOInstrUse = ProfileFile;
  307. break;
  308. case SampleUse:
  309. Builder.PGOSampleUse = ProfileFile;
  310. break;
  311. default:
  312. break;
  313. }
  314. switch (CSPGOKindFlag) {
  315. case CSInstrGen:
  316. Builder.EnablePGOCSInstrGen = true;
  317. break;
  318. case CSInstrUse:
  319. Builder.EnablePGOCSInstrUse = true;
  320. break;
  321. default:
  322. break;
  323. }
  324. Builder.populateFunctionPassManager(FPM);
  325. Builder.populateModulePassManager(MPM);
  326. }
  327. static void AddStandardLinkPasses(legacy::PassManagerBase &PM) {
  328. PassManagerBuilder Builder;
  329. Builder.VerifyInput = true;
  330. if (DisableOptimizations)
  331. Builder.OptLevel = 0;
  332. if (!DisableInline)
  333. Builder.Inliner = createFunctionInliningPass();
  334. Builder.populateLTOPassManager(PM);
  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. for (const auto &P : PassNameExactToIgnore)
  405. if (Pass == P)
  406. return false;
  407. std::vector<StringRef> PassNamePrefix = {
  408. "x86-", "xcore-", "wasm-", "systemz-", "ppc-", "nvvm-", "nvptx-",
  409. "mips-", "lanai-", "hexagon-", "bpf-", "avr-", "thumb2-", "arm-",
  410. "si-", "gcn-", "amdgpu-", "aarch64-", "amdgcn-", "polly-"};
  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. "divergence", "atomic-expand",
  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. "polyhedral-info"};
  424. for (const auto &P : PassNamePrefix)
  425. if (Pass.startswith(P))
  426. return true;
  427. for (const auto &P : PassNameContain)
  428. if (Pass.contains(P))
  429. return true;
  430. for (const auto &P : PassNameExact)
  431. if (Pass == P)
  432. return true;
  433. return false;
  434. }
  435. // For use in NPM transition.
  436. static bool shouldForceLegacyPM() {
  437. for (const auto &P : PassList) {
  438. StringRef Arg = P->getPassArgument();
  439. if (shouldPinPassToLegacyPM(Arg))
  440. return true;
  441. }
  442. return false;
  443. }
  444. //===----------------------------------------------------------------------===//
  445. // main for opt
  446. //
  447. int main(int argc, char **argv) {
  448. InitLLVM X(argc, argv);
  449. // Enable debug stream buffering.
  450. EnableDebugBuffering = true;
  451. LLVMContext Context;
  452. InitializeAllTargets();
  453. InitializeAllTargetMCs();
  454. InitializeAllAsmPrinters();
  455. InitializeAllAsmParsers();
  456. // Initialize passes
  457. PassRegistry &Registry = *PassRegistry::getPassRegistry();
  458. initializeCore(Registry);
  459. initializeCoroutines(Registry);
  460. initializeScalarOpts(Registry);
  461. initializeObjCARCOpts(Registry);
  462. initializeVectorization(Registry);
  463. initializeIPO(Registry);
  464. initializeAnalysis(Registry);
  465. initializeTransformUtils(Registry);
  466. initializeInstCombine(Registry);
  467. initializeAggressiveInstCombine(Registry);
  468. initializeInstrumentation(Registry);
  469. initializeTarget(Registry);
  470. // For codegen passes, only passes that do IR to IR transformation are
  471. // supported.
  472. initializeExpandMemCmpPassPass(Registry);
  473. initializeScalarizeMaskedMemIntrinLegacyPassPass(Registry);
  474. initializeCodeGenPreparePass(Registry);
  475. initializeAtomicExpandPass(Registry);
  476. initializeRewriteSymbolsLegacyPassPass(Registry);
  477. initializeWinEHPreparePass(Registry);
  478. initializeDwarfEHPrepareLegacyPassPass(Registry);
  479. initializeSafeStackLegacyPassPass(Registry);
  480. initializeSjLjEHPreparePass(Registry);
  481. initializePreISelIntrinsicLoweringLegacyPassPass(Registry);
  482. initializeGlobalMergePass(Registry);
  483. initializeIndirectBrExpandPassPass(Registry);
  484. initializeInterleavedLoadCombinePass(Registry);
  485. initializeInterleavedAccessPass(Registry);
  486. initializeEntryExitInstrumenterPass(Registry);
  487. initializePostInlineEntryExitInstrumenterPass(Registry);
  488. initializeUnreachableBlockElimLegacyPassPass(Registry);
  489. initializeExpandReductionsPass(Registry);
  490. initializeWasmEHPreparePass(Registry);
  491. initializeWriteBitcodePassPass(Registry);
  492. initializeHardwareLoopsPass(Registry);
  493. initializeTypePromotionPass(Registry);
  494. #ifdef BUILD_EXAMPLES
  495. initializeExampleIRTransforms(Registry);
  496. #endif
  497. cl::ParseCommandLineOptions(argc, argv,
  498. "llvm .bc -> .bc modular optimizer and analysis printer\n");
  499. if (AnalyzeOnly && NoOutput) {
  500. errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
  501. return 1;
  502. }
  503. TimeTracerRAII TimeTracer(argv[0]);
  504. SMDiagnostic Err;
  505. Context.setDiscardValueNames(DiscardValueNames);
  506. if (!DisableDITypeMap)
  507. Context.enableDebugTypeODRUniquing();
  508. Expected<std::unique_ptr<ToolOutputFile>> RemarksFileOrErr =
  509. setupLLVMOptimizationRemarks(Context, RemarksFilename, RemarksPasses,
  510. RemarksFormat, RemarksWithHotness,
  511. RemarksHotnessThreshold);
  512. if (Error E = RemarksFileOrErr.takeError()) {
  513. errs() << toString(std::move(E)) << '\n';
  514. return 1;
  515. }
  516. std::unique_ptr<ToolOutputFile> RemarksFile = std::move(*RemarksFileOrErr);
  517. // Load the input module...
  518. auto SetDataLayout = [](StringRef) -> Optional<std::string> {
  519. if (ClDataLayout.empty())
  520. return None;
  521. return ClDataLayout;
  522. };
  523. std::unique_ptr<Module> M;
  524. if (NoUpgradeDebugInfo)
  525. M = parseAssemblyFileWithIndexNoUpgradeDebugInfo(
  526. InputFilename, Err, Context, nullptr, SetDataLayout)
  527. .Mod;
  528. else
  529. M = parseIRFile(InputFilename, Err, Context, SetDataLayout);
  530. if (!M) {
  531. Err.print(argv[0], errs());
  532. return 1;
  533. }
  534. // Strip debug info before running the verifier.
  535. if (StripDebug)
  536. StripDebugInfo(*M);
  537. // Erase module-level named metadata, if requested.
  538. if (StripNamedMetadata) {
  539. while (!M->named_metadata_empty()) {
  540. NamedMDNode *NMD = &*M->named_metadata_begin();
  541. M->eraseNamedMetadata(NMD);
  542. }
  543. }
  544. // If we are supposed to override the target triple or data layout, do so now.
  545. if (!TargetTriple.empty())
  546. M->setTargetTriple(Triple::normalize(TargetTriple));
  547. // Immediately run the verifier to catch any problems before starting up the
  548. // pass pipelines. Otherwise we can crash on broken code during
  549. // doInitialization().
  550. if (!NoVerify && verifyModule(*M, &errs())) {
  551. errs() << argv[0] << ": " << InputFilename
  552. << ": error: input module is broken!\n";
  553. return 1;
  554. }
  555. // Enable testing of whole program devirtualization on this module by invoking
  556. // the facility for updating public visibility to linkage unit visibility when
  557. // specified by an internal option. This is normally done during LTO which is
  558. // not performed via opt.
  559. updateVCallVisibilityInModule(*M,
  560. /* WholeProgramVisibilityEnabledInLTO */ false);
  561. // Figure out what stream we are supposed to write to...
  562. std::unique_ptr<ToolOutputFile> Out;
  563. std::unique_ptr<ToolOutputFile> ThinLinkOut;
  564. if (NoOutput) {
  565. if (!OutputFilename.empty())
  566. errs() << "WARNING: The -o (output filename) option is ignored when\n"
  567. "the --disable-output option is used.\n";
  568. } else {
  569. // Default to standard output.
  570. if (OutputFilename.empty())
  571. OutputFilename = "-";
  572. std::error_code EC;
  573. sys::fs::OpenFlags Flags = OutputAssembly ? sys::fs::OF_Text
  574. : sys::fs::OF_None;
  575. Out.reset(new ToolOutputFile(OutputFilename, EC, Flags));
  576. if (EC) {
  577. errs() << EC.message() << '\n';
  578. return 1;
  579. }
  580. if (!ThinLinkBitcodeFile.empty()) {
  581. ThinLinkOut.reset(
  582. new ToolOutputFile(ThinLinkBitcodeFile, EC, sys::fs::OF_None));
  583. if (EC) {
  584. errs() << EC.message() << '\n';
  585. return 1;
  586. }
  587. }
  588. }
  589. Triple ModuleTriple(M->getTargetTriple());
  590. std::string CPUStr, FeaturesStr;
  591. TargetMachine *Machine = nullptr;
  592. const TargetOptions Options =
  593. codegen::InitTargetOptionsFromCodeGenFlags(ModuleTriple);
  594. if (ModuleTriple.getArch()) {
  595. CPUStr = codegen::getCPUStr();
  596. FeaturesStr = codegen::getFeaturesStr();
  597. Machine = GetTargetMachine(ModuleTriple, CPUStr, FeaturesStr, Options);
  598. } else if (ModuleTriple.getArchName() != "unknown" &&
  599. ModuleTriple.getArchName() != "") {
  600. errs() << argv[0] << ": unrecognized architecture '"
  601. << ModuleTriple.getArchName() << "' provided.\n";
  602. return 1;
  603. }
  604. std::unique_ptr<TargetMachine> TM(Machine);
  605. // Override function attributes based on CPUStr, FeaturesStr, and command line
  606. // flags.
  607. codegen::setFunctionAttributes(CPUStr, FeaturesStr, *M);
  608. // If the output is set to be emitted to standard out, and standard out is a
  609. // console, print out a warning message and refuse to do it. We don't
  610. // impress anyone by spewing tons of binary goo to a terminal.
  611. if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
  612. if (CheckBitcodeOutputToConsole(Out->os()))
  613. NoOutput = true;
  614. if (OutputThinLTOBC)
  615. M->addModuleFlag(Module::Error, "EnableSplitLTOUnit", SplitLTOUnit);
  616. // Add an appropriate TargetLibraryInfo pass for the module's triple.
  617. TargetLibraryInfoImpl TLII(ModuleTriple);
  618. // The -disable-simplify-libcalls flag actually disables all builtin optzns.
  619. if (DisableSimplifyLibCalls)
  620. TLII.disableAllFunctions();
  621. else {
  622. // Disable individual builtin functions in TargetLibraryInfo.
  623. LibFunc F;
  624. for (auto &FuncName : DisableBuiltins)
  625. if (TLII.getLibFunc(FuncName, F))
  626. TLII.setUnavailable(F);
  627. else {
  628. errs() << argv[0] << ": cannot disable nonexistent builtin function "
  629. << FuncName << '\n';
  630. return 1;
  631. }
  632. }
  633. // If `-passes=` is specified, use NPM.
  634. // If `-enable-new-pm` is specified and there are no codegen passes, use NPM.
  635. // e.g. `-enable-new-pm -sroa` will use NPM.
  636. // but `-enable-new-pm -codegenprepare` will still revert to legacy PM.
  637. if ((EnableNewPassManager && !shouldForceLegacyPM()) ||
  638. PassPipeline.getNumOccurrences() > 0) {
  639. if (AnalyzeOnly) {
  640. errs() << "Cannot specify -analyze under new pass manager\n";
  641. return 1;
  642. }
  643. if (PassPipeline.getNumOccurrences() > 0 && PassList.size() > 0) {
  644. errs()
  645. << "Cannot specify passes via both -foo-pass and --passes=foo-pass\n";
  646. return 1;
  647. }
  648. SmallVector<StringRef, 4> Passes;
  649. if (OptLevelO0)
  650. Passes.push_back("default<O0>");
  651. if (OptLevelO1)
  652. Passes.push_back("default<O1>");
  653. if (OptLevelO2)
  654. Passes.push_back("default<O2>");
  655. if (OptLevelO3)
  656. Passes.push_back("default<O3>");
  657. if (OptLevelOs)
  658. Passes.push_back("default<Os>");
  659. if (OptLevelOz)
  660. Passes.push_back("default<Oz>");
  661. for (const auto &P : PassList)
  662. Passes.push_back(P->getPassArgument());
  663. OutputKind OK = OK_NoOutput;
  664. if (!NoOutput)
  665. OK = OutputAssembly
  666. ? OK_OutputAssembly
  667. : (OutputThinLTOBC ? OK_OutputThinLTOBitcode : OK_OutputBitcode);
  668. VerifierKind VK = VK_VerifyInAndOut;
  669. if (NoVerify)
  670. VK = VK_NoVerifier;
  671. else if (VerifyEach)
  672. VK = VK_VerifyEachPass;
  673. // The user has asked to use the new pass manager and provided a pipeline
  674. // string. Hand off the rest of the functionality to the new code for that
  675. // layer.
  676. return runPassPipeline(argv[0], *M, TM.get(), &TLII, Out.get(),
  677. ThinLinkOut.get(), RemarksFile.get(), PassPipeline,
  678. Passes, OK, VK, PreserveAssemblyUseListOrder,
  679. PreserveBitcodeUseListOrder, EmitSummaryIndex,
  680. EmitModuleHash, EnableDebugify, Coroutines)
  681. ? 0
  682. : 1;
  683. }
  684. // Create a PassManager to hold and optimize the collection of passes we are
  685. // about to build. If the -debugify-each option is set, wrap each pass with
  686. // the (-check)-debugify passes.
  687. DebugifyCustomPassManager Passes;
  688. if (DebugifyEach)
  689. Passes.enableDebugifyEach();
  690. bool AddOneTimeDebugifyPasses = EnableDebugify && !DebugifyEach;
  691. Passes.add(new TargetLibraryInfoWrapperPass(TLII));
  692. // Add internal analysis passes from the target machine.
  693. Passes.add(createTargetTransformInfoWrapperPass(TM ? TM->getTargetIRAnalysis()
  694. : TargetIRAnalysis()));
  695. if (AddOneTimeDebugifyPasses)
  696. Passes.add(createDebugifyModulePass());
  697. std::unique_ptr<legacy::FunctionPassManager> FPasses;
  698. if (OptLevelO0 || OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz ||
  699. OptLevelO3) {
  700. FPasses.reset(new legacy::FunctionPassManager(M.get()));
  701. FPasses->add(createTargetTransformInfoWrapperPass(
  702. TM ? TM->getTargetIRAnalysis() : TargetIRAnalysis()));
  703. }
  704. if (PrintBreakpoints) {
  705. // Default to standard output.
  706. if (!Out) {
  707. if (OutputFilename.empty())
  708. OutputFilename = "-";
  709. std::error_code EC;
  710. Out = std::make_unique<ToolOutputFile>(OutputFilename, EC,
  711. sys::fs::OF_None);
  712. if (EC) {
  713. errs() << EC.message() << '\n';
  714. return 1;
  715. }
  716. }
  717. Passes.add(createBreakpointPrinter(Out->os()));
  718. NoOutput = true;
  719. }
  720. if (TM) {
  721. // FIXME: We should dyn_cast this when supported.
  722. auto &LTM = static_cast<LLVMTargetMachine &>(*TM);
  723. Pass *TPC = LTM.createPassConfig(Passes);
  724. Passes.add(TPC);
  725. }
  726. // Create a new optimization pass for each one specified on the command line
  727. for (unsigned i = 0; i < PassList.size(); ++i) {
  728. if (StandardLinkOpts &&
  729. StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
  730. AddStandardLinkPasses(Passes);
  731. StandardLinkOpts = false;
  732. }
  733. if (OptLevelO0 && OptLevelO0.getPosition() < PassList.getPosition(i)) {
  734. AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0);
  735. OptLevelO0 = false;
  736. }
  737. if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
  738. AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0);
  739. OptLevelO1 = false;
  740. }
  741. if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
  742. AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0);
  743. OptLevelO2 = false;
  744. }
  745. if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) {
  746. AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1);
  747. OptLevelOs = false;
  748. }
  749. if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) {
  750. AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2);
  751. OptLevelOz = false;
  752. }
  753. if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
  754. AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0);
  755. OptLevelO3 = false;
  756. }
  757. const PassInfo *PassInf = PassList[i];
  758. Pass *P = nullptr;
  759. if (PassInf->getNormalCtor())
  760. P = PassInf->getNormalCtor()();
  761. else
  762. errs() << argv[0] << ": cannot create pass: "
  763. << PassInf->getPassName() << "\n";
  764. if (P) {
  765. PassKind Kind = P->getPassKind();
  766. addPass(Passes, P);
  767. if (AnalyzeOnly) {
  768. switch (Kind) {
  769. case PT_Region:
  770. Passes.add(createRegionPassPrinter(PassInf, Out->os()));
  771. break;
  772. case PT_Loop:
  773. Passes.add(createLoopPassPrinter(PassInf, Out->os()));
  774. break;
  775. case PT_Function:
  776. Passes.add(createFunctionPassPrinter(PassInf, Out->os()));
  777. break;
  778. case PT_CallGraphSCC:
  779. Passes.add(createCallGraphPassPrinter(PassInf, Out->os()));
  780. break;
  781. default:
  782. Passes.add(createModulePassPrinter(PassInf, Out->os()));
  783. break;
  784. }
  785. }
  786. }
  787. if (PrintEachXForm)
  788. Passes.add(
  789. createPrintModulePass(errs(), "", PreserveAssemblyUseListOrder));
  790. }
  791. if (StandardLinkOpts) {
  792. AddStandardLinkPasses(Passes);
  793. StandardLinkOpts = false;
  794. }
  795. if (OptLevelO0)
  796. AddOptimizationPasses(Passes, *FPasses, TM.get(), 0, 0);
  797. if (OptLevelO1)
  798. AddOptimizationPasses(Passes, *FPasses, TM.get(), 1, 0);
  799. if (OptLevelO2)
  800. AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 0);
  801. if (OptLevelOs)
  802. AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 1);
  803. if (OptLevelOz)
  804. AddOptimizationPasses(Passes, *FPasses, TM.get(), 2, 2);
  805. if (OptLevelO3)
  806. AddOptimizationPasses(Passes, *FPasses, TM.get(), 3, 0);
  807. if (FPasses) {
  808. FPasses->doInitialization();
  809. for (Function &F : *M)
  810. FPasses->run(F);
  811. FPasses->doFinalization();
  812. }
  813. // Check that the module is well formed on completion of optimization
  814. if (!NoVerify && !VerifyEach)
  815. Passes.add(createVerifierPass());
  816. if (AddOneTimeDebugifyPasses)
  817. Passes.add(createCheckDebugifyModulePass(false));
  818. // In run twice mode, we want to make sure the output is bit-by-bit
  819. // equivalent if we run the pass manager again, so setup two buffers and
  820. // a stream to write to them. Note that llc does something similar and it
  821. // may be worth to abstract this out in the future.
  822. SmallVector<char, 0> Buffer;
  823. SmallVector<char, 0> FirstRunBuffer;
  824. std::unique_ptr<raw_svector_ostream> BOS;
  825. raw_ostream *OS = nullptr;
  826. const bool ShouldEmitOutput = !NoOutput && !AnalyzeOnly;
  827. // Write bitcode or assembly to the output as the last step...
  828. if (ShouldEmitOutput || RunTwice) {
  829. assert(Out);
  830. OS = &Out->os();
  831. if (RunTwice) {
  832. BOS = std::make_unique<raw_svector_ostream>(Buffer);
  833. OS = BOS.get();
  834. }
  835. if (OutputAssembly) {
  836. if (EmitSummaryIndex)
  837. report_fatal_error("Text output is incompatible with -module-summary");
  838. if (EmitModuleHash)
  839. report_fatal_error("Text output is incompatible with -module-hash");
  840. Passes.add(createPrintModulePass(*OS, "", PreserveAssemblyUseListOrder));
  841. } else if (OutputThinLTOBC)
  842. Passes.add(createWriteThinLTOBitcodePass(
  843. *OS, ThinLinkOut ? &ThinLinkOut->os() : nullptr));
  844. else
  845. Passes.add(createBitcodeWriterPass(*OS, PreserveBitcodeUseListOrder,
  846. EmitSummaryIndex, EmitModuleHash));
  847. }
  848. // Before executing passes, print the final values of the LLVM options.
  849. cl::PrintOptionValues();
  850. if (!RunTwice) {
  851. // Now that we have all of the passes ready, run them.
  852. Passes.run(*M);
  853. } else {
  854. // If requested, run all passes twice with the same pass manager to catch
  855. // bugs caused by persistent state in the passes.
  856. std::unique_ptr<Module> M2(CloneModule(*M));
  857. // Run all passes on the original module first, so the second run processes
  858. // the clone to catch CloneModule bugs.
  859. Passes.run(*M);
  860. FirstRunBuffer = Buffer;
  861. Buffer.clear();
  862. Passes.run(*M2);
  863. // Compare the two outputs and make sure they're the same
  864. assert(Out);
  865. if (Buffer.size() != FirstRunBuffer.size() ||
  866. (memcmp(Buffer.data(), FirstRunBuffer.data(), Buffer.size()) != 0)) {
  867. errs()
  868. << "Running the pass manager twice changed the output.\n"
  869. "Writing the result of the second run to the specified output.\n"
  870. "To generate the one-run comparison binary, just run without\n"
  871. "the compile-twice option\n";
  872. if (ShouldEmitOutput) {
  873. Out->os() << BOS->str();
  874. Out->keep();
  875. }
  876. if (RemarksFile)
  877. RemarksFile->keep();
  878. return 1;
  879. }
  880. if (ShouldEmitOutput)
  881. Out->os() << BOS->str();
  882. }
  883. if (DebugifyEach && !DebugifyExport.empty())
  884. exportDebugifyStats(DebugifyExport, Passes.getDebugifyStatsMap());
  885. // Declare success.
  886. if (!NoOutput || PrintBreakpoints)
  887. Out->keep();
  888. if (RemarksFile)
  889. RemarksFile->keep();
  890. if (ThinLinkOut)
  891. ThinLinkOut->keep();
  892. return 0;
  893. }