BlockFrequencyInfo.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- BlockFrequencyInfo.h - Block Frequency Analysis ----------*- 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. // Loops should be simplified before this analysis.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
  18. #define LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
  19. #include "llvm/ADT/Optional.h"
  20. #include "llvm/IR/PassManager.h"
  21. #include "llvm/Pass.h"
  22. #include "llvm/Support/BlockFrequency.h"
  23. #include <cstdint>
  24. #include <memory>
  25. namespace llvm {
  26. class BasicBlock;
  27. class BranchProbabilityInfo;
  28. class Function;
  29. class LoopInfo;
  30. class Module;
  31. class raw_ostream;
  32. template <class BlockT> class BlockFrequencyInfoImpl;
  33. enum PGOViewCountsType { PGOVCT_None, PGOVCT_Graph, PGOVCT_Text };
  34. /// BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to
  35. /// estimate IR basic block frequencies.
  36. class BlockFrequencyInfo {
  37. using ImplType = BlockFrequencyInfoImpl<BasicBlock>;
  38. std::unique_ptr<ImplType> BFI;
  39. public:
  40. BlockFrequencyInfo();
  41. BlockFrequencyInfo(const Function &F, const BranchProbabilityInfo &BPI,
  42. const LoopInfo &LI);
  43. BlockFrequencyInfo(const BlockFrequencyInfo &) = delete;
  44. BlockFrequencyInfo &operator=(const BlockFrequencyInfo &) = delete;
  45. BlockFrequencyInfo(BlockFrequencyInfo &&Arg);
  46. BlockFrequencyInfo &operator=(BlockFrequencyInfo &&RHS);
  47. ~BlockFrequencyInfo();
  48. /// Handle invalidation explicitly.
  49. bool invalidate(Function &F, const PreservedAnalyses &PA,
  50. FunctionAnalysisManager::Invalidator &);
  51. const Function *getFunction() const;
  52. const BranchProbabilityInfo *getBPI() const;
  53. void view(StringRef = "BlockFrequencyDAGs") const;
  54. /// getblockFreq - Return block frequency. Return 0 if we don't have the
  55. /// information. Please note that initial frequency is equal to ENTRY_FREQ. It
  56. /// means that we should not rely on the value itself, but only on the
  57. /// comparison to the other block frequencies. We do this to avoid using of
  58. /// floating points.
  59. BlockFrequency getBlockFreq(const BasicBlock *BB) const;
  60. /// Returns the estimated profile count of \p BB.
  61. /// This computes the relative block frequency of \p BB and multiplies it by
  62. /// the enclosing function's count (if available) and returns the value.
  63. Optional<uint64_t> getBlockProfileCount(const BasicBlock *BB,
  64. bool AllowSynthetic = false) const;
  65. /// Returns the estimated profile count of \p Freq.
  66. /// This uses the frequency \p Freq and multiplies it by
  67. /// the enclosing function's count (if available) and returns the value.
  68. Optional<uint64_t> getProfileCountFromFreq(uint64_t Freq) const;
  69. /// Returns true if \p BB is an irreducible loop header
  70. /// block. Otherwise false.
  71. bool isIrrLoopHeader(const BasicBlock *BB);
  72. // Set the frequency of the given basic block.
  73. void setBlockFreq(const BasicBlock *BB, uint64_t Freq);
  74. /// Set the frequency of \p ReferenceBB to \p Freq and scale the frequencies
  75. /// of the blocks in \p BlocksToScale such that their frequencies relative
  76. /// to \p ReferenceBB remain unchanged.
  77. void setBlockFreqAndScale(const BasicBlock *ReferenceBB, uint64_t Freq,
  78. SmallPtrSetImpl<BasicBlock *> &BlocksToScale);
  79. /// calculate - compute block frequency info for the given function.
  80. void calculate(const Function &F, const BranchProbabilityInfo &BPI,
  81. const LoopInfo &LI);
  82. // Print the block frequency Freq to OS using the current functions entry
  83. // frequency to convert freq into a relative decimal form.
  84. raw_ostream &printBlockFreq(raw_ostream &OS, const BlockFrequency Freq) const;
  85. // Convenience method that attempts to look up the frequency associated with
  86. // BB and print it to OS.
  87. raw_ostream &printBlockFreq(raw_ostream &OS, const BasicBlock *BB) const;
  88. uint64_t getEntryFreq() const;
  89. void releaseMemory();
  90. void print(raw_ostream &OS) const;
  91. // Compare to the other BFI and verify they match.
  92. void verifyMatch(BlockFrequencyInfo &Other) const;
  93. };
  94. /// Analysis pass which computes \c BlockFrequencyInfo.
  95. class BlockFrequencyAnalysis
  96. : public AnalysisInfoMixin<BlockFrequencyAnalysis> {
  97. friend AnalysisInfoMixin<BlockFrequencyAnalysis>;
  98. static AnalysisKey Key;
  99. public:
  100. /// Provide the result type for this analysis pass.
  101. using Result = BlockFrequencyInfo;
  102. /// Run the analysis pass over a function and produce BFI.
  103. Result run(Function &F, FunctionAnalysisManager &AM);
  104. };
  105. /// Printer pass for the \c BlockFrequencyInfo results.
  106. class BlockFrequencyPrinterPass
  107. : public PassInfoMixin<BlockFrequencyPrinterPass> {
  108. raw_ostream &OS;
  109. public:
  110. explicit BlockFrequencyPrinterPass(raw_ostream &OS) : OS(OS) {}
  111. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  112. };
  113. /// Legacy analysis pass which computes \c BlockFrequencyInfo.
  114. class BlockFrequencyInfoWrapperPass : public FunctionPass {
  115. BlockFrequencyInfo BFI;
  116. public:
  117. static char ID;
  118. BlockFrequencyInfoWrapperPass();
  119. ~BlockFrequencyInfoWrapperPass() override;
  120. BlockFrequencyInfo &getBFI() { return BFI; }
  121. const BlockFrequencyInfo &getBFI() const { return BFI; }
  122. void getAnalysisUsage(AnalysisUsage &AU) const override;
  123. bool runOnFunction(Function &F) override;
  124. void releaseMemory() override;
  125. void print(raw_ostream &OS, const Module *M) const override;
  126. };
  127. } // end namespace llvm
  128. #endif // LLVM_ANALYSIS_BLOCKFREQUENCYINFO_H
  129. #ifdef __GNUC__
  130. #pragma GCC diagnostic pop
  131. #endif