UnrollLoop.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. bool Force;
  64. bool Runtime;
  65. bool AllowExpensiveTripCount;
  66. bool UnrollRemainder;
  67. bool ForgetAllSCEV;
  68. };
  69. LoopUnrollResult UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
  70. ScalarEvolution *SE, DominatorTree *DT,
  71. AssumptionCache *AC,
  72. const llvm::TargetTransformInfo *TTI,
  73. OptimizationRemarkEmitter *ORE, bool PreserveLCSSA,
  74. Loop **RemainderLoop = nullptr);
  75. bool UnrollRuntimeLoopRemainder(
  76. Loop *L, unsigned Count, bool AllowExpensiveTripCount,
  77. bool UseEpilogRemainder, bool UnrollRemainder, bool ForgetAllSCEV,
  78. LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC,
  79. const TargetTransformInfo *TTI, bool PreserveLCSSA,
  80. Loop **ResultLoop = nullptr);
  81. LoopUnrollResult UnrollAndJamLoop(Loop *L, unsigned Count, unsigned TripCount,
  82. unsigned TripMultiple, bool UnrollRemainder,
  83. LoopInfo *LI, ScalarEvolution *SE,
  84. DominatorTree *DT, AssumptionCache *AC,
  85. const TargetTransformInfo *TTI,
  86. OptimizationRemarkEmitter *ORE,
  87. Loop **EpilogueLoop = nullptr);
  88. bool isSafeToUnrollAndJam(Loop *L, ScalarEvolution &SE, DominatorTree &DT,
  89. DependenceInfo &DI, LoopInfo &LI);
  90. bool computeUnrollCount(Loop *L, const TargetTransformInfo &TTI,
  91. DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE,
  92. const SmallPtrSetImpl<const Value *> &EphValues,
  93. OptimizationRemarkEmitter *ORE, unsigned TripCount,
  94. unsigned MaxTripCount, bool MaxOrZero,
  95. unsigned TripMultiple, unsigned LoopSize,
  96. TargetTransformInfo::UnrollingPreferences &UP,
  97. TargetTransformInfo::PeelingPreferences &PP,
  98. bool &UseUpperBound);
  99. void simplifyLoopAfterUnroll(Loop *L, bool SimplifyIVs, LoopInfo *LI,
  100. ScalarEvolution *SE, DominatorTree *DT,
  101. AssumptionCache *AC,
  102. const TargetTransformInfo *TTI);
  103. MDNode *GetUnrollMetadata(MDNode *LoopID, StringRef Name);
  104. TargetTransformInfo::UnrollingPreferences gatherUnrollingPreferences(
  105. Loop *L, ScalarEvolution &SE, const TargetTransformInfo &TTI,
  106. BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI,
  107. llvm::OptimizationRemarkEmitter &ORE, int OptLevel,
  108. Optional<unsigned> UserThreshold, Optional<unsigned> UserCount,
  109. Optional<bool> UserAllowPartial, Optional<bool> UserRuntime,
  110. Optional<bool> UserUpperBound, Optional<unsigned> UserFullUnrollMaxCount);
  111. unsigned ApproximateLoopSize(const Loop *L, unsigned &NumCalls,
  112. bool &NotDuplicatable, bool &Convergent,
  113. const TargetTransformInfo &TTI,
  114. const SmallPtrSetImpl<const Value *> &EphValues,
  115. unsigned BEInsns);
  116. } // end namespace llvm
  117. #endif // LLVM_TRANSFORMS_UTILS_UNROLLLOOP_H
  118. #ifdef __GNUC__
  119. #pragma GCC diagnostic pop
  120. #endif