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

            Bug ID: 126332
           Summary: -ftracer hurts phiopt load commoning
           Product: gcc
           Version: 16.0
            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: ---

The hot loop from Snappy decompression gets worse codegen when compiled with
PGO because -ftracer messes up the load commoning that was introduced in
phiopt:

#include <stddef.h>
#include <stdint.h>

__attribute__ ((noinline, noclone))
const uint8_t *
advance (const uint8_t *ip, size_t tag, const uint8_t *end)
{
  size_t sum = 0;

  while (ip < end)
    {
      size_t type = tag & 3;
      if (type == 0)
        {
          size_t n = (tag >> 2) + 1;
          tag = ip[n];
          ip += n + 1;
        }
      else
        {
          tag = ip[type];
          ip += type + 1;
        }
      sum += tag;
    }

  return ip - (sum & 1);
}

Compiled for aarch64 with -O3 gives:
advance(unsigned char const*, unsigned long, unsigned char const*):
        cmp     x0, x2
        bcs     .L1
        mov     w5, 0
.L5:
        ands    x3, x1, 3
        lsr     x1, x1, 2
        add     w4, w3, 1
        csinc   x3, x3, x1, ne
        add     x1, x1, 2
        csel    x4, x4, x1, ne
        ldrb    w1, [x0, x3]
        add     x0, x0, x4
        add     x5, x5, x1
        cmp     x0, x2
        bcc     .L5
        and     x5, x5, 1
        sub     x0, x0, x5
.L1:
        ret

with -O3 -ftracer it gives:
advance(unsigned char const*, unsigned long, unsigned char const*):
        mov     w4, 0
        cmp     x0, x2
        bcs     .L1
.L2:
        lsr     x3, x1, 2
        ands    x1, x1, 3
        add     x6, x0, x3
        add     w5, w1, 1
        add     x3, x3, 2
        bne     .L4
        ldrb    w1, [x6, 1]
        add     x0, x0, x3
        add     x4, x4, x1
        cmp     x0, x2
        bcc     .L2
        and     x4, x4, 1
        sub     x0, x0, x4
.L1:
        ret
.L4:
        ldrb    w1, [x0, x1]
        add     x0, x0, x5
        add     x4, x4, x1
        cmp     x2, x0
        bhi     .L2
        and     x4, x4, 1
        sub     x0, x0, x4
        b       .L1

Reply via email to