https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128468

            Bug ID: 128468
           Summary: Missed phi-opt optimization
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: law at gcc dot gnu.org
            Blocks: 26163
  Target Milestone: ---

This is teeny-tiny in x264 and while the motivating case is supposed to be
pretty standard, I don't see that it'd likely be that common in encoders. 
Perhaps this would make a good easyhack as a learning experience?

Consider:

int
f (int v)
{
  int t = v * 2;
  return t > 1 ? t : 1 - t;
}


That's equivalent to max (t, 1-t)

Right now we have this in gimple:

  t_3 = v_2(D) * 2;
  if (t_3 <= 1)
    goto <bb 3>; [41.00%]
  else
    goto <bb 4>; [59.00%]
;;    succ:       3
;;                4

;;   basic block 3, loop depth 0
;;    pred:       2
  _4 = 1 - t_3;
;;    succ:       4

;;   basic block 4, loop depth 0
;;    pred:       2
;;                3
  # _1 = PHI <t_3(2), _4(3)>

So looks like a fairly straightforward phi-opt transformation.

Currently we get something like this on rv64gcbv_zicond:

        slliw   a4,a0,1
        li      a5,1
        subw    a0,a5,a4
        sgt     t0,a4,a5
        czero.nez       t1,a0,t0
        czero.eqz       t2,a4,t0
        add     a0,t2,t1
        ret

Clearly the RTL if-converter caught this case.  We'd like to get this:

        slliw   a4,a0,1
        li      a5,1
        subw    a0,a5,a4
        max     a0,a4,a0
        ret


While I think we could possibly to it in ifcvt.cc, phiopt seems like a better
place given how simple the case looks in gimple.


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=26163
[Bug 26163] [meta-bug] missed optimization in SPEC (2026, 2k17, 2k and 2k6 and
95)

Reply via email to