MachineBlockFrequencyInfo.h 4.1 KB

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