MachineBranchProbabilityInfo.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/InitializePasses.h"
  15. #include "llvm/Support/CommandLine.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. namespace llvm {
  23. cl::opt<unsigned>
  24. StaticLikelyProb("static-likely-prob",
  25. cl::desc("branch probability threshold in percentage"
  26. "to be considered very likely"),
  27. cl::init(80), cl::Hidden);
  28. cl::opt<unsigned> ProfileLikelyProb(
  29. "profile-likely-prob",
  30. cl::desc("branch probability threshold in percentage to be considered"
  31. " very likely when profile is available"),
  32. cl::init(51), cl::Hidden);
  33. } // namespace llvm
  34. char MachineBranchProbabilityInfo::ID = 0;
  35. MachineBranchProbabilityInfo::MachineBranchProbabilityInfo()
  36. : ImmutablePass(ID) {
  37. PassRegistry &Registry = *PassRegistry::getPassRegistry();
  38. initializeMachineBranchProbabilityInfoPass(Registry);
  39. }
  40. void MachineBranchProbabilityInfo::anchor() {}
  41. BranchProbability MachineBranchProbabilityInfo::getEdgeProbability(
  42. const MachineBasicBlock *Src,
  43. MachineBasicBlock::const_succ_iterator Dst) const {
  44. return Src->getSuccProbability(Dst);
  45. }
  46. BranchProbability MachineBranchProbabilityInfo::getEdgeProbability(
  47. const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const {
  48. // This is a linear search. Try to use the const_succ_iterator version when
  49. // possible.
  50. return getEdgeProbability(Src, find(Src->successors(), Dst));
  51. }
  52. bool MachineBranchProbabilityInfo::isEdgeHot(
  53. const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const {
  54. BranchProbability HotProb(StaticLikelyProb, 100);
  55. return getEdgeProbability(Src, Dst) > HotProb;
  56. }
  57. raw_ostream &MachineBranchProbabilityInfo::printEdgeProbability(
  58. raw_ostream &OS, const MachineBasicBlock *Src,
  59. const MachineBasicBlock *Dst) const {
  60. const BranchProbability Prob = getEdgeProbability(Src, Dst);
  61. OS << "edge " << printMBBReference(*Src) << " -> " << printMBBReference(*Dst)
  62. << " probability is " << Prob
  63. << (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
  64. return OS;
  65. }