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 /// \brief Scale the given BlockFrequency by N/D. Return the remainder from 31 /// the division by D. Upon overflow, the routine will saturate and 32 /// additionally will return the remainder set to D. 33 uint32_t scale(uint32_t N, uint32_t D); 34 35 public: 36 BlockFrequency(uint64_t Freq = 0) : Frequency(Freq) { } 37 38 /// \brief Returns the frequency of the entry block of the function. 39 static uint64_t getEntryFrequency() { return ENTRY_FREQ; } 40 41 /// \brief Returns the maximum possible frequency, the saturation value. 42 static uint64_t getMaxFrequency() { return -1ULL; } 43 44 /// \brief Returns the frequency as a fixpoint number scaled by the entry 45 /// frequency. 46 uint64_t getFrequency() const { return Frequency; } 47 48 /// \brief Multiplies with a branch probability. The computation will never 49 /// overflow. 50 BlockFrequency &operator*=(const BranchProbability &Prob); 51 const BlockFrequency operator*(const BranchProbability &Prob) const; 52 53 /// \brief Divide by a non-zero branch probability using saturating 54 /// arithmetic. 55 BlockFrequency &operator/=(const BranchProbability &Prob); 56 BlockFrequency operator/(const BranchProbability &Prob) const; 57 58 /// \brief Adds another block frequency using saturating arithmetic. 59 BlockFrequency &operator+=(const BlockFrequency &Freq); 60 const BlockFrequency operator+(const BlockFrequency &Freq) const; 61 62 /// \brief Scale the given BlockFrequency by N/D. Return the remainder from 63 /// the division by D. Upon overflow, the routine will saturate. 64 uint32_t scale(const BranchProbability &Prob); 65 66 bool operator<(const BlockFrequency &RHS) const { 67 return Frequency < RHS.Frequency; 68 } 69 70 bool operator<=(const BlockFrequency &RHS) const { 71 return Frequency <= RHS.Frequency; 72 } 73 74 bool operator>(const BlockFrequency &RHS) const { 75 return Frequency > RHS.Frequency; 76 } 77 78 bool operator>=(const BlockFrequency &RHS) const { 79 return Frequency >= RHS.Frequency; 80 } 81 82 void print(raw_ostream &OS) const; 83 }; 84 85 raw_ostream &operator<<(raw_ostream &OS, const BlockFrequency &Freq); 86 87 } 88 89 #endif 90