diff options
author | Benjamin Kramer <benny.kra@googlemail.com> | 2011-10-24 13:50:56 +0000 |
---|---|---|
committer | Benjamin Kramer <benny.kra@googlemail.com> | 2011-10-24 13:50:56 +0000 |
commit | 812da49172d19efed8dadf4edc0f17c6a29e7f10 (patch) | |
tree | 10963e766905a3e0e04c1f15c71538bbddc9bf25 | |
parent | 7111f4564cf32410227255e7817c486d30d49fd7 (diff) | |
download | bcm5719-llvm-812da49172d19efed8dadf4edc0f17c6a29e7f10.tar.gz bcm5719-llvm-812da49172d19efed8dadf4edc0f17c6a29e7f10.zip |
Implement comparison operators for BranchProbability in a way that can't overflow INT64_MAX.
Add a test case for the edge case that triggers this. Thanks to Chandler for bringing this to my attention.
llvm-svn: 142794
-rw-r--r-- | llvm/include/llvm/Support/BranchProbability.h | 28 | ||||
-rw-r--r-- | llvm/unittests/Support/BlockFrequencyTest.cpp | 9 |
2 files changed, 27 insertions, 10 deletions
diff --git a/llvm/include/llvm/Support/BranchProbability.h b/llvm/include/llvm/Support/BranchProbability.h index 6b83159de6b..4b5d9044007 100644 --- a/llvm/include/llvm/Support/BranchProbability.h +++ b/llvm/include/llvm/Support/BranchProbability.h @@ -29,10 +29,6 @@ class BranchProbability { // 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) : N(n), D(d) { assert(d > 0 && "Denomiator cannot be 0!"); @@ -54,12 +50,24 @@ public: 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; } + 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 (uint64_t)N * RHS.D <= (uint64_t)D * RHS.N; + } + bool operator>=(BranchProbability RHS) const { + return RHS <= *this; + } }; raw_ostream &operator<<(raw_ostream &OS, const BranchProbability &Prob); diff --git a/llvm/unittests/Support/BlockFrequencyTest.cpp b/llvm/unittests/Support/BlockFrequencyTest.cpp index ac3cedf1869..df256424b82 100644 --- a/llvm/unittests/Support/BlockFrequencyTest.cpp +++ b/llvm/unittests/Support/BlockFrequencyTest.cpp @@ -71,6 +71,15 @@ TEST(BlockFrequencyTest, ProbabilityCompare) { EXPECT_TRUE(B > C); EXPECT_FALSE(B <= C); EXPECT_TRUE(B >= C); + + BranchProbability BigZero(0, UINT32_MAX); + BranchProbability BigOne(UINT32_MAX, UINT32_MAX); + EXPECT_FALSE(BigZero == BigOne); + EXPECT_TRUE(BigZero != BigOne); + EXPECT_TRUE(BigZero < BigOne); + EXPECT_FALSE(BigZero > BigOne); + EXPECT_TRUE(BigZero <= BigOne); + EXPECT_FALSE(BigZero >= BigOne); } } |