[llvm] r341444 - Prevent unsigned overflow.
Richard Trieu via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 4 21:19:15 PDT 2018
Author: rtrieu
Date: Tue Sep 4 21:19:15 2018
New Revision: 341444
URL: http://llvm.org/viewvc/llvm-project?rev=341444&view=rev
Log:
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.
Modified:
llvm/trunk/lib/Transforms/Instrumentation/ControlHeightReduction.cpp
Modified: llvm/trunk/lib/Transforms/Instrumentation/ControlHeightReduction.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/ControlHeightReduction.cpp?rev=341444&r1=341443&r2=341444&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Instrumentation/ControlHeightReduction.cpp (original)
+++ llvm/trunk/lib/Transforms/Instrumentation/ControlHeightReduction.cpp Tue Sep 4 21:19:15 2018
@@ -614,13 +614,15 @@ static bool CheckMDProf(MDNode *MD, Bran
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;
}
More information about the llvm-commits
mailing list