PassManagerBuilder.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. // llvm/Transforms/IPO/PassManagerBuilder.h - Build Standard Pass -*- C++ -*-=//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the PassManagerBuilder class, which is used to set up a
  15. // "standard" optimization sequence suitable for languages like C and C++.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_TRANSFORMS_IPO_PASSMANAGERBUILDER_H
  19. #define LLVM_TRANSFORMS_IPO_PASSMANAGERBUILDER_H
  20. #include "llvm-c/Transforms/PassManagerBuilder.h"
  21. #include <functional>
  22. #include <memory>
  23. #include <string>
  24. #include <vector>
  25. namespace llvm {
  26. class ModuleSummaryIndex;
  27. class Pass;
  28. class TargetLibraryInfoImpl;
  29. // The old pass manager infrastructure is hidden in a legacy namespace now.
  30. namespace legacy {
  31. class FunctionPassManager;
  32. class PassManagerBase;
  33. }
  34. /// PassManagerBuilder - This class is used to set up a standard optimization
  35. /// sequence for languages like C and C++, allowing some APIs to customize the
  36. /// pass sequence in various ways. A simple example of using it would be:
  37. ///
  38. /// PassManagerBuilder Builder;
  39. /// Builder.OptLevel = 2;
  40. /// Builder.populateFunctionPassManager(FPM);
  41. /// Builder.populateModulePassManager(MPM);
  42. ///
  43. /// In addition to setting up the basic passes, PassManagerBuilder allows
  44. /// frontends to vend a plugin API, where plugins are allowed to add extensions
  45. /// to the default pass manager. They do this by specifying where in the pass
  46. /// pipeline they want to be added, along with a callback function that adds
  47. /// the pass(es). For example, a plugin that wanted to add a loop optimization
  48. /// could do something like this:
  49. ///
  50. /// static void addMyLoopPass(const PMBuilder &Builder, PassManagerBase &PM) {
  51. /// if (Builder.getOptLevel() > 2 && Builder.getOptSizeLevel() == 0)
  52. /// PM.add(createMyAwesomePass());
  53. /// }
  54. /// ...
  55. /// Builder.addExtension(PassManagerBuilder::EP_LoopOptimizerEnd,
  56. /// addMyLoopPass);
  57. /// ...
  58. class PassManagerBuilder {
  59. public:
  60. /// Extensions are passed to the builder itself (so they can see how it is
  61. /// configured) as well as the pass manager to add stuff to.
  62. typedef std::function<void(const PassManagerBuilder &Builder,
  63. legacy::PassManagerBase &PM)>
  64. ExtensionFn;
  65. typedef int GlobalExtensionID;
  66. enum ExtensionPointTy {
  67. /// EP_EarlyAsPossible - This extension point allows adding passes before
  68. /// any other transformations, allowing them to see the code as it is coming
  69. /// out of the frontend.
  70. EP_EarlyAsPossible,
  71. /// EP_ModuleOptimizerEarly - This extension point allows adding passes
  72. /// just before the main module-level optimization passes.
  73. EP_ModuleOptimizerEarly,
  74. /// EP_LoopOptimizerEnd - This extension point allows adding loop passes to
  75. /// the end of the loop optimizer.
  76. EP_LoopOptimizerEnd,
  77. /// EP_ScalarOptimizerLate - This extension point allows adding optimization
  78. /// passes after most of the main optimizations, but before the last
  79. /// cleanup-ish optimizations.
  80. EP_ScalarOptimizerLate,
  81. /// EP_OptimizerLast -- This extension point allows adding passes that
  82. /// run after everything else.
  83. EP_OptimizerLast,
  84. /// EP_VectorizerStart - This extension point allows adding optimization
  85. /// passes before the vectorizer and other highly target specific
  86. /// optimization passes are executed.
  87. EP_VectorizerStart,
  88. /// EP_EnabledOnOptLevel0 - This extension point allows adding passes that
  89. /// should not be disabled by O0 optimization level. The passes will be
  90. /// inserted after the inlining pass.
  91. EP_EnabledOnOptLevel0,
  92. /// EP_Peephole - This extension point allows adding passes that perform
  93. /// peephole optimizations similar to the instruction combiner. These passes
  94. /// will be inserted after each instance of the instruction combiner pass.
  95. EP_Peephole,
  96. /// EP_LateLoopOptimizations - This extension point allows adding late loop
  97. /// canonicalization and simplification passes. This is the last point in
  98. /// the loop optimization pipeline before loop deletion. Each pass added
  99. /// here must be an instance of LoopPass.
  100. /// This is the place to add passes that can remove loops, such as target-
  101. /// specific loop idiom recognition.
  102. EP_LateLoopOptimizations,
  103. /// EP_CGSCCOptimizerLate - This extension point allows adding CallGraphSCC
  104. /// passes at the end of the main CallGraphSCC passes and before any
  105. /// function simplification passes run by CGPassManager.
  106. EP_CGSCCOptimizerLate,
  107. /// EP_FullLinkTimeOptimizationEarly - This extensions point allow adding
  108. /// passes that
  109. /// run at Link Time, before Full Link Time Optimization.
  110. EP_FullLinkTimeOptimizationEarly,
  111. /// EP_FullLinkTimeOptimizationLast - This extensions point allow adding
  112. /// passes that
  113. /// run at Link Time, after Full Link Time Optimization.
  114. EP_FullLinkTimeOptimizationLast,
  115. };
  116. /// The Optimization Level - Specify the basic optimization level.
  117. /// 0 = -O0, 1 = -O1, 2 = -O2, 3 = -O3
  118. unsigned OptLevel;
  119. /// SizeLevel - How much we're optimizing for size.
  120. /// 0 = none, 1 = -Os, 2 = -Oz
  121. unsigned SizeLevel;
  122. /// LibraryInfo - Specifies information about the runtime library for the
  123. /// optimizer. If this is non-null, it is added to both the function and
  124. /// per-module pass pipeline.
  125. TargetLibraryInfoImpl *LibraryInfo;
  126. /// Inliner - Specifies the inliner to use. If this is non-null, it is
  127. /// added to the per-module passes.
  128. Pass *Inliner;
  129. /// The module summary index to use for exporting information from the
  130. /// regular LTO phase, for example for the CFI and devirtualization type
  131. /// tests.
  132. ModuleSummaryIndex *ExportSummary = nullptr;
  133. /// The module summary index to use for importing information to the
  134. /// thin LTO backends, for example for the CFI and devirtualization type
  135. /// tests.
  136. const ModuleSummaryIndex *ImportSummary = nullptr;
  137. bool DisableUnrollLoops;
  138. bool CallGraphProfile;
  139. bool SLPVectorize;
  140. bool LoopVectorize;
  141. bool LoopsInterleaved;
  142. bool RerollLoops;
  143. bool NewGVN;
  144. bool DisableGVNLoadPRE;
  145. bool ForgetAllSCEVInLoopUnroll;
  146. bool VerifyInput;
  147. bool VerifyOutput;
  148. bool MergeFunctions;
  149. bool PrepareForLTO;
  150. bool PrepareForThinLTO;
  151. bool PerformThinLTO;
  152. bool DivergentTarget;
  153. unsigned LicmMssaOptCap;
  154. unsigned LicmMssaNoAccForPromotionCap;
  155. /// Enable profile instrumentation pass.
  156. bool EnablePGOInstrGen;
  157. /// Enable profile context sensitive instrumentation pass.
  158. bool EnablePGOCSInstrGen;
  159. /// Enable profile context sensitive profile use pass.
  160. bool EnablePGOCSInstrUse;
  161. /// Profile data file name that the instrumentation will be written to.
  162. std::string PGOInstrGen;
  163. /// Path of the profile data file.
  164. std::string PGOInstrUse;
  165. /// Path of the sample Profile data file.
  166. std::string PGOSampleUse;
  167. private:
  168. /// ExtensionList - This is list of all of the extensions that are registered.
  169. std::vector<std::pair<ExtensionPointTy, ExtensionFn>> Extensions;
  170. public:
  171. PassManagerBuilder();
  172. ~PassManagerBuilder();
  173. /// Adds an extension that will be used by all PassManagerBuilder instances.
  174. /// This is intended to be used by plugins, to register a set of
  175. /// optimisations to run automatically.
  176. ///
  177. /// \returns A global extension identifier that can be used to remove the
  178. /// extension.
  179. static GlobalExtensionID addGlobalExtension(ExtensionPointTy Ty,
  180. ExtensionFn Fn);
  181. /// Removes an extension that was previously added using addGlobalExtension.
  182. /// This is also intended to be used by plugins, to remove any extension that
  183. /// was previously registered before being unloaded.
  184. ///
  185. /// \param ExtensionID Identifier of the extension to be removed.
  186. static void removeGlobalExtension(GlobalExtensionID ExtensionID);
  187. void addExtension(ExtensionPointTy Ty, ExtensionFn Fn);
  188. private:
  189. void addExtensionsToPM(ExtensionPointTy ETy,
  190. legacy::PassManagerBase &PM) const;
  191. void addInitialAliasAnalysisPasses(legacy::PassManagerBase &PM) const;
  192. void addLTOOptimizationPasses(legacy::PassManagerBase &PM);
  193. void addLateLTOOptimizationPasses(legacy::PassManagerBase &PM);
  194. void addPGOInstrPasses(legacy::PassManagerBase &MPM, bool IsCS);
  195. void addFunctionSimplificationPasses(legacy::PassManagerBase &MPM);
  196. void addVectorPasses(legacy::PassManagerBase &PM, bool IsFullLTO);
  197. public:
  198. /// populateFunctionPassManager - This fills in the function pass manager,
  199. /// which is expected to be run on each function immediately as it is
  200. /// generated. The idea is to reduce the size of the IR in memory.
  201. void populateFunctionPassManager(legacy::FunctionPassManager &FPM);
  202. /// populateModulePassManager - This sets up the primary pass manager.
  203. void populateModulePassManager(legacy::PassManagerBase &MPM);
  204. void populateLTOPassManager(legacy::PassManagerBase &PM);
  205. void populateThinLTOPassManager(legacy::PassManagerBase &PM);
  206. };
  207. /// Registers a function for adding a standard set of passes. This should be
  208. /// used by optimizer plugins to allow all front ends to transparently use
  209. /// them. Create a static instance of this class in your plugin, providing a
  210. /// private function that the PassManagerBuilder can use to add your passes.
  211. class RegisterStandardPasses {
  212. PassManagerBuilder::GlobalExtensionID ExtensionID;
  213. public:
  214. RegisterStandardPasses(PassManagerBuilder::ExtensionPointTy Ty,
  215. PassManagerBuilder::ExtensionFn Fn) {
  216. ExtensionID = PassManagerBuilder::addGlobalExtension(Ty, std::move(Fn));
  217. }
  218. ~RegisterStandardPasses() {
  219. // If the collection holding the global extensions is destroyed after the
  220. // plugin is unloaded, the extension has to be removed here. Indeed, the
  221. // destructor of the ExtensionFn may reference code in the plugin.
  222. PassManagerBuilder::removeGlobalExtension(ExtensionID);
  223. }
  224. };
  225. inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
  226. return reinterpret_cast<PassManagerBuilder*>(P);
  227. }
  228. inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
  229. return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
  230. }
  231. } // end namespace llvm
  232. #endif
  233. #ifdef __GNUC__
  234. #pragma GCC diagnostic pop
  235. #endif