BlockFrequency: Saturate at 1 instead of 0 when multiplying a frequency with a branch...
[oota-llvm.git] / include / llvm / Support / BlockFrequency.h
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 = 1024;
29
30 public:
31   BlockFrequency(uint64_t Freq = 0) : Frequency(Freq) { }
32
33   /// \brief Returns the frequency of the entry block of the function.
34   static uint64_t getEntryFrequency() { return ENTRY_FREQ; }
35
36   /// \brief Returns the frequency as a fixpoint number scaled by the entry
37   /// frequency.
38   uint64_t getFrequency() const { return Frequency; }
39
40   /// \brief Multiplies with a branch probability. The computation will never
41   /// overflow. If the result is equal to zero but the input wasn't this method
42   /// will return a frequency of one.
43   BlockFrequency &operator*=(const BranchProbability &Prob);
44   const BlockFrequency operator*(const BranchProbability &Prob) const;
45
46   /// \brief Adds another block frequency using saturating arithmetic.
47   BlockFrequency &operator+=(const BlockFrequency &Freq);
48   const BlockFrequency operator+(const BlockFrequency &Freq) const;
49
50   bool operator<(const BlockFrequency &RHS) const {
51     return Frequency < RHS.Frequency;
52   }
53
54   bool operator<=(const BlockFrequency &RHS) const {
55     return Frequency <= RHS.Frequency;
56   }
57
58   bool operator>(const BlockFrequency &RHS) const {
59     return Frequency > RHS.Frequency;
60   }
61
62   bool operator>=(const BlockFrequency &RHS) const {
63     return Frequency >= RHS.Frequency;
64   }
65
66   void print(raw_ostream &OS) const;
67 };
68
69 raw_ostream &operator<<(raw_ostream &OS, const BlockFrequency &Freq);
70
71 }
72
73 #endif