BranchProbability.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //===- BranchProbability.h - Branch Probability Wrapper ---------*- 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. // Definition of BranchProbability shared by IR and Machine Instructions.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_SUPPORT_BRANCHPROBABILITY_H
  14. #define LLVM_SUPPORT_BRANCHPROBABILITY_H
  15. #include "llvm/Support/DataTypes.h"
  16. #include <cassert>
  17. namespace llvm {
  18. class raw_ostream;
  19. // This class represents Branch Probability as a non-negative fraction.
  20. class BranchProbability {
  21. // Numerator
  22. uint32_t N;
  23. // Denominator
  24. uint32_t D;
  25. public:
  26. BranchProbability(uint32_t n, uint32_t d) : N(n), D(d) {
  27. assert(d > 0 && "Denominator cannot be 0!");
  28. assert(n <= d && "Probability cannot be bigger than 1!");
  29. }
  30. static BranchProbability getZero() { return BranchProbability(0, 1); }
  31. static BranchProbability getOne() { return BranchProbability(1, 1); }
  32. uint32_t getNumerator() const { return N; }
  33. uint32_t getDenominator() const { return D; }
  34. // Return (1 - Probability).
  35. BranchProbability getCompl() const {
  36. return BranchProbability(D - N, D);
  37. }
  38. raw_ostream &print(raw_ostream &OS) const;
  39. void dump() const;
  40. /// \brief Scale a large integer.
  41. ///
  42. /// Scales \c Num. Guarantees full precision. Returns the floor of the
  43. /// result.
  44. ///
  45. /// \return \c Num times \c this.
  46. uint64_t scale(uint64_t Num) const;
  47. /// \brief Scale a large integer by the inverse.
  48. ///
  49. /// Scales \c Num by the inverse of \c this. Guarantees full precision.
  50. /// Returns the floor of the result.
  51. ///
  52. /// \return \c Num divided by \c this.
  53. uint64_t scaleByInverse(uint64_t Num) const;
  54. bool operator==(BranchProbability RHS) const {
  55. return (uint64_t)N * RHS.D == (uint64_t)D * RHS.N;
  56. }
  57. bool operator!=(BranchProbability RHS) const {
  58. return !(*this == RHS);
  59. }
  60. bool operator<(BranchProbability RHS) const {
  61. return (uint64_t)N * RHS.D < (uint64_t)D * RHS.N;
  62. }
  63. bool operator>(BranchProbability RHS) const { return RHS < *this; }
  64. bool operator<=(BranchProbability RHS) const { return !(RHS < *this); }
  65. bool operator>=(BranchProbability RHS) const { return !(*this < RHS); }
  66. };
  67. inline raw_ostream &operator<<(raw_ostream &OS, const BranchProbability &Prob) {
  68. return Prob.print(OS);
  69. }
  70. }
  71. #endif