1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-21 03:53:04 +02:00

Prevent unsigned overflow.

The sum of the weights is caculated in an APInt, which has a width smaller than
64.  In certain cases, the sum of the widths would overflow when calculations
are done inside an APInt, but would not if done with uint64_t.  Since the
values will be passed as uint64_t in the function call anyways, do all the math
in 64 bits.  Also added an assert in case the probabilities overflow 64 bits.

llvm-svn: 341444
This commit is contained in:
Richard Trieu 2018-09-05 04:19:15 +00:00
parent 36e6b77321
commit 5cc0f882a6

View File

@ -614,13 +614,15 @@ static bool CheckMDProf(MDNode *MD, BranchProbability &TrueProb,
ConstantInt *FalseWeight = mdconst::extract<ConstantInt>(MD->getOperand(2));
if (!TrueWeight || !FalseWeight)
return false;
APInt TrueWt = TrueWeight->getValue();
APInt FalseWt = FalseWeight->getValue();
APInt SumWt = TrueWt + FalseWt;
TrueProb = BranchProbability::getBranchProbability(TrueWt.getZExtValue(),
SumWt.getZExtValue());
FalseProb = BranchProbability::getBranchProbability(FalseWt.getZExtValue(),
SumWt.getZExtValue());
uint64_t TrueWt = TrueWeight->getValue().getZExtValue();
uint64_t FalseWt = FalseWeight->getValue().getZExtValue();
uint64_t SumWt = TrueWt + FalseWt;
assert(SumWt >= TrueWt && SumWt >= FalseWt &&
"Overflow calculating branch probabilities.");
TrueProb = BranchProbability::getBranchProbability(TrueWt, SumWt);
FalseProb = BranchProbability::getBranchProbability(FalseWt, SumWt);
return true;
}