LCOV - code coverage report
Current view: top level - include/llvm/Support - BranchProbability.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 51 51 100.0 %
Date: 2017-09-14 15:23:50 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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 <algorithm>
      19             : #include <cassert>
      20             : #include <climits>
      21             : #include <numeric>
      22             : 
      23             : namespace llvm {
      24             : 
      25             : class raw_ostream;
      26             : 
      27             : // This class represents Branch Probability as a non-negative fraction that is
      28             : // no greater than 1. It uses a fixed-point-like implementation, in which the
      29             : // denominator is always a constant value (here we use 1<<31 for maximum
      30             : // precision).
      31             : class BranchProbability {
      32             :   // Numerator
      33             :   uint32_t N;
      34             : 
      35             :   // Denominator, which is a constant value.
      36             :   static const uint32_t D = 1u << 31;
      37             :   static const uint32_t UnknownN = UINT32_MAX;
      38             : 
      39             :   // Construct a BranchProbability with only numerator assuming the denominator
      40             :   // is 1<<31. For internal use only.
      41             :   explicit BranchProbability(uint32_t n) : N(n) {}
      42             : 
      43             : public:
      44      842052 :   BranchProbability() : N(UnknownN) {}
      45             :   BranchProbability(uint32_t Numerator, uint32_t Denominator);
      46             : 
      47             :   bool isZero() const { return N == 0; }
      48     2102089 :   bool isUnknown() const { return N == UnknownN; }
      49             : 
      50     1876679 :   static BranchProbability getZero() { return BranchProbability(0); }
      51      218796 :   static BranchProbability getOne() { return BranchProbability(D); }
      52      156501 :   static BranchProbability getUnknown() { return BranchProbability(UnknownN); }
      53             :   // Create a BranchProbability object with the given numerator and 1<<31
      54             :   // as denominator.
      55       74357 :   static BranchProbability getRaw(uint32_t N) { return BranchProbability(N); }
      56             :   // Create a BranchProbability object from 64-bit integers.
      57             :   static BranchProbability getBranchProbability(uint64_t Numerator,
      58             :                                                 uint64_t Denominator);
      59             : 
      60             :   // Normalize given probabilties so that the sum of them becomes approximate
      61             :   // one.
      62             :   template <class ProbabilityIter>
      63             :   static void normalizeProbabilities(ProbabilityIter Begin,
      64             :                                      ProbabilityIter End);
      65             : 
      66        1981 :   uint32_t getNumerator() const { return N; }
      67             :   static uint32_t getDenominator() { return D; }
      68             : 
      69             :   // Return (1 - Probability).
      70      780918 :   BranchProbability getCompl() const { return BranchProbability(D - N); }
      71             : 
      72             :   raw_ostream &print(raw_ostream &OS) const;
      73             : 
      74             :   void dump() const;
      75             : 
      76             :   /// \brief Scale a large integer.
      77             :   ///
      78             :   /// Scales \c Num.  Guarantees full precision.  Returns the floor of the
      79             :   /// result.
      80             :   ///
      81             :   /// \return \c Num times \c this.
      82             :   uint64_t scale(uint64_t Num) const;
      83             : 
      84             :   /// \brief Scale a large integer by the inverse.
      85             :   ///
      86             :   /// Scales \c Num by the inverse of \c this.  Guarantees full precision.
      87             :   /// Returns the floor of the result.
      88             :   ///
      89             :   /// \return \c Num divided by \c this.
      90             :   uint64_t scaleByInverse(uint64_t Num) const;
      91             : 
      92             :   BranchProbability &operator+=(BranchProbability RHS) {
      93             :     assert(N != UnknownN && RHS.N != UnknownN &&
      94             :            "Unknown probability cannot participate in arithmetics.");
      95             :     // Saturate the result in case of overflow.
      96      644942 :     N = (uint64_t(N) + RHS.N > D) ? D : N + RHS.N;
      97             :     return *this;
      98             :   }
      99             : 
     100             :   BranchProbability &operator-=(BranchProbability RHS) {
     101             :     assert(N != UnknownN && RHS.N != UnknownN &&
     102             :            "Unknown probability cannot participate in arithmetics.");
     103             :     // Saturate the result in case of underflow.
     104       85512 :     N = N < RHS.N ? 0 : N - RHS.N;
     105             :     return *this;
     106             :   }
     107             : 
     108             :   BranchProbability &operator*=(BranchProbability RHS) {
     109             :     assert(N != UnknownN && RHS.N != UnknownN &&
     110             :            "Unknown probability cannot participate in arithmetics.");
     111         189 :     N = (static_cast<uint64_t>(N) * RHS.N + D / 2) / D;
     112             :     return *this;
     113             :   }
     114             : 
     115             :   BranchProbability &operator*=(uint32_t RHS) {
     116             :     assert(N != UnknownN &&
     117             :            "Unknown probability cannot participate in arithmetics.");
     118        5520 :     N = (uint64_t(N) * RHS > D) ? D : N * RHS;
     119             :     return *this;
     120             :   }
     121             : 
     122             :   BranchProbability &operator/=(uint32_t RHS) {
     123             :     assert(N != UnknownN &&
     124             :            "Unknown probability cannot participate in arithmetics.");
     125             :     assert(RHS > 0 && "The divider cannot be zero.");
     126      603319 :     N /= RHS;
     127             :     return *this;
     128             :   }
     129             : 
     130             :   BranchProbability operator+(BranchProbability RHS) const {
     131         499 :     BranchProbability Prob(*this);
     132         460 :     return Prob += RHS;
     133             :   }
     134             : 
     135             :   BranchProbability operator-(BranchProbability RHS) const {
     136       18490 :     BranchProbability Prob(*this);
     137       18491 :     return Prob -= RHS;
     138             :   }
     139             : 
     140             :   BranchProbability operator*(BranchProbability RHS) const {
     141         132 :     BranchProbability Prob(*this);
     142         133 :     return Prob *= RHS;
     143             :   }
     144             : 
     145             :   BranchProbability operator*(uint32_t RHS) const {
     146        5520 :     BranchProbability Prob(*this);
     147        5520 :     return Prob *= RHS;
     148             :   }
     149             : 
     150             :   BranchProbability operator/(uint32_t RHS) const {
     151      603318 :     BranchProbability Prob(*this);
     152      603319 :     return Prob /= RHS;
     153             :   }
     154             : 
     155         763 :   bool operator==(BranchProbability RHS) const { return N == RHS.N; }
     156         125 :   bool operator!=(BranchProbability RHS) const { return !(*this == RHS); }
     157             : 
     158             :   bool operator<(BranchProbability RHS) const {
     159             :     assert(N != UnknownN && RHS.N != UnknownN &&
     160             :            "Unknown probability cannot participate in comparisons.");
     161       23520 :     return N < RHS.N;
     162             :   }
     163             : 
     164             :   bool operator>(BranchProbability RHS) const {
     165             :     assert(N != UnknownN && RHS.N != UnknownN &&
     166             :            "Unknown probability cannot participate in comparisons.");
     167       16441 :     return RHS < *this;
     168             :   }
     169             : 
     170             :   bool operator<=(BranchProbability RHS) const {
     171             :     assert(N != UnknownN && RHS.N != UnknownN &&
     172             :            "Unknown probability cannot participate in comparisons.");
     173        2881 :     return !(RHS < *this);
     174             :   }
     175             : 
     176             :   bool operator>=(BranchProbability RHS) const {
     177             :     assert(N != UnknownN && RHS.N != UnknownN &&
     178             :            "Unknown probability cannot participate in comparisons.");
     179       16334 :     return !(*this < RHS);
     180             :   }
     181             : };
     182             : 
     183             : inline raw_ostream &operator<<(raw_ostream &OS, BranchProbability Prob) {
     184       17851 :   return Prob.print(OS);
     185             : }
     186             : 
     187             : template <class ProbabilityIter>
     188       76343 : void BranchProbability::normalizeProbabilities(ProbabilityIter Begin,
     189             :                                                ProbabilityIter End) {
     190       76343 :   if (Begin == End)
     191        8096 :     return;
     192             : 
     193       69860 :   unsigned UnknownProbCount = 0;
     194       70859 :   uint64_t Sum = std::accumulate(Begin, End, uint64_t(0),
     195             :                                  [&](uint64_t S, const BranchProbability &BP) {
     196      139654 :                                    if (!BP.isUnknown())
     197      137644 :                                      return S + BP.N;
     198        2010 :                                    UnknownProbCount++;
     199             :                                    return S;
     200             :                                  });
     201             : 
     202       69860 :   if (UnknownProbCount > 0) {
     203        1431 :     BranchProbability ProbForUnknown = BranchProbability::getZero();
     204             :     // If the sum of all known probabilities is less than one, evenly distribute
     205             :     // the complement of sum to unknown probabilities. Otherwise, set unknown
     206             :     // probabilities to zeros and continue to normalize known probabilities.
     207        1431 :     if (Sum < BranchProbability::getDenominator())
     208        2856 :       ProbForUnknown = BranchProbability::getRaw(
     209        1428 :           (BranchProbability::getDenominator() - Sum) / UnknownProbCount);
     210             : 
     211        2862 :     std::replace_if(Begin, End,
     212        2023 :                     [](const BranchProbability &BP) { return BP.isUnknown(); },
     213             :                     ProbForUnknown);
     214             : 
     215        1431 :     if (Sum <= BranchProbability::getDenominator())
     216             :       return;
     217             :   }
     218             : 
     219       68430 :   if (Sum == 0) {
     220         183 :     BranchProbability BP(1, std::distance(Begin, End));
     221         183 :     std::fill(Begin, End, BP);
     222             :     return;
     223             :   }
     224             : 
     225      206938 :   for (auto I = Begin; I != End; ++I)
     226      137360 :     I->N = (I->N * uint64_t(D) + Sum / 2) / Sum;
     227             : }
     228             : 
     229             : }
     230             : 
     231             : #endif

Generated by: LCOV version 1.13