LoopUnrollPass.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- LoopUnrollPass.h -----------------------------------------*- 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. #ifndef LLVM_TRANSFORMS_SCALAR_LOOPUNROLLPASS_H
  14. #define LLVM_TRANSFORMS_SCALAR_LOOPUNROLLPASS_H
  15. #include "llvm/Analysis/LoopAnalysisManager.h"
  16. #include "llvm/IR/PassManager.h"
  17. #include "llvm/Support/CommandLine.h"
  18. #include <optional>
  19. namespace llvm {
  20. extern cl::opt<bool> ForgetSCEVInLoopUnroll;
  21. class Function;
  22. class Loop;
  23. class LPMUpdater;
  24. /// Loop unroll pass that only does full loop unrolling and peeling.
  25. class LoopFullUnrollPass : public PassInfoMixin<LoopFullUnrollPass> {
  26. const int OptLevel;
  27. /// If false, use a cost model to determine whether unrolling of a loop is
  28. /// profitable. If true, only loops that explicitly request unrolling via
  29. /// metadata are considered. All other loops are skipped.
  30. const bool OnlyWhenForced;
  31. /// If true, forget all loops when unrolling. If false, forget top-most loop
  32. /// of the currently processed loops, which removes one entry at a time from
  33. /// the internal SCEV records. For large loops, the former is faster.
  34. const bool ForgetSCEV;
  35. public:
  36. explicit LoopFullUnrollPass(int OptLevel = 2, bool OnlyWhenForced = false,
  37. bool ForgetSCEV = false)
  38. : OptLevel(OptLevel), OnlyWhenForced(OnlyWhenForced),
  39. ForgetSCEV(ForgetSCEV) {}
  40. PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
  41. LoopStandardAnalysisResults &AR, LPMUpdater &U);
  42. };
  43. /// A set of parameters used to control various transforms performed by the
  44. /// LoopUnroll pass. Each of the boolean parameters can be set to:
  45. /// true - enabling the transformation.
  46. /// false - disabling the transformation.
  47. /// None - relying on a global default.
  48. ///
  49. /// There is also OptLevel parameter, which is used for additional loop unroll
  50. /// tuning.
  51. ///
  52. /// Intended use is to create a default object, modify parameters with
  53. /// additional setters and then pass it to LoopUnrollPass.
  54. ///
  55. struct LoopUnrollOptions {
  56. std::optional<bool> AllowPartial;
  57. std::optional<bool> AllowPeeling;
  58. std::optional<bool> AllowRuntime;
  59. std::optional<bool> AllowUpperBound;
  60. std::optional<bool> AllowProfileBasedPeeling;
  61. std::optional<unsigned> FullUnrollMaxCount;
  62. int OptLevel;
  63. /// If false, use a cost model to determine whether unrolling of a loop is
  64. /// profitable. If true, only loops that explicitly request unrolling via
  65. /// metadata are considered. All other loops are skipped.
  66. bool OnlyWhenForced;
  67. /// If true, forget all loops when unrolling. If false, forget top-most loop
  68. /// of the currently processed loops, which removes one entry at a time from
  69. /// the internal SCEV records. For large loops, the former is faster.
  70. const bool ForgetSCEV;
  71. LoopUnrollOptions(int OptLevel = 2, bool OnlyWhenForced = false,
  72. bool ForgetSCEV = false)
  73. : OptLevel(OptLevel), OnlyWhenForced(OnlyWhenForced),
  74. ForgetSCEV(ForgetSCEV) {}
  75. /// Enables or disables partial unrolling. When disabled only full unrolling
  76. /// is allowed.
  77. LoopUnrollOptions &setPartial(bool Partial) {
  78. AllowPartial = Partial;
  79. return *this;
  80. }
  81. /// Enables or disables unrolling of loops with runtime trip count.
  82. LoopUnrollOptions &setRuntime(bool Runtime) {
  83. AllowRuntime = Runtime;
  84. return *this;
  85. }
  86. /// Enables or disables loop peeling.
  87. LoopUnrollOptions &setPeeling(bool Peeling) {
  88. AllowPeeling = Peeling;
  89. return *this;
  90. }
  91. /// Enables or disables the use of trip count upper bound
  92. /// in loop unrolling.
  93. LoopUnrollOptions &setUpperBound(bool UpperBound) {
  94. AllowUpperBound = UpperBound;
  95. return *this;
  96. }
  97. // Sets "optimization level" tuning parameter for loop unrolling.
  98. LoopUnrollOptions &setOptLevel(int O) {
  99. OptLevel = O;
  100. return *this;
  101. }
  102. // Enables or disables loop peeling basing on profile.
  103. LoopUnrollOptions &setProfileBasedPeeling(int O) {
  104. AllowProfileBasedPeeling = O;
  105. return *this;
  106. }
  107. // Sets the max full unroll count.
  108. LoopUnrollOptions &setFullUnrollMaxCount(unsigned O) {
  109. FullUnrollMaxCount = O;
  110. return *this;
  111. }
  112. };
  113. /// Loop unroll pass that will support both full and partial unrolling.
  114. /// It is a function pass to have access to function and module analyses.
  115. /// It will also put loops into canonical form (simplified and LCSSA).
  116. class LoopUnrollPass : public PassInfoMixin<LoopUnrollPass> {
  117. LoopUnrollOptions UnrollOpts;
  118. public:
  119. /// This uses the target information (or flags) to control the thresholds for
  120. /// different unrolling stategies but supports all of them.
  121. explicit LoopUnrollPass(LoopUnrollOptions UnrollOpts = {})
  122. : UnrollOpts(UnrollOpts) {}
  123. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  124. void printPipeline(raw_ostream &OS,
  125. function_ref<StringRef(StringRef)> MapClassName2PassName);
  126. };
  127. } // end namespace llvm
  128. #endif // LLVM_TRANSFORMS_SCALAR_LOOPUNROLLPASS_H
  129. #ifdef __GNUC__
  130. #pragma GCC diagnostic pop
  131. #endif