UnrollLoop.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/Transforms/Utils/UnrollLoop.h - Unrolling utilities -*- 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 some loop unrolling utilities. It does not define any
  15. // actual pass or policy, but provides a single function to perform loop
  16. // unrolling.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_TRANSFORMS_UTILS_UNROLLLOOP_H
  20. #define LLVM_TRANSFORMS_UTILS_UNROLLLOOP_H
  21. #include "llvm/ADT/DenseMap.h"
  22. #include "llvm/Analysis/TargetTransformInfo.h"
  23. #include "llvm/Support/InstructionCost.h"
  24. namespace llvm {
  25. class AssumptionCache;
  26. class BasicBlock;
  27. class BlockFrequencyInfo;
  28. class DependenceInfo;
  29. class DominatorTree;
  30. class Loop;
  31. class LoopInfo;
  32. class MDNode;
  33. class ProfileSummaryInfo;
  34. class OptimizationRemarkEmitter;
  35. class ScalarEvolution;
  36. class StringRef;
  37. class Value;
  38. using NewLoopsMap = SmallDenseMap<const Loop *, Loop *, 4>;
  39. /// @{
  40. /// Metadata attribute names
  41. const char *const LLVMLoopUnrollFollowupAll = "llvm.loop.unroll.followup_all";
  42. const char *const LLVMLoopUnrollFollowupUnrolled =
  43. "llvm.loop.unroll.followup_unrolled";
  44. const char *const LLVMLoopUnrollFollowupRemainder =
  45. "llvm.loop.unroll.followup_remainder";
  46. /// @}
  47. const Loop* addClonedBlockToLoopInfo(BasicBlock *OriginalBB,
  48. BasicBlock *ClonedBB, LoopInfo *LI,
  49. NewLoopsMap &NewLoops);
  50. /// Represents the result of a \c UnrollLoop invocation.
  51. enum class LoopUnrollResult {
  52. /// The loop was not modified.
  53. Unmodified,
  54. /// The loop was partially unrolled -- we still have a loop, but with a
  55. /// smaller trip count. We may also have emitted epilogue loop if the loop
  56. /// had a non-constant trip count.
  57. PartiallyUnrolled,
  58. /// The loop was fully unrolled into straight-line code. We no longer have
  59. /// any back-edges.
  60. FullyUnrolled
  61. };
  62. struct UnrollLoopOptions {
  63. unsigned Count;
  64. bool Force;
  65. bool Runtime;
  66. bool AllowExpensiveTripCount;
  67. bool UnrollRemainder;
  68. bool ForgetAllSCEV;
  69. };
  70. LoopUnrollResult UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
  71. ScalarEvolution *SE, DominatorTree *DT,
  72. AssumptionCache *AC,
  73. const llvm::TargetTransformInfo *TTI,
  74. OptimizationRemarkEmitter *ORE, bool PreserveLCSSA,
  75. Loop **RemainderLoop = nullptr);
  76. bool UnrollRuntimeLoopRemainder(
  77. Loop *L, unsigned Count, bool AllowExpensiveTripCount,
  78. bool UseEpilogRemainder, bool UnrollRemainder, bool ForgetAllSCEV,
  79. LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC,
  80. const TargetTransformInfo *TTI, bool PreserveLCSSA,
  81. Loop **ResultLoop = nullptr);
  82. LoopUnrollResult UnrollAndJamLoop(Loop *L, unsigned Count, unsigned TripCount,
  83. unsigned TripMultiple, bool UnrollRemainder,
  84. LoopInfo *LI, ScalarEvolution *SE,
  85. DominatorTree *DT, AssumptionCache *AC,
  86. const TargetTransformInfo *TTI,
  87. OptimizationRemarkEmitter *ORE,
  88. Loop **EpilogueLoop = nullptr);
  89. bool isSafeToUnrollAndJam(Loop *L, ScalarEvolution &SE, DominatorTree &DT,
  90. DependenceInfo &DI, LoopInfo &LI);
  91. bool computeUnrollCount(Loop *L, const TargetTransformInfo &TTI,
  92. DominatorTree &DT, LoopInfo *LI, AssumptionCache *AC,
  93. ScalarEvolution &SE,
  94. const SmallPtrSetImpl<const Value *> &EphValues,
  95. OptimizationRemarkEmitter *ORE, unsigned TripCount,
  96. unsigned MaxTripCount, bool MaxOrZero,
  97. unsigned TripMultiple, unsigned LoopSize,
  98. TargetTransformInfo::UnrollingPreferences &UP,
  99. TargetTransformInfo::PeelingPreferences &PP,
  100. bool &UseUpperBound);
  101. void simplifyLoopAfterUnroll(Loop *L, bool SimplifyIVs, LoopInfo *LI,
  102. ScalarEvolution *SE, DominatorTree *DT,
  103. AssumptionCache *AC,
  104. const TargetTransformInfo *TTI);
  105. MDNode *GetUnrollMetadata(MDNode *LoopID, StringRef Name);
  106. TargetTransformInfo::UnrollingPreferences gatherUnrollingPreferences(
  107. Loop *L, ScalarEvolution &SE, const TargetTransformInfo &TTI,
  108. BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI,
  109. llvm::OptimizationRemarkEmitter &ORE, int OptLevel,
  110. std::optional<unsigned> UserThreshold, std::optional<unsigned> UserCount,
  111. std::optional<bool> UserAllowPartial, std::optional<bool> UserRuntime,
  112. std::optional<bool> UserUpperBound,
  113. std::optional<unsigned> UserFullUnrollMaxCount);
  114. InstructionCost ApproximateLoopSize(const Loop *L, unsigned &NumCalls,
  115. bool &NotDuplicatable, bool &Convergent, const TargetTransformInfo &TTI,
  116. const SmallPtrSetImpl<const Value *> &EphValues, unsigned BEInsns);
  117. } // end namespace llvm
  118. #endif // LLVM_TRANSFORMS_UTILS_UNROLLLOOP_H
  119. #ifdef __GNUC__
  120. #pragma GCC diagnostic pop
  121. #endif