summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCong Hou <congh@google.com>2015-10-12 18:34:00 +0000
committerCong Hou <congh@google.com>2015-10-12 18:34:00 +0000
commit61e13de40859751d0c605d7509eeb5a99cf7e9c1 (patch)
tree09dc049b5bb7a4c7c46e6e8f5853d569259d2e37
parent928eb33a9e02f40a8d76da9bd018ce47bf9a59b9 (diff)
downloadbcm5719-llvm-61e13de40859751d0c605d7509eeb5a99cf7e9c1.tar.gz
bcm5719-llvm-61e13de40859751d0c605d7509eeb5a99cf7e9c1.zip
Add - and -= operators to BlockFrequency using saturating arithmetic.
llvm-svn: 250077
-rw-r--r--llvm/include/llvm/Support/BlockFrequency.h4
-rw-r--r--llvm/lib/Support/BlockFrequency.cpp15
-rw-r--r--llvm/unittests/Support/BlockFrequencyTest.cpp6
3 files changed, 25 insertions, 0 deletions
diff --git a/llvm/include/llvm/Support/BlockFrequency.h b/llvm/include/llvm/Support/BlockFrequency.h
index 2dccfa26d89..1b45cc52973 100644
--- a/llvm/include/llvm/Support/BlockFrequency.h
+++ b/llvm/include/llvm/Support/BlockFrequency.h
@@ -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);
diff --git a/llvm/lib/Support/BlockFrequency.cpp b/llvm/lib/Support/BlockFrequency.cpp
index e0430c199eb..e7f3e1764c5 100644
--- a/llvm/lib/Support/BlockFrequency.cpp
+++ b/llvm/lib/Support/BlockFrequency.cpp
@@ -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);
diff --git a/llvm/unittests/Support/BlockFrequencyTest.cpp b/llvm/unittests/Support/BlockFrequencyTest.cpp
index c3273bf4ab2..c1f5671815b 100644
--- a/llvm/unittests/Support/BlockFrequencyTest.cpp
+++ b/llvm/unittests/Support/BlockFrequencyTest.cpp
@@ -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);
OpenPOWER on IntegriCloud