Add compare operators to BranchProbability and use it to determine if an edge is hot.

llvm-svn: 142751
This commit is contained in:
Benjamin Kramer 2011-10-23 11:19:14 +00:00
parent 68ba25c47d
commit 9adc582e35
4 changed files with 40 additions and 17 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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);
}

View File

@ -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);
}
}