Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:28

0001 //===-------- BlockFrequency.h - Block Frequency Wrapper --------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file implements Block Frequency class.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_SUPPORT_BLOCKFREQUENCY_H
0014 #define LLVM_SUPPORT_BLOCKFREQUENCY_H
0015 
0016 #include <cassert>
0017 #include <cstdint>
0018 #include <optional>
0019 
0020 namespace llvm {
0021 
0022 class raw_ostream;
0023 class BranchProbability;
0024 
0025 // This class represents Block Frequency as a 64-bit value.
0026 class BlockFrequency {
0027   uint64_t Frequency;
0028 
0029 public:
0030   BlockFrequency() : Frequency(0) {}
0031   explicit BlockFrequency(uint64_t Freq) : Frequency(Freq) {}
0032 
0033   /// Returns the maximum possible frequency, the saturation value.
0034   static BlockFrequency max() { return BlockFrequency(UINT64_MAX); }
0035 
0036   /// Returns the frequency as a fixpoint number scaled by the entry
0037   /// frequency.
0038   uint64_t getFrequency() const { return Frequency; }
0039 
0040   /// Multiplies with a branch probability. The computation will never
0041   /// overflow.
0042   BlockFrequency &operator*=(BranchProbability Prob);
0043   BlockFrequency operator*(BranchProbability Prob) const;
0044 
0045   /// Divide by a non-zero branch probability using saturating
0046   /// arithmetic.
0047   BlockFrequency &operator/=(BranchProbability Prob);
0048   BlockFrequency operator/(BranchProbability Prob) const;
0049 
0050   /// Adds another block frequency using saturating arithmetic.
0051   BlockFrequency &operator+=(BlockFrequency Freq) {
0052     uint64_t Before = Freq.Frequency;
0053     Frequency += Freq.Frequency;
0054 
0055     // If overflow, set frequency to the maximum value.
0056     if (Frequency < Before)
0057       Frequency = UINT64_MAX;
0058 
0059     return *this;
0060   }
0061   BlockFrequency operator+(BlockFrequency Freq) const {
0062     BlockFrequency NewFreq(Frequency);
0063     NewFreq += Freq;
0064     return NewFreq;
0065   }
0066 
0067   /// Subtracts another block frequency using saturating arithmetic.
0068   BlockFrequency &operator-=(BlockFrequency Freq) {
0069     // If underflow, set frequency to 0.
0070     if (Frequency <= Freq.Frequency)
0071       Frequency = 0;
0072     else
0073       Frequency -= Freq.Frequency;
0074     return *this;
0075   }
0076   BlockFrequency operator-(BlockFrequency Freq) const {
0077     BlockFrequency NewFreq(Frequency);
0078     NewFreq -= Freq;
0079     return NewFreq;
0080   }
0081 
0082   /// Multiplies frequency with `Factor`. Returns `nullopt` in case of overflow.
0083   std::optional<BlockFrequency> mul(uint64_t Factor) const;
0084 
0085   /// Shift block frequency to the right by count digits saturating to 1.
0086   BlockFrequency &operator>>=(const unsigned count) {
0087     // Frequency can never be 0 by design.
0088     assert(Frequency != 0);
0089 
0090     // Shift right by count.
0091     Frequency >>= count;
0092 
0093     // Saturate to 1 if we are 0.
0094     Frequency |= Frequency == 0;
0095     return *this;
0096   }
0097 
0098   bool operator<(BlockFrequency RHS) const {
0099     return Frequency < RHS.Frequency;
0100   }
0101 
0102   bool operator<=(BlockFrequency RHS) const {
0103     return Frequency <= RHS.Frequency;
0104   }
0105 
0106   bool operator>(BlockFrequency RHS) const {
0107     return Frequency > RHS.Frequency;
0108   }
0109 
0110   bool operator>=(BlockFrequency RHS) const {
0111     return Frequency >= RHS.Frequency;
0112   }
0113 
0114   bool operator==(BlockFrequency RHS) const {
0115     return Frequency == RHS.Frequency;
0116   }
0117 
0118   bool operator!=(BlockFrequency RHS) const {
0119     return Frequency != RHS.Frequency;
0120   }
0121 };
0122 
0123 void printRelativeBlockFreq(raw_ostream &OS, BlockFrequency EntryFreq,
0124                             BlockFrequency Freq);
0125 
0126 } // namespace llvm
0127 
0128 #endif