MachineBranchProbabilityInfo.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //=- MachineBranchProbabilityInfo.h - Branch Probability Analysis -*- C++ -*-=//
  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 pass is used to evaluate branch probabilties on machine basic blocks.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CODEGEN_MACHINEBRANCHPROBABILITYINFO_H
  14. #define LLVM_CODEGEN_MACHINEBRANCHPROBABILITYINFO_H
  15. #include "llvm/CodeGen/MachineBasicBlock.h"
  16. #include "llvm/Pass.h"
  17. #include "llvm/Support/BranchProbability.h"
  18. #include <climits>
  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() : ImmutablePass(ID) {
  32. PassRegistry &Registry = *PassRegistry::getPassRegistry();
  33. initializeMachineBranchProbabilityInfoPass(Registry);
  34. }
  35. void getAnalysisUsage(AnalysisUsage &AU) const override {
  36. AU.setPreservesAll();
  37. }
  38. // Return edge weight. If we don't have any informations about it - return
  39. // DEFAULT_WEIGHT.
  40. uint32_t getEdgeWeight(const MachineBasicBlock *Src,
  41. const MachineBasicBlock *Dst) const;
  42. // Same thing, but using a const_succ_iterator from Src. This is faster when
  43. // the iterator is already available.
  44. uint32_t getEdgeWeight(const MachineBasicBlock *Src,
  45. MachineBasicBlock::const_succ_iterator Dst) const;
  46. // Get sum of the block successors' weights, potentially scaling them to fit
  47. // within 32-bits. If scaling is required, sets Scale based on the necessary
  48. // adjustment. Any edge weights used with the sum should be divided by Scale.
  49. uint32_t getSumForBlock(const MachineBasicBlock *MBB, uint32_t &Scale) const;
  50. // A 'Hot' edge is an edge which probability is >= 80%.
  51. bool isEdgeHot(const MachineBasicBlock *Src,
  52. const MachineBasicBlock *Dst) const;
  53. // Return a hot successor for the block BB or null if there isn't one.
  54. // NB: This routine's complexity is linear on the number of successors.
  55. MachineBasicBlock *getHotSucc(MachineBasicBlock *MBB) const;
  56. // Return a probability as a fraction between 0 (0% probability) and
  57. // 1 (100% probability), however the value is never equal to 0, and can be 1
  58. // only iff SRC block has only one successor.
  59. // NB: This routine's complexity is linear on the number of successors of
  60. // Src. Querying sequentially for each successor's probability is a quadratic
  61. // query pattern.
  62. BranchProbability getEdgeProbability(const MachineBasicBlock *Src,
  63. const MachineBasicBlock *Dst) const;
  64. // Print value between 0 (0% probability) and 1 (100% probability),
  65. // however the value is never equal to 0, and can be 1 only iff SRC block
  66. // has only one successor.
  67. raw_ostream &printEdgeProbability(raw_ostream &OS,
  68. const MachineBasicBlock *Src,
  69. const MachineBasicBlock *Dst) const;
  70. };
  71. }
  72. #endif