diff options
author | Benjamin Kramer <benny.kra@googlemail.com> | 2011-10-27 16:38:50 +0000 |
---|---|---|
committer | Benjamin Kramer <benny.kra@googlemail.com> | 2011-10-27 16:38:50 +0000 |
commit | 2aed4393b86314f31488609d803a5525d1e0cf88 (patch) | |
tree | f652176abe4485954c482de646563e85c546a780 /llvm/lib/Support/BlockFrequency.cpp | |
parent | 9563a09cee195efb0091895c6810baa2aae20f1c (diff) | |
download | bcm5719-llvm-2aed4393b86314f31488609d803a5525d1e0cf88.tar.gz bcm5719-llvm-2aed4393b86314f31488609d803a5525d1e0cf88.zip |
BlockFrequency: Use a smarter overflow check.
This trades one 64 bit div for one 64 bit mul and some arithmetic.
llvm-svn: 143106
Diffstat (limited to 'llvm/lib/Support/BlockFrequency.cpp')
-rw-r--r-- | llvm/lib/Support/BlockFrequency.cpp | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/llvm/lib/Support/BlockFrequency.cpp b/llvm/lib/Support/BlockFrequency.cpp index a63bf83f203..84a993e3e5b 100644 --- a/llvm/lib/Support/BlockFrequency.cpp +++ b/llvm/lib/Support/BlockFrequency.cpp @@ -70,8 +70,13 @@ BlockFrequency &BlockFrequency::operator*=(const BranchProbability &Prob) { assert(n <= d && "Probability must be less or equal to 1."); - // If we can overflow use 96-bit operations. - if (n > 0 && Frequency > UINT64_MAX / n) { + // Calculate Frequency * n. + uint64_t mulLo = (Frequency & UINT32_MAX) * n; + uint64_t mulHi = (Frequency >> 32) * n; + uint64_t mulRes = (mulHi << 32) + mulLo; + + // If there was overflow use 96-bit operations. + if (mulHi > UINT32_MAX || mulRes < mulLo) { // 96-bit value represented as W[1]:W[0]. uint64_t W[2]; @@ -82,8 +87,7 @@ BlockFrequency &BlockFrequency::operator*=(const BranchProbability &Prob) { return *this; } - Frequency *= n; - Frequency /= d; + Frequency = mulRes / d; return *this; } |