Revert "[llvm-profdata] Add SaturatingAdd/SaturatingMultiply Helper Functions"
authorNathan Slingerland <slingn@gmail.com>
Wed, 18 Nov 2015 00:55:15 +0000 (00:55 +0000)
committerNathan Slingerland <slingn@gmail.com>
Wed, 18 Nov 2015 00:55:15 +0000 (00:55 +0000)
Not ready for merge.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@253415 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ProfileData/InstrProf.h
include/llvm/ProfileData/SampleProf.h
include/llvm/Support/MathExtras.h
unittests/Support/MathExtrasTest.cpp

index 384edddf85a956a70fa7413cf4e5d1eed86d4dbc..aa100a796f2d60e23b59b51e6215a54229f124f9 100644 (file)
@@ -226,7 +226,7 @@ struct InstrProfValueSiteRecord {
       while (I != IE && I->Value < J->Value)
         ++I;
       if (I != IE && I->Value == J->Value) {
       while (I != IE && I->Value < J->Value)
         ++I;
       if (I != IE && I->Value == J->Value) {
-        I->Count = SaturatingAdd(I->Count, J->Count);
+        I->Count += J->Count;
         ++I;
         continue;
       }
         ++I;
         continue;
       }
index a8960cf7bc12032295c11da4a9574afde0f0f188..d7596d7b1b77340f924978caf5e9024c2957a5cf 100644 (file)
@@ -173,7 +173,10 @@ public:
   /// Sample counts accumulate using saturating arithmetic, to avoid wrapping
   /// around unsigned integers.
   void addSamples(uint64_t S) {
   /// Sample counts accumulate using saturating arithmetic, to avoid wrapping
   /// around unsigned integers.
   void addSamples(uint64_t S) {
-    NumSamples = SaturatingAdd(NumSamples, S);
+    if (NumSamples <= std::numeric_limits<uint64_t>::max() - S)
+      NumSamples += S;
+    else
+      NumSamples = std::numeric_limits<uint64_t>::max();
   }
 
   /// Add called function \p F with samples \p S.
   }
 
   /// Add called function \p F with samples \p S.
@@ -182,7 +185,10 @@ public:
   /// around unsigned integers.
   void addCalledTarget(StringRef F, uint64_t S) {
     uint64_t &TargetSamples = CallTargets[F];
   /// around unsigned integers.
   void addCalledTarget(StringRef F, uint64_t S) {
     uint64_t &TargetSamples = CallTargets[F];
-    TargetSamples = SaturatingAdd(TargetSamples, S);
+    if (TargetSamples <= std::numeric_limits<uint64_t>::max() - S)
+      TargetSamples += S;
+    else
+      TargetSamples = std::numeric_limits<uint64_t>::max();
   }
 
   /// Return true if this sample record contains function calls.
   }
 
   /// Return true if this sample record contains function calls.
index b7b3c02dcfe76be2cf9572cb4308f4aed4dc9905..2c515bbd242a13b725b37c5ad30ad4478a836418 100644 (file)
@@ -653,32 +653,6 @@ inline int64_t SignExtend64(uint64_t X, unsigned B) {
   return int64_t(X << (64 - B)) >> (64 - B);
 }
 
   return int64_t(X << (64 - B)) >> (64 - B);
 }
 
-/// \brief Add two unsigned integers, X and Y, of type T.
-/// Clamp the result to the maximum representable value of T on overflow.
-template <typename T>
-typename std::enable_if<std::is_unsigned<T>::value, T>::type
-SaturatingAdd(T X, T Y) {
-  // Hacker's Delight, p. 29
-  T Z = X + Y;
-  if (Z < X || Z < Y)
-    return std::numeric_limits<T>::max();
-  else
-    return Z;
-}
-
-/// \brief Multiply two unsigned integers, X and Y, of type T.
-/// Clamp the result to the maximum representable value of T on overflow.
-template <typename T>
-typename std::enable_if<std::is_unsigned<T>::value, T>::type
-SaturatingMultiply(T X, T Y) {
-  // Hacker's Delight, p. 30
-  T Z = X * Y;
-  if (Y != 0 && Z / Y != X)
-    return std::numeric_limits<T>::max();
-  else
-    return Z;
-}
-
 extern const float huge_valf;
 } // End llvm namespace
 
 extern const float huge_valf;
 } // End llvm namespace
 
index 8adde02ed4ee1b6fac8041014ae921f85bf77827..8bd47e34c7ec1eed32a1a645f184d149c76db023 100644 (file)
@@ -190,52 +190,4 @@ TEST(MathExtras, RoundUpToAlignment) {
   EXPECT_EQ(552u, RoundUpToAlignment(321, 255, 42));
 }
 
   EXPECT_EQ(552u, RoundUpToAlignment(321, 255, 42));
 }
 
-template<typename T>
-void SaturatingAddTestHelper()
-{
-  EXPECT_EQ(static_cast<T>(3),
-            SaturatingAdd(static_cast<T>(1), static_cast<T>(2)));
-  EXPECT_EQ(std::numeric_limits<T>::max(),
-            SaturatingAdd(std::numeric_limits<T>::max(), static_cast<T>(1)));
-  EXPECT_EQ(std::numeric_limits<T>::max(),
-            SaturatingAdd(static_cast<T>(1), std::numeric_limits<T>::max()));
-  EXPECT_EQ(std::numeric_limits<T>::max(),
-            SaturatingAdd(std::numeric_limits<T>::max(),
-                          std::numeric_limits<T>::max()));
-}
-
-TEST(MathExtras, SaturatingAdd) {
-  SaturatingAddTestHelper<uint8_t>();
-  SaturatingAddTestHelper<uint16_t>();
-  SaturatingAddTestHelper<uint32_t>();
-  SaturatingAddTestHelper<uint64_t>();
-}
-
-template<typename T>
-void SaturatingMultiplyTestHelper()
-{
-  EXPECT_EQ(static_cast<T>(0),
-            SaturatingMultiply(static_cast<T>(1), static_cast<T>(0)));
-  EXPECT_EQ(static_cast<T>(0),
-            SaturatingMultiply(static_cast<T>(0), static_cast<T>(1)));
-  EXPECT_EQ(static_cast<T>(6),
-            SaturatingMultiply(static_cast<T>(2), static_cast<T>(3)));
-  EXPECT_EQ(std::numeric_limits<T>::max(),
-            SaturatingMultiply(std::numeric_limits<T>::max(),
-                               static_cast<T>(2)));
-  EXPECT_EQ(std::numeric_limits<T>::max(),
-            SaturatingMultiply(static_cast<T>(2),
-                               std::numeric_limits<T>::max()));
-  EXPECT_EQ(std::numeric_limits<T>::max(),
-            SaturatingMultiply(std::numeric_limits<T>::max(),
-                          std::numeric_limits<T>::max()));
-}
-
-TEST(MathExtras, SaturatingMultiply) {
-  SaturatingMultiplyTestHelper<uint8_t>();
-  SaturatingMultiplyTestHelper<uint16_t>();
-  SaturatingMultiplyTestHelper<uint32_t>();
-  SaturatingMultiplyTestHelper<uint64_t>();
-}
-
 }
 }