BlockFrequency.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //===-------- BlockFrequency.h - Block Frequency Wrapper --------*- 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 file implements Block Frequency class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_SUPPORT_BLOCKFREQUENCY_H
  13. #define LLVM_SUPPORT_BLOCKFREQUENCY_H
  14. #include "llvm/Support/BranchProbability.h"
  15. #include "llvm/Support/DataTypes.h"
  16. namespace llvm {
  17. class raw_ostream;
  18. // This class represents Block Frequency as a 64-bit value.
  19. class BlockFrequency {
  20. uint64_t Frequency;
  21. public:
  22. BlockFrequency(uint64_t Freq = 0) : Frequency(Freq) { }
  23. /// Returns the maximum possible frequency, the saturation value.
  24. static uint64_t getMaxFrequency() { return -1ULL; }
  25. /// Returns the frequency as a fixpoint number scaled by the entry
  26. /// frequency.
  27. uint64_t getFrequency() const { return Frequency; }
  28. /// Multiplies with a branch probability. The computation will never
  29. /// overflow.
  30. BlockFrequency &operator*=(BranchProbability Prob);
  31. BlockFrequency operator*(BranchProbability Prob) const;
  32. /// Divide by a non-zero branch probability using saturating
  33. /// arithmetic.
  34. BlockFrequency &operator/=(BranchProbability Prob);
  35. BlockFrequency operator/(BranchProbability Prob) const;
  36. /// Adds another block frequency using saturating arithmetic.
  37. BlockFrequency &operator+=(BlockFrequency Freq);
  38. BlockFrequency operator+(BlockFrequency Freq) const;
  39. /// Subtracts another block frequency using saturating arithmetic.
  40. BlockFrequency &operator-=(BlockFrequency Freq);
  41. BlockFrequency operator-(BlockFrequency Freq) const;
  42. /// Shift block frequency to the right by count digits saturating to 1.
  43. BlockFrequency &operator>>=(const unsigned count);
  44. bool operator<(BlockFrequency RHS) const {
  45. return Frequency < RHS.Frequency;
  46. }
  47. bool operator<=(BlockFrequency RHS) const {
  48. return Frequency <= RHS.Frequency;
  49. }
  50. bool operator>(BlockFrequency RHS) const {
  51. return Frequency > RHS.Frequency;
  52. }
  53. bool operator>=(BlockFrequency RHS) const {
  54. return Frequency >= RHS.Frequency;
  55. }
  56. bool operator==(BlockFrequency RHS) const {
  57. return Frequency == RHS.Frequency;
  58. }
  59. };
  60. }
  61. #endif