tree-optimization/110963 - more PRE when optimizing for size
commit4d6132e59327e809a4d4e39fb9465dbd43775b7c
authorRichard Biener <rguenther@suse.de>
Thu, 10 Aug 2023 11:55:36 +0000 (10 13:55 +0200)
committerRichard Biener <rguenther@suse.de>
Tue, 15 Aug 2023 09:07:25 +0000 (15 11:07 +0200)
tree9e4df2fd9ebba5263dcd9c476af604b703007ced
parentbcdbedb3e6083ad01d844ed97cf19645c1ef6568
tree-optimization/110963 - more PRE when optimizing for size

The following adjusts the heuristic when we perform PHI insertion
during GIMPLE PRE from requiring at least one edge that is supposed
to be optimized for speed to also doing insertion when the expression
is available on all edges (but possibly with different value) and
we'd at most have one copy from a constant.  The first ensures
we optimize two computations on all paths to one plus a possible
copy due to the PHI, the second makes sure we do not need to insert
many possibly large copies from constants, disregarding the
cummulative size cost of the register copies when they are not
coalesced.

The case in the testcase is

  <bb 5>
  _14 = h;
  if (_14 == 0B)
    goto <bb 7>;
  else
    goto <bb 6>;

  <bb 6>
  h = 0B;

  <bb 7>
  h.6_12 = h;

and we want to optimize that to

  <bb 7>
  # h.6_12 = PHI <_14(5), 0B(6)>

If we want to consider the cost of the register copies I think the
only simplistic enough way would be to restrict the special-case to
two incoming edges - we'd assume one register copy is coalesced
leaving one copy from a register or from a constant.

As with every optimization the downstream effects are probably
bigger than what we can locally estimate.

PR tree-optimization/110963
* tree-ssa-pre.cc (do_pre_regular_insertion): Also insert
a PHI node when the expression is available on all edges
and we insert at most one copy from a constant.

* gcc.dg/tree-ssa/ssa-pre-34.c: New testcase.
gcc/testsuite/gcc.dg/tree-ssa/ssa-pre-34.c [new file with mode: 0644]
gcc/tree-ssa-pre.cc