llvm.org GIT mirror llvm / 341473c
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 Benjamin Kramer 8 years ago
4 changed file(s) with 40 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
1414 #define LLVM_SUPPORT_BRANCHPROBABILITY_H
1515
1616 #include "llvm/Support/DataTypes.h"
17 #include
1718
1819 namespace llvm {
1920
2122
2223 // This class represents Branch Probability as a non-negative fraction.
2324 class BranchProbability {
24
2525 // Numerator
2626 uint32_t N;
2727
2828 // Denominator
2929 uint32_t D;
3030
31 int64_t compare(BranchProbability RHS) const {
32 return (uint64_t)N * RHS.D - (uint64_t)D * RHS.N;
33 }
34
3135 public:
32 BranchProbability(uint32_t n, uint32_t d);
36 BranchProbability(uint32_t n, uint32_t d) : N(n), D(d) {
37 assert(d > 0 && "Denomiator cannot be 0!");
38 assert(n <= d && "Probability cannot be bigger than 1!");
39 }
3340
3441 uint32_t getNumerator() const { return N; }
3542 uint32_t getDenominator() const { return D; }
4249 void print(raw_ostream &OS) const;
4350
4451 void dump() const;
52
53 bool operator==(BranchProbability RHS) const { return compare(RHS) == 0; }
54 bool operator!=(BranchProbability RHS) const { return compare(RHS) != 0; }
55 bool operator< (BranchProbability RHS) const { return compare(RHS) < 0; }
56 bool operator> (BranchProbability RHS) const { return compare(RHS) > 0; }
57 bool operator<=(BranchProbability RHS) const { return compare(RHS) <= 0; }
58 bool operator>=(BranchProbability RHS) const { return compare(RHS) >= 0; }
4559 };
4660
4761 raw_ostream &operator<<(raw_ostream &OS, const BranchProbability &Prob);
475475 bool BranchProbabilityInfo::
476476 isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const {
477477 // Hot probability is at least 4/5 = 80%
478 uint32_t Weight = getEdgeWeight(Src, Dst);
479 uint32_t Sum = getSumForBlock(Src);
480
481 // FIXME: Implement BranchProbability::compare then change this code to
482 // compare this BranchProbability against a static "hot" BranchProbability.
483 return (uint64_t)Weight * 5 > (uint64_t)Sum * 4;
478 // FIXME: Compare against a static "hot" BranchProbability.
479 return getEdgeProbability(Src, Dst) > BranchProbability(4, 5);
484480 }
485481
486482 BasicBlock *BranchProbabilityInfo::getHotSucc(BasicBlock *BB) const {
502498 }
503499 }
504500
505 // FIXME: Use BranchProbability::compare.
506 if ((uint64_t)MaxWeight * 5 > (uint64_t)Sum * 4)
501 // Hot probability is at least 4/5 = 80%
502 if (BranchProbability(MaxWeight, Sum) > BranchProbability(4, 5))
507503 return MaxSucc;
508504
509505 return 0;
1616
1717 using namespace llvm;
1818
19 BranchProbability::BranchProbability(uint32_t n, uint32_t d) {
20 assert(d > 0 && "Denomiator cannot be 0!");
21 assert(n <= d && "Probability cannot be bigger than 1!");
22 N = n;
23 D = d;
24 }
25
2619 void BranchProbability::print(raw_ostream &OS) const {
2720 OS << N << " / " << D << " = " << ((double)N / D);
2821 }
5252 EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
5353 }
5454
55 TEST(BlockFrequencyTest, ProbabilityCompare) {
56 BranchProbability A(4, 5);
57 BranchProbability B(4U << 29, 5U << 29);
58 BranchProbability C(3, 4);
59
60 EXPECT_TRUE(A == B);
61 EXPECT_FALSE(A != B);
62 EXPECT_FALSE(A < B);
63 EXPECT_FALSE(A > B);
64 EXPECT_TRUE(A <= B);
65 EXPECT_TRUE(A >= B);
66
67 EXPECT_FALSE(B == C);
68 EXPECT_TRUE(B != C);
69 EXPECT_FALSE(B < C);
70 EXPECT_TRUE(B > C);
71 EXPECT_FALSE(B <= C);
72 EXPECT_TRUE(B >= C);
5573 }
74
75 }