MachineBranchProbabilityInfo.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //===- MachineBranchProbabilityInfo.cpp - Machine Branch Probability Info -===//
  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. // This analysis uses probability info stored in Machine Basic Blocks.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
  13. #include "llvm/CodeGen/MachineBasicBlock.h"
  14. #include "llvm/IR/Instructions.h"
  15. #include "llvm/Support/Debug.h"
  16. #include "llvm/Support/raw_ostream.h"
  17. using namespace llvm;
  18. INITIALIZE_PASS_BEGIN(MachineBranchProbabilityInfo, "machine-branch-prob",
  19. "Machine Branch Probability Analysis", false, true)
  20. INITIALIZE_PASS_END(MachineBranchProbabilityInfo, "machine-branch-prob",
  21. "Machine Branch Probability Analysis", false, true)
  22. cl::opt<unsigned>
  23. StaticLikelyProb("static-likely-prob",
  24. cl::desc("branch probability threshold in percentage"
  25. "to be considered very likely"),
  26. cl::init(80), cl::Hidden);
  27. cl::opt<unsigned> ProfileLikelyProb(
  28. "profile-likely-prob",
  29. cl::desc("branch probability threshold in percentage to be considered"
  30. " very likely when profile is available"),
  31. cl::init(51), cl::Hidden);
  32. char MachineBranchProbabilityInfo::ID = 0;
  33. void MachineBranchProbabilityInfo::anchor() {}
  34. BranchProbability MachineBranchProbabilityInfo::getEdgeProbability(
  35. const MachineBasicBlock *Src,
  36. MachineBasicBlock::const_succ_iterator Dst) const {
  37. return Src->getSuccProbability(Dst);
  38. }
  39. BranchProbability MachineBranchProbabilityInfo::getEdgeProbability(
  40. const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const {
  41. // This is a linear search. Try to use the const_succ_iterator version when
  42. // possible.
  43. return getEdgeProbability(Src, find(Src->successors(), Dst));
  44. }
  45. bool MachineBranchProbabilityInfo::isEdgeHot(
  46. const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const {
  47. BranchProbability HotProb(StaticLikelyProb, 100);
  48. return getEdgeProbability(Src, Dst) > HotProb;
  49. }
  50. MachineBasicBlock *
  51. MachineBranchProbabilityInfo::getHotSucc(MachineBasicBlock *MBB) const {
  52. auto MaxProb = BranchProbability::getZero();
  53. MachineBasicBlock *MaxSucc = nullptr;
  54. for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
  55. E = MBB->succ_end(); I != E; ++I) {
  56. auto Prob = getEdgeProbability(MBB, I);
  57. if (Prob > MaxProb) {
  58. MaxProb = Prob;
  59. MaxSucc = *I;
  60. }
  61. }
  62. BranchProbability HotProb(StaticLikelyProb, 100);
  63. if (getEdgeProbability(MBB, MaxSucc) >= HotProb)
  64. return MaxSucc;
  65. return nullptr;
  66. }
  67. raw_ostream &MachineBranchProbabilityInfo::printEdgeProbability(
  68. raw_ostream &OS, const MachineBasicBlock *Src,
  69. const MachineBasicBlock *Dst) const {
  70. const BranchProbability Prob = getEdgeProbability(Src, Dst);
  71. OS << "edge " << printMBBReference(*Src) << " -> " << printMBBReference(*Dst)
  72. << " probability is " << Prob
  73. << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
  74. return OS;
  75. }