Support: Add BranchProbability::scale() and ::scaleByInverse()
[oota-llvm.git] / include / llvm / Support / BranchProbability.h
1 //===- BranchProbability.h - Branch Probability 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 // Definition of BranchProbability shared by IR and Machine Instructions.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SUPPORT_BRANCHPROBABILITY_H
15 #define LLVM_SUPPORT_BRANCHPROBABILITY_H
16
17 #include "llvm/Support/DataTypes.h"
18 #include <cassert>
19
20 namespace llvm {
21
22 class raw_ostream;
23
24 // This class represents Branch Probability as a non-negative fraction.
25 class BranchProbability {
26   // Numerator
27   uint32_t N;
28
29   // Denominator
30   uint32_t D;
31
32 public:
33   BranchProbability(uint32_t n, uint32_t d) : N(n), D(d) {
34     assert(d > 0 && "Denomiator cannot be 0!");
35     assert(n <= d && "Probability cannot be bigger than 1!");
36   }
37
38   static BranchProbability getZero() { return BranchProbability(0, 1); }
39   static BranchProbability getOne() { return BranchProbability(1, 1); }
40
41   uint32_t getNumerator() const { return N; }
42   uint32_t getDenominator() const { return D; }
43
44   // Return (1 - Probability).
45   BranchProbability getCompl() const {
46     return BranchProbability(D - N, D);
47   }
48
49   void print(raw_ostream &OS) const;
50
51   void dump() const;
52
53   /// \brief Scale a large integer.
54   ///
55   /// Scales \c Num.  Guarantees full precision.  Returns the floor of the
56   /// result.
57   ///
58   /// \return \c Num times \c this.
59   ///
60   /// \note This code should be shared with (or replaced by) the implementation
61   /// of \a BlockFrequency::scale(), which seems to be calculating something
62   /// similar.
63   uint64_t scale(uint64_t Num) const;
64
65   /// \brief Scale a large integer by the inverse.
66   ///
67   /// Scales \c Num by the inverse of \c this.  Guarantees full precision.
68   /// Returns the floor of the result.
69   ///
70   /// \return \c Num divided by \c this.
71   ///
72   /// \note This code should be shared with (or replaced by) the implementation
73   /// of \a BlockFrequency::scale(), which seems to be calculating something
74   /// similar.
75   uint64_t scaleByInverse(uint64_t Num) const;
76
77   bool operator==(BranchProbability RHS) const {
78     return (uint64_t)N * RHS.D == (uint64_t)D * RHS.N;
79   }
80   bool operator!=(BranchProbability RHS) const {
81     return !(*this == RHS);
82   }
83   bool operator<(BranchProbability RHS) const {
84     return (uint64_t)N * RHS.D < (uint64_t)D * RHS.N;
85   }
86   bool operator>(BranchProbability RHS) const { return RHS < *this; }
87   bool operator<=(BranchProbability RHS) const { return !(RHS < *this); }
88   bool operator>=(BranchProbability RHS) const { return !(*this < RHS); }
89 };
90
91 raw_ostream &operator<<(raw_ostream &OS, const BranchProbability &Prob);
92
93 }
94
95 #endif