2
0

MachineBranchProbabilityInfo.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //===- MachineBranchProbabilityInfo.cpp - Machine Branch Probability Info -===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This analysis uses probability info stored in Machine Basic Blocks.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
  14. #include "llvm/CodeGen/MachineBasicBlock.h"
  15. #include "llvm/IR/Instructions.h"
  16. #include "llvm/Support/Debug.h"
  17. #include "llvm/Support/raw_ostream.h"
  18. using namespace llvm;
  19. INITIALIZE_PASS_BEGIN(MachineBranchProbabilityInfo, "machine-branch-prob",
  20. "Machine Branch Probability Analysis", false, true)
  21. INITIALIZE_PASS_END(MachineBranchProbabilityInfo, "machine-branch-prob",
  22. "Machine Branch Probability Analysis", false, true)
  23. char MachineBranchProbabilityInfo::ID = 0;
  24. void MachineBranchProbabilityInfo::anchor() { }
  25. uint32_t MachineBranchProbabilityInfo::
  26. getSumForBlock(const MachineBasicBlock *MBB, uint32_t &Scale) const {
  27. // First we compute the sum with 64-bits of precision, ensuring that cannot
  28. // overflow by bounding the number of weights considered. Hopefully no one
  29. // actually needs 2^32 successors.
  30. assert(MBB->succ_size() < UINT32_MAX);
  31. uint64_t Sum = 0;
  32. Scale = 1;
  33. for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
  34. E = MBB->succ_end(); I != E; ++I) {
  35. uint32_t Weight = getEdgeWeight(MBB, I);
  36. Sum += Weight;
  37. }
  38. // If the computed sum fits in 32-bits, we're done.
  39. if (Sum <= UINT32_MAX)
  40. return Sum;
  41. // Otherwise, compute the scale necessary to cause the weights to fit, and
  42. // re-sum with that scale applied.
  43. assert((Sum / UINT32_MAX) < UINT32_MAX);
  44. Scale = (Sum / UINT32_MAX) + 1;
  45. Sum = 0;
  46. for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
  47. E = MBB->succ_end(); I != E; ++I) {
  48. uint32_t Weight = getEdgeWeight(MBB, I);
  49. Sum += Weight / Scale;
  50. }
  51. assert(Sum <= UINT32_MAX);
  52. return Sum;
  53. }
  54. uint32_t MachineBranchProbabilityInfo::
  55. getEdgeWeight(const MachineBasicBlock *Src,
  56. MachineBasicBlock::const_succ_iterator Dst) const {
  57. uint32_t Weight = Src->getSuccWeight(Dst);
  58. if (!Weight)
  59. return DEFAULT_WEIGHT;
  60. return Weight;
  61. }
  62. uint32_t MachineBranchProbabilityInfo::
  63. getEdgeWeight(const MachineBasicBlock *Src,
  64. const MachineBasicBlock *Dst) const {
  65. // This is a linear search. Try to use the const_succ_iterator version when
  66. // possible.
  67. return getEdgeWeight(Src, std::find(Src->succ_begin(), Src->succ_end(), Dst));
  68. }
  69. bool
  70. MachineBranchProbabilityInfo::isEdgeHot(const MachineBasicBlock *Src,
  71. const MachineBasicBlock *Dst) const {
  72. // Hot probability is at least 4/5 = 80%
  73. // FIXME: Compare against a static "hot" BranchProbability.
  74. return getEdgeProbability(Src, Dst) > BranchProbability(4, 5);
  75. }
  76. MachineBasicBlock *
  77. MachineBranchProbabilityInfo::getHotSucc(MachineBasicBlock *MBB) const {
  78. uint32_t MaxWeight = 0;
  79. MachineBasicBlock *MaxSucc = nullptr;
  80. for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
  81. E = MBB->succ_end(); I != E; ++I) {
  82. uint32_t Weight = getEdgeWeight(MBB, I);
  83. if (Weight > MaxWeight) {
  84. MaxWeight = Weight;
  85. MaxSucc = *I;
  86. }
  87. }
  88. if (getEdgeProbability(MBB, MaxSucc) >= BranchProbability(4, 5))
  89. return MaxSucc;
  90. return nullptr;
  91. }
  92. BranchProbability MachineBranchProbabilityInfo::getEdgeProbability(
  93. const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const {
  94. uint32_t Scale = 1;
  95. uint32_t D = getSumForBlock(Src, Scale);
  96. uint32_t N = getEdgeWeight(Src, Dst) / Scale;
  97. return BranchProbability(N, D);
  98. }
  99. raw_ostream &MachineBranchProbabilityInfo::printEdgeProbability(
  100. raw_ostream &OS, const MachineBasicBlock *Src,
  101. const MachineBasicBlock *Dst) const {
  102. const BranchProbability Prob = getEdgeProbability(Src, Dst);
  103. OS << "edge MBB#" << Src->getNumber() << " -> MBB#" << Dst->getNumber()
  104. << " probability is " << Prob
  105. << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
  106. return OS;
  107. }