[llvm] r250077 - Add - and -= operators to BlockFrequency using saturating arithmetic.
Cong Hou via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 12 11:34:00 PDT 2015
Author: conghou
Date: Mon Oct 12 13:34:00 2015
New Revision: 250077
URL: http://llvm.org/viewvc/llvm-project?rev=250077&view=rev
Log:
Add - and -= operators to BlockFrequency using saturating arithmetic.
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=250077&r1=250076&r2=250077&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/BlockFrequency.h (original)
+++ llvm/trunk/include/llvm/Support/BlockFrequency.h Mon Oct 12 13:34:00 2015
@@ -49,6 +49,10 @@ public:
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);
Modified: llvm/trunk/lib/Support/BlockFrequency.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/BlockFrequency.cpp?rev=250077&r1=250076&r2=250077&view=diff
==============================================================================
--- llvm/trunk/lib/Support/BlockFrequency.cpp (original)
+++ llvm/trunk/lib/Support/BlockFrequency.cpp Mon Oct 12 13:34:00 2015
@@ -56,6 +56,21 @@ BlockFrequency BlockFrequency::operator+
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);
Modified: llvm/trunk/unittests/Support/BlockFrequencyTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/BlockFrequencyTest.cpp?rev=250077&r1=250076&r2=250077&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/BlockFrequencyTest.cpp (original)
+++ llvm/trunk/unittests/Support/BlockFrequencyTest.cpp Mon Oct 12 13:34:00 2015
@@ -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);
More information about the llvm-commits
mailing list