[llvm] r197302 - [block-freq] Add a right shift to BlockFrequency that saturates at 1.
Michael Gottesman
mgottesman at apple.com
Fri Dec 13 18:24:22 PST 2013
Author: mgottesman
Date: Fri Dec 13 20:24:22 2013
New Revision: 197302
URL: http://llvm.org/viewvc/llvm-project?rev=197302&view=rev
Log:
[block-freq] Add a right shift to BlockFrequency that saturates at 1.
Modified:
llvm/trunk/include/llvm/Support/BlockFrequency.h
llvm/trunk/lib/Support/BlockFrequency.cpp
llvm/trunk/unittests/Support/BlockFrequencyTest.cpp
Modified: llvm/trunk/include/llvm/Support/BlockFrequency.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/BlockFrequency.h?rev=197302&r1=197301&r2=197302&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/BlockFrequency.h (original)
+++ llvm/trunk/include/llvm/Support/BlockFrequency.h Fri Dec 13 20:24:22 2013
@@ -55,6 +55,9 @@ public:
BlockFrequency &operator+=(const BlockFrequency &Freq);
const BlockFrequency operator+(const BlockFrequency &Freq) const;
+ /// \brief Shift block frequency to the right by count digits saturating to 1.
+ BlockFrequency &operator>>=(const unsigned count);
+
/// \brief Scale the given BlockFrequency by N/D. Return the remainder from
/// the division by D. Upon overflow, the routine will saturate.
uint32_t scale(const BranchProbability &Prob);
Modified: llvm/trunk/lib/Support/BlockFrequency.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/BlockFrequency.cpp?rev=197302&r1=197301&r2=197302&view=diff
==============================================================================
--- llvm/trunk/lib/Support/BlockFrequency.cpp (original)
+++ llvm/trunk/lib/Support/BlockFrequency.cpp Fri Dec 13 20:24:22 2013
@@ -145,6 +145,18 @@ BlockFrequency::operator+(const BlockFre
return Freq;
}
+BlockFrequency &BlockFrequency::operator>>=(const unsigned count) {
+ // Frequency can never be 0 by design.
+ assert(Frequency != 0);
+
+ // Shift right by count.
+ Frequency >>= count;
+
+ // Saturate to 1 if we are 0.
+ Frequency |= Frequency == 0;
+ return *this;
+}
+
uint32_t BlockFrequency::scale(const BranchProbability &Prob) {
return scale(Prob.getNumerator(), Prob.getDenominator());
}
Modified: llvm/trunk/unittests/Support/BlockFrequencyTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/BlockFrequencyTest.cpp?rev=197302&r1=197301&r2=197302&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/BlockFrequencyTest.cpp (original)
+++ llvm/trunk/unittests/Support/BlockFrequencyTest.cpp Fri Dec 13 20:24:22 2013
@@ -237,4 +237,12 @@ TEST(BlockFrequencyTest, ProbabilityComp
EXPECT_FALSE(BigZero >= BigOne);
}
+TEST(BlockFrequencyTest, SaturatingRightShift) {
+ BlockFrequency Freq(0x10080ULL);
+ Freq >>= 2;
+ EXPECT_EQ(Freq.getFrequency(), 0x4020ULL);
+ Freq >>= 20;
+ EXPECT_EQ(Freq.getFrequency(), 0x1ULL);
+}
+
}
More information about the llvm-commits
mailing list