LCOV - code coverage report
Current view: top level - lib/Support - BranchProbability.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 37 38 97.4 %
Date: 2018-07-13 00:08:38 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-------------- lib/Support/BranchProbability.cpp -----------*- 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 Branch Probability class.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/Support/BranchProbability.h"
      15             : #include "llvm/Config/llvm-config.h"
      16             : #include "llvm/Support/Debug.h"
      17             : #include "llvm/Support/Format.h"
      18             : #include "llvm/Support/raw_ostream.h"
      19             : #include <cassert>
      20             : 
      21             : using namespace llvm;
      22             : 
      23             : const uint32_t BranchProbability::D;
      24             : 
      25         910 : raw_ostream &BranchProbability::print(raw_ostream &OS) const {
      26         910 :   if (isUnknown())
      27           0 :     return OS << "?%";
      28             : 
      29             :   // Get a percentage rounded to two decimal digits. This avoids
      30             :   // implementation-defined rounding inside printf.
      31         910 :   double Percent = rint(((double)N / D) * 100.0 * 100.0) / 100.0;
      32         910 :   return OS << format("0x%08" PRIx32 " / 0x%08" PRIx32 " = %.2f%%", N, D,
      33         910 :                       Percent);
      34             : }
      35             : 
      36             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
      37             : LLVM_DUMP_METHOD void BranchProbability::dump() const { print(dbgs()) << '\n'; }
      38             : #endif
      39             : 
      40     3697350 : BranchProbability::BranchProbability(uint32_t Numerator, uint32_t Denominator) {
      41             :   assert(Denominator > 0 && "Denominator cannot be 0!");
      42             :   assert(Numerator <= Denominator && "Probability cannot be bigger than 1!");
      43     3697350 :   if (Denominator == D)
      44      804082 :     N = Numerator;
      45             :   else {
      46     2893268 :     uint64_t Prob64 =
      47     2893268 :         (Numerator * static_cast<uint64_t>(D) + Denominator / 2) / Denominator;
      48     2893268 :     N = static_cast<uint32_t>(Prob64);
      49             :   }
      50     3697350 : }
      51             : 
      52             : BranchProbability
      53        1530 : BranchProbability::getBranchProbability(uint64_t Numerator,
      54             :                                         uint64_t Denominator) {
      55             :   assert(Numerator <= Denominator && "Probability cannot be bigger than 1!");
      56             :   // Scale down Denominator to fit in a 32-bit integer.
      57             :   int Scale = 0;
      58        4882 :   while (Denominator > UINT32_MAX) {
      59        1676 :     Denominator >>= 1;
      60        1676 :     Scale++;
      61             :   }
      62        1530 :   return BranchProbability(Numerator >> Scale, Denominator);
      63             : }
      64             : 
      65             : // If ConstD is not zero, then replace D by ConstD so that division and modulo
      66             : // operations by D can be optimized, in case this function is not inlined by the
      67             : // compiler.
      68             : template <uint32_t ConstD>
      69     2656119 : static uint64_t scale(uint64_t Num, uint32_t N, uint32_t D) {
      70             :   if (ConstD > 0)
      71             :     D = ConstD;
      72             : 
      73             :   assert(D && "divide by 0");
      74             : 
      75             :   // Fast path for multiplying by 1.0.
      76     2656119 :   if (!Num || D == N)
      77             :     return Num;
      78             : 
      79             :   // Split Num into upper and lower parts to multiply, then recombine.
      80     1118250 :   uint64_t ProductHigh = (Num >> 32) * N;
      81     1118250 :   uint64_t ProductLow = (Num & UINT32_MAX) * N;
      82             : 
      83             :   // Split into 32-bit digits.
      84     1118250 :   uint32_t Upper32 = ProductHigh >> 32;
      85             :   uint32_t Lower32 = ProductLow & UINT32_MAX;
      86     1118250 :   uint32_t Mid32Partial = ProductHigh & UINT32_MAX;
      87     1118250 :   uint32_t Mid32 = Mid32Partial + (ProductLow >> 32);
      88             : 
      89             :   // Carry.
      90     1118250 :   Upper32 += Mid32 < Mid32Partial;
      91             : 
      92             :   // Check for overflow.
      93     1118250 :   if (Upper32 >= D)
      94             :     return UINT64_MAX;
      95             : 
      96     1118242 :   uint64_t Rem = (uint64_t(Upper32) << 32) | Mid32;
      97     1118242 :   uint64_t UpperQ = Rem / D;
      98             : 
      99             :   // Check for overflow.
     100     1118242 :   if (UpperQ > UINT32_MAX)
     101             :     return UINT64_MAX;
     102             : 
     103     1118242 :   Rem = ((Rem % D) << 32) | Lower32;
     104     1118242 :   uint64_t LowerQ = Rem / D;
     105     1118242 :   uint64_t Q = (UpperQ << 32) + LowerQ;
     106             : 
     107             :   // Check for overflow.
     108     1118242 :   return Q < LowerQ ? UINT64_MAX : Q;
     109             : }
     110             : 
     111     2654232 : uint64_t BranchProbability::scale(uint64_t Num) const {
     112     2654232 :   return ::scale<D>(Num, N, D);
     113             : }
     114             : 
     115        1887 : uint64_t BranchProbability::scaleByInverse(uint64_t Num) const {
     116        1887 :   return ::scale<0>(Num, D, N);
     117             : }

Generated by: LCOV version 1.13