blob: 9aab6ace6e3f0e249ec027157a72d56eea81bf3d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
//===- BranchProbability.h - Branch Probability Wrapper ---------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Definition of BranchProbability shared by IR and Machine Instructions.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_SUPPORT_BRANCHPROBABILITY_H
#define LLVM_SUPPORT_BRANCHPROBABILITY_H
#include "llvm/Support/DataTypes.h"
#include <cassert>
namespace llvm {
class raw_ostream;
// This class represents Branch Probability as a non-negative fraction.
class BranchProbability {
// Numerator
uint32_t N;
// Denominator
uint32_t D;
public:
BranchProbability(uint32_t n, uint32_t d) : N(n), D(d) {
assert(d > 0 && "Denomiator cannot be 0!");
assert(n <= d && "Probability cannot be bigger than 1!");
}
static BranchProbability getZero() { return BranchProbability(0, 1); }
static BranchProbability getOne() { return BranchProbability(1, 1); }
uint32_t getNumerator() const { return N; }
uint32_t getDenominator() const { return D; }
// Return (1 - Probability).
BranchProbability getCompl() const {
return BranchProbability(D - N, D);
}
raw_ostream &print(raw_ostream &OS) const;
void dump() const;
/// \brief Scale a large integer.
///
/// Scales \c Num. Guarantees full precision. Returns the floor of the
/// result.
///
/// \return \c Num times \c this.
uint64_t scale(uint64_t Num) const;
/// \brief Scale a large integer by the inverse.
///
/// Scales \c Num by the inverse of \c this. Guarantees full precision.
/// Returns the floor of the result.
///
/// \return \c Num divided by \c this.
uint64_t scaleByInverse(uint64_t Num) const;
bool operator==(BranchProbability RHS) const {
return (uint64_t)N * RHS.D == (uint64_t)D * RHS.N;
}
bool operator!=(BranchProbability RHS) const {
return !(*this == RHS);
}
bool operator<(BranchProbability RHS) const {
return (uint64_t)N * RHS.D < (uint64_t)D * RHS.N;
}
bool operator>(BranchProbability RHS) const { return RHS < *this; }
bool operator<=(BranchProbability RHS) const { return !(RHS < *this); }
bool operator>=(BranchProbability RHS) const { return !(*this < RHS); }
};
inline raw_ostream &operator<<(raw_ostream &OS, const BranchProbability &Prob) {
return Prob.print(OS);
}
}
#endif
|