1 //===-------- BlockFrequency.h - Block Frequency 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 // This file implements Block Frequency class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_SUPPORT_BLOCKFREQUENCY_H 15 #define LLVM_SUPPORT_BLOCKFREQUENCY_H 16 17 #include "llvm/Support/DataTypes.h" 18 19 namespace llvm { 20 21 class raw_ostream; 22 class BranchProbability; 23 24 // This class represents Block Frequency as a 64-bit value. 25 class BlockFrequency { 26 27 uint64_t Frequency; 28 static const int64_t ENTRY_FREQ = 1 << 14; 29 30 // Scale frequency by N/D, saturating on overflow. 31 void scale(uint32_t N, uint32_t D); 32 33 public: 34 BlockFrequency(uint64_t Freq = 0) : Frequency(Freq) { } 35 36 /// \brief Returns the frequency of the entry block of the function. 37 static uint64_t getEntryFrequency() { return ENTRY_FREQ; } 38 39 /// \brief Returns the maximum possible frequency, the saturation value. 40 static uint64_t getMaxFrequency() { return -1ULL; } 41 42 /// \brief Returns the frequency as a fixpoint number scaled by the entry 43 /// frequency. 44 uint64_t getFrequency() const { return Frequency; } 45 46 /// \brief Multiplies with a branch probability. The computation will never 47 /// overflow. 48 BlockFrequency &operator*=(const BranchProbability &Prob); 49 const BlockFrequency operator*(const BranchProbability &Prob) const; 50 51 /// \brief Divide by a non-zero branch probability using saturating 52 /// arithmetic. 53 BlockFrequency &operator/=(const BranchProbability &Prob); 54 BlockFrequency operator/(const BranchProbability &Prob) const; 55 56 /// \brief Adds another block frequency using saturating arithmetic. 57 BlockFrequency &operator+=(const BlockFrequency &Freq); 58 const BlockFrequency operator+(const BlockFrequency &Freq) const; 59 60 bool operator<(const BlockFrequency &RHS) const { 61 return Frequency < RHS.Frequency; 62 } 63 64 bool operator<=(const BlockFrequency &RHS) const { 65 return Frequency <= RHS.Frequency; 66 } 67 68 bool operator>(const BlockFrequency &RHS) const { 69 return Frequency > RHS.Frequency; 70 } 71 72 bool operator>=(const BlockFrequency &RHS) const { 73 return Frequency >= RHS.Frequency; 74 } 75 76 void print(raw_ostream &OS) const; 77 }; 78 79 raw_ostream &operator<<(raw_ostream &OS, const BlockFrequency &Freq); 80 81 } 82 83 #endif 84