mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-08-21 18:29:46 +00:00
Add compare operators to BranchProbability and use it to determine if an edge is hot.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@142751 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
3071363bcd
commit
341473c86d
@ -15,6 +15,7 @@
|
||||
#define LLVM_SUPPORT_BRANCHPROBABILITY_H
|
||||
|
||||
#include "llvm/Support/DataTypes.h"
|
||||
#include <cassert>
|
||||
|
||||
namespace llvm {
|
||||
|
||||
@ -22,15 +23,21 @@ class raw_ostream;
|
||||
|
||||
// This class represents Branch Probability as a non-negative fraction.
|
||||
class BranchProbability {
|
||||
|
||||
// Numerator
|
||||
uint32_t N;
|
||||
|
||||
// Denominator
|
||||
uint32_t D;
|
||||
|
||||
int64_t compare(BranchProbability RHS) const {
|
||||
return (uint64_t)N * RHS.D - (uint64_t)D * RHS.N;
|
||||
}
|
||||
|
||||
public:
|
||||
BranchProbability(uint32_t n, uint32_t d);
|
||||
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!");
|
||||
}
|
||||
|
||||
uint32_t getNumerator() const { return N; }
|
||||
uint32_t getDenominator() const { return D; }
|
||||
@ -43,6 +50,13 @@ public:
|
||||
void print(raw_ostream &OS) const;
|
||||
|
||||
void dump() const;
|
||||
|
||||
bool operator==(BranchProbability RHS) const { return compare(RHS) == 0; }
|
||||
bool operator!=(BranchProbability RHS) const { return compare(RHS) != 0; }
|
||||
bool operator< (BranchProbability RHS) const { return compare(RHS) < 0; }
|
||||
bool operator> (BranchProbability RHS) const { return compare(RHS) > 0; }
|
||||
bool operator<=(BranchProbability RHS) const { return compare(RHS) <= 0; }
|
||||
bool operator>=(BranchProbability RHS) const { return compare(RHS) >= 0; }
|
||||
};
|
||||
|
||||
raw_ostream &operator<<(raw_ostream &OS, const BranchProbability &Prob);
|
||||
|
@ -476,12 +476,8 @@ uint32_t BranchProbabilityInfo::getSumForBlock(const BasicBlock *BB) const {
|
||||
bool BranchProbabilityInfo::
|
||||
isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const {
|
||||
// Hot probability is at least 4/5 = 80%
|
||||
uint32_t Weight = getEdgeWeight(Src, Dst);
|
||||
uint32_t Sum = getSumForBlock(Src);
|
||||
|
||||
// FIXME: Implement BranchProbability::compare then change this code to
|
||||
// compare this BranchProbability against a static "hot" BranchProbability.
|
||||
return (uint64_t)Weight * 5 > (uint64_t)Sum * 4;
|
||||
// FIXME: Compare against a static "hot" BranchProbability.
|
||||
return getEdgeProbability(Src, Dst) > BranchProbability(4, 5);
|
||||
}
|
||||
|
||||
BasicBlock *BranchProbabilityInfo::getHotSucc(BasicBlock *BB) const {
|
||||
@ -503,8 +499,8 @@ BasicBlock *BranchProbabilityInfo::getHotSucc(BasicBlock *BB) const {
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: Use BranchProbability::compare.
|
||||
if ((uint64_t)MaxWeight * 5 > (uint64_t)Sum * 4)
|
||||
// Hot probability is at least 4/5 = 80%
|
||||
if (BranchProbability(MaxWeight, Sum) > BranchProbability(4, 5))
|
||||
return MaxSucc;
|
||||
|
||||
return 0;
|
||||
|
@ -17,13 +17,6 @@
|
||||
|
||||
using namespace llvm;
|
||||
|
||||
BranchProbability::BranchProbability(uint32_t n, uint32_t d) {
|
||||
assert(d > 0 && "Denomiator cannot be 0!");
|
||||
assert(n <= d && "Probability cannot be bigger than 1!");
|
||||
N = n;
|
||||
D = d;
|
||||
}
|
||||
|
||||
void BranchProbability::print(raw_ostream &OS) const {
|
||||
OS << N << " / " << D << " = " << ((double)N / D);
|
||||
}
|
||||
|
@ -53,4 +53,24 @@ TEST(BlockFrequencyTest, MaxToMax) {
|
||||
EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
|
||||
}
|
||||
|
||||
TEST(BlockFrequencyTest, ProbabilityCompare) {
|
||||
BranchProbability A(4, 5);
|
||||
BranchProbability B(4U << 29, 5U << 29);
|
||||
BranchProbability C(3, 4);
|
||||
|
||||
EXPECT_TRUE(A == B);
|
||||
EXPECT_FALSE(A != B);
|
||||
EXPECT_FALSE(A < B);
|
||||
EXPECT_FALSE(A > B);
|
||||
EXPECT_TRUE(A <= B);
|
||||
EXPECT_TRUE(A >= B);
|
||||
|
||||
EXPECT_FALSE(B == C);
|
||||
EXPECT_TRUE(B != C);
|
||||
EXPECT_FALSE(B < C);
|
||||
EXPECT_TRUE(B > C);
|
||||
EXPECT_FALSE(B <= C);
|
||||
EXPECT_TRUE(B >= C);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user