xref: /freebsd-src/contrib/llvm-project/llvm/lib/Support/BranchProbability.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
10b57cec5SDimitry Andric //===-------------- lib/Support/BranchProbability.cpp -----------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements Branch Probability class.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "llvm/Support/BranchProbability.h"
140b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
150b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
160b57cec5SDimitry Andric #include "llvm/Support/Format.h"
170b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
180b57cec5SDimitry Andric #include <cassert>
19*fe6060f1SDimitry Andric #include <cmath>
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric using namespace llvm;
220b57cec5SDimitry Andric 
235ffd83dbSDimitry Andric constexpr uint32_t BranchProbability::D;
240b57cec5SDimitry Andric 
print(raw_ostream & OS) const250b57cec5SDimitry Andric raw_ostream &BranchProbability::print(raw_ostream &OS) const {
260b57cec5SDimitry Andric   if (isUnknown())
270b57cec5SDimitry Andric     return OS << "?%";
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric   // Get a percentage rounded to two decimal digits. This avoids
300b57cec5SDimitry Andric   // implementation-defined rounding inside printf.
310b57cec5SDimitry Andric   double Percent = rint(((double)N / D) * 100.0 * 100.0) / 100.0;
320b57cec5SDimitry Andric   return OS << format("0x%08" PRIx32 " / 0x%08" PRIx32 " = %.2f%%", N, D,
330b57cec5SDimitry Andric                       Percent);
340b57cec5SDimitry Andric }
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const370b57cec5SDimitry Andric LLVM_DUMP_METHOD void BranchProbability::dump() const { print(dbgs()) << '\n'; }
380b57cec5SDimitry Andric #endif
390b57cec5SDimitry Andric 
BranchProbability(uint32_t Numerator,uint32_t Denominator)400b57cec5SDimitry Andric BranchProbability::BranchProbability(uint32_t Numerator, uint32_t Denominator) {
410b57cec5SDimitry Andric   assert(Denominator > 0 && "Denominator cannot be 0!");
420b57cec5SDimitry Andric   assert(Numerator <= Denominator && "Probability cannot be bigger than 1!");
430b57cec5SDimitry Andric   if (Denominator == D)
440b57cec5SDimitry Andric     N = Numerator;
450b57cec5SDimitry Andric   else {
460b57cec5SDimitry Andric     uint64_t Prob64 =
470b57cec5SDimitry Andric         (Numerator * static_cast<uint64_t>(D) + Denominator / 2) / Denominator;
480b57cec5SDimitry Andric     N = static_cast<uint32_t>(Prob64);
490b57cec5SDimitry Andric   }
500b57cec5SDimitry Andric }
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric BranchProbability
getBranchProbability(uint64_t Numerator,uint64_t Denominator)530b57cec5SDimitry Andric BranchProbability::getBranchProbability(uint64_t Numerator,
540b57cec5SDimitry Andric                                         uint64_t Denominator) {
550b57cec5SDimitry Andric   assert(Numerator <= Denominator && "Probability cannot be bigger than 1!");
560b57cec5SDimitry Andric   // Scale down Denominator to fit in a 32-bit integer.
570b57cec5SDimitry Andric   int Scale = 0;
580b57cec5SDimitry Andric   while (Denominator > UINT32_MAX) {
590b57cec5SDimitry Andric     Denominator >>= 1;
600b57cec5SDimitry Andric     Scale++;
610b57cec5SDimitry Andric   }
620b57cec5SDimitry Andric   return BranchProbability(Numerator >> Scale, Denominator);
630b57cec5SDimitry Andric }
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric // If ConstD is not zero, then replace D by ConstD so that division and modulo
660b57cec5SDimitry Andric // operations by D can be optimized, in case this function is not inlined by the
670b57cec5SDimitry Andric // compiler.
680b57cec5SDimitry Andric template <uint32_t ConstD>
scale(uint64_t Num,uint32_t N,uint32_t D)690b57cec5SDimitry Andric static uint64_t scale(uint64_t Num, uint32_t N, uint32_t D) {
700b57cec5SDimitry Andric   if (ConstD > 0)
710b57cec5SDimitry Andric     D = ConstD;
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric   assert(D && "divide by 0");
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric   // Fast path for multiplying by 1.0.
760b57cec5SDimitry Andric   if (!Num || D == N)
770b57cec5SDimitry Andric     return Num;
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric   // Split Num into upper and lower parts to multiply, then recombine.
800b57cec5SDimitry Andric   uint64_t ProductHigh = (Num >> 32) * N;
810b57cec5SDimitry Andric   uint64_t ProductLow = (Num & UINT32_MAX) * N;
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric   // Split into 32-bit digits.
840b57cec5SDimitry Andric   uint32_t Upper32 = ProductHigh >> 32;
850b57cec5SDimitry Andric   uint32_t Lower32 = ProductLow & UINT32_MAX;
860b57cec5SDimitry Andric   uint32_t Mid32Partial = ProductHigh & UINT32_MAX;
870b57cec5SDimitry Andric   uint32_t Mid32 = Mid32Partial + (ProductLow >> 32);
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric   // Carry.
900b57cec5SDimitry Andric   Upper32 += Mid32 < Mid32Partial;
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric   uint64_t Rem = (uint64_t(Upper32) << 32) | Mid32;
930b57cec5SDimitry Andric   uint64_t UpperQ = Rem / D;
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric   // Check for overflow.
960b57cec5SDimitry Andric   if (UpperQ > UINT32_MAX)
970b57cec5SDimitry Andric     return UINT64_MAX;
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric   Rem = ((Rem % D) << 32) | Lower32;
1000b57cec5SDimitry Andric   uint64_t LowerQ = Rem / D;
1010b57cec5SDimitry Andric   uint64_t Q = (UpperQ << 32) + LowerQ;
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric   // Check for overflow.
1040b57cec5SDimitry Andric   return Q < LowerQ ? UINT64_MAX : Q;
1050b57cec5SDimitry Andric }
1060b57cec5SDimitry Andric 
scale(uint64_t Num) const1070b57cec5SDimitry Andric uint64_t BranchProbability::scale(uint64_t Num) const {
1080b57cec5SDimitry Andric   return ::scale<D>(Num, N, D);
1090b57cec5SDimitry Andric }
1100b57cec5SDimitry Andric 
scaleByInverse(uint64_t Num) const1110b57cec5SDimitry Andric uint64_t BranchProbability::scaleByInverse(uint64_t Num) const {
1120b57cec5SDimitry Andric   return ::scale<0>(Num, D, N);
1130b57cec5SDimitry Andric }
114