Getting consistent block frequency information


I’ve been trying to get frequency information for basic blocks and edges between basic blocks using MachineBlockFrequencyInfo and MachineBranchProbabilityInfo. Unfortunately, the results returned don’t seem to give high consistency.

If I have code like the following:

define i32 @foo(i32 %i, i32* %addr) {
  %tobool = icmp ne i32 %i, 0
  br i1 %tobool, label %if.end, label %if.then

  %add = add i32 %i, 1
  store i32 %add, i32* %addr
  br label %return

  %mul = mul i32 %i, 2
  store i32 %mul, i32* %addr
  br label %return

  %ret = load i32, i32* %addr
  ret i32 %ret

The branch probability information suggests that the edge from entry->if.then and the edge from entry->if.end are executed in a 5:3 ratio. The resulting block frequency information, when queried in integer form, reports that entry and return has an integer count of 21, if.then as 13, and if.end as 8. I get different results if I do the obvious thing of getting the branch probability and multiplying it by the block frequency information to derive edge frequency; for example, in this case, multiplying 21 by 3/8 returns 7 instead of 8 for the edge from entry to if.end. Choosing a particular order to generate the block orders (to match up with the derivation of block mass estimates) doesn’t appear to yield the correct results.

Is there any way to get more accurate results?