Skip to content

Commit

Permalink
Add - and -= operators to BlockFrequency using saturating arithmetic.
Browse files Browse the repository at this point in the history
llvm-svn: 250077
  • Loading branch information
Cong Hou committed Oct 12, 2015
1 parent 928eb33 commit 61e13de
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions llvm/include/llvm/Support/BlockFrequency.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class BlockFrequency {
BlockFrequency &operator+=(BlockFrequency Freq);
BlockFrequency operator+(BlockFrequency Freq) const;

/// \brief Subtracts another block frequency using saturating arithmetic.
BlockFrequency &operator-=(BlockFrequency Freq);
BlockFrequency operator-(BlockFrequency Freq) const;

/// \brief Shift block frequency to the right by count digits saturating to 1.
BlockFrequency &operator>>=(const unsigned count);

Expand Down
15 changes: 15 additions & 0 deletions llvm/lib/Support/BlockFrequency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ BlockFrequency BlockFrequency::operator+(BlockFrequency Freq) const {
return NewFreq;
}

BlockFrequency &BlockFrequency::operator-=(BlockFrequency Freq) {
// If underflow, set frequency to 0.
if (Frequency <= Freq.Frequency)
Frequency = 0;
else
Frequency -= Freq.Frequency;
return *this;
}

BlockFrequency BlockFrequency::operator-(BlockFrequency Freq) const {
BlockFrequency NewFreq(Frequency);
NewFreq -= Freq;
return NewFreq;
}

BlockFrequency &BlockFrequency::operator>>=(const unsigned count) {
// Frequency can never be 0 by design.
assert(Frequency != 0);
Expand Down
6 changes: 6 additions & 0 deletions llvm/unittests/Support/BlockFrequencyTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ TEST(BlockFrequencyTest, MaxToMax) {
EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
}

TEST(BlockFrequencyTest, Subtract) {
BlockFrequency Freq1(0), Freq2(1);
EXPECT_EQ((Freq1 - Freq2).getFrequency(), 0u);
EXPECT_EQ((Freq2 - Freq1).getFrequency(), 1u);
}

TEST(BlockFrequency, Divide) {
BlockFrequency Freq(0x3333333333333333ULL);
Freq /= BranchProbability(1, 2);
Expand Down

0 comments on commit 61e13de

Please sign in to comment.