# [llvm] r248633 - Use fixed-point representation for BranchProbability.

İsmail Dönmez via llvm-commits llvm-commits at lists.llvm.org
Sat Sep 26 00:13:08 PDT 2015

```Hi,

On Sat, Sep 26, 2015 at 2:09 AM, Cong Hou via llvm-commits
<llvm-commits at lists.llvm.org> wrote:
> Author: conghou
> Date: Fri Sep 25 18:09:59 2015
> New Revision: 248633
>
> URL: http://llvm.org/viewvc/llvm-project?rev=248633&view=rev
> Log:
> Use fixed-point representation for BranchProbability.
>
> BranchProbability now is represented by its numerator and denominator in uint32_t type. This patch changes this representation into a fixed point that is represented by the numerator in uint32_t type and a constant denominator 1<<31. This is quite similar to the representation of BlockMass in BlockFrequencyInfoImpl.h. There are several pros and cons of this change:
>
> Pros:
>
> 1. It uses only a half space of the current one.
> 2. Some operations are much faster like plus, subtraction, comparison, and scaling by an integer.
>
> Cons:
>
> 1. Constructing a probability using arbitrary numerator and denominator needs additional calculations.
> 2. It is a little less precise than before as we use a fixed denominator. For example, 1 - 1/3 may not be exactly identical to 1 / 3 (this will lead to many BranchProbability unit test failures). This should not matter when we only use it for branch probability. If we use it like a rational value for some precise calculations we may need another construct like ValueRatio.
>
> One important reason for this change is that we propose to store branch probabilities instead of edge weights in MachineBasicBlock. We also want clients to use probability instead of weight when adding successors to a MBB. The current BranchProbability has more space which may be a concern.
>
> Differential revision: http://reviews.llvm.org/D12603
>
>
> Modified:
[...]
>     llvm/trunk/test/Analysis/BranchProbabilityInfo/basic.ll
>     llvm/trunk/test/Analysis/BranchProbabilityInfo/loop.ll

These two tests fail with MSVC 2015 :

C:\cygwin64\home\ismail\src\llvm\test\Analysis\BranchProbabilityInfo\basic.ll:18:10:
; CHECK: edge body -> exit probability is 0x04000000 / 0x80000000 = 3.12%
^
<stdin>:4:2: note: scanning from here
edge body -> exit probability is 0x04000000 / 0x80000000 = 3.13%
^

C:\cygwin64\home\ismail\src\llvm\test\Analysis\BranchProbabilityInfo\loop.ll:27:10: