MachineBranchProbabilityInfo.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //=- MachineBranchProbabilityInfo.h - Branch Probability 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. // This pass is used to evaluate branch probabilties on machine basic blocks.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_CODEGEN_MACHINEBRANCHPROBABILITYINFO_H
  13. #define LLVM_CODEGEN_MACHINEBRANCHPROBABILITYINFO_H
  14. #include "llvm/CodeGen/MachineBasicBlock.h"
  15. #include "llvm/Pass.h"
  16. #include "llvm/Support/BranchProbability.h"
  17. #include <climits>
  18. #include <numeric>
  19. namespace llvm {
  20. class MachineBranchProbabilityInfo : public ImmutablePass {
  21. virtual void anchor();
  22. // Default weight value. Used when we don't have information about the edge.
  23. // TODO: DEFAULT_WEIGHT makes sense during static predication, when none of
  24. // the successors have a weight yet. But it doesn't make sense when providing
  25. // weight to an edge that may have siblings with non-zero weights. This can
  26. // be handled various ways, but it's probably fine for an edge with unknown
  27. // weight to just "inherit" the non-zero weight of an adjacent successor.
  28. static const uint32_t DEFAULT_WEIGHT = 16;
  29. public:
  30. static char ID;
  31. MachineBranchProbabilityInfo();
  32. void getAnalysisUsage(AnalysisUsage &AU) const override {
  33. AU.setPreservesAll();
  34. }
  35. // Return edge probability.
  36. BranchProbability getEdgeProbability(const MachineBasicBlock *Src,
  37. const MachineBasicBlock *Dst) const;
  38. // Same as above, but using a const_succ_iterator from Src. This is faster
  39. // when the iterator is already available.
  40. BranchProbability
  41. getEdgeProbability(const MachineBasicBlock *Src,
  42. MachineBasicBlock::const_succ_iterator Dst) const;
  43. // A 'Hot' edge is an edge which probability is >= 80%.
  44. bool isEdgeHot(const MachineBasicBlock *Src,
  45. const MachineBasicBlock *Dst) const;
  46. // Return a hot successor for the block BB or null if there isn't one.
  47. // NB: This routine's complexity is linear on the number of successors.
  48. MachineBasicBlock *getHotSucc(MachineBasicBlock *MBB) const;
  49. // Print value between 0 (0% probability) and 1 (100% probability),
  50. // however the value is never equal to 0, and can be 1 only iff SRC block
  51. // has only one successor.
  52. raw_ostream &printEdgeProbability(raw_ostream &OS,
  53. const MachineBasicBlock *Src,
  54. const MachineBasicBlock *Dst) const;
  55. };
  56. }
  57. #endif