UnrollLoop.h 5.5 KB

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