BlockFrequency.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //====--------------- lib/Support/BlockFrequency.cpp -----------*- 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 file implements Block Frequency class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Support/BranchProbability.h"
  14. #include "llvm/Support/BlockFrequency.h"
  15. #include "llvm/Support/raw_ostream.h"
  16. #include <cassert>
  17. using namespace llvm;
  18. BlockFrequency &BlockFrequency::operator*=(const BranchProbability &Prob) {
  19. Frequency = Prob.scale(Frequency);
  20. return *this;
  21. }
  22. const BlockFrequency
  23. BlockFrequency::operator*(const BranchProbability &Prob) const {
  24. BlockFrequency Freq(Frequency);
  25. Freq *= Prob;
  26. return Freq;
  27. }
  28. BlockFrequency &BlockFrequency::operator/=(const BranchProbability &Prob) {
  29. Frequency = Prob.scaleByInverse(Frequency);
  30. return *this;
  31. }
  32. BlockFrequency BlockFrequency::operator/(const BranchProbability &Prob) const {
  33. BlockFrequency Freq(Frequency);
  34. Freq /= Prob;
  35. return Freq;
  36. }
  37. BlockFrequency &BlockFrequency::operator+=(const BlockFrequency &Freq) {
  38. uint64_t Before = Freq.Frequency;
  39. Frequency += Freq.Frequency;
  40. // If overflow, set frequency to the maximum value.
  41. if (Frequency < Before)
  42. Frequency = UINT64_MAX;
  43. return *this;
  44. }
  45. const BlockFrequency
  46. BlockFrequency::operator+(const BlockFrequency &Prob) const {
  47. BlockFrequency Freq(Frequency);
  48. Freq += Prob;
  49. return Freq;
  50. }
  51. BlockFrequency &BlockFrequency::operator>>=(const unsigned count) {
  52. // Frequency can never be 0 by design.
  53. assert(Frequency != 0);
  54. // Shift right by count.
  55. Frequency >>= count;
  56. // Saturate to 1 if we are 0.
  57. Frequency |= Frequency == 0;
  58. return *this;
  59. }