2011-06-11 01:05:22 +00:00
|
|
|
//===-------------- lib/Support/BranchProbability.cpp -----------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-06-11 01:05:22 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements Branch Probability class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/BranchProbability.h"
|
2018-04-30 14:59:11 +00:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2011-06-11 01:05:22 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2011-10-23 11:32:54 +00:00
|
|
|
#include "llvm/Support/Format.h"
|
2011-06-11 01:05:22 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2015-09-25 23:09:59 +00:00
|
|
|
#include <cassert>
|
2011-06-11 01:05:22 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2020-04-22 10:09:37 +00:00
|
|
|
constexpr uint32_t BranchProbability::D;
|
2015-09-26 10:09:36 +00:00
|
|
|
|
2014-04-29 17:07:42 +00:00
|
|
|
raw_ostream &BranchProbability::print(raw_ostream &OS) const {
|
2015-12-01 05:29:22 +00:00
|
|
|
if (isUnknown())
|
|
|
|
return OS << "?%";
|
|
|
|
|
2015-09-26 10:09:36 +00:00
|
|
|
// Get a percentage rounded to two decimal digits. This avoids
|
|
|
|
// implementation-defined rounding inside printf.
|
|
|
|
double Percent = rint(((double)N / D) * 100.0 * 100.0) / 100.0;
|
2015-12-01 05:29:22 +00:00
|
|
|
return OS << format("0x%08" PRIx32 " / 0x%08" PRIx32 " = %.2f%%", N, D,
|
|
|
|
Percent);
|
2011-06-11 01:05:22 +00:00
|
|
|
}
|
|
|
|
|
2017-10-15 14:32:27 +00:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2016-01-29 20:50:44 +00:00
|
|
|
LLVM_DUMP_METHOD void BranchProbability::dump() const { print(dbgs()) << '\n'; }
|
2017-01-28 02:02:38 +00:00
|
|
|
#endif
|
2011-06-11 01:05:22 +00:00
|
|
|
|
2015-09-25 23:09:59 +00:00
|
|
|
BranchProbability::BranchProbability(uint32_t Numerator, uint32_t Denominator) {
|
|
|
|
assert(Denominator > 0 && "Denominator cannot be 0!");
|
|
|
|
assert(Numerator <= Denominator && "Probability cannot be bigger than 1!");
|
|
|
|
if (Denominator == D)
|
|
|
|
N = Numerator;
|
|
|
|
else {
|
|
|
|
uint64_t Prob64 =
|
|
|
|
(Numerator * static_cast<uint64_t>(D) + Denominator / 2) / Denominator;
|
|
|
|
N = static_cast<uint32_t>(Prob64);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-01 05:29:22 +00:00
|
|
|
BranchProbability
|
|
|
|
BranchProbability::getBranchProbability(uint64_t Numerator,
|
|
|
|
uint64_t Denominator) {
|
|
|
|
assert(Numerator <= Denominator && "Probability cannot be bigger than 1!");
|
|
|
|
// Scale down Denominator to fit in a 32-bit integer.
|
|
|
|
int Scale = 0;
|
|
|
|
while (Denominator > UINT32_MAX) {
|
|
|
|
Denominator >>= 1;
|
|
|
|
Scale++;
|
|
|
|
}
|
|
|
|
return BranchProbability(Numerator >> Scale, Denominator);
|
|
|
|
}
|
|
|
|
|
2015-09-25 23:09:59 +00:00
|
|
|
// If ConstD is not zero, then replace D by ConstD so that division and modulo
|
|
|
|
// operations by D can be optimized, in case this function is not inlined by the
|
|
|
|
// compiler.
|
|
|
|
template <uint32_t ConstD>
|
2015-09-26 10:09:36 +00:00
|
|
|
static uint64_t scale(uint64_t Num, uint32_t N, uint32_t D) {
|
2015-09-25 23:09:59 +00:00
|
|
|
if (ConstD > 0)
|
|
|
|
D = ConstD;
|
|
|
|
|
2014-04-29 16:15:35 +00:00
|
|
|
assert(D && "divide by 0");
|
|
|
|
|
|
|
|
// Fast path for multiplying by 1.0.
|
|
|
|
if (!Num || D == N)
|
|
|
|
return Num;
|
|
|
|
|
|
|
|
// Split Num into upper and lower parts to multiply, then recombine.
|
|
|
|
uint64_t ProductHigh = (Num >> 32) * N;
|
|
|
|
uint64_t ProductLow = (Num & UINT32_MAX) * N;
|
|
|
|
|
|
|
|
// Split into 32-bit digits.
|
|
|
|
uint32_t Upper32 = ProductHigh >> 32;
|
|
|
|
uint32_t Lower32 = ProductLow & UINT32_MAX;
|
|
|
|
uint32_t Mid32Partial = ProductHigh & UINT32_MAX;
|
|
|
|
uint32_t Mid32 = Mid32Partial + (ProductLow >> 32);
|
|
|
|
|
|
|
|
// Carry.
|
|
|
|
Upper32 += Mid32 < Mid32Partial;
|
|
|
|
|
|
|
|
uint64_t Rem = (uint64_t(Upper32) << 32) | Mid32;
|
|
|
|
uint64_t UpperQ = Rem / D;
|
|
|
|
|
|
|
|
// Check for overflow.
|
|
|
|
if (UpperQ > UINT32_MAX)
|
|
|
|
return UINT64_MAX;
|
|
|
|
|
|
|
|
Rem = ((Rem % D) << 32) | Lower32;
|
|
|
|
uint64_t LowerQ = Rem / D;
|
|
|
|
uint64_t Q = (UpperQ << 32) + LowerQ;
|
|
|
|
|
|
|
|
// Check for overflow.
|
|
|
|
return Q < LowerQ ? UINT64_MAX : Q;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t BranchProbability::scale(uint64_t Num) const {
|
2015-09-25 23:09:59 +00:00
|
|
|
return ::scale<D>(Num, N, D);
|
2014-04-29 16:15:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t BranchProbability::scaleByInverse(uint64_t Num) const {
|
2015-09-25 23:09:59 +00:00
|
|
|
return ::scale<0>(Num, D, N);
|
2014-04-29 16:15:35 +00:00
|
|
|
}
|