BlockFrequencyInfo.h 5.6 KB

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