[llvm] r185226 - Fix a bad overflow check pointed out by Ben.
Jakob Stoklund Olesen
stoklund at 2pi.dk
Fri Jun 28 14:51:18 PDT 2013
Author: stoklund
Date: Fri Jun 28 16:51:18 2013
New Revision: 185226
URL: http://llvm.org/viewvc/llvm-project?rev=185226&view=rev
Log:
Fix a bad overflow check pointed out by Ben.
Modified:
llvm/trunk/lib/Support/BlockFrequency.cpp
llvm/trunk/unittests/Support/BlockFrequencyTest.cpp
Modified: llvm/trunk/lib/Support/BlockFrequency.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/BlockFrequency.cpp?rev=185226&r1=185225&r2=185226&view=diff
==============================================================================
--- llvm/trunk/lib/Support/BlockFrequency.cpp (original)
+++ llvm/trunk/lib/Support/BlockFrequency.cpp Fri Jun 28 16:51:18 2013
@@ -47,9 +47,7 @@ static uint64_t div96bit(uint64_t W[2],
uint64_t x = W[1];
unsigned i;
- // This is really a 64-bit division.
- if (!x)
- return y / D;
+ assert(x != 0 && "This is really a 64-bit division");
// This long division algorithm automatically saturates on overflow.
for (i = 0; i < 64 && x; ++i) {
@@ -75,7 +73,7 @@ void BlockFrequency::scale(uint32_t N, u
uint64_t MulRes = (MulHi << 32) + MulLo;
// If the product fits in 64 bits, just use built-in division.
- if (MulHi <= UINT32_MAX && MulRes <= MulLo) {
+ if (MulHi <= UINT32_MAX && MulRes >= MulLo) {
Frequency = MulRes / D;
return;
}
Modified: llvm/trunk/unittests/Support/BlockFrequencyTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/BlockFrequencyTest.cpp?rev=185226&r1=185225&r2=185226&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/BlockFrequencyTest.cpp (original)
+++ llvm/trunk/unittests/Support/BlockFrequencyTest.cpp Fri Jun 28 16:51:18 2013
@@ -68,6 +68,11 @@ TEST(BlockFrequencyTest, Saturate) {
Freq = 0x1000000000000000ULL;
Freq /= BranchProbability(10000, 160000);
EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
+
+ // Try to cheat the multiplication overflow check.
+ Freq = 0x00000001f0000001ull;
+ Freq /= BranchProbability(1000, 0xf000000f);
+ EXPECT_EQ(33506781356485509ULL, Freq.getFrequency());
}
TEST(BlockFrequencyTest, ProbabilityCompare) {
More information about the llvm-commits
mailing list