Home | History | Annotate | Download | only in Support
      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   raw_ostream &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   uint64_t scale(uint64_t Num) const;
     60 
     61   /// \brief Scale a large integer by the inverse.
     62   ///
     63   /// Scales \c Num by the inverse of \c this.  Guarantees full precision.
     64   /// Returns the floor of the result.
     65   ///
     66   /// \return \c Num divided by \c this.
     67   uint64_t scaleByInverse(uint64_t Num) const;
     68 
     69   bool operator==(BranchProbability RHS) const {
     70     return (uint64_t)N * RHS.D == (uint64_t)D * RHS.N;
     71   }
     72   bool operator!=(BranchProbability RHS) const {
     73     return !(*this == RHS);
     74   }
     75   bool operator<(BranchProbability RHS) const {
     76     return (uint64_t)N * RHS.D < (uint64_t)D * RHS.N;
     77   }
     78   bool operator>(BranchProbability RHS) const { return RHS < *this; }
     79   bool operator<=(BranchProbability RHS) const { return !(RHS < *this); }
     80   bool operator>=(BranchProbability RHS) const { return !(*this < RHS); }
     81 };
     82 
     83 inline raw_ostream &operator<<(raw_ostream &OS, const BranchProbability &Prob) {
     84   return Prob.print(OS);
     85 }
     86 
     87 }
     88 
     89 #endif
     90