This is an archive of the discontinued LLVM Phabricator instance.

Fix Bug 18705: BranchProbabilityInfo is not using the right heuristics inside loops.
ClosedPublic

Authored by ahatanak on Apr 11 2014, 5:28 PM.

Details

Summary

The attached patch fixes bug 18705.

http://llvm.org/bugs/show_bug.cgi?id=18705

Currently, BranchProbabilityInfo::calcLoopBranchHeuristics, which is one of the heuristics used to determine branch weights, sets the weights for all successor edges of a basic block inside a loop, even when it doesn't have enough information to estimate the branch probabilities correctly.

For example, when calcLoopBranchHeuristics handles basic block "for.body" in the code below, it classifies both successor edges of "for.body" (for.body => %if.then and for.body => %if.else) as "InEdges", since neither of them is an exit edge nor a back edge, and gives equal weights to them.

The patch fixes the function to exit early if it doesn't see any exit edges or back edges and let the later heuristics decide the weights. In the example below, it would defer the decision to calcFloatingPointHeuristics, which would set the edges weights to FPH_TAKEN_WEIGHT and FPH_NONTAKEN_WEIGHT.

for.body:

...

%cmp3 = fcmp une double %4, %5

br i1 %cmp3, label %if.then, label %if.else

if.then:

...
br label %for.inc

if.else:

...
br label %for.inc

for.inc:

...
br i1 %exitcond, label %for.end, label %for.body

Diff Detail

Repository
rL LLVM

Event Timeline

Test case in pr18705.ll is a better example for explaining what this patch is trying to fix.

The current code in calcLoopBranchHeuristics gives equal weights to edges "while.body -> if.then" and "while.body -> if.else". The patch fixes it to give weights 20 (FPH_TAKEN_WEIGHT) and 12 (FPH_NONTAKEN_WEIGHT).

ahatanak added a subscriber: Unknown Object (MLST).Apr 11 2014, 5:39 PM

CC'ing list.

ahatanak closed this revision.May 19 2014, 7:23 AM
ahatanak updated this revision to Diff 9536.

Closed by commit rL206194 (authored by @ahatanak).