[block-freq] Add a right shift to BlockFrequency that saturates at 1.
authorMichael Gottesman <mgottesman@apple.com>
Sat, 14 Dec 2013 02:24:22 +0000 (02:24 +0000)
committerMichael Gottesman <mgottesman@apple.com>
Sat, 14 Dec 2013 02:24:22 +0000 (02:24 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@197302 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/BlockFrequency.h
lib/Support/BlockFrequency.cpp
unittests/Support/BlockFrequencyTest.cpp

index 997d11e47ab752c655327dcacba6ae876e7bf0da..dae520b54e482e121d2cf9ba5eba221d4f5e739d 100644 (file)
@@ -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);
index d1f8408dfcbb699776f47eb918735473881fa315..00cf75bd5cf3b3decc4a9d33f8d6cd5800390ddb 100644 (file)
@@ -145,6 +145,18 @@ BlockFrequency::operator+(const BlockFrequency &Prob) const {
   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());
 }
index ffdea2c1790fce80a52877eee0bcc00a352477ad..c3184515046aebeb74221b9d00174dd79761d359 100644 (file)
@@ -237,4 +237,12 @@ TEST(BlockFrequencyTest, ProbabilityCompare) {
   EXPECT_FALSE(BigZero >= BigOne);
 }
 
+TEST(BlockFrequencyTest, SaturatingRightShift) {
+  BlockFrequency Freq(0x10080ULL);
+  Freq >>= 2;
+  EXPECT_EQ(Freq.getFrequency(), 0x4020ULL);
+  Freq >>= 20;
+  EXPECT_EQ(Freq.getFrequency(), 0x1ULL);
+}
+
 }