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

            Bug ID: 70159
           Summary: missed CSE optimization
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: spop at gcc dot gnu.org
  Target Milestone: ---

$ cat h.c
float foo_p(float d, float min, float max, float a)
{
  float tmin;
  float tmax;

  float inv = 1.0f / d;
  if (inv >= 0) {
    tmin = (min - a) * inv;
    tmax = (max - a) * inv;
  } else {
    tmin = (max - a) * inv;
    tmax = (min - a) * inv;
  }

  return tmax + tmin;
}

$ gcc h.c -Ofast -S -o- 
foo_p:
        fmov    s4, 1.0e+0
        fdiv    s0, s4, s0
        fcmpe   s0, #0.0
        blt     .L6
        fsub    s1, s1, s3
        fsub    s2, s2, s3
        fmul    s1, s1, s0
        fmul    s0, s2, s0
        fadd    s0, s1, s0
        ret
        .p2align 3
.L6:
        fsub    s4, s2, s3
        fsub    s2, s1, s3
        fmul    s1, s4, s0
        fmul    s0, s2, s0
        fadd    s0, s1, s0
        ret

$ clang h.c -Ofast -S -o-
foo_p:                                  // @foo_p
// BB#0:                                // %entry
        fmov    s4, #1.00000000
        fdiv    s0, s4, s0
        fcmp    s0, #0.0
        fcsel   s4, s1, s2, lt
        fcsel   s1, s2, s1, lt
        fsub    s1, s1, s3
        fsub    s2, s4, s3
        fadd    s1, s2, s1
        fmul    s0, s1, s0
        ret

The computations in both branches are redundant.
Even without if-conversion (fcsel), GCC should be able to sink/hoist fsub and
fmul.

Reply via email to