MachineBranchProbabilityInfo.cpp 3.0 KB

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