From 341473c86d19cad5f3be432533ecdb42d9e07044 Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Sun, 23 Oct 2011 11:19:14 +0000 Subject: [PATCH] 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 --- include/llvm/Support/BranchProbability.h | 18 ++++++++++++++++-- lib/Analysis/BranchProbabilityInfo.cpp | 12 ++++-------- lib/Support/BranchProbability.cpp | 7 ------- unittests/Support/BlockFrequencyTest.cpp | 20 ++++++++++++++++++++ 4 files changed, 40 insertions(+), 17 deletions(-) diff --git a/include/llvm/Support/BranchProbability.h b/include/llvm/Support/BranchProbability.h index 05c24d4fcfc..a1e5ceb6255 100644 --- a/include/llvm/Support/BranchProbability.h +++ b/include/llvm/Support/BranchProbability.h @@ -15,6 +15,7 @@ #define LLVM_SUPPORT_BRANCHPROBABILITY_H #include "llvm/Support/DataTypes.h" +#include 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); diff --git a/lib/Analysis/BranchProbabilityInfo.cpp b/lib/Analysis/BranchProbabilityInfo.cpp index fc69da9e672..4f15858aa34 100644 --- a/lib/Analysis/BranchProbabilityInfo.cpp +++ b/lib/Analysis/BranchProbabilityInfo.cpp @@ -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; diff --git a/lib/Support/BranchProbability.cpp b/lib/Support/BranchProbability.cpp index 49d04ed8365..5a5e07583d9 100644 --- a/lib/Support/BranchProbability.cpp +++ b/lib/Support/BranchProbability.cpp @@ -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); } diff --git a/unittests/Support/BlockFrequencyTest.cpp b/unittests/Support/BlockFrequencyTest.cpp index edeea9b357f..ac3cedf1869 100644 --- a/unittests/Support/BlockFrequencyTest.cpp +++ b/unittests/Support/BlockFrequencyTest.cpp @@ -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); +} + } -- 2.34.1