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

            Bug ID: 108446
           Summary: GCC fails to elide udiv/msub when doing modulus by
                    select of constants
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ktkachov at gcc dot gnu.org
  Target Milestone: ---

unsigned foo(int vl, unsigned len) {
  unsigned pad = vl <= 256 ? 128 : 256;
  return len % pad;
}

At -O2 aarch64 gcc generates:
foo:
        cmp     w0, 256
        mov     w2, 256
        mov     w0, 128
        csel    w2, w2, w0, gt
        udiv    w0, w1, w2
        msub    w0, w0, w2, w1
        ret

clang, for example can generate the cheaper:
foo:                                    // @foo
        cmp     w0, #256
        mov     w8, #127
        mov     w9, #255
        csel    w8, w9, w8, gt
        and     w0, w8, w1
        ret

Similar situation on x86.
I suppose this could be a match.pd fix or otherwise something during
expand-time?

Reply via email to