BlockFrequency.cpp 2.2 KB

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