• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
BranchProbability(uint32_t n,uint32_t d)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 
getZero()38   static BranchProbability getZero() { return BranchProbability(0, 1); }
getOne()39   static BranchProbability getOne() { return BranchProbability(1, 1); }
40 
getNumerator()41   uint32_t getNumerator() const { return N; }
getDenominator()42   uint32_t getDenominator() const { return D; }
43 
44   // Return (1 - Probability).
getCompl()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   bool operator==(BranchProbability RHS) const {
54     return (uint64_t)N * RHS.D == (uint64_t)D * RHS.N;
55   }
56   bool operator!=(BranchProbability RHS) const {
57     return !(*this == RHS);
58   }
59   bool operator<(BranchProbability RHS) const {
60     return (uint64_t)N * RHS.D < (uint64_t)D * RHS.N;
61   }
62   bool operator>(BranchProbability RHS) const {
63     return RHS < *this;
64   }
65   bool operator<=(BranchProbability RHS) const {
66     return (uint64_t)N * RHS.D <= (uint64_t)D * RHS.N;
67   }
68   bool operator>=(BranchProbability RHS) const {
69     return RHS <= *this;
70   }
71 };
72 
73 raw_ostream &operator<<(raw_ostream &OS, const BranchProbability &Prob);
74 
75 }
76 
77 #endif
78