This is an archive of the discontinued LLVM Phabricator instance.

Use BranchProbability::scale() to scale an integer with a probability in ARMBaseInstrInfo.cpp,
ClosedPublic

Authored by congh on Aug 24 2015, 2:57 PM.

Details

Summary

Previously in isProfitableToIfCvt() in ARMBaseInstrInfo.cpp, the multiplication between an integer and a branch probability is done manually in an unsafe way that may lead to overflow. This patch corrects those cases by using BranchProbability's member function scale() to avoid overflow (which stores the intermediate result in int64).

Diff Detail

Repository
rL LLVM

Event Timeline

congh updated this revision to Diff 33002.Aug 24 2015, 2:57 PM
congh retitled this revision from to Use BranchProbability::scale() to scale an integer with a probability in ARMBaseInstrInfo.cpp,.
congh updated this object.
congh added reviewers: rengolin, t.p.northover.
congh added subscribers: llvm-commits, davidxl.
rengolin edited edge metadata.Aug 26 2015, 11:22 AM

This looks sane to me. Do you have a code where it fails? Would it be possible to turn into a test?

congh added a comment.Aug 26 2015, 1:32 PM

This looks sane to me. Do you have a code where it fails? Would it be possible to turn into a test?

When the probability is represented with a very large numerator, we will get overflow here (this happened to me when I changed the representation of the BranchProbability by using a very large fixed denominator). BranchProbability provides scale() interface just for this case, so using it should be the right way to scale an integer. Is a test necessary for this fix?

rengolin accepted this revision.Aug 26 2015, 1:42 PM
rengolin edited edge metadata.

When the probability is represented with a very large numerator, we will get overflow here (this happened to me when I changed the representation of the BranchProbability by using a very large fixed denominator). BranchProbability provides scale() interface just for this case, so using it should be the right way to scale an integer. Is a test necessary for this fix?

If the problem was found with a test case, then yes. But if it was just you fiddling with internal constants, I think the patch is obvious and should be ok without a test.

This revision is now accepted and ready to land.Aug 26 2015, 1:42 PM
congh added a comment.Aug 26 2015, 2:17 PM

When the probability is represented with a very large numerator, we will get overflow here (this happened to me when I changed the representation of the BranchProbability by using a very large fixed denominator). BranchProbability provides scale() interface just for this case, so using it should be the right way to scale an integer. Is a test necessary for this fix?

If the problem was found with a test case, then yes. But if it was just you fiddling with internal constants, I think the patch is obvious and should be ok without a test.

This should be the latter case. Thanks for the review!

This revision was automatically updated to reflect the committed changes.