diff --git a/llvm/include/llvm/ProfileData/InstrProf.h b/llvm/include/llvm/ProfileData/InstrProf.h index aa100a796f2d6..384edddf85a95 100644 --- a/llvm/include/llvm/ProfileData/InstrProf.h +++ b/llvm/include/llvm/ProfileData/InstrProf.h @@ -226,7 +226,7 @@ struct InstrProfValueSiteRecord { while (I != IE && I->Value < J->Value) ++I; if (I != IE && I->Value == J->Value) { - I->Count += J->Count; + I->Count = SaturatingAdd(I->Count, J->Count); ++I; continue; } diff --git a/llvm/include/llvm/ProfileData/SampleProf.h b/llvm/include/llvm/ProfileData/SampleProf.h index d7596d7b1b773..a8960cf7bc120 100644 --- a/llvm/include/llvm/ProfileData/SampleProf.h +++ b/llvm/include/llvm/ProfileData/SampleProf.h @@ -173,10 +173,7 @@ class SampleRecord { /// Sample counts accumulate using saturating arithmetic, to avoid wrapping /// around unsigned integers. void addSamples(uint64_t S) { - if (NumSamples <= std::numeric_limits::max() - S) - NumSamples += S; - else - NumSamples = std::numeric_limits::max(); + NumSamples = SaturatingAdd(NumSamples, S); } /// Add called function \p F with samples \p S. @@ -185,10 +182,7 @@ class SampleRecord { /// around unsigned integers. void addCalledTarget(StringRef F, uint64_t S) { uint64_t &TargetSamples = CallTargets[F]; - if (TargetSamples <= std::numeric_limits::max() - S) - TargetSamples += S; - else - TargetSamples = std::numeric_limits::max(); + TargetSamples = SaturatingAdd(TargetSamples, S); } /// Return true if this sample record contains function calls. diff --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h index 2c515bbd242a1..b7b3c02dcfe76 100644 --- a/llvm/include/llvm/Support/MathExtras.h +++ b/llvm/include/llvm/Support/MathExtras.h @@ -653,6 +653,32 @@ inline int64_t SignExtend64(uint64_t X, unsigned 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 std::enable_if::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::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 std::enable_if::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::max(); + else + return Z; +} + extern const float huge_valf; } // End llvm namespace diff --git a/llvm/unittests/Support/MathExtrasTest.cpp b/llvm/unittests/Support/MathExtrasTest.cpp index 8bd47e34c7ec1..8adde02ed4ee1 100644 --- a/llvm/unittests/Support/MathExtrasTest.cpp +++ b/llvm/unittests/Support/MathExtrasTest.cpp @@ -190,4 +190,52 @@ TEST(MathExtras, RoundUpToAlignment) { EXPECT_EQ(552u, RoundUpToAlignment(321, 255, 42)); } +template +void SaturatingAddTestHelper() +{ + EXPECT_EQ(static_cast(3), + SaturatingAdd(static_cast(1), static_cast(2))); + EXPECT_EQ(std::numeric_limits::max(), + SaturatingAdd(std::numeric_limits::max(), static_cast(1))); + EXPECT_EQ(std::numeric_limits::max(), + SaturatingAdd(static_cast(1), std::numeric_limits::max())); + EXPECT_EQ(std::numeric_limits::max(), + SaturatingAdd(std::numeric_limits::max(), + std::numeric_limits::max())); +} + +TEST(MathExtras, SaturatingAdd) { + SaturatingAddTestHelper(); + SaturatingAddTestHelper(); + SaturatingAddTestHelper(); + SaturatingAddTestHelper(); +} + +template +void SaturatingMultiplyTestHelper() +{ + EXPECT_EQ(static_cast(0), + SaturatingMultiply(static_cast(1), static_cast(0))); + EXPECT_EQ(static_cast(0), + SaturatingMultiply(static_cast(0), static_cast(1))); + EXPECT_EQ(static_cast(6), + SaturatingMultiply(static_cast(2), static_cast(3))); + EXPECT_EQ(std::numeric_limits::max(), + SaturatingMultiply(std::numeric_limits::max(), + static_cast(2))); + EXPECT_EQ(std::numeric_limits::max(), + SaturatingMultiply(static_cast(2), + std::numeric_limits::max())); + EXPECT_EQ(std::numeric_limits::max(), + SaturatingMultiply(std::numeric_limits::max(), + std::numeric_limits::max())); +} + +TEST(MathExtras, SaturatingMultiply) { + SaturatingMultiplyTestHelper(); + SaturatingMultiplyTestHelper(); + SaturatingMultiplyTestHelper(); + SaturatingMultiplyTestHelper(); +} + }