PassManagerBuilder.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // llvm/Transforms/IPO/PassManagerBuilder.h - Build Standard Pass -*- C++ -*-=//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the PassManagerBuilder class, which is used to set up a
  11. // "standard" optimization sequence suitable for languages like C and C++.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_TRANSFORMS_IPO_PASSMANAGERBUILDER_H
  15. #define LLVM_TRANSFORMS_IPO_PASSMANAGERBUILDER_H
  16. #include <vector>
  17. namespace hlsl {
  18. class HLSLExtensionsCodegenHelper;
  19. }
  20. namespace llvm {
  21. class Pass;
  22. class TargetLibraryInfoImpl;
  23. class TargetMachine;
  24. // The old pass manager infrastructure is hidden in a legacy namespace now.
  25. namespace legacy {
  26. class FunctionPassManager;
  27. class PassManagerBase;
  28. }
  29. /// PassManagerBuilder - This class is used to set up a standard optimization
  30. /// sequence for languages like C and C++, allowing some APIs to customize the
  31. /// pass sequence in various ways. A simple example of using it would be:
  32. ///
  33. /// PassManagerBuilder Builder;
  34. /// Builder.OptLevel = 2;
  35. /// Builder.populateFunctionPassManager(FPM);
  36. /// Builder.populateModulePassManager(MPM);
  37. ///
  38. /// In addition to setting up the basic passes, PassManagerBuilder allows
  39. /// frontends to vend a plugin API, where plugins are allowed to add extensions
  40. /// to the default pass manager. They do this by specifying where in the pass
  41. /// pipeline they want to be added, along with a callback function that adds
  42. /// the pass(es). For example, a plugin that wanted to add a loop optimization
  43. /// could do something like this:
  44. ///
  45. /// static void addMyLoopPass(const PMBuilder &Builder, PassManagerBase &PM) {
  46. /// if (Builder.getOptLevel() > 2 && Builder.getOptSizeLevel() == 0)
  47. /// PM.add(createMyAwesomePass());
  48. /// }
  49. /// ...
  50. /// Builder.addExtension(PassManagerBuilder::EP_LoopOptimizerEnd,
  51. /// addMyLoopPass);
  52. /// ...
  53. class PassManagerBuilder {
  54. public:
  55. /// Extensions are passed the builder itself (so they can see how it is
  56. /// configured) as well as the pass manager to add stuff to.
  57. typedef void (*ExtensionFn)(const PassManagerBuilder &Builder,
  58. legacy::PassManagerBase &PM);
  59. enum ExtensionPointTy {
  60. /// EP_EarlyAsPossible - This extension point allows adding passes before
  61. /// any other transformations, allowing them to see the code as it is coming
  62. /// out of the frontend.
  63. EP_EarlyAsPossible,
  64. /// EP_ModuleOptimizerEarly - This extension point allows adding passes
  65. /// just before the main module-level optimization passes.
  66. EP_ModuleOptimizerEarly,
  67. /// EP_LoopOptimizerEnd - This extension point allows adding loop passes to
  68. /// the end of the loop optimizer.
  69. EP_LoopOptimizerEnd,
  70. /// EP_ScalarOptimizerLate - This extension point allows adding optimization
  71. /// passes after most of the main optimizations, but before the last
  72. /// cleanup-ish optimizations.
  73. EP_ScalarOptimizerLate,
  74. /// EP_OptimizerLast -- This extension point allows adding passes that
  75. /// run after everything else.
  76. EP_OptimizerLast,
  77. /// EP_EnabledOnOptLevel0 - This extension point allows adding passes that
  78. /// should not be disabled by O0 optimization level. The passes will be
  79. /// inserted after the inlining pass.
  80. EP_EnabledOnOptLevel0,
  81. /// EP_Peephole - This extension point allows adding passes that perform
  82. /// peephole optimizations similar to the instruction combiner. These passes
  83. /// will be inserted after each instance of the instruction combiner pass.
  84. EP_Peephole,
  85. };
  86. /// The Optimization Level - Specify the basic optimization level.
  87. /// 0 = -O0, 1 = -O1, 2 = -O2, 3 = -O3
  88. unsigned OptLevel;
  89. /// SizeLevel - How much we're optimizing for size.
  90. /// 0 = none, 1 = -Os, 2 = -Oz
  91. unsigned SizeLevel;
  92. /// LibraryInfo - Specifies information about the runtime library for the
  93. /// optimizer. If this is non-null, it is added to both the function and
  94. /// per-module pass pipeline.
  95. TargetLibraryInfoImpl *LibraryInfo;
  96. /// Inliner - Specifies the inliner to use. If this is non-null, it is
  97. /// added to the per-module passes.
  98. Pass *Inliner;
  99. bool DisableTailCalls;
  100. bool DisableUnitAtATime;
  101. bool DisableUnrollLoops;
  102. bool BBVectorize;
  103. bool SLPVectorize;
  104. bool LoopVectorize;
  105. bool RerollLoops;
  106. bool LoadCombine;
  107. bool DisableGVNLoadPRE;
  108. bool VerifyInput;
  109. bool VerifyOutput;
  110. bool MergeFunctions;
  111. bool PrepareForLTO;
  112. bool HLSLHighLevel = false; // HLSL Change
  113. hlsl::HLSLExtensionsCodegenHelper *HLSLExtensionsCodeGen = nullptr; // HLSL Change
  114. private:
  115. /// ExtensionList - This is list of all of the extensions that are registered.
  116. std::vector<std::pair<ExtensionPointTy, ExtensionFn> > Extensions;
  117. public:
  118. PassManagerBuilder();
  119. ~PassManagerBuilder();
  120. /// Adds an extension that will be used by all PassManagerBuilder instances.
  121. /// This is intended to be used by plugins, to register a set of
  122. /// optimisations to run automatically.
  123. static void addGlobalExtension(ExtensionPointTy Ty, ExtensionFn Fn);
  124. void addExtension(ExtensionPointTy Ty, ExtensionFn Fn);
  125. private:
  126. void addExtensionsToPM(ExtensionPointTy ETy,
  127. legacy::PassManagerBase &PM) const;
  128. void addInitialAliasAnalysisPasses(legacy::PassManagerBase &PM) const;
  129. void addLTOOptimizationPasses(legacy::PassManagerBase &PM);
  130. void addLateLTOOptimizationPasses(legacy::PassManagerBase &PM);
  131. public:
  132. /// populateFunctionPassManager - This fills in the function pass manager,
  133. /// which is expected to be run on each function immediately as it is
  134. /// generated. The idea is to reduce the size of the IR in memory.
  135. void populateFunctionPassManager(legacy::FunctionPassManager &FPM);
  136. /// populateModulePassManager - This sets up the primary pass manager.
  137. void populateModulePassManager(legacy::PassManagerBase &MPM);
  138. void populateLTOPassManager(legacy::PassManagerBase &PM);
  139. };
  140. /// Registers a function for adding a standard set of passes. This should be
  141. /// used by optimizer plugins to allow all front ends to transparently use
  142. /// them. Create a static instance of this class in your plugin, providing a
  143. /// private function that the PassManagerBuilder can use to add your passes.
  144. struct RegisterStandardPasses {
  145. RegisterStandardPasses(PassManagerBuilder::ExtensionPointTy Ty,
  146. PassManagerBuilder::ExtensionFn Fn) {
  147. PassManagerBuilder::addGlobalExtension(Ty, Fn);
  148. }
  149. };
  150. } // end namespace llvm
  151. #endif