[SimplifyCFG] track branch fold costs across transformations

SimplifyCFG folds

bool foo() {
if (cond1) return false;
if (cond2) return false;
return true;


bool foo() {
  if (cond1 | cond2) return false
  return true;

‘cond2’ is called ‘bonus insts’ in branch folding since they introduce overhead
since the original CFG could do early exit but the folded CFG always executes
them. SimplifyCFG calculates the costs of ‘bonus insts’ of a folding a BB into
its predecessor BB which shares the destination. If it is below bonus-inst-threshold,
SimplifyCFG will fold that BB into its predecessor and cond2 will always be executed.

When SimplifyCFG calculates the cost of ‘bonus insts’, it only consider ‘bonus’ insts
in the current BB to be considered for folding. This causes issue for unrolled loops
which share destinations, e.g.

bool foo(int *a) {
  for (int i = 0; i < 32; i++)
    if (a[i] > 0) return false;
  return true;

After unrolling, it becomes

bool foo(int *a) {
  if(a[0]>0) return false
  if(a[1]>0) return false;
  if(a[31]>0) return false;
  return true;

SimplifyCFG will merge each BB with its predecessor BB,
and ends up with 32 ‘bonus insts’ which are always executed, which
is much slower than the original CFG.

The root cause is that SimplifyCFG does not consider the
accumulated cost of ‘bonus insts’ which are folded from
different BB’s.

One way to fix this is to introduce a ValueMap to track
costs of ‘bonus insts’ coming from different BB’s into
the same BB, and cuts off if the accumulated cost
exceeds a threshold.

A Phabricator review has been created for this approach ⚙ D132408 [SimplifyCFG] accumulate bonus insts cost . Since there are different opinions about this approach, open this RFC for discussion.