[BPI] Fix two potential divide-by-zero operations that are introduced in r256263.

llvm-svn: 256303
This commit is contained in:
Cong Hou 2015-12-22 23:45:55 +00:00
parent 69b537cdd4
commit d72d3cbece
2 changed files with 20 additions and 9 deletions

View File

@ -221,8 +221,14 @@ bool BranchProbabilityInfo::calcMetadataWeights(BasicBlock *BB) {
Weights[i] /= ScalingFactor;
WeightSum += Weights[i];
}
for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
setEdgeProbability(BB, i, {Weights[i], static_cast<uint32_t>(WeightSum)});
if (WeightSum == 0) {
for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
setEdgeProbability(BB, i, {1, e});
} else {
for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
setEdgeProbability(BB, i, {Weights[i], static_cast<uint32_t>(WeightSum)});
}
assert(WeightSum <= UINT32_MAX &&
"Expected weights to scale down to 32 bits");

View File

@ -1647,14 +1647,19 @@ void JumpThreading::UpdateBlockFreqAndEdgeWeight(BasicBlock *PredBB,
uint64_t MaxBBSuccFreq =
*std::max_element(BBSuccFreq.begin(), BBSuccFreq.end());
SmallVector<BranchProbability, 4> BBSuccProbs;
for (uint64_t Freq : BBSuccFreq)
BBSuccProbs.push_back(
BranchProbability::getBranchProbability(Freq, MaxBBSuccFreq));
// Normalize edge probabilities so that they sum up to one.
BranchProbability::normalizeProbabilities(BBSuccProbs.begin(),
BBSuccProbs.end());
SmallVector<BranchProbability, 4> BBSuccProbs;
if (MaxBBSuccFreq == 0)
BBSuccProbs.assign(BBSuccFreq.size(),
{1, static_cast<unsigned>(BBSuccFreq.size())});
else {
for (uint64_t Freq : BBSuccFreq)
BBSuccProbs.push_back(
BranchProbability::getBranchProbability(Freq, MaxBBSuccFreq));
// Normalize edge probabilities so that they sum up to one.
BranchProbability::normalizeProbabilities(BBSuccProbs.begin(),
BBSuccProbs.end());
}
// Update edge probabilities in BPI.
for (int I = 0, E = BBSuccProbs.size(); I < E; I++)