MachineBlockFrequencyInfo.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- MachineBlockFrequencyInfo.h - MBB 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_CODEGEN_MACHINEBLOCKFREQUENCYINFO_H
  18. #define LLVM_CODEGEN_MACHINEBLOCKFREQUENCYINFO_H
  19. #include "llvm/CodeGen/MachineFunctionPass.h"
  20. #include "llvm/Support/BlockFrequency.h"
  21. #include <cstdint>
  22. #include <memory>
  23. #include <optional>
  24. namespace llvm {
  25. template <class BlockT> class BlockFrequencyInfoImpl;
  26. class MachineBasicBlock;
  27. class MachineBranchProbabilityInfo;
  28. class MachineFunction;
  29. class MachineLoopInfo;
  30. class raw_ostream;
  31. /// MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation
  32. /// to estimate machine basic block frequencies.
  33. class MachineBlockFrequencyInfo : public MachineFunctionPass {
  34. using ImplType = BlockFrequencyInfoImpl<MachineBasicBlock>;
  35. std::unique_ptr<ImplType> MBFI;
  36. public:
  37. static char ID;
  38. MachineBlockFrequencyInfo();
  39. explicit MachineBlockFrequencyInfo(MachineFunction &F,
  40. MachineBranchProbabilityInfo &MBPI,
  41. MachineLoopInfo &MLI);
  42. ~MachineBlockFrequencyInfo() override;
  43. void getAnalysisUsage(AnalysisUsage &AU) const override;
  44. bool runOnMachineFunction(MachineFunction &F) override;
  45. /// calculate - compute block frequency info for the given function.
  46. void calculate(const MachineFunction &F,
  47. const MachineBranchProbabilityInfo &MBPI,
  48. const MachineLoopInfo &MLI);
  49. void releaseMemory() override;
  50. /// getblockFreq - Return block frequency. Return 0 if we don't have the
  51. /// information. Please note that initial frequency is equal to 1024. It means
  52. /// that we should not rely on the value itself, but only on the comparison to
  53. /// the other block frequencies. We do this to avoid using of floating points.
  54. /// For example, to get the frequency of a block relative to the entry block,
  55. /// divide the integral value returned by this function (the
  56. /// BlockFrequency::getFrequency() value) by getEntryFreq().
  57. BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const;
  58. /// Compute the frequency of the block, relative to the entry block.
  59. /// This API assumes getEntryFreq() is non-zero.
  60. float getBlockFreqRelativeToEntryBlock(const MachineBasicBlock *MBB) const {
  61. return getBlockFreq(MBB).getFrequency() * (1.0f / getEntryFreq());
  62. }
  63. std::optional<uint64_t>
  64. getBlockProfileCount(const MachineBasicBlock *MBB) const;
  65. std::optional<uint64_t> getProfileCountFromFreq(uint64_t Freq) const;
  66. bool isIrrLoopHeader(const MachineBasicBlock *MBB) const;
  67. /// incrementally calculate block frequencies when we split edges, to avoid
  68. /// full CFG traversal.
  69. void onEdgeSplit(const MachineBasicBlock &NewPredecessor,
  70. const MachineBasicBlock &NewSuccessor,
  71. const MachineBranchProbabilityInfo &MBPI);
  72. const MachineFunction *getFunction() const;
  73. const MachineBranchProbabilityInfo *getMBPI() const;
  74. /// Pop up a ghostview window with the current block frequency propagation
  75. /// rendered using dot.
  76. void view(const Twine &Name, bool isSimple = true) const;
  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,
  83. const MachineBasicBlock *MBB) const;
  84. /// Divide a block's BlockFrequency::getFrequency() value by this value to
  85. /// obtain the entry block - relative frequency of said block.
  86. uint64_t getEntryFreq() const;
  87. };
  88. } // end namespace llvm
  89. #endif // LLVM_CODEGEN_MACHINEBLOCKFREQUENCYINFO_H
  90. #ifdef __GNUC__
  91. #pragma GCC diagnostic pop
  92. #endif