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