From: Benjamin Kramer Date: Sun, 9 Mar 2014 14:42:55 +0000 (+0000) Subject: SimplifyCFG: Simplify the weight scaling algorithm. X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=4b484628f452dcd119ad855f114ca29d26829d90;p=oota-llvm.git SimplifyCFG: Simplify the weight scaling algorithm. No change in functionality. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@203413 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/Utils/SimplifyCFG.cpp b/lib/Transforms/Utils/SimplifyCFG.cpp index 80f122ab83c..5b3bdbe4be2 100644 --- a/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/lib/Transforms/Utils/SimplifyCFG.cpp @@ -732,8 +732,7 @@ static void GetBranchWeights(TerminatorInst *TI, MDNode* MD = TI->getMetadata(LLVMContext::MD_prof); assert(MD); for (unsigned i = 1, e = MD->getNumOperands(); i < e; ++i) { - ConstantInt* CI = dyn_cast(MD->getOperand(i)); - assert(CI); + ConstantInt *CI = cast(MD->getOperand(i)); Weights.push_back(CI->getValue().getZExtValue()); } @@ -750,19 +749,11 @@ static void GetBranchWeights(TerminatorInst *TI, /// Keep halving the weights until all can fit in uint32_t. static void FitWeights(MutableArrayRef Weights) { - while (true) { - bool Halve = false; - for (unsigned i = 0; i < Weights.size(); ++i) - if (Weights[i] > UINT_MAX) { - Halve = true; - break; - } - - if (! Halve) - return; - - for (unsigned i = 0; i < Weights.size(); ++i) - Weights[i] /= 2; + uint64_t Max = *std::max_element(Weights.begin(), Weights.end()); + if (Max > UINT_MAX) { + unsigned Offset = 32 - countLeadingZeros(Max); + for (uint64_t &I : Weights) + I >>= Offset; } }